source: main/waeup.kofa/trunk/src/waeup/kofa/students/utils.py @ 11028

Last change on this file since 11028 was 10803, checked in by Henrik Bettermann, 11 years ago

Resolve configuration conflict.

  • Property svn:keywords set to Id
File size: 34.6 KB
RevLine 
[7191]1## $Id: utils.py 10803 2013-11-28 15:12:39Z henrik $
2##
3## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
[7358]18"""General helper functions and utilities for the student section.
[6651]19"""
[7150]20import grok
[8595]21from time import time
[7318]22from reportlab.lib import colors
[7019]23from reportlab.lib.units import cm
24from reportlab.lib.pagesizes import A4
[9015]25from reportlab.lib.styles import getSampleStyleSheet
26from reportlab.platypus import Paragraph, Image, Table, Spacer
[9922]27from zope.schema.interfaces import ConstraintNotSatisfied
[9015]28from zope.component import getUtility, createObject
[7019]29from zope.formlib.form import setUpEditWidgets
[9015]30from zope.i18n import translate
[8596]31from waeup.kofa.interfaces import (
[9762]32    IExtFileStore, IKofaUtils, RETURNING, PAID, CLEARED,
33    academic_sessions_vocab)
[7811]34from waeup.kofa.interfaces import MessageFactory as _
35from waeup.kofa.students.interfaces import IStudentsUtils
[10706]36from waeup.kofa.students.workflow import ADMITTED
[9910]37from waeup.kofa.browser.pdf import (
[9965]38    ENTRY1_STYLE, format_html, NOTE_STYLE, HEADING_STYLE,
39    get_signature_tables)
[9910]40from waeup.kofa.browser.interfaces import IPDFCreator
[10256]41from waeup.kofa.utils.helpers import to_timezone
[6651]42
[7318]43SLIP_STYLE = [
44    ('VALIGN',(0,0),(-1,-1),'TOP'),
45    #('FONT', (0,0), (-1,-1), 'Helvetica', 11),
46    ]
[7019]47
[7318]48CONTENT_STYLE = [
49    ('VALIGN',(0,0),(-1,-1),'TOP'),
50    #('FONT', (0,0), (-1,-1), 'Helvetica', 8),
51    #('TEXTCOLOR',(0,0),(-1,0),colors.white),
[9906]52    #('BACKGROUND',(0,0),(-1,0),colors.black),
53    ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
54    ('BOX', (0,0), (-1,-1), 1, colors.black),
55
[7318]56    ]
[7304]57
[7318]58FONT_SIZE = 10
59FONT_COLOR = 'black'
60
[8112]61def trans(text, lang):
62    # shortcut
63    return translate(text, 'waeup.kofa', target_language=lang)
64
[10261]65def formatted_text(text, color=FONT_COLOR, lang='en'):
[7511]66    """Turn `text`, `color` and `size` into an HTML snippet.
[7318]67
[7511]68    The snippet is suitable for use with reportlab and generating PDFs.
69    Wraps the `text` into a ``<font>`` tag with passed attributes.
70
71    Also non-strings are converted. Raw strings are expected to be
72    utf-8 encoded (usually the case for widgets etc.).
73
[7804]74    Finally, a br tag is added if widgets contain div tags
75    which are not supported by reportlab.
76
[7511]77    The returned snippet is unicode type.
78    """
79    if not isinstance(text, unicode):
80        if isinstance(text, basestring):
81            text = text.decode('utf-8')
82        else:
83            text = unicode(text)
[9717]84    if text == 'None':
85        text = ''
[8141]86    # Mainly for boolean values we need our customized
87    # localisation of the zope domain
[10261]88    text = translate(text, 'zope', target_language=lang)
[7804]89    text = text.replace('</div>', '<br /></div>')
[9910]90    tag1 = u'<font color="%s">' % (color)
[7511]91    return tag1 + u'%s</font>' % text
92
[8481]93def generate_student_id():
[8410]94    students = grok.getSite()['students']
95    new_id = students.unique_student_id
96    return new_id
[6742]97
[7186]98def set_up_widgets(view, ignore_request=False):
[7019]99    view.adapters = {}
100    view.widgets = setUpEditWidgets(
101        view.form_fields, view.prefix, view.context, view.request,
102        adapters=view.adapters, for_display=True,
103        ignore_request=ignore_request
104        )
105
[10261]106def render_student_data(studentview, omit_fields=(), lang='en'):
[7318]107    """Render student table for an existing frame.
108    """
109    width, height = A4
[7186]110    set_up_widgets(studentview, ignore_request=True)
[7318]111    data_left = []
112    data_right = []
[7019]113    style = getSampleStyleSheet()
[7280]114    img = getUtility(IExtFileStore).getFileByContext(
115        studentview.context, attr='passport.jpg')
116    if img is None:
[7811]117        from waeup.kofa.browser import DEFAULT_PASSPORT_IMAGE_PATH
[7280]118        img = open(DEFAULT_PASSPORT_IMAGE_PATH, 'rb')
[7318]119    doc_img = Image(img.name, width=4*cm, height=4*cm, kind='bound')
120    data_left.append([doc_img])
121    #data.append([Spacer(1, 12)])
[9141]122
[10261]123    f_label = trans(_('Name:'), lang)
[9910]124    f_label = Paragraph(f_label, ENTRY1_STYLE)
[9911]125    f_text = formatted_text(studentview.context.display_fullname)
[9910]126    f_text = Paragraph(f_text, ENTRY1_STYLE)
[9141]127    data_right.append([f_label,f_text])
128
[7019]129    for widget in studentview.widgets:
[9141]130        if 'name' in widget.name:
[7019]131            continue
[9911]132        f_label = translate(
[7811]133            widget.label.strip(), 'waeup.kofa',
[10261]134            target_language=lang)
[9911]135        f_label = Paragraph('%s:' % f_label, ENTRY1_STYLE)
[10261]136        f_text = formatted_text(widget(), lang=lang)
[9910]137        f_text = Paragraph(f_text, ENTRY1_STYLE)
[7318]138        data_right.append([f_label,f_text])
[9141]139
[9452]140    if getattr(studentview.context, 'certcode', None):
[10250]141        if not 'certificate' in omit_fields:
[10261]142            f_label = trans(_('Study Course:'), lang)
[10250]143            f_label = Paragraph(f_label, ENTRY1_STYLE)
144            f_text = formatted_text(
[10650]145                studentview.context['studycourse'].certificate.longtitle)
[10250]146            f_text = Paragraph(f_text, ENTRY1_STYLE)
147            data_right.append([f_label,f_text])
148        if not 'department' in omit_fields:
[10261]149            f_label = trans(_('Department:'), lang)
[10250]150            f_label = Paragraph(f_label, ENTRY1_STYLE)
151            f_text = formatted_text(
152                studentview.context[
[10650]153                'studycourse'].certificate.__parent__.__parent__.longtitle,
[10250]154                )
155            f_text = Paragraph(f_text, ENTRY1_STYLE)
156            data_right.append([f_label,f_text])
157        if not 'faculty' in omit_fields:
[10261]158            f_label = trans(_('Faculty:'), lang)
[10250]159            f_label = Paragraph(f_label, ENTRY1_STYLE)
160            f_text = formatted_text(
161                studentview.context[
[10650]162                'studycourse'].certificate.__parent__.__parent__.__parent__.longtitle,
[10250]163                )
164            f_text = Paragraph(f_text, ENTRY1_STYLE)
165            data_right.append([f_label,f_text])
[10688]166        if not 'current_mode' in omit_fields:
167            studymodes_dict = getUtility(IKofaUtils).STUDY_MODES_DICT
168            sm = studymodes_dict[studentview.context[
169                'studycourse'].certificate.study_mode]
170            f_label = trans(_('Study Mode:'), lang)
171            f_label = Paragraph(f_label, ENTRY1_STYLE)
172            f_text = formatted_text(sm)
173            f_text = Paragraph(f_text, ENTRY1_STYLE)
174            data_right.append([f_label,f_text])
[10250]175        if not 'entry_session' in omit_fields:
[10261]176            f_label = trans(_('Entry Session:'), lang)
[10250]177            f_label = Paragraph(f_label, ENTRY1_STYLE)
178            entry_session = studentview.context['studycourse'].entry_session
179            entry_session = academic_sessions_vocab.getTerm(entry_session).title
180            f_text = formatted_text(entry_session)
181            f_text = Paragraph(f_text, ENTRY1_STYLE)
182            data_right.append([f_label,f_text])
[10256]183        if not 'date_of_birth' in omit_fields:
[10261]184            f_label = trans(_('Date of Birth:'), lang)
[10256]185            f_label = Paragraph(f_label, ENTRY1_STYLE)
186            date_of_birth = studentview.context.date_of_birth
187            tz = getUtility(IKofaUtils).tzinfo
188            date_of_birth = to_timezone(date_of_birth, tz)
189            if date_of_birth is not None:
190                date_of_birth = date_of_birth.strftime("%d/%m/%Y")
191            f_text = formatted_text(date_of_birth)
192            f_text = Paragraph(f_text, ENTRY1_STYLE)
193            data_right.append([f_label,f_text])
[9141]194
[7318]195    table_left = Table(data_left,style=SLIP_STYLE)
[8112]196    table_right = Table(data_right,style=SLIP_STYLE, colWidths=[5*cm, 6*cm])
[7318]197    table = Table([[table_left, table_right],],style=SLIP_STYLE)
[7019]198    return table
199
[10261]200def render_table_data(tableheader, tabledata, lang='en'):
[7318]201    """Render children table for an existing frame.
202    """
[7304]203    data = []
[7318]204    #data.append([Spacer(1, 12)])
[7304]205    line = []
206    style = getSampleStyleSheet()
207    for element in tableheader:
[10261]208        field = '<strong>%s</strong>' % formatted_text(element[0], lang=lang)
[7310]209        field = Paragraph(field, style["Normal"])
[7304]210        line.append(field)
211    data.append(line)
212    for ticket in tabledata:
213        line = []
214        for element in tableheader:
[7511]215              field = formatted_text(getattr(ticket,element[1],u' '))
[7318]216              field = Paragraph(field, style["Normal"])
[7304]217              line.append(field)
218        data.append(line)
[7310]219    table = Table(data,colWidths=[
[7318]220        element[2]*cm for element in tableheader], style=CONTENT_STYLE)
[7304]221    return table
222
[10261]223def render_transcript_data(view, tableheader, levels_data, lang='en'):
[10250]224    """Render children table for an existing frame.
225    """
226    data = []
227    style = getSampleStyleSheet()
228    for level in levels_data:
229        level_obj = level['level']
[10251]230        tickets = level['tickets_1'] + level['tickets_2'] + level['tickets_3']
231        headerline = []
232        tabledata = []
[10261]233        subheader = '%s %s, %s %s' % (
234            trans(_('Session'), lang),
[10250]235            view.session_dict[level_obj.level_session],
[10261]236            trans(_('Level'), lang),
[10266]237            view.level_dict[level_obj.level])
[10250]238        data.append(Paragraph(subheader, HEADING_STYLE))
239        for element in tableheader:
240            field = '<strong>%s</strong>' % formatted_text(element[0])
241            field = Paragraph(field, style["Normal"])
[10251]242            headerline.append(field)
243        tabledata.append(headerline)
[10250]244        for ticket in tickets:
[10251]245            ticketline = []
[10250]246            for element in tableheader:
247                  field = formatted_text(getattr(ticket,element[1],u' '))
248                  field = Paragraph(field, style["Normal"])
[10251]249                  ticketline.append(field)
250            tabledata.append(ticketline)
[10250]251        table = Table(tabledata,colWidths=[
252            element[2]*cm for element in tableheader], style=CONTENT_STYLE)
253        data.append(table)
[10479]254        sgpa = '%s: %s' % (trans('Sessional GPA (rectified)', lang), level['sgpa'])
[10261]255        data.append(Paragraph(sgpa, style["Normal"]))
[10250]256    return data
257
[8112]258def docs_as_flowables(view, lang='en'):
259    """Create reportlab flowables out of scanned docs.
260    """
261    # XXX: fix circular import problem
262    from waeup.kofa.students.viewlets import FileManager
263    from waeup.kofa.browser import DEFAULT_IMAGE_PATH
264    style = getSampleStyleSheet()
265    data = []
[7318]266
[8112]267    # Collect viewlets
268    fm = FileManager(view.context, view.request, view)
269    fm.update()
270    if fm.viewlets:
271        sc_translation = trans(_('Scanned Documents'), lang)
[9910]272        data.append(Paragraph(sc_translation, HEADING_STYLE))
[8112]273        # Insert list of scanned documents
274        table_data = []
275        for viewlet in fm.viewlets:
[10020]276            if viewlet.file_exists:
277                # Show viewlet only if file exists
278                f_label = Paragraph(trans(viewlet.label, lang), ENTRY1_STYLE)
279                img_path = getattr(getUtility(IExtFileStore).getFileByContext(
280                    view.context, attr=viewlet.download_name), 'name', None)
281                #f_text = Paragraph(trans(_('(not provided)'),lang), ENTRY1_STYLE)
282                if img_path is None:
283                    pass
284                elif not img_path[-4:] in ('.jpg', '.JPG'):
285                    # reportlab requires jpg images, I think.
286                    f_text = Paragraph('%s (not displayable)' % (
287                        viewlet.title,), ENTRY1_STYLE)
288                else:
289                    f_text = Image(img_path, width=2*cm, height=1*cm, kind='bound')
290                table_data.append([f_label, f_text])
[8112]291        if table_data:
292            # safety belt; empty tables lead to problems.
293            data.append(Table(table_data, style=SLIP_STYLE))
294    return data
295
[7150]296class StudentsUtils(grok.GlobalUtility):
297    """A collection of methods subject to customization.
298    """
299    grok.implements(IStudentsUtils)
[7019]300
[8268]301    def getReturningData(self, student):
[9005]302        """ Define what happens after school fee payment
[7841]303        depending on the student's senate verdict.
304
305        In the base configuration current level is always increased
306        by 100 no matter which verdict has been assigned.
307        """
[8268]308        new_level = student['studycourse'].current_level + 100
309        new_session = student['studycourse'].current_session + 1
310        return new_session, new_level
311
312    def setReturningData(self, student):
[9005]313        """ Define what happens after school fee payment
314        depending on the student's senate verdict.
[8268]315
[9005]316        This method folllows the same algorithm as getReturningData but
317        it also sets the new values.
[8268]318        """
319        new_session, new_level = self.getReturningData(student)
[9922]320        try:
321            student['studycourse'].current_level = new_level
322        except ConstraintNotSatisfied:
323            # Do not change level if level exceeds the
324            # certificate's end_level.
325            pass
[8268]326        student['studycourse'].current_session = new_session
[7615]327        verdict = student['studycourse'].current_verdict
[8820]328        student['studycourse'].current_verdict = '0'
[7615]329        student['studycourse'].previous_verdict = verdict
330        return
331
[9519]332    def _getSessionConfiguration(self, session):
333        try:
334            return grok.getSite()['configuration'][str(session)]
335        except KeyError:
336            return None
337
[9148]338    def setPaymentDetails(self, category, student,
[9151]339            previous_session, previous_level):
[8595]340        """Create Payment object and set the payment data of a student for
341        the payment category specified.
342
[7841]343        """
[8595]344        p_item = u''
345        amount = 0.0
[9148]346        if previous_session:
[9517]347            if previous_session < student['studycourse'].entry_session:
348                return _('The previous session must not fall below '
349                         'your entry session.'), None
350            if category == 'schoolfee':
351                # School fee is always paid for the following session
352                if previous_session > student['studycourse'].current_session:
353                    return _('This is not a previous session.'), None
354            else:
355                if previous_session > student['studycourse'].current_session - 1:
356                    return _('This is not a previous session.'), None
[9148]357            p_session = previous_session
358            p_level = previous_level
359            p_current = False
360        else:
361            p_session = student['studycourse'].current_session
362            p_level = student['studycourse'].current_level
363            p_current = True
[9519]364        academic_session = self._getSessionConfiguration(p_session)
365        if academic_session == None:
[8595]366            return _(u'Session configuration object is not available.'), None
[9521]367        # Determine fee.
[7150]368        if category == 'schoolfee':
[8595]369            try:
[8596]370                certificate = student['studycourse'].certificate
371                p_item = certificate.code
[8595]372            except (AttributeError, TypeError):
373                return _('Study course data are incomplete.'), None
[9148]374            if previous_session:
[9916]375                # Students can pay for previous sessions in all
376                # workflow states.  Fresh students are excluded by the
377                # update method of the PreviousPaymentAddFormPage.
[9148]378                if previous_level == 100:
379                    amount = getattr(certificate, 'school_fee_1', 0.0)
380                else:
381                    amount = getattr(certificate, 'school_fee_2', 0.0)
382            else:
383                if student.state == CLEARED:
384                    amount = getattr(certificate, 'school_fee_1', 0.0)
385                elif student.state == RETURNING:
[9916]386                    # In case of returning school fee payment the
387                    # payment session and level contain the values of
388                    # the session the student has paid for. Payment
389                    # session is always next session.
[9148]390                    p_session, p_level = self.getReturningData(student)
[9519]391                    academic_session = self._getSessionConfiguration(p_session)
392                    if academic_session == None:
[9916]393                        return _(
394                            u'Session configuration object is not available.'
395                            ), None
[9148]396                    amount = getattr(certificate, 'school_fee_2', 0.0)
397                elif student.is_postgrad and student.state == PAID:
[9916]398                    # Returning postgraduate students also pay for the
399                    # next session but their level always remains the
400                    # same.
[9148]401                    p_session += 1
[9519]402                    academic_session = self._getSessionConfiguration(p_session)
403                    if academic_session == None:
[9916]404                        return _(
405                            u'Session configuration object is not available.'
406                            ), None
[9148]407                    amount = getattr(certificate, 'school_fee_2', 0.0)
[7150]408        elif category == 'clearance':
[9178]409            try:
410                p_item = student['studycourse'].certificate.code
411            except (AttributeError, TypeError):
412                return _('Study course data are incomplete.'), None
[8595]413            amount = academic_session.clearance_fee
[7150]414        elif category == 'bed_allocation':
[8595]415            p_item = self.getAccommodationDetails(student)['bt']
416            amount = academic_session.booking_fee
[9423]417        elif category == 'hostel_maintenance':
[10681]418            amount = 0.0
[9429]419            bedticket = student['accommodation'].get(
420                str(student.current_session), None)
421            if bedticket:
422                p_item = bedticket.bed_coordinates
[10681]423                if bedticket.bed.__parent__.maint_fee > 0:
424                    amount = bedticket.bed.__parent__.maint_fee
425                else:
426                    # fallback
427                    amount = academic_session.maint_fee
[9429]428            else:
429                # Should not happen because this is already checked
430                # in the browser module, but anyway ...
431                portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
432                p_item = trans(_('no bed allocated'), portal_language)
[10449]433        elif category == 'transcript':
434            amount = academic_session.transcript_fee
[8595]435        if amount in (0.0, None):
[9517]436            return _('Amount could not be determined.'), None
[8595]437        for key in student['payments'].keys():
438            ticket = student['payments'][key]
439            if ticket.p_state == 'paid' and\
440               ticket.p_category == category and \
441               ticket.p_item == p_item and \
442               ticket.p_session == p_session:
[9517]443                  return _('This type of payment has already been made.'), None
[8708]444        payment = createObject(u'waeup.StudentOnlinePayment')
[8951]445        timestamp = ("%d" % int(time()*10000))[1:]
[8595]446        payment.p_id = "p%s" % timestamp
447        payment.p_category = category
448        payment.p_item = p_item
449        payment.p_session = p_session
450        payment.p_level = p_level
[9148]451        payment.p_current = p_current
[8595]452        payment.amount_auth = amount
453        return None, payment
[7019]454
[9868]455    def setBalanceDetails(self, category, student,
[9864]456            balance_session, balance_level, balance_amount):
457        """Create Payment object and set the payment data of a student for.
458
459        """
[9868]460        p_item = u'Balance'
[9864]461        p_session = balance_session
462        p_level = balance_level
463        p_current = False
464        amount = balance_amount
465        academic_session = self._getSessionConfiguration(p_session)
466        if academic_session == None:
467            return _(u'Session configuration object is not available.'), None
[9874]468        if amount in (0.0, None) or amount < 0:
469            return _('Amount must be greater than 0.'), None
[9864]470        for key in student['payments'].keys():
471            ticket = student['payments'][key]
472            if ticket.p_state == 'paid' and\
473               ticket.p_category == 'balance' and \
474               ticket.p_item == p_item and \
475               ticket.p_session == p_session:
476                  return _('This type of payment has already been made.'), None
477        payment = createObject(u'waeup.StudentOnlinePayment')
478        timestamp = ("%d" % int(time()*10000))[1:]
479        payment.p_id = "p%s" % timestamp
[9868]480        payment.p_category = category
[9864]481        payment.p_item = p_item
482        payment.p_session = p_session
483        payment.p_level = p_level
484        payment.p_current = p_current
485        payment.amount_auth = amount
486        return None, payment
487
[7186]488    def getAccommodationDetails(self, student):
[9219]489        """Determine the accommodation data of a student.
[7841]490        """
[7150]491        d = {}
492        d['error'] = u''
[8685]493        hostels = grok.getSite()['hostels']
494        d['booking_session'] = hostels.accommodation_session
495        d['allowed_states'] = hostels.accommodation_states
[8688]496        d['startdate'] = hostels.startdate
497        d['enddate'] = hostels.enddate
498        d['expired'] = hostels.expired
[7150]499        # Determine bed type
500        studycourse = student['studycourse']
[7369]501        certificate = getattr(studycourse,'certificate',None)
[7150]502        entry_session = studycourse.entry_session
503        current_level = studycourse.current_level
[9187]504        if None in (entry_session, current_level, certificate):
505            return d
[7369]506        end_level = certificate.end_level
[9148]507        if current_level == 10:
508            bt = 'pr'
509        elif entry_session == grok.getSite()['hostels'].accommodation_session:
[7150]510            bt = 'fr'
511        elif current_level >= end_level:
512            bt = 'fi'
513        else:
514            bt = 're'
515        if student.sex == 'f':
516            sex = 'female'
517        else:
518            sex = 'male'
519        special_handling = 'regular'
520        d['bt'] = u'%s_%s_%s' % (special_handling,sex,bt)
521        return d
[7019]522
[7186]523    def selectBed(self, available_beds):
[7841]524        """Select a bed from a list of available beds.
525
526        In the base configuration we select the first bed found,
527        but can also randomize the selection if we like.
528        """
[7150]529        return available_beds[0]
530
[9981]531    def _admissionText(self, student, portal_language):
[9979]532        inst_name = grok.getSite()['configuration'].name
533        text = trans(_(
534            'This is to inform you that you have been provisionally'
535            ' admitted into ${a} as follows:', mapping = {'a': inst_name}),
536            portal_language)
537        return text
538
[10686]539    def renderPDFAdmissionLetter(self, view, student=None, omit_fields=(),
540                                 pre_text=None, post_text=None,):
[9191]541        """Render pdf admission letter.
542        """
543        if student is None:
544            return
545        style = getSampleStyleSheet()
[9949]546        creator = self.getPDFCreator(student)
[9979]547        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
[9191]548        data = []
549        doc_title = view.label
550        author = '%s (%s)' % (view.request.principal.title,
551                              view.request.principal.id)
[9944]552        footer_text = view.label.split('\n')
553        if len(footer_text) > 1:
554            # We can add a department in first line
555            footer_text = footer_text[1]
556        else:
557            # Only the first line is used for the footer
558            footer_text = footer_text[0]
[9191]559        if getattr(student, 'student_id', None) is not None:
560            footer_text = "%s - %s - " % (student.student_id, footer_text)
561
[10702]562        # Text before student data
[10686]563        if pre_text is None:
564            html = format_html(self._admissionText(student, portal_language))
565        else:
566            html = format_html(pre_text)
[9191]567        data.append(Paragraph(html, NOTE_STYLE))
568        data.append(Spacer(1, 20))
569
570        # Student data
[10261]571        data.append(render_student_data(view, omit_fields, lang=portal_language))
[9191]572
[10702]573        # Text after student data
[9191]574        data.append(Spacer(1, 20))
[10686]575        if post_text is None:
576            datelist = student.history.messages[0].split()[0].split('-')
577            creation_date = u'%s/%s/%s' % (datelist[2], datelist[1], datelist[0])
[10702]578            post_text = trans(_(
[10686]579                'Your Kofa student record was created on ${a}.',
580                mapping = {'a': creation_date}),
581                portal_language)
[10702]582        #html = format_html(post_text)
583        #data.append(Paragraph(html, NOTE_STYLE))
[9191]584
585        # Create pdf stream
586        view.response.setHeader(
587            'Content-Type', 'application/pdf')
588        pdf_stream = creator.create_pdf(
589            data, None, doc_title, author=author, footer=footer_text,
[10702]590            note=post_text)
[9191]591        return pdf_stream
592
[9949]593    def getPDFCreator(self, context):
594        """Get a pdf creator suitable for `context`.
595
596        The default implementation always returns the default creator.
597        """
598        return getUtility(IPDFCreator)
599
[8257]600    def renderPDF(self, view, filename='slip.pdf', student=None,
[9906]601                  studentview=None,
[10439]602                  tableheader=[], tabledata=[],
[9555]603                  note=None, signatures=None, sigs_in_footer=(),
[10250]604                  show_scans=True, topMargin=1.5,
605                  omit_fields=()):
[7841]606        """Render pdf slips for various pages.
607        """
[10261]608        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
[9916]609        # XXX: tell what the different parameters mean
[8112]610        style = getSampleStyleSheet()
[9949]611        creator = self.getPDFCreator(student)
[8112]612        data = []
613        doc_title = view.label
614        author = '%s (%s)' % (view.request.principal.title,
615                              view.request.principal.id)
[9913]616        footer_text = view.label.split('\n')
617        if len(footer_text) > 2:
618            # We can add a department in first line
619            footer_text = footer_text[1]
620        else:
[9917]621            # Only the first line is used for the footer
[9913]622            footer_text = footer_text[0]
[7714]623        if getattr(student, 'student_id', None) is not None:
[7310]624            footer_text = "%s - %s - " % (student.student_id, footer_text)
[7150]625
[7318]626        # Insert student data table
[7310]627        if student is not None:
[8112]628            bd_translation = trans(_('Base Data'), portal_language)
[9910]629            data.append(Paragraph(bd_translation, HEADING_STYLE))
[10261]630            data.append(render_student_data(
631                studentview, omit_fields, lang=portal_language))
[7304]632
[7318]633        # Insert widgets
[9191]634        if view.form_fields:
[9910]635            data.append(Paragraph(view.title, HEADING_STYLE))
[9191]636            separators = getattr(self, 'SEPARATORS_DICT', {})
637            table = creator.getWidgetsTable(
638                view.form_fields, view.context, None, lang=portal_language,
639                separators=separators)
640            data.append(table)
[7318]641
[8112]642        # Insert scanned docs
[9550]643        if show_scans:
644            data.extend(docs_as_flowables(view, portal_language))
[7318]645
[9452]646        # Insert history
[9910]647        if filename.startswith('clearance'):
[9452]648            hist_translation = trans(_('Workflow History'), portal_language)
[9910]649            data.append(Paragraph(hist_translation, HEADING_STYLE))
[9452]650            data.extend(creator.fromStringList(student.history.messages))
651
[10438]652        # Insert content tables (optionally on second page)
[10439]653        if hasattr(view, 'tabletitle'):
654            for i in range(len(view.tabletitle)):
655                if tabledata[i] and tableheader[i]:
656                    #data.append(PageBreak())
657                    #data.append(Spacer(1, 20))
658                    data.append(Paragraph(view.tabletitle[i], HEADING_STYLE))
659                    data.append(Spacer(1, 8))
660                    contenttable = render_table_data(tableheader[i],tabledata[i])
661                    data.append(contenttable)
[7318]662
[9010]663        # Insert signatures
[9965]664        # XXX: We are using only sigs_in_footer in waeup.kofa, so we
665        # do not have a test for the following lines.
[9555]666        if signatures and not sigs_in_footer:
[9010]667            data.append(Spacer(1, 20))
[9966]668            # Render one signature table per signature to
669            # get date and signature in line.
670            for signature in signatures:
671                signaturetables = get_signature_tables(signature)
672                data.append(signaturetables[0])
[9010]673
[7150]674        view.response.setHeader(
675            'Content-Type', 'application/pdf')
[8112]676        try:
677            pdf_stream = creator.create_pdf(
[8257]678                data, None, doc_title, author=author, footer=footer_text,
[9948]679                note=note, sigs_in_footer=sigs_in_footer, topMargin=topMargin)
[8112]680        except IOError:
681            view.flash('Error in image file.')
682            return view.redirect(view.url(view.context))
683        return pdf_stream
[7620]684
[10578]685    gpa_boundaries = ((1, 'Fail'),
686                      (1.5, 'Pass'),
687                      (2.4, '3rd Class'),
688                      (3.5, '2nd Class Lower'),
689                      (4.5, '2nd Class Upper'),
690                      (5, '1st Class'))
[10576]691
[10445]692    def getClassFromCGPA(self, gpa):
[10578]693        if gpa < self.gpa_boundaries[0][0]:
694            return 0, self.gpa_boundaries[0][1]
695        if gpa < self.gpa_boundaries[1][0]:
696            return 1, self.gpa_boundaries[1][1]
697        if gpa < self.gpa_boundaries[2][0]:
698            return 2, self.gpa_boundaries[2][1]
699        if gpa < self.gpa_boundaries[3][0]:
700            return 3, self.gpa_boundaries[3][1]
701        if gpa < self.gpa_boundaries[4][0]:
702            return 4, self.gpa_boundaries[4][1]
703        if gpa <= self.gpa_boundaries[5][0]:
704            return 5, self.gpa_boundaries[5][1]
705        return 'N/A'
[10445]706
[10250]707    def renderPDFTranscript(self, view, filename='transcript.pdf',
708                  student=None,
709                  studentview=None,
710                  note=None, signatures=None, sigs_in_footer=(),
711                  show_scans=True, topMargin=1.5,
712                  omit_fields=(),
713                  tableheader=None):
714        """Render pdf slips for transcript.
715        """
[10261]716        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
[10250]717        # XXX: tell what the different parameters mean
718        style = getSampleStyleSheet()
719        creator = self.getPDFCreator(student)
720        data = []
721        doc_title = view.label
722        author = '%s (%s)' % (view.request.principal.title,
723                              view.request.principal.id)
724        footer_text = view.label.split('\n')
725        if len(footer_text) > 2:
726            # We can add a department in first line
727            footer_text = footer_text[1]
728        else:
729            # Only the first line is used for the footer
730            footer_text = footer_text[0]
731        if getattr(student, 'student_id', None) is not None:
732            footer_text = "%s - %s - " % (student.student_id, footer_text)
733
734        # Insert student data table
735        if student is not None:
736            #bd_translation = trans(_('Base Data'), portal_language)
737            #data.append(Paragraph(bd_translation, HEADING_STYLE))
[10261]738            data.append(render_student_data(
739                studentview, omit_fields, lang=portal_language))
[10250]740
741
742        transcript_data = view.context.getTranscriptData()
743        levels_data = transcript_data[0]
744        gpa = transcript_data[1]
745
746        contextdata = []
[10261]747        f_label = trans(_('Course of Study:'), portal_language)
[10250]748        f_label = Paragraph(f_label, ENTRY1_STYLE)
[10650]749        f_text = formatted_text(view.context.certificate.longtitle)
[10250]750        f_text = Paragraph(f_text, ENTRY1_STYLE)
751        contextdata.append([f_label,f_text])
752
[10261]753        f_label = trans(_('Faculty:'), portal_language)
[10250]754        f_label = Paragraph(f_label, ENTRY1_STYLE)
755        f_text = formatted_text(
[10650]756            view.context.certificate.__parent__.__parent__.__parent__.longtitle)
[10250]757        f_text = Paragraph(f_text, ENTRY1_STYLE)
758        contextdata.append([f_label,f_text])
759
[10261]760        f_label = trans(_('Department:'), portal_language)
[10250]761        f_label = Paragraph(f_label, ENTRY1_STYLE)
762        f_text = formatted_text(
[10650]763            view.context.certificate.__parent__.__parent__.longtitle)
[10250]764        f_text = Paragraph(f_text, ENTRY1_STYLE)
765        contextdata.append([f_label,f_text])
766
[10261]767        f_label = trans(_('Entry Session:'), portal_language)
[10250]768        f_label = Paragraph(f_label, ENTRY1_STYLE)
[10256]769        f_text = formatted_text(
770            view.session_dict.get(view.context.entry_session))
[10250]771        f_text = Paragraph(f_text, ENTRY1_STYLE)
772        contextdata.append([f_label,f_text])
773
[10261]774        f_label = trans(_('Entry Mode:'), portal_language)
[10250]775        f_label = Paragraph(f_label, ENTRY1_STYLE)
[10256]776        f_text = formatted_text(view.studymode_dict.get(
777            view.context.entry_mode))
[10250]778        f_text = Paragraph(f_text, ENTRY1_STYLE)
779        contextdata.append([f_label,f_text])
780
[10262]781        f_label = trans(_('Cumulative GPA:'), portal_language)
[10250]782        f_label = Paragraph(f_label, ENTRY1_STYLE)
[10576]783        f_text = formatted_text('%s (%s)' % (gpa, self.getClassFromCGPA(gpa)[1]))
[10250]784        f_text = Paragraph(f_text, ENTRY1_STYLE)
785        contextdata.append([f_label,f_text])
786
787        contexttable = Table(contextdata,style=SLIP_STYLE)
788        data.append(contexttable)
789
790        transcripttables = render_transcript_data(
[10261]791            view, tableheader, levels_data, lang=portal_language)
[10250]792        data.extend(transcripttables)
793
794        # Insert signatures
795        # XXX: We are using only sigs_in_footer in waeup.kofa, so we
796        # do not have a test for the following lines.
797        if signatures and not sigs_in_footer:
798            data.append(Spacer(1, 20))
799            # Render one signature table per signature to
800            # get date and signature in line.
801            for signature in signatures:
802                signaturetables = get_signature_tables(signature)
803                data.append(signaturetables[0])
804
805        view.response.setHeader(
806            'Content-Type', 'application/pdf')
807        try:
808            pdf_stream = creator.create_pdf(
809                data, None, doc_title, author=author, footer=footer_text,
810                note=note, sigs_in_footer=sigs_in_footer, topMargin=topMargin)
811        except IOError:
[10261]812            view.flash(_('Error in image file.'))
[10250]813            return view.redirect(view.url(view.context))
814        return pdf_stream
815
[9830]816    def maxCredits(self, studylevel):
817        """Return maximum credits.
[9532]818
[9830]819        In some universities maximum credits is not constant, it
820        depends on the student's study level.
821        """
822        return 50
823
[9532]824    def maxCreditsExceeded(self, studylevel, course):
[9830]825        max_credits = self.maxCredits(studylevel)
826        if max_credits and \
827            studylevel.total_credits + course.credits > max_credits:
828            return max_credits
[9532]829        return 0
830
[9987]831    def getBedCoordinates(self, bedticket):
832        """Return bed coordinates.
833
834        This method can be used to customize the display_coordinates
835        property method.
836        """
837        return bedticket.bed_coordinates
838
[7841]839    VERDICTS_DICT = {
[8820]840        '0': _('(not yet)'),
[7841]841        'A': 'Successful student',
842        'B': 'Student with carryover courses',
843        'C': 'Student on probation',
844        }
[8099]845
846    SEPARATORS_DICT = {
847        }
[8410]848
[10021]849    SKIP_UPLOAD_VIEWLETS = ()
850
[10803]851    PWCHANGE_STATES = (ADMITTED,)
[10706]852
[8410]853    #: A prefix used when generating new student ids. Each student id will
854    #: start with this string. The default is 'K' for ``Kofa``.
855    STUDENT_ID_PREFIX = u'K'
Note: See TracBrowser for help on using the repository browser.