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

Last change on this file since 15332 was 15332, checked in by Henrik Bettermann, 6 years ago

Set getattr default value.

  • Property svn:keywords set to Id
File size: 47.9 KB
Line 
1## $Id: utils.py 15332 2019-02-15 21:21:00Z 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##
18"""General helper functions and utilities for the students section.
19"""
20import grok
21import textwrap
22from time import time
23from cStringIO import StringIO
24from reportlab.lib import colors
25from reportlab.lib.units import cm
26from reportlab.lib.pagesizes import A4
27from reportlab.lib.styles import getSampleStyleSheet
28from reportlab.platypus import Paragraph, Image, Table, Spacer
29from reportlab.platypus.doctemplate import LayoutError
30from zope.event import notify
31from zope.schema.interfaces import ConstraintNotSatisfied
32from zope.component import getUtility, createObject
33from zope.formlib.form import setUpEditWidgets
34from zope.i18n import translate
35from waeup.kofa.interfaces import (
36    IExtFileStore, IKofaUtils, RETURNING, PAID, CLEARED,
37    academic_sessions_vocab, IFileStoreNameChooser)
38from waeup.kofa.interfaces import MessageFactory as _
39from waeup.kofa.students.interfaces import IStudentsUtils
40from waeup.kofa.students.workflow import ADMITTED
41from waeup.kofa.students.vocabularies import StudyLevelSource, MatNumNotInSource
42from waeup.kofa.browser.pdf import (
43    ENTRY1_STYLE, format_html, NOTE_STYLE, HEADING_STYLE,
44    get_signature_tables, get_qrcode)
45from waeup.kofa.browser.interfaces import IPDFCreator
46from waeup.kofa.utils.helpers import to_timezone
47
48SLIP_STYLE = [
49    ('VALIGN',(0,0),(-1,-1),'TOP'),
50    #('FONT', (0,0), (-1,-1), 'Helvetica', 11),
51    ]
52
53CONTENT_STYLE = [
54    ('VALIGN',(0,0),(-1,-1),'TOP'),
55    #('FONT', (0,0), (-1,-1), 'Helvetica', 8),
56    #('TEXTCOLOR',(0,0),(-1,0),colors.white),
57    #('BACKGROUND',(0,0),(-1,0),colors.black),
58    ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
59    ('BOX', (0,0), (-1,-1), 1, colors.black),
60    ]
61
62FONT_SIZE = 10
63FONT_COLOR = 'black'
64
65def trans(text, lang):
66    # shortcut
67    return translate(text, 'waeup.kofa', target_language=lang)
68
69def formatted_text(text, color=FONT_COLOR, lang='en'):
70    """Turn `text`, `color` and `size` into an HTML snippet.
71
72    The snippet is suitable for use with reportlab and generating PDFs.
73    Wraps the `text` into a ``<font>`` tag with passed attributes.
74
75    Also non-strings are converted. Raw strings are expected to be
76    utf-8 encoded (usually the case for widgets etc.).
77
78    Finally, a br tag is added if widgets contain div tags
79    which are not supported by reportlab.
80
81    The returned snippet is unicode type.
82    """
83    if not isinstance(text, unicode):
84        if isinstance(text, basestring):
85            text = text.decode('utf-8')
86        else:
87            text = unicode(text)
88    if text == 'None':
89        text = ''
90    # Very long matriculation numbers need to be wrapped
91    if text.find(' ') == -1 and len(text.split('/')) > 6:
92        text = '/'.join(text.split('/')[:5]) + \
93            '/ ' + '/'.join(text.split('/')[5:])
94    # Mainly for boolean values we need our customized
95    # localisation of the zope domain
96    text = translate(text, 'zope', target_language=lang)
97    text = text.replace('</div>', '<br /></div>')
98    tag1 = u'<font color="%s">' % (color)
99    return tag1 + u'%s</font>' % text
100
101def generate_student_id():
102    students = grok.getSite()['students']
103    new_id = students.unique_student_id
104    return new_id
105
106def set_up_widgets(view, ignore_request=False):
107    view.adapters = {}
108    view.widgets = setUpEditWidgets(
109        view.form_fields, view.prefix, view.context, view.request,
110        adapters=view.adapters, for_display=True,
111        ignore_request=ignore_request
112        )
113
114def render_student_data(studentview, context, omit_fields=(),
115                        lang='en', slipname=None, no_passport=False):
116    """Render student table for an existing frame.
117    """
118    width, height = A4
119    set_up_widgets(studentview, ignore_request=True)
120    data_left = []
121    data_middle = []
122    style = getSampleStyleSheet()
123    img = getUtility(IExtFileStore).getFileByContext(
124        studentview.context, attr='passport.jpg')
125    if img is None:
126        from waeup.kofa.browser import DEFAULT_PASSPORT_IMAGE_PATH
127        img = open(DEFAULT_PASSPORT_IMAGE_PATH, 'rb')
128    doc_img = Image(img.name, width=4*cm, height=4*cm, kind='bound')
129    data_left.append([doc_img])
130    #data.append([Spacer(1, 12)])
131
132    f_label = trans(_('Name:'), lang)
133    f_label = Paragraph(f_label, ENTRY1_STYLE)
134    f_text = formatted_text(studentview.context.display_fullname)
135    f_text = Paragraph(f_text, ENTRY1_STYLE)
136    data_middle.append([f_label,f_text])
137
138    for widget in studentview.widgets:
139        if 'name' in widget.name:
140            continue
141        f_label = translate(
142            widget.label.strip(), 'waeup.kofa',
143            target_language=lang)
144        f_label = Paragraph('%s:' % f_label, ENTRY1_STYLE)
145        f_text = formatted_text(widget(), lang=lang)
146        f_text = Paragraph(f_text, ENTRY1_STYLE)
147        data_middle.append([f_label,f_text])
148
149    if getattr(studentview.context, 'certcode', None):
150        if not 'certificate' in omit_fields:
151            f_label = trans(_('Study Course:'), lang)
152            f_label = Paragraph(f_label, ENTRY1_STYLE)
153            f_text = formatted_text(
154                studentview.context['studycourse'].certificate.longtitle)
155            f_text = Paragraph(f_text, ENTRY1_STYLE)
156            data_middle.append([f_label,f_text])
157        if not 'department' in omit_fields:
158            f_label = trans(_('Department:'), lang)
159            f_label = Paragraph(f_label, ENTRY1_STYLE)
160            f_text = formatted_text(
161                studentview.context[
162                'studycourse'].certificate.__parent__.__parent__.longtitle,
163                )
164            f_text = Paragraph(f_text, ENTRY1_STYLE)
165            data_middle.append([f_label,f_text])
166        if not 'faculty' in omit_fields:
167            f_label = trans(_('Faculty:'), lang)
168            f_label = Paragraph(f_label, ENTRY1_STYLE)
169            f_text = formatted_text(
170                studentview.context[
171                'studycourse'].certificate.__parent__.__parent__.__parent__.longtitle,
172                )
173            f_text = Paragraph(f_text, ENTRY1_STYLE)
174            data_middle.append([f_label,f_text])
175        if not 'current_mode' in omit_fields:
176            studymodes_dict = getUtility(IKofaUtils).STUDY_MODES_DICT
177            sm = studymodes_dict[studentview.context.current_mode]
178            f_label = trans(_('Study Mode:'), lang)
179            f_label = Paragraph(f_label, ENTRY1_STYLE)
180            f_text = formatted_text(sm)
181            f_text = Paragraph(f_text, ENTRY1_STYLE)
182            data_middle.append([f_label,f_text])
183        if not 'entry_session' in omit_fields:
184            f_label = trans(_('Entry Session:'), lang)
185            f_label = Paragraph(f_label, ENTRY1_STYLE)
186            entry_session = studentview.context.entry_session
187            entry_session = academic_sessions_vocab.getTerm(entry_session).title
188            f_text = formatted_text(entry_session)
189            f_text = Paragraph(f_text, ENTRY1_STYLE)
190            data_middle.append([f_label,f_text])
191        # Requested by Uniben, does not really make sense
192        if not 'current_level' in omit_fields:
193            f_label = trans(_('Current Level:'), lang)
194            f_label = Paragraph(f_label, ENTRY1_STYLE)
195            current_level = studentview.context['studycourse'].current_level
196            studylevelsource = StudyLevelSource().factory
197            current_level = studylevelsource.getTitle(
198                studentview.context, current_level)
199            f_text = formatted_text(current_level)
200            f_text = Paragraph(f_text, ENTRY1_STYLE)
201            data_middle.append([f_label,f_text])
202        if not 'date_of_birth' in omit_fields:
203            f_label = trans(_('Date of Birth:'), lang)
204            f_label = Paragraph(f_label, ENTRY1_STYLE)
205            date_of_birth = studentview.context.date_of_birth
206            tz = getUtility(IKofaUtils).tzinfo
207            date_of_birth = to_timezone(date_of_birth, tz)
208            if date_of_birth is not None:
209                date_of_birth = date_of_birth.strftime("%d/%m/%Y")
210            f_text = formatted_text(date_of_birth)
211            f_text = Paragraph(f_text, ENTRY1_STYLE)
212            data_middle.append([f_label,f_text])
213
214    if no_passport:
215        table = Table(data_middle,style=SLIP_STYLE)
216        table.hAlign = 'LEFT'
217        return table
218
219    # append QR code to the right
220    if slipname:
221        url = studentview.url(context, slipname)
222        data_right = [[get_qrcode(url, width=70.0)]]
223        table_right = Table(data_right,style=SLIP_STYLE)
224    else:
225        table_right = None
226
227    table_left = Table(data_left,style=SLIP_STYLE)
228    table_middle = Table(data_middle,style=SLIP_STYLE, colWidths=[5*cm, 5*cm])
229    table = Table([[table_left, table_middle, table_right],],style=SLIP_STYLE)
230    return table
231
232def render_table_data(tableheader, tabledata, lang='en'):
233    """Render children table for an existing frame.
234    """
235    data = []
236    #data.append([Spacer(1, 12)])
237    line = []
238    style = getSampleStyleSheet()
239    for element in tableheader:
240        field = '<strong>%s</strong>' % formatted_text(element[0], lang=lang)
241        field = Paragraph(field, style["Normal"])
242        line.append(field)
243    data.append(line)
244    for ticket in tabledata:
245        line = []
246        for element in tableheader:
247              field = formatted_text(getattr(ticket,element[1],u' '))
248              field = Paragraph(field, style["Normal"])
249              line.append(field)
250        data.append(line)
251    table = Table(data,colWidths=[
252        element[2]*cm for element in tableheader], style=CONTENT_STYLE)
253    return table
254
255def render_transcript_data(view, tableheader, levels_data, lang='en'):
256    """Render children table for an existing frame.
257    """
258    data = []
259    style = getSampleStyleSheet()
260    format_float = getUtility(IKofaUtils).format_float
261    for level in levels_data:
262        level_obj = level['level']
263        tickets = level['tickets_1'] + level['tickets_2'] + level['tickets_3']
264        headerline = []
265        tabledata = []
266        if 'evel' in view.level_dict.get('ticket.level', str(level_obj.level)):
267            subheader = '%s %s, %s' % (
268                trans(_('Session'), lang),
269                view.session_dict[level_obj.level_session],
270                view.level_dict.get('ticket.level', str(level_obj.level)))
271        else:
272            subheader = '%s %s, %s %s' % (
273                trans(_('Session'), lang),
274                view.session_dict[level_obj.level_session],
275                trans(_('Level'), lang),
276                view.level_dict.get(level_obj.level, str(level_obj.level)))
277        data.append(Paragraph(subheader, HEADING_STYLE))
278        for element in tableheader:
279            field = '<strong>%s</strong>' % formatted_text(element[0])
280            field = Paragraph(field, style["Normal"])
281            headerline.append(field)
282        tabledata.append(headerline)
283        for ticket in tickets:
284            ticketline = []
285            for element in tableheader:
286                  field = formatted_text(getattr(ticket,element[1],u' '))
287                  field = Paragraph(field, style["Normal"])
288                  ticketline.append(field)
289            tabledata.append(ticketline)
290        table = Table(tabledata,colWidths=[
291            element[2]*cm for element in tableheader], style=CONTENT_STYLE)
292        data.append(table)
293        sgpa = format_float(level['sgpa'], 2)
294        sgpa = '%s: %s' % (trans('Sessional GPA (rectified)', lang), sgpa)
295        #sgpa = '%s: %.2f' % (trans('Sessional GPA (rectified)', lang), level['sgpa'])
296        data.append(Paragraph(sgpa, style["Normal"]))
297        if getattr(level_obj, 'remark', None):
298            remark = '%s: %s' % (
299                trans('Remark', lang), getattr(level_obj, 'remark'))
300            data.append(Paragraph(remark, style["Normal"]))
301    return data
302
303def docs_as_flowables(view, lang='en'):
304    """Create reportlab flowables out of scanned docs.
305    """
306    # XXX: fix circular import problem
307    from waeup.kofa.browser.fileviewlets import FileManager
308    from waeup.kofa.browser import DEFAULT_IMAGE_PATH
309    style = getSampleStyleSheet()
310    data = []
311
312    # Collect viewlets
313    fm = FileManager(view.context, view.request, view)
314    fm.update()
315    if fm.viewlets:
316        sc_translation = trans(_('Scanned Documents'), lang)
317        data.append(Paragraph(sc_translation, HEADING_STYLE))
318        # Insert list of scanned documents
319        table_data = []
320        for viewlet in fm.viewlets:
321            if viewlet.file_exists:
322                # Show viewlet only if file exists
323                f_label = Paragraph(trans(viewlet.label, lang), ENTRY1_STYLE)
324                img_path = getattr(getUtility(IExtFileStore).getFileByContext(
325                    view.context, attr=viewlet.download_name), 'name', None)
326                #f_text = Paragraph(trans(_('(not provided)'),lang), ENTRY1_STYLE)
327                if img_path is None:
328                    pass
329                elif not img_path[-4:] in ('.jpg', '.JPG'):
330                    # reportlab requires jpg images, I think.
331                    f_text = Paragraph('%s (not displayable)' % (
332                        viewlet.title,), ENTRY1_STYLE)
333                else:
334                    f_text = Image(img_path, width=2*cm, height=1*cm, kind='bound')
335                table_data.append([f_label, f_text])
336        if table_data:
337            # safety belt; empty tables lead to problems.
338            data.append(Table(table_data, style=SLIP_STYLE))
339    return data
340
341class StudentsUtils(grok.GlobalUtility):
342    """A collection of methods subject to customization.
343    """
344    grok.implements(IStudentsUtils)
345
346    def getReturningData(self, student):
347        """ Define what happens after school fee payment
348        depending on the student's senate verdict.
349        In the base configuration current level is always increased
350        by 100 no matter which verdict has been assigned.
351        """
352        new_level = student['studycourse'].current_level + 100
353        new_session = student['studycourse'].current_session + 1
354        return new_session, new_level
355
356    def setReturningData(self, student):
357        """ Define what happens after school fee payment
358        depending on the student's senate verdict.
359        This method folllows the same algorithm as `getReturningData` but
360        it also sets the new values.
361        """
362        new_session, new_level = self.getReturningData(student)
363        try:
364            student['studycourse'].current_level = new_level
365        except ConstraintNotSatisfied:
366            # Do not change level if level exceeds the
367            # certificate's end_level.
368            pass
369        student['studycourse'].current_session = new_session
370        verdict = student['studycourse'].current_verdict
371        student['studycourse'].current_verdict = '0'
372        student['studycourse'].previous_verdict = verdict
373        return
374
375    def _getSessionConfiguration(self, session):
376        try:
377            return grok.getSite()['configuration'][str(session)]
378        except KeyError:
379            return None
380
381    def _isPaymentDisabled(self, p_session, category, student):
382        academic_session = self._getSessionConfiguration(p_session)
383        if category == 'schoolfee' and \
384            'sf_all' in academic_session.payment_disabled:
385            return True
386        return False
387
388    def samePaymentMade(self, student, category, p_item, p_session):
389        for key in student['payments'].keys():
390            ticket = student['payments'][key]
391            if ticket.p_state == 'paid' and\
392               ticket.p_category == category and \
393               ticket.p_item == p_item and \
394               ticket.p_session == p_session:
395                  return True
396        return False
397
398    def setPaymentDetails(self, category, student,
399            previous_session, previous_level):
400        """Create a payment ticket and set the payment data of a
401        student for the payment category specified.
402        """
403        p_item = u''
404        amount = 0.0
405        if previous_session:
406            if previous_session < student['studycourse'].entry_session:
407                return _('The previous session must not fall below '
408                         'your entry session.'), None
409            if category == 'schoolfee':
410                # School fee is always paid for the following session
411                if previous_session > student['studycourse'].current_session:
412                    return _('This is not a previous session.'), None
413            else:
414                if previous_session > student['studycourse'].current_session - 1:
415                    return _('This is not a previous session.'), None
416            p_session = previous_session
417            p_level = previous_level
418            p_current = False
419        else:
420            p_session = student['studycourse'].current_session
421            p_level = student['studycourse'].current_level
422            p_current = True
423        academic_session = self._getSessionConfiguration(p_session)
424        if academic_session == None:
425            return _(u'Session configuration object is not available.'), None
426        # Determine fee.
427        if category == 'schoolfee':
428            try:
429                certificate = student['studycourse'].certificate
430                p_item = certificate.code
431            except (AttributeError, TypeError):
432                return _('Study course data are incomplete.'), None
433            if previous_session:
434                # Students can pay for previous sessions in all
435                # workflow states.  Fresh students are excluded by the
436                # update method of the PreviousPaymentAddFormPage.
437                if previous_level == 100:
438                    amount = getattr(certificate, 'school_fee_1', 0.0)
439                else:
440                    amount = getattr(certificate, 'school_fee_2', 0.0)
441            else:
442                if student.state == CLEARED:
443                    amount = getattr(certificate, 'school_fee_1', 0.0)
444                elif student.state == RETURNING:
445                    # In case of returning school fee payment the
446                    # payment session and level contain the values of
447                    # the session the student has paid for. Payment
448                    # session is always next session.
449                    p_session, p_level = self.getReturningData(student)
450                    academic_session = self._getSessionConfiguration(p_session)
451                    if academic_session == None:
452                        return _(
453                            u'Session configuration object is not available.'
454                            ), None
455                    amount = getattr(certificate, 'school_fee_2', 0.0)
456                elif student.is_postgrad and student.state == PAID:
457                    # Returning postgraduate students also pay for the
458                    # next session but their level always remains the
459                    # same.
460                    p_session += 1
461                    academic_session = self._getSessionConfiguration(p_session)
462                    if academic_session == None:
463                        return _(
464                            u'Session configuration object is not available.'
465                            ), None
466                    amount = getattr(certificate, 'school_fee_2', 0.0)
467        elif category == 'clearance':
468            try:
469                p_item = student['studycourse'].certificate.code
470            except (AttributeError, TypeError):
471                return _('Study course data are incomplete.'), None
472            amount = academic_session.clearance_fee
473        elif category == 'bed_allocation':
474            p_item = self.getAccommodationDetails(student)['bt']
475            amount = academic_session.booking_fee
476        elif category == 'hostel_maintenance':
477            amount = 0.0
478            bedticket = student['accommodation'].get(
479                str(student.current_session), None)
480            if bedticket is not None and bedticket.bed is not None:
481                p_item = bedticket.bed_coordinates
482                if bedticket.bed.__parent__.maint_fee > 0:
483                    amount = bedticket.bed.__parent__.maint_fee
484                else:
485                    # fallback
486                    amount = academic_session.maint_fee
487            else:
488                return _(u'No bed allocated.'), None
489        elif category == 'transcript':
490            amount = academic_session.transcript_fee
491        elif category == 'transfer':
492            amount = academic_session.transfer_fee
493        elif category == 'late_registration':
494            amount = academic_session.late_registration_fee
495        if amount in (0.0, None):
496            return _('Amount could not be determined.'), None
497        if self.samePaymentMade(student, category, p_item, p_session):
498            return _('This type of payment has already been made.'), None
499        if self._isPaymentDisabled(p_session, category, student):
500            return _('This category of payments has been disabled.'), None
501        payment = createObject(u'waeup.StudentOnlinePayment')
502        timestamp = ("%d" % int(time()*10000))[1:]
503        payment.p_id = "p%s" % timestamp
504        payment.p_category = category
505        payment.p_item = p_item
506        payment.p_session = p_session
507        payment.p_level = p_level
508        payment.p_current = p_current
509        payment.amount_auth = amount
510        return None, payment
511
512    def setBalanceDetails(self, category, student,
513            balance_session, balance_level, balance_amount):
514        """Create a balance payment ticket and set the payment data
515        as selected by the student.
516        """
517        p_item = u'Balance'
518        p_session = balance_session
519        p_level = balance_level
520        p_current = False
521        amount = balance_amount
522        academic_session = self._getSessionConfiguration(p_session)
523        if academic_session == None:
524            return _(u'Session configuration object is not available.'), None
525        if amount in (0.0, None) or amount < 0:
526            return _('Amount must be greater than 0.'), None
527        payment = createObject(u'waeup.StudentOnlinePayment')
528        timestamp = ("%d" % int(time()*10000))[1:]
529        payment.p_id = "p%s" % timestamp
530        payment.p_category = category
531        payment.p_item = p_item
532        payment.p_session = p_session
533        payment.p_level = p_level
534        payment.p_current = p_current
535        payment.amount_auth = amount
536        return None, payment
537
538    def increaseMatricInteger(self, student):
539        """Increase counter for matric numbers.
540        This counter can be a centrally stored attribute or an attribute of
541        faculties, departments or certificates. In the base package the counter
542        is as an attribute of the site configuration container.
543        """
544        grok.getSite()['configuration'].next_matric_integer += 1
545        return
546
547    def constructMatricNumber(self, student):
548        """Fetch the matric number counter which fits the student and
549        construct the new matric number of the student.
550        In the base package the counter is returned which is as an attribute
551        of the site configuration container.
552        """
553        next_integer = grok.getSite()['configuration'].next_matric_integer
554        if next_integer == 0:
555            return _('Matriculation number cannot be set.'), None
556        return None, unicode(next_integer)
557
558    def setMatricNumber(self, student):
559        """Set matriculation number of student. If the student's matric number
560        is unset a new matric number is
561        constructed according to the matriculation number construction rules
562        defined in the `constructMatricNumber` method. The new matric number is
563        set, the students catalog updated. The corresponding matric number
564        counter is increased by one.
565
566        This method is tested but not used in the base package. It can
567        be used in custom packages by adding respective views
568        and by customizing `increaseMatricInteger` and `constructMatricNumber`
569        according to the university's matriculation number construction rules.
570
571        The method can be disabled by setting the counter to zero.
572        """
573        if student.matric_number is not None:
574            return _('Matriculation number already set.'), None
575        if student.certcode is None:
576            return _('No certificate assigned.'), None
577        error, matric_number = self.constructMatricNumber(student)
578        if error:
579            return error, None
580        try:
581            student.matric_number = matric_number
582        except MatNumNotInSource:
583            return _('Matriculation number %s exists.' % matric_number), None
584        notify(grok.ObjectModifiedEvent(student))
585        self.increaseMatricInteger(student)
586        return None, matric_number
587
588    def getAccommodationDetails(self, student):
589        """Determine the accommodation data of a student.
590        """
591        d = {}
592        d['error'] = u''
593        hostels = grok.getSite()['hostels']
594        d['booking_session'] = hostels.accommodation_session
595        d['allowed_states'] = hostels.accommodation_states
596        d['startdate'] = hostels.startdate
597        d['enddate'] = hostels.enddate
598        d['expired'] = hostels.expired
599        # Determine bed type
600        studycourse = student['studycourse']
601        certificate = getattr(studycourse,'certificate',None)
602        entry_session = studycourse.entry_session
603        current_level = studycourse.current_level
604        if None in (entry_session, current_level, certificate):
605            return d
606        end_level = certificate.end_level
607        if current_level == 10:
608            bt = 'pr'
609        elif entry_session == grok.getSite()['hostels'].accommodation_session:
610            bt = 'fr'
611        elif current_level >= end_level:
612            bt = 'fi'
613        else:
614            bt = 're'
615        if student.sex == 'f':
616            sex = 'female'
617        else:
618            sex = 'male'
619        special_handling = 'regular'
620        d['bt'] = u'%s_%s_%s' % (special_handling,sex,bt)
621        return d
622
623    def checkAccommodationRequirements(self, student, acc_details):
624        if acc_details.get('expired', False):
625            startdate = acc_details.get('startdate')
626            enddate = acc_details.get('enddate')
627            if startdate and enddate:
628                tz = getUtility(IKofaUtils).tzinfo
629                startdate = to_timezone(
630                    startdate, tz).strftime("%d/%m/%Y %H:%M:%S")
631                enddate = to_timezone(
632                    enddate, tz).strftime("%d/%m/%Y %H:%M:%S")
633                return _("Outside booking period: ${a} - ${b}",
634                         mapping = {'a': startdate, 'b': enddate})
635            else:
636                return _("Outside booking period.")
637        if not acc_details.get('bt'):
638            return _("Your data are incomplete.")
639        if not student.state in acc_details['allowed_states']:
640            return _("You are in the wrong registration state.")
641        if student['studycourse'].current_session != acc_details[
642            'booking_session']:
643            return _('Your current session does not '
644                     'match accommodation session.')
645        bsession = str(acc_details['booking_session'])
646        if bsession in student['accommodation'].keys() \
647            and not 'booking expired' in \
648            student['accommodation'][bsession].bed_coordinates:
649            return _('You already booked a bed space in '
650                     'current accommodation session.')
651        return
652
653    def selectBed(self, available_beds, desired_hostel=None):
654        """Select a bed from a filtered list of available beds.
655        In the base configuration beds are sorted by the sort id
656        of the hostel and the bed number. The first bed found in
657        this sorted list is taken.
658        """
659        sorted_beds = sorted(available_beds,
660                key=lambda bed: 1000 * bed.__parent__.sort_id + bed.bed_number)
661        if desired_hostel and desired_hostel != 'no':
662            # Filter desired hostel beds
663            filtered_beds = [bed for bed in sorted_beds
664                             if bed.bed_id.startswith(desired_hostel)]
665            if not filtered_beds:
666                return
667            return filtered_beds[0]
668        return sorted_beds[0]
669
670    def _admissionText(self, student, portal_language):
671        inst_name = grok.getSite()['configuration'].name
672        text = trans(_(
673            'This is to inform you that you have been provisionally'
674            ' admitted into ${a} as follows:', mapping = {'a': inst_name}),
675            portal_language)
676        return text
677
678    def renderPDFAdmissionLetter(self, view, student=None, omit_fields=(),
679                                 pre_text=None, post_text=None,):
680        """Render pdf admission letter.
681        """
682        if student is None:
683            return
684        style = getSampleStyleSheet()
685        creator = self.getPDFCreator(student)
686        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
687        data = []
688        doc_title = view.label
689        author = '%s (%s)' % (view.request.principal.title,
690                              view.request.principal.id)
691        footer_text = view.label.split('\n')
692        if len(footer_text) > 1:
693            # We can add a department in first line
694            footer_text = footer_text[1]
695        else:
696            # Only the first line is used for the footer
697            footer_text = footer_text[0]
698        if getattr(student, 'student_id', None) is not None:
699            footer_text = "%s - %s - " % (student.student_id, footer_text)
700
701        # Text before student data
702        if pre_text is None:
703            html = format_html(self._admissionText(student, portal_language))
704        else:
705            html = format_html(pre_text)
706        if html:
707            data.append(Paragraph(html, NOTE_STYLE))
708            data.append(Spacer(1, 20))
709
710        # Student data
711        data.append(render_student_data(view, student,
712                    omit_fields, lang=portal_language,
713                    slipname='admission_slip.pdf'))
714
715        # Text after student data
716        data.append(Spacer(1, 20))
717        if post_text is None:
718            datelist = student.history.messages[0].split()[0].split('-')
719            creation_date = u'%s/%s/%s' % (datelist[2], datelist[1], datelist[0])
720            post_text = trans(_(
721                'Your Kofa student record was created on ${a}.',
722                mapping = {'a': creation_date}),
723                portal_language)
724        #html = format_html(post_text)
725        #data.append(Paragraph(html, NOTE_STYLE))
726
727        # Create pdf stream
728        view.response.setHeader(
729            'Content-Type', 'application/pdf')
730        pdf_stream = creator.create_pdf(
731            data, None, doc_title, author=author, footer=footer_text,
732            note=post_text)
733        return pdf_stream
734
735    def getPDFCreator(self, context):
736        """Get a pdf creator suitable for `context`.
737        The default implementation always returns the default creator.
738        """
739        return getUtility(IPDFCreator)
740
741    def renderPDF(self, view, filename='slip.pdf', student=None,
742                  studentview=None,
743                  tableheader=[], tabledata=[],
744                  note=None, signatures=None, sigs_in_footer=(),
745                  show_scans=True, topMargin=1.5,
746                  omit_fields=()):
747        """Render pdf slips for various pages (also some pages
748        in the applicants module).
749        """
750        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
751        # XXX: tell what the different parameters mean
752        style = getSampleStyleSheet()
753        creator = self.getPDFCreator(student)
754        data = []
755        doc_title = view.label
756        author = '%s (%s)' % (view.request.principal.title,
757                              view.request.principal.id)
758        footer_text = view.label.split('\n')
759        if len(footer_text) > 1:
760            # We can add a department in first line, second line is used
761            footer_text = footer_text[1]
762        else:
763            # Only the first line is used for the footer
764            footer_text = footer_text[0]
765        if getattr(student, 'student_id', None) is not None:
766            footer_text = "%s - %s - " % (student.student_id, footer_text)
767
768        # Insert student data table
769        if student is not None:
770            bd_translation = trans(_('Base Data'), portal_language)
771            data.append(Paragraph(bd_translation, HEADING_STYLE))
772            data.append(render_student_data(
773                studentview, view.context, omit_fields, lang=portal_language,
774                slipname=filename))
775
776        # Insert widgets
777        if view.form_fields:
778            data.append(Paragraph(view.title, HEADING_STYLE))
779            separators = getattr(self, 'SEPARATORS_DICT', {})
780            table = creator.getWidgetsTable(
781                view.form_fields, view.context, None, lang=portal_language,
782                separators=separators)
783            data.append(table)
784
785        # Insert scanned docs
786        if show_scans:
787            data.extend(docs_as_flowables(view, portal_language))
788
789        # Insert history
790        if filename.startswith('clearance'):
791            hist_translation = trans(_('Workflow History'), portal_language)
792            data.append(Paragraph(hist_translation, HEADING_STYLE))
793            data.extend(creator.fromStringList(student.history.messages))
794
795        # Insert content tables (optionally on second page)
796        if hasattr(view, 'tabletitle'):
797            for i in range(len(view.tabletitle)):
798                if tabledata[i] and tableheader[i]:
799                    #data.append(PageBreak())
800                    #data.append(Spacer(1, 20))
801                    data.append(Paragraph(view.tabletitle[i], HEADING_STYLE))
802                    data.append(Spacer(1, 8))
803                    contenttable = render_table_data(tableheader[i],tabledata[i])
804                    data.append(contenttable)
805
806        # Insert signatures
807        # XXX: We are using only sigs_in_footer in waeup.kofa, so we
808        # do not have a test for the following lines.
809        if signatures and not sigs_in_footer:
810            data.append(Spacer(1, 20))
811            # Render one signature table per signature to
812            # get date and signature in line.
813            for signature in signatures:
814                signaturetables = get_signature_tables(signature)
815                data.append(signaturetables[0])
816
817        view.response.setHeader(
818            'Content-Type', 'application/pdf')
819        try:
820            pdf_stream = creator.create_pdf(
821                data, None, doc_title, author=author, footer=footer_text,
822                note=note, sigs_in_footer=sigs_in_footer, topMargin=topMargin)
823        except IOError:
824            view.flash('Error in image file.')
825            return view.redirect(view.url(view.context))
826        except LayoutError, err:
827            view.flash(
828                'PDF file could not be created. Reportlab error message: %s'
829                % escape(err.message),
830                type="danger")
831            return view.redirect(view.url(view.context))
832        return pdf_stream
833
834    def GPABoundaries(self, faccode=None, depcode=None, certcode=None):
835        return ((1, 'Fail'),
836               (1.5, 'Pass'),
837               (2.4, '3rd Class'),
838               (3.5, '2nd Class Lower'),
839               (4.5, '2nd Class Upper'),
840               (5, '1st Class'))
841
842    def getClassFromCGPA(self, gpa, student):
843        """Determine the class of degree. In some custom packages
844        this class depends on e.g. the entry session of the student. In the
845        base package, it does not.
846        """
847        if gpa < self.GPABoundaries()[0][0]:
848            return 0, self.GPABoundaries()[0][1]
849        if gpa < self.GPABoundaries()[1][0]:
850            return 1, self.GPABoundaries()[1][1]
851        if gpa < self.GPABoundaries()[2][0]:
852            return 2, self.GPABoundaries()[2][1]
853        if gpa < self.GPABoundaries()[3][0]:
854            return 3, self.GPABoundaries()[3][1]
855        if gpa < self.GPABoundaries()[4][0]:
856            return 4, self.GPABoundaries()[4][1]
857        if gpa <= self.GPABoundaries()[5][0]:
858            return 5, self.GPABoundaries()[5][1]
859        return
860
861    def getDegreeClassNumber(self, level_obj):
862        """Get degree class number (used for SessionResultsPresentation
863        reports).
864        """
865        if level_obj.gpa_params[1] == 0:
866            # No credits weighted
867            return 6
868        return self.getClassFromCGPA(
869            level_obj.cumulative_params[0], level_obj.student)[0]
870
871    def _saveTranscriptPDF(self, student, transcript):
872        """Create a transcript PDF file and store it in student folder.
873        """
874        file_store = getUtility(IExtFileStore)
875        file_id = IFileStoreNameChooser(student).chooseName(
876            attr="final_transcript.pdf")
877        file_store.createFile(file_id, StringIO(transcript))
878        return
879
880    def renderPDFTranscript(self, view, filename='transcript.pdf',
881                  student=None,
882                  studentview=None,
883                  note=None,
884                  signatures=(),
885                  sigs_in_footer=(),
886                  digital_sigs=(),
887                  show_scans=True, topMargin=1.5,
888                  omit_fields=(),
889                  tableheader=None,
890                  no_passport=False,
891                  save_file=False):
892        """Render pdf slip of a transcripts.
893        """
894        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
895        # XXX: tell what the different parameters mean
896        style = getSampleStyleSheet()
897        creator = self.getPDFCreator(student)
898        data = []
899        doc_title = view.label
900        author = '%s (%s)' % (view.request.principal.title,
901                              view.request.principal.id)
902        footer_text = view.label.split('\n')
903        if len(footer_text) > 2:
904            # We can add a department in first line
905            footer_text = footer_text[1]
906        else:
907            # Only the first line is used for the footer
908            footer_text = footer_text[0]
909        if getattr(student, 'student_id', None) is not None:
910            footer_text = "%s - %s - " % (student.student_id, footer_text)
911
912        # Insert student data table
913        if student is not None:
914            #bd_translation = trans(_('Base Data'), portal_language)
915            #data.append(Paragraph(bd_translation, HEADING_STYLE))
916            data.append(render_student_data(
917                studentview, view.context,
918                omit_fields, lang=portal_language,
919                slipname=filename,
920                no_passport=no_passport))
921
922        transcript_data = view.context.getTranscriptData()
923        levels_data = transcript_data[0]
924
925        contextdata = []
926        f_label = trans(_('Course of Study:'), portal_language)
927        f_label = Paragraph(f_label, ENTRY1_STYLE)
928        f_text = formatted_text(view.context.certificate.longtitle)
929        f_text = Paragraph(f_text, ENTRY1_STYLE)
930        contextdata.append([f_label,f_text])
931
932        f_label = trans(_('Faculty:'), portal_language)
933        f_label = Paragraph(f_label, ENTRY1_STYLE)
934        f_text = formatted_text(
935            view.context.certificate.__parent__.__parent__.__parent__.longtitle)
936        f_text = Paragraph(f_text, ENTRY1_STYLE)
937        contextdata.append([f_label,f_text])
938
939        f_label = trans(_('Department:'), portal_language)
940        f_label = Paragraph(f_label, ENTRY1_STYLE)
941        f_text = formatted_text(
942            view.context.certificate.__parent__.__parent__.longtitle)
943        f_text = Paragraph(f_text, ENTRY1_STYLE)
944        contextdata.append([f_label,f_text])
945
946        f_label = trans(_('Entry Session:'), portal_language)
947        f_label = Paragraph(f_label, ENTRY1_STYLE)
948        f_text = formatted_text(
949            view.session_dict.get(view.context.entry_session))
950        f_text = Paragraph(f_text, ENTRY1_STYLE)
951        contextdata.append([f_label,f_text])
952
953        f_label = trans(_('Entry Mode:'), portal_language)
954        f_label = Paragraph(f_label, ENTRY1_STYLE)
955        f_text = formatted_text(view.studymode_dict.get(
956            view.context.entry_mode))
957        f_text = Paragraph(f_text, ENTRY1_STYLE)
958        contextdata.append([f_label,f_text])
959
960        f_label = trans(_('Cumulative GPA:'), portal_language)
961        f_label = Paragraph(f_label, ENTRY1_STYLE)
962        format_float = getUtility(IKofaUtils).format_float
963        cgpa = format_float(transcript_data[1], 3)
964        f_text = formatted_text('%s (%s)' % (
965            cgpa, self.getClassFromCGPA(transcript_data[1], student)[1]))
966        f_text = Paragraph(f_text, ENTRY1_STYLE)
967        contextdata.append([f_label,f_text])
968
969        contexttable = Table(contextdata,style=SLIP_STYLE)
970        data.append(contexttable)
971
972        transcripttables = render_transcript_data(
973            view, tableheader, levels_data, lang=portal_language)
974        data.extend(transcripttables)
975
976        # Insert signatures
977        # XXX: We are using only sigs_in_footer in waeup.kofa, so we
978        # do not have a test for the following lines.
979        if signatures and not sigs_in_footer:
980            data.append(Spacer(1, 20))
981            # Render one signature table per signature to
982            # get date and signature in line.
983            for signature in signatures:
984                signaturetables = get_signature_tables(signature)
985                data.append(signaturetables[0])
986
987        # Insert digital signatures
988        if digital_sigs:
989            data.append(Spacer(1, 20))
990            sigs = digital_sigs.split('\n')
991            for sig in sigs:
992                data.append(Paragraph(sig, NOTE_STYLE))
993
994        view.response.setHeader(
995            'Content-Type', 'application/pdf')
996        try:
997            pdf_stream = creator.create_pdf(
998                data, None, doc_title, author=author, footer=footer_text,
999                note=note, sigs_in_footer=sigs_in_footer, topMargin=topMargin)
1000        except IOError:
1001            view.flash(_('Error in image file.'))
1002            return view.redirect(view.url(view.context))
1003        if save_file:
1004            self._saveTranscriptPDF(student, pdf_stream)
1005            return
1006        return pdf_stream
1007
1008    def renderPDFCourseticketsOverview(
1009            self, view, session, data, lecturers, orientation,
1010            title_length, note):
1011        """Render pdf slip of course tickets for a lecturer.
1012        """
1013        filename = 'coursetickets_%s_%s_%s.pdf' % (
1014            view.context.code, session, view.request.principal.id)
1015        session = academic_sessions_vocab.getTerm(session).title
1016        creator = getUtility(IPDFCreator, name=orientation)
1017        style = getSampleStyleSheet()
1018        pdf_data = []
1019        pdf_data += [Paragraph(
1020            translate(_('<b>Lecturer(s): ${a}</b>',
1021                      mapping = {'a':lecturers})), style["Normal"]),]
1022        pdf_data += [Paragraph(
1023            translate(_('<b>Credits: ${a}</b>',
1024                      mapping = {'a':view.context.credits})), style["Normal"]),]
1025        # Not used in base package.
1026        if data[1]:
1027            pdf_data += [Paragraph(
1028                translate(_('<b>${a}</b>',
1029                    mapping = {'a':data[1][0]})), style["Normal"]),]
1030            pdf_data += [Paragraph(
1031                translate(_('<b>${a}</b>',
1032                    mapping = {'a':data[1][1]})), style["Normal"]),]
1033
1034            pdf_data += [Paragraph(
1035                translate(_('<b>Total Students: ${a}</b>',
1036                    mapping = {'a':data[1][2]})), style["Normal"]),]
1037            pdf_data += [Paragraph(
1038                translate(_('<b>Total Pass: ${a} (${b}%)</b>',
1039                mapping = {'a':data[1][3],'b':data[1][4]})), style["Normal"]),]
1040            pdf_data += [Paragraph(
1041                translate(_('<b>Total Fail: ${a} (${b}%)</b>',
1042                mapping = {'a':data[1][5],'b':data[1][6]})), style["Normal"]),]
1043        pdf_data.append(Spacer(1, 20))
1044        colWidths = [None] * len(data[0][0])
1045        pdf_data += [Table(data[0], colWidths=colWidths, style=CONTENT_STYLE)]
1046        # Process title if too long
1047        title = " ".join(view.context.title.split())
1048        ct = textwrap.fill(title, title_length)
1049        ft = title
1050        if len(textwrap.wrap(title, title_length)) > 1:
1051            ft = textwrap.wrap(title, title_length)[0] + ' ...'
1052        doc_title = translate(_('${a} (${b})\nAcademic Session ${d}',
1053            mapping = {'a':ct,
1054                       'b':view.context.code,
1055                       'd':session}))
1056        footer_title = translate(_('${a} (${b}) - ${d}',
1057            mapping = {'a':ft,
1058                       'b':view.context.code,
1059                       'd':session}))
1060        author = '%s (%s)' % (view.request.principal.title,
1061                              view.request.principal.id)
1062        view.response.setHeader(
1063            'Content-Type', 'application/pdf')
1064        view.response.setHeader(
1065            'Content-Disposition:', 'attachment; filename="%s' % filename)
1066        pdf_stream = creator.create_pdf(
1067            pdf_data, None, doc_title, author, footer_title + ' -', note
1068            )
1069        return pdf_stream
1070
1071    def warnCreditsOOR(self, studylevel, course=None):
1072        """Return message if credits are out of range. In the base
1073        package only maximum credits is set.
1074        """
1075        if course and studylevel.total_credits + course.credits > 50:
1076            return _('Maximum credits exceeded.')
1077        elif studylevel.total_credits > 50:
1078            return _('Maximum credits exceeded.')
1079        return
1080
1081    def getBedCoordinates(self, bedticket):
1082        """Return descriptive bed coordinates.
1083        This method can be used to customize the `display_coordinates`
1084        property method in order to  display a
1085        customary description of the bed space.
1086        """
1087        return bedticket.bed_coordinates
1088
1089    def clearance_disabled_message(self, student):
1090        """Render message if clearance is disabled.
1091        """
1092        try:
1093            session_config = grok.getSite()[
1094                'configuration'][str(student.current_session)]
1095        except KeyError:
1096            return _('Session configuration object is not available.')
1097        if not session_config.clearance_enabled:
1098            return _('Clearance is disabled for this session.')
1099        return None
1100
1101    #: A dictionary which maps widget names to headlines. The headline
1102    #: is rendered in forms and on pdf slips above the respective
1103    #: display or input widget. There are no separating headlines
1104    #: in the base package.
1105    SEPARATORS_DICT = {}
1106
1107    #: A tuple containing names of file upload viewlets which are not shown
1108    #: on the `StudentClearanceManageFormPage`. Nothing is being skipped
1109    #: in the base package. This attribute makes only sense, if intermediate
1110    #: custom packages are being used, like we do for all Nigerian portals.
1111    SKIP_UPLOAD_VIEWLETS = ()
1112
1113    #: A tuple containing the names of registration states in which changing of
1114    #: passport pictures is allowed.
1115    PORTRAIT_CHANGE_STATES = (ADMITTED,)
1116
1117    #: A tuple containing all exporter names referring to students or
1118    #: subobjects thereof.
1119    STUDENT_EXPORTER_NAMES = ('students', 'studentstudycourses',
1120            'studentstudylevels', 'coursetickets',
1121            'studentpayments', 'studentunpaidpayments',
1122            'bedtickets', 'sfpaymentsoverview', 'sessionpaymentsoverview',
1123            'studylevelsoverview', 'combocard', 'bursary',
1124            'accommodationpayments')
1125
1126    #: A tuple containing all exporter names needed for backing
1127    #: up student data
1128    STUDENT_BACKUP_EXPORTER_NAMES = ('students', 'studentstudycourses',
1129            'studentstudylevels', 'coursetickets',
1130            'studentpayments', 'bedtickets')
1131
1132    #: A prefix used when generating new student ids. Each student id will
1133    #: start with this string. The default is 'K' for Kofa.
1134    STUDENT_ID_PREFIX = u'K'
Note: See TracBrowser for help on using the repository browser.