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