[7191] | 1 | ## $Id: utils.py 9864 2013-01-11 17:19:38Z henrik $ |
---|
| 2 | ## |
---|
| 3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
| 4 | ## This program is free software; you can redistribute it and/or modify |
---|
| 5 | ## it under the terms of the GNU General Public License as published by |
---|
| 6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | ## (at your option) any later version. |
---|
| 8 | ## |
---|
| 9 | ## This program is distributed in the hope that it will be useful, |
---|
| 10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | ## GNU General Public License for more details. |
---|
| 13 | ## |
---|
| 14 | ## You should have received a copy of the GNU General Public License |
---|
| 15 | ## along with this program; if not, write to the Free Software |
---|
| 16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 17 | ## |
---|
[7358] | 18 | """General helper functions and utilities for the student section. |
---|
[6651] | 19 | """ |
---|
[7150] | 20 | import grok |
---|
[8595] | 21 | from time import time |
---|
[7318] | 22 | from reportlab.lib import colors |
---|
[7019] | 23 | from reportlab.lib.units import cm |
---|
| 24 | from reportlab.lib.pagesizes import A4 |
---|
[9015] | 25 | from reportlab.lib.styles import getSampleStyleSheet |
---|
| 26 | from reportlab.platypus import Paragraph, Image, Table, Spacer |
---|
| 27 | from zope.component import getUtility, createObject |
---|
[7019] | 28 | from zope.formlib.form import setUpEditWidgets |
---|
[9015] | 29 | from zope.i18n import translate |
---|
[8596] | 30 | from waeup.kofa.interfaces import ( |
---|
[9762] | 31 | IExtFileStore, IKofaUtils, RETURNING, PAID, CLEARED, |
---|
| 32 | academic_sessions_vocab) |
---|
[7811] | 33 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
| 34 | from waeup.kofa.students.interfaces import IStudentsUtils |
---|
[6651] | 35 | |
---|
[7318] | 36 | SLIP_STYLE = [ |
---|
| 37 | ('VALIGN',(0,0),(-1,-1),'TOP'), |
---|
| 38 | #('FONT', (0,0), (-1,-1), 'Helvetica', 11), |
---|
| 39 | ] |
---|
[7019] | 40 | |
---|
[7318] | 41 | CONTENT_STYLE = [ |
---|
| 42 | ('VALIGN',(0,0),(-1,-1),'TOP'), |
---|
| 43 | #('FONT', (0,0), (-1,-1), 'Helvetica', 8), |
---|
| 44 | #('TEXTCOLOR',(0,0),(-1,0),colors.white), |
---|
| 45 | ('BACKGROUND',(0,0),(-1,0),colors.black), |
---|
| 46 | ] |
---|
[7304] | 47 | |
---|
[7318] | 48 | FONT_SIZE = 10 |
---|
| 49 | FONT_COLOR = 'black' |
---|
| 50 | |
---|
[7319] | 51 | def formatted_label(color=FONT_COLOR, size=FONT_SIZE): |
---|
[7318] | 52 | tag1 ='<font color=%s size=%d>' % (color, size) |
---|
[7319] | 53 | return tag1 + '%s:</font>' |
---|
| 54 | |
---|
[8112] | 55 | def trans(text, lang): |
---|
| 56 | # shortcut |
---|
| 57 | return translate(text, 'waeup.kofa', target_language=lang) |
---|
| 58 | |
---|
[7511] | 59 | def formatted_text(text, color=FONT_COLOR, size=FONT_SIZE): |
---|
| 60 | """Turn `text`, `color` and `size` into an HTML snippet. |
---|
[7318] | 61 | |
---|
[7511] | 62 | The snippet is suitable for use with reportlab and generating PDFs. |
---|
| 63 | Wraps the `text` into a ``<font>`` tag with passed attributes. |
---|
| 64 | |
---|
| 65 | Also non-strings are converted. Raw strings are expected to be |
---|
| 66 | utf-8 encoded (usually the case for widgets etc.). |
---|
| 67 | |
---|
[7804] | 68 | Finally, a br tag is added if widgets contain div tags |
---|
| 69 | which are not supported by reportlab. |
---|
| 70 | |
---|
[7511] | 71 | The returned snippet is unicode type. |
---|
| 72 | """ |
---|
[8142] | 73 | try: |
---|
| 74 | # In unit tests IKofaUtils has not been registered |
---|
| 75 | portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
| 76 | except: |
---|
| 77 | portal_language = 'en' |
---|
[7511] | 78 | if not isinstance(text, unicode): |
---|
| 79 | if isinstance(text, basestring): |
---|
| 80 | text = text.decode('utf-8') |
---|
| 81 | else: |
---|
| 82 | text = unicode(text) |
---|
[9717] | 83 | if text == 'None': |
---|
| 84 | text = '' |
---|
[8141] | 85 | # Mainly for boolean values we need our customized |
---|
| 86 | # localisation of the zope domain |
---|
| 87 | text = translate(text, 'zope', target_language=portal_language) |
---|
[7804] | 88 | text = text.replace('</div>', '<br /></div>') |
---|
[7511] | 89 | tag1 = u'<font color="%s" size="%d">' % (color, size) |
---|
| 90 | return tag1 + u'%s</font>' % text |
---|
| 91 | |
---|
[8481] | 92 | def generate_student_id(): |
---|
[8410] | 93 | students = grok.getSite()['students'] |
---|
| 94 | new_id = students.unique_student_id |
---|
| 95 | return new_id |
---|
[6742] | 96 | |
---|
[7186] | 97 | def set_up_widgets(view, ignore_request=False): |
---|
[7019] | 98 | view.adapters = {} |
---|
| 99 | view.widgets = setUpEditWidgets( |
---|
| 100 | view.form_fields, view.prefix, view.context, view.request, |
---|
| 101 | adapters=view.adapters, for_display=True, |
---|
| 102 | ignore_request=ignore_request |
---|
| 103 | ) |
---|
| 104 | |
---|
[7310] | 105 | def render_student_data(studentview): |
---|
[7318] | 106 | """Render student table for an existing frame. |
---|
| 107 | """ |
---|
| 108 | width, height = A4 |
---|
[7186] | 109 | set_up_widgets(studentview, ignore_request=True) |
---|
[7318] | 110 | data_left = [] |
---|
| 111 | data_right = [] |
---|
[7019] | 112 | style = getSampleStyleSheet() |
---|
[7280] | 113 | img = getUtility(IExtFileStore).getFileByContext( |
---|
| 114 | studentview.context, attr='passport.jpg') |
---|
| 115 | if img is None: |
---|
[7811] | 116 | from waeup.kofa.browser import DEFAULT_PASSPORT_IMAGE_PATH |
---|
[7280] | 117 | img = open(DEFAULT_PASSPORT_IMAGE_PATH, 'rb') |
---|
[7318] | 118 | doc_img = Image(img.name, width=4*cm, height=4*cm, kind='bound') |
---|
| 119 | data_left.append([doc_img]) |
---|
| 120 | #data.append([Spacer(1, 12)]) |
---|
[7819] | 121 | portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
[9141] | 122 | |
---|
| 123 | f_label = formatted_label(size=12) % _('Name') |
---|
| 124 | f_label = Paragraph(f_label, style["Normal"]) |
---|
| 125 | f_text = formatted_text(studentview.context.display_fullname, size=12) |
---|
| 126 | f_text = Paragraph(f_text, style["Normal"]) |
---|
| 127 | data_right.append([f_label,f_text]) |
---|
| 128 | |
---|
[7019] | 129 | for widget in studentview.widgets: |
---|
[9141] | 130 | if 'name' in widget.name: |
---|
[7019] | 131 | continue |
---|
[7714] | 132 | f_label = formatted_label(size=12) % translate( |
---|
[7811] | 133 | widget.label.strip(), 'waeup.kofa', |
---|
[7714] | 134 | target_language=portal_language) |
---|
[7019] | 135 | f_label = Paragraph(f_label, style["Normal"]) |
---|
[7714] | 136 | f_text = formatted_text(widget(), size=12) |
---|
[7019] | 137 | f_text = Paragraph(f_text, style["Normal"]) |
---|
[7318] | 138 | data_right.append([f_label,f_text]) |
---|
[9141] | 139 | |
---|
[9452] | 140 | if getattr(studentview.context, 'certcode', None): |
---|
[9141] | 141 | f_label = formatted_label(size=12) % _('Study Course') |
---|
| 142 | f_label = Paragraph(f_label, style["Normal"]) |
---|
[9191] | 143 | f_text = formatted_text( |
---|
| 144 | studentview.context['studycourse'].certificate.longtitle(), size=12) |
---|
[9141] | 145 | f_text = Paragraph(f_text, style["Normal"]) |
---|
| 146 | data_right.append([f_label,f_text]) |
---|
| 147 | |
---|
[9191] | 148 | f_label = formatted_label(size=12) % _('Department') |
---|
| 149 | f_label = Paragraph(f_label, style["Normal"]) |
---|
| 150 | f_text = formatted_text( |
---|
| 151 | studentview.context[ |
---|
| 152 | 'studycourse'].certificate.__parent__.__parent__.longtitle(), |
---|
| 153 | size=12) |
---|
| 154 | f_text = Paragraph(f_text, style["Normal"]) |
---|
| 155 | data_right.append([f_label,f_text]) |
---|
| 156 | |
---|
| 157 | f_label = formatted_label(size=12) % _('Faculty') |
---|
| 158 | f_label = Paragraph(f_label, style["Normal"]) |
---|
| 159 | f_text = formatted_text( |
---|
| 160 | studentview.context[ |
---|
| 161 | 'studycourse'].certificate.__parent__.__parent__.__parent__.longtitle(), |
---|
| 162 | size=12) |
---|
| 163 | f_text = Paragraph(f_text, style["Normal"]) |
---|
| 164 | data_right.append([f_label,f_text]) |
---|
| 165 | |
---|
[9762] | 166 | f_label = formatted_label(size=12) % _('Entry Session') |
---|
| 167 | f_label = Paragraph(f_label, style["Normal"]) |
---|
| 168 | entry_session = studentview.context['studycourse'].entry_session |
---|
| 169 | entry_session = academic_sessions_vocab.getTerm(entry_session).title |
---|
| 170 | f_text = formatted_text(entry_session, size=12) |
---|
| 171 | f_text = Paragraph(f_text, style["Normal"]) |
---|
| 172 | data_right.append([f_label,f_text]) |
---|
| 173 | |
---|
[7318] | 174 | table_left = Table(data_left,style=SLIP_STYLE) |
---|
[8112] | 175 | table_right = Table(data_right,style=SLIP_STYLE, colWidths=[5*cm, 6*cm]) |
---|
[7318] | 176 | table = Table([[table_left, table_right],],style=SLIP_STYLE) |
---|
[7019] | 177 | return table |
---|
| 178 | |
---|
[7304] | 179 | def render_table_data(tableheader,tabledata): |
---|
[7318] | 180 | """Render children table for an existing frame. |
---|
| 181 | """ |
---|
[7304] | 182 | data = [] |
---|
[7318] | 183 | #data.append([Spacer(1, 12)]) |
---|
[7304] | 184 | line = [] |
---|
| 185 | style = getSampleStyleSheet() |
---|
| 186 | for element in tableheader: |
---|
[7511] | 187 | field = formatted_text(element[0], color='white') |
---|
[7310] | 188 | field = Paragraph(field, style["Normal"]) |
---|
[7304] | 189 | line.append(field) |
---|
| 190 | data.append(line) |
---|
| 191 | for ticket in tabledata: |
---|
| 192 | line = [] |
---|
| 193 | for element in tableheader: |
---|
[7511] | 194 | field = formatted_text(getattr(ticket,element[1],u' ')) |
---|
[7318] | 195 | field = Paragraph(field, style["Normal"]) |
---|
[7304] | 196 | line.append(field) |
---|
| 197 | data.append(line) |
---|
[7310] | 198 | table = Table(data,colWidths=[ |
---|
[7318] | 199 | element[2]*cm for element in tableheader], style=CONTENT_STYLE) |
---|
[7304] | 200 | return table |
---|
| 201 | |
---|
[9014] | 202 | def get_signature_table(signatures, lang='en'): |
---|
| 203 | """Return a reportlab table containing signature fields (with date). |
---|
| 204 | """ |
---|
[9010] | 205 | style = getSampleStyleSheet() |
---|
[9014] | 206 | space_width = 0.4 # width in cm of space between signatures |
---|
| 207 | table_width = 16.0 # supposed width of signature table in cms |
---|
| 208 | # width of signature cells in cm... |
---|
| 209 | sig_col_width = table_width - ((len(signatures) - 1) * space_width) |
---|
| 210 | sig_col_width = sig_col_width / len(signatures) |
---|
[9010] | 211 | data = [] |
---|
[9014] | 212 | col_widths = [] # widths of columns |
---|
| 213 | |
---|
| 214 | sig_style = [ |
---|
| 215 | ('VALIGN',(0,-1),(-1,-1),'TOP'), |
---|
| 216 | ('FONT', (0,0), (-1,-1), 'Helvetica-BoldOblique', 12), |
---|
| 217 | ('BOTTOMPADDING', (0,0), (-1,0), 36), |
---|
| 218 | ('TOPPADDING', (0,-1), (-1,-1), 0), |
---|
| 219 | ] |
---|
| 220 | for num, elem in enumerate(signatures): |
---|
| 221 | # draw a line above each signature cell (not: empty cells in between) |
---|
| 222 | sig_style.append( |
---|
| 223 | ('LINEABOVE', (num*2,-1), (num*2, -1), 1, colors.black)) |
---|
| 224 | |
---|
| 225 | row = [] |
---|
[9010] | 226 | for signature in signatures: |
---|
[9014] | 227 | row.append(trans(_('Date:'), lang)) |
---|
| 228 | row.append('') |
---|
| 229 | if len(signatures) > 1: |
---|
| 230 | col_widths.extend([sig_col_width*cm, space_width*cm]) |
---|
| 231 | else: |
---|
| 232 | col_widths.extend([sig_col_width/2*cm, sig_col_width/2*cm]) |
---|
| 233 | row.append('') # empty spaceholder on right |
---|
| 234 | data.append(row[:-1]) |
---|
| 235 | data.extend(([''],)*3) # insert 3 empty rows... |
---|
| 236 | row = [] |
---|
[9010] | 237 | for signature in signatures: |
---|
[9014] | 238 | row.append(Paragraph(trans(signature, lang), style["Normal"])) |
---|
| 239 | row.append('') |
---|
| 240 | data.append(row[:-1]) |
---|
| 241 | table = Table(data, style=sig_style, repeatRows=len(data), |
---|
| 242 | colWidths=col_widths) |
---|
[9010] | 243 | return table |
---|
| 244 | |
---|
[8112] | 245 | def docs_as_flowables(view, lang='en'): |
---|
| 246 | """Create reportlab flowables out of scanned docs. |
---|
| 247 | """ |
---|
| 248 | # XXX: fix circular import problem |
---|
| 249 | from waeup.kofa.students.viewlets import FileManager |
---|
| 250 | from waeup.kofa.browser import DEFAULT_IMAGE_PATH |
---|
| 251 | from waeup.kofa.browser.pdf import NORMAL_STYLE, ENTRY1_STYLE |
---|
| 252 | style = getSampleStyleSheet() |
---|
| 253 | data = [] |
---|
[7318] | 254 | |
---|
[8112] | 255 | # Collect viewlets |
---|
| 256 | fm = FileManager(view.context, view.request, view) |
---|
| 257 | fm.update() |
---|
| 258 | if fm.viewlets: |
---|
| 259 | sc_translation = trans(_('Scanned Documents'), lang) |
---|
| 260 | data.append(Paragraph(sc_translation, style["Heading3"])) |
---|
| 261 | # Insert list of scanned documents |
---|
| 262 | table_data = [] |
---|
| 263 | for viewlet in fm.viewlets: |
---|
| 264 | f_label = Paragraph(trans(viewlet.label, lang), ENTRY1_STYLE) |
---|
| 265 | img_path = getattr(getUtility(IExtFileStore).getFileByContext( |
---|
| 266 | view.context, attr=viewlet.download_name), 'name', None) |
---|
[8120] | 267 | f_text = Paragraph(trans(_('(not provided)'),lang), ENTRY1_STYLE) |
---|
[8112] | 268 | if img_path is None: |
---|
| 269 | pass |
---|
[9016] | 270 | elif not img_path[-4:] in ('.jpg', '.JPG'): |
---|
[8112] | 271 | # reportlab requires jpg images, I think. |
---|
[9016] | 272 | f_text = Paragraph('%s (not displayable)' % ( |
---|
[8112] | 273 | viewlet.title,), ENTRY1_STYLE) |
---|
| 274 | else: |
---|
[8117] | 275 | f_text = Image(img_path, width=2*cm, height=1*cm, kind='bound') |
---|
[8112] | 276 | table_data.append([f_label, f_text]) |
---|
| 277 | if table_data: |
---|
| 278 | # safety belt; empty tables lead to problems. |
---|
| 279 | data.append(Table(table_data, style=SLIP_STYLE)) |
---|
| 280 | return data |
---|
| 281 | |
---|
[7150] | 282 | class StudentsUtils(grok.GlobalUtility): |
---|
| 283 | """A collection of methods subject to customization. |
---|
| 284 | """ |
---|
| 285 | grok.implements(IStudentsUtils) |
---|
[7019] | 286 | |
---|
[8268] | 287 | def getReturningData(self, student): |
---|
[9005] | 288 | """ Define what happens after school fee payment |
---|
[7841] | 289 | depending on the student's senate verdict. |
---|
| 290 | |
---|
| 291 | In the base configuration current level is always increased |
---|
| 292 | by 100 no matter which verdict has been assigned. |
---|
| 293 | """ |
---|
[8268] | 294 | new_level = student['studycourse'].current_level + 100 |
---|
| 295 | new_session = student['studycourse'].current_session + 1 |
---|
| 296 | return new_session, new_level |
---|
| 297 | |
---|
| 298 | def setReturningData(self, student): |
---|
[9005] | 299 | """ Define what happens after school fee payment |
---|
| 300 | depending on the student's senate verdict. |
---|
[8268] | 301 | |
---|
[9005] | 302 | This method folllows the same algorithm as getReturningData but |
---|
| 303 | it also sets the new values. |
---|
[8268] | 304 | """ |
---|
| 305 | new_session, new_level = self.getReturningData(student) |
---|
| 306 | student['studycourse'].current_level = new_level |
---|
| 307 | student['studycourse'].current_session = new_session |
---|
[7615] | 308 | verdict = student['studycourse'].current_verdict |
---|
[8820] | 309 | student['studycourse'].current_verdict = '0' |
---|
[7615] | 310 | student['studycourse'].previous_verdict = verdict |
---|
| 311 | return |
---|
| 312 | |
---|
[9519] | 313 | def _getSessionConfiguration(self, session): |
---|
| 314 | try: |
---|
| 315 | return grok.getSite()['configuration'][str(session)] |
---|
| 316 | except KeyError: |
---|
| 317 | return None |
---|
| 318 | |
---|
[9148] | 319 | def setPaymentDetails(self, category, student, |
---|
[9151] | 320 | previous_session, previous_level): |
---|
[8595] | 321 | """Create Payment object and set the payment data of a student for |
---|
| 322 | the payment category specified. |
---|
| 323 | |
---|
[7841] | 324 | """ |
---|
[8595] | 325 | p_item = u'' |
---|
| 326 | amount = 0.0 |
---|
[9148] | 327 | if previous_session: |
---|
[9517] | 328 | if previous_session < student['studycourse'].entry_session: |
---|
| 329 | return _('The previous session must not fall below ' |
---|
| 330 | 'your entry session.'), None |
---|
| 331 | if category == 'schoolfee': |
---|
| 332 | # School fee is always paid for the following session |
---|
| 333 | if previous_session > student['studycourse'].current_session: |
---|
| 334 | return _('This is not a previous session.'), None |
---|
| 335 | else: |
---|
| 336 | if previous_session > student['studycourse'].current_session - 1: |
---|
| 337 | return _('This is not a previous session.'), None |
---|
[9148] | 338 | p_session = previous_session |
---|
| 339 | p_level = previous_level |
---|
| 340 | p_current = False |
---|
| 341 | else: |
---|
| 342 | p_session = student['studycourse'].current_session |
---|
| 343 | p_level = student['studycourse'].current_level |
---|
| 344 | p_current = True |
---|
[9519] | 345 | academic_session = self._getSessionConfiguration(p_session) |
---|
| 346 | if academic_session == None: |
---|
[8595] | 347 | return _(u'Session configuration object is not available.'), None |
---|
[9521] | 348 | # Determine fee. |
---|
[7150] | 349 | if category == 'schoolfee': |
---|
[8595] | 350 | try: |
---|
[8596] | 351 | certificate = student['studycourse'].certificate |
---|
| 352 | p_item = certificate.code |
---|
[8595] | 353 | except (AttributeError, TypeError): |
---|
| 354 | return _('Study course data are incomplete.'), None |
---|
[9148] | 355 | if previous_session: |
---|
[9517] | 356 | # Students can pay for previous sessions in all workflow states. |
---|
| 357 | # Fresh students are excluded by the update method of the |
---|
| 358 | # PreviousPaymentAddFormPage. |
---|
[9148] | 359 | if previous_level == 100: |
---|
| 360 | amount = getattr(certificate, 'school_fee_1', 0.0) |
---|
| 361 | else: |
---|
| 362 | amount = getattr(certificate, 'school_fee_2', 0.0) |
---|
| 363 | else: |
---|
| 364 | if student.state == CLEARED: |
---|
| 365 | amount = getattr(certificate, 'school_fee_1', 0.0) |
---|
| 366 | elif student.state == RETURNING: |
---|
| 367 | # In case of returning school fee payment the payment session |
---|
| 368 | # and level contain the values of the session the student |
---|
[9517] | 369 | # has paid for. Payment session is always next session. |
---|
[9148] | 370 | p_session, p_level = self.getReturningData(student) |
---|
[9519] | 371 | academic_session = self._getSessionConfiguration(p_session) |
---|
| 372 | if academic_session == None: |
---|
| 373 | return _(u'Session configuration object is not available.'), None |
---|
[9148] | 374 | amount = getattr(certificate, 'school_fee_2', 0.0) |
---|
| 375 | elif student.is_postgrad and student.state == PAID: |
---|
| 376 | # Returning postgraduate students also pay for the next session |
---|
| 377 | # but their level always remains the same. |
---|
| 378 | p_session += 1 |
---|
[9519] | 379 | academic_session = self._getSessionConfiguration(p_session) |
---|
| 380 | if academic_session == None: |
---|
| 381 | return _(u'Session configuration object is not available.'), None |
---|
[9148] | 382 | amount = getattr(certificate, 'school_fee_2', 0.0) |
---|
[7150] | 383 | elif category == 'clearance': |
---|
[9178] | 384 | try: |
---|
| 385 | p_item = student['studycourse'].certificate.code |
---|
| 386 | except (AttributeError, TypeError): |
---|
| 387 | return _('Study course data are incomplete.'), None |
---|
[8595] | 388 | amount = academic_session.clearance_fee |
---|
[7150] | 389 | elif category == 'bed_allocation': |
---|
[8595] | 390 | p_item = self.getAccommodationDetails(student)['bt'] |
---|
| 391 | amount = academic_session.booking_fee |
---|
[9423] | 392 | elif category == 'hostel_maintenance': |
---|
| 393 | amount = academic_session.maint_fee |
---|
[9429] | 394 | bedticket = student['accommodation'].get( |
---|
| 395 | str(student.current_session), None) |
---|
| 396 | if bedticket: |
---|
| 397 | p_item = bedticket.bed_coordinates |
---|
| 398 | else: |
---|
| 399 | # Should not happen because this is already checked |
---|
| 400 | # in the browser module, but anyway ... |
---|
| 401 | portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
| 402 | p_item = trans(_('no bed allocated'), portal_language) |
---|
[8595] | 403 | if amount in (0.0, None): |
---|
[9517] | 404 | return _('Amount could not be determined.'), None |
---|
[8595] | 405 | for key in student['payments'].keys(): |
---|
| 406 | ticket = student['payments'][key] |
---|
| 407 | if ticket.p_state == 'paid' and\ |
---|
| 408 | ticket.p_category == category and \ |
---|
| 409 | ticket.p_item == p_item and \ |
---|
| 410 | ticket.p_session == p_session: |
---|
[9517] | 411 | return _('This type of payment has already been made.'), None |
---|
[8708] | 412 | payment = createObject(u'waeup.StudentOnlinePayment') |
---|
[8951] | 413 | timestamp = ("%d" % int(time()*10000))[1:] |
---|
[8595] | 414 | payment.p_id = "p%s" % timestamp |
---|
| 415 | payment.p_category = category |
---|
| 416 | payment.p_item = p_item |
---|
| 417 | payment.p_session = p_session |
---|
| 418 | payment.p_level = p_level |
---|
[9148] | 419 | payment.p_current = p_current |
---|
[8595] | 420 | payment.amount_auth = amount |
---|
| 421 | return None, payment |
---|
[7019] | 422 | |
---|
[9864] | 423 | def setBalanceDetails(self, balance_item, student, |
---|
| 424 | balance_session, balance_level, balance_amount): |
---|
| 425 | """Create Payment object and set the payment data of a student for. |
---|
| 426 | |
---|
| 427 | """ |
---|
| 428 | p_item = getUtility(IKofaUtils).BALANCE_PAYMENT_ITEMS[balance_item] |
---|
| 429 | p_item = unicode(p_item) |
---|
| 430 | p_session = balance_session |
---|
| 431 | p_level = balance_level |
---|
| 432 | p_current = False |
---|
| 433 | amount = balance_amount |
---|
| 434 | academic_session = self._getSessionConfiguration(p_session) |
---|
| 435 | if academic_session == None: |
---|
| 436 | return _(u'Session configuration object is not available.'), None |
---|
| 437 | if amount in (0.0, None): |
---|
| 438 | return _('Amount could not be determined.'), None |
---|
| 439 | for key in student['payments'].keys(): |
---|
| 440 | ticket = student['payments'][key] |
---|
| 441 | if ticket.p_state == 'paid' and\ |
---|
| 442 | ticket.p_category == 'balance' and \ |
---|
| 443 | ticket.p_item == p_item and \ |
---|
| 444 | ticket.p_session == p_session: |
---|
| 445 | return _('This type of payment has already been made.'), None |
---|
| 446 | payment = createObject(u'waeup.StudentOnlinePayment') |
---|
| 447 | timestamp = ("%d" % int(time()*10000))[1:] |
---|
| 448 | payment.p_id = "p%s" % timestamp |
---|
| 449 | payment.p_category = 'balance' |
---|
| 450 | payment.p_item = p_item |
---|
| 451 | payment.p_session = p_session |
---|
| 452 | payment.p_level = p_level |
---|
| 453 | payment.p_current = p_current |
---|
| 454 | payment.amount_auth = amount |
---|
| 455 | return None, payment |
---|
| 456 | |
---|
[7186] | 457 | def getAccommodationDetails(self, student): |
---|
[9219] | 458 | """Determine the accommodation data of a student. |
---|
[7841] | 459 | """ |
---|
[7150] | 460 | d = {} |
---|
| 461 | d['error'] = u'' |
---|
[8685] | 462 | hostels = grok.getSite()['hostels'] |
---|
| 463 | d['booking_session'] = hostels.accommodation_session |
---|
| 464 | d['allowed_states'] = hostels.accommodation_states |
---|
[8688] | 465 | d['startdate'] = hostels.startdate |
---|
| 466 | d['enddate'] = hostels.enddate |
---|
| 467 | d['expired'] = hostels.expired |
---|
[7150] | 468 | # Determine bed type |
---|
| 469 | studycourse = student['studycourse'] |
---|
[7369] | 470 | certificate = getattr(studycourse,'certificate',None) |
---|
[7150] | 471 | entry_session = studycourse.entry_session |
---|
| 472 | current_level = studycourse.current_level |
---|
[9187] | 473 | if None in (entry_session, current_level, certificate): |
---|
| 474 | return d |
---|
[7369] | 475 | end_level = certificate.end_level |
---|
[9148] | 476 | if current_level == 10: |
---|
| 477 | bt = 'pr' |
---|
| 478 | elif entry_session == grok.getSite()['hostels'].accommodation_session: |
---|
[7150] | 479 | bt = 'fr' |
---|
| 480 | elif current_level >= end_level: |
---|
| 481 | bt = 'fi' |
---|
| 482 | else: |
---|
| 483 | bt = 're' |
---|
| 484 | if student.sex == 'f': |
---|
| 485 | sex = 'female' |
---|
| 486 | else: |
---|
| 487 | sex = 'male' |
---|
| 488 | special_handling = 'regular' |
---|
| 489 | d['bt'] = u'%s_%s_%s' % (special_handling,sex,bt) |
---|
| 490 | return d |
---|
[7019] | 491 | |
---|
[7186] | 492 | def selectBed(self, available_beds): |
---|
[7841] | 493 | """Select a bed from a list of available beds. |
---|
| 494 | |
---|
| 495 | In the base configuration we select the first bed found, |
---|
| 496 | but can also randomize the selection if we like. |
---|
| 497 | """ |
---|
[7150] | 498 | return available_beds[0] |
---|
| 499 | |
---|
[9191] | 500 | def renderPDFAdmissionLetter(self, view, student=None): |
---|
| 501 | """Render pdf admission letter. |
---|
| 502 | """ |
---|
| 503 | # XXX: we have to fix the import problems here. |
---|
| 504 | from waeup.kofa.browser.interfaces import IPDFCreator |
---|
| 505 | from waeup.kofa.browser.pdf import format_html, NOTE_STYLE |
---|
| 506 | if student is None: |
---|
| 507 | return |
---|
| 508 | style = getSampleStyleSheet() |
---|
| 509 | creator = getUtility(IPDFCreator) |
---|
| 510 | data = [] |
---|
| 511 | doc_title = view.label |
---|
| 512 | author = '%s (%s)' % (view.request.principal.title, |
---|
| 513 | view.request.principal.id) |
---|
| 514 | footer_text = view.label |
---|
| 515 | if getattr(student, 'student_id', None) is not None: |
---|
| 516 | footer_text = "%s - %s - " % (student.student_id, footer_text) |
---|
| 517 | |
---|
| 518 | # Admission text |
---|
| 519 | portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
| 520 | inst_name = grok.getSite()['configuration'].name |
---|
| 521 | text = trans(_( |
---|
| 522 | 'This is to inform you that you have been provisionally' |
---|
| 523 | ' admitted into ${a} as follows:', mapping = {'a': inst_name}), |
---|
| 524 | portal_language) |
---|
| 525 | html = format_html(text) |
---|
| 526 | data.append(Paragraph(html, NOTE_STYLE)) |
---|
| 527 | data.append(Spacer(1, 20)) |
---|
| 528 | |
---|
| 529 | # Student data |
---|
| 530 | data.append(render_student_data(view)) |
---|
| 531 | |
---|
| 532 | # Insert history |
---|
| 533 | data.append(Spacer(1, 20)) |
---|
| 534 | datelist = student.history.messages[0].split()[0].split('-') |
---|
| 535 | creation_date = u'%s/%s/%s' % (datelist[2], datelist[1], datelist[0]) |
---|
| 536 | text = trans(_( |
---|
| 537 | 'Your Kofa student record was created on ${a}.', |
---|
| 538 | mapping = {'a': creation_date}), |
---|
| 539 | portal_language) |
---|
| 540 | html = format_html(text) |
---|
| 541 | data.append(Paragraph(html, NOTE_STYLE)) |
---|
| 542 | |
---|
| 543 | # Create pdf stream |
---|
| 544 | view.response.setHeader( |
---|
| 545 | 'Content-Type', 'application/pdf') |
---|
| 546 | pdf_stream = creator.create_pdf( |
---|
| 547 | data, None, doc_title, author=author, footer=footer_text, |
---|
| 548 | note=None) |
---|
| 549 | return pdf_stream |
---|
| 550 | |
---|
[8257] | 551 | def renderPDF(self, view, filename='slip.pdf', student=None, |
---|
| 552 | studentview=None, tableheader=None, tabledata=None, |
---|
[9555] | 553 | note=None, signatures=None, sigs_in_footer=(), |
---|
[9550] | 554 | show_scans=True): |
---|
[7841] | 555 | """Render pdf slips for various pages. |
---|
| 556 | """ |
---|
[8112] | 557 | # XXX: we have to fix the import problems here. |
---|
| 558 | from waeup.kofa.browser.interfaces import IPDFCreator |
---|
| 559 | from waeup.kofa.browser.pdf import NORMAL_STYLE, ENTRY1_STYLE |
---|
| 560 | style = getSampleStyleSheet() |
---|
| 561 | creator = getUtility(IPDFCreator) |
---|
| 562 | data = [] |
---|
| 563 | doc_title = view.label |
---|
| 564 | author = '%s (%s)' % (view.request.principal.title, |
---|
| 565 | view.request.principal.id) |
---|
[7310] | 566 | footer_text = view.label |
---|
[7714] | 567 | if getattr(student, 'student_id', None) is not None: |
---|
[7310] | 568 | footer_text = "%s - %s - " % (student.student_id, footer_text) |
---|
[7150] | 569 | |
---|
[7318] | 570 | # Insert student data table |
---|
[7819] | 571 | portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
[7310] | 572 | if student is not None: |
---|
[8112] | 573 | bd_translation = trans(_('Base Data'), portal_language) |
---|
| 574 | data.append(Paragraph(bd_translation, style["Heading3"])) |
---|
| 575 | data.append(render_student_data(studentview)) |
---|
[7304] | 576 | |
---|
[7318] | 577 | # Insert widgets |
---|
[9191] | 578 | if view.form_fields: |
---|
| 579 | data.append(Paragraph(view.title, style["Heading3"])) |
---|
| 580 | portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
| 581 | separators = getattr(self, 'SEPARATORS_DICT', {}) |
---|
| 582 | table = creator.getWidgetsTable( |
---|
| 583 | view.form_fields, view.context, None, lang=portal_language, |
---|
| 584 | separators=separators) |
---|
| 585 | data.append(table) |
---|
[7318] | 586 | |
---|
[8112] | 587 | # Insert scanned docs |
---|
[9550] | 588 | if show_scans: |
---|
| 589 | data.extend(docs_as_flowables(view, portal_language)) |
---|
[7318] | 590 | |
---|
[9452] | 591 | # Insert history |
---|
| 592 | if filename.startswith('clearance') or filename.startswith('course'): |
---|
| 593 | hist_translation = trans(_('Workflow History'), portal_language) |
---|
| 594 | data.append(Paragraph(hist_translation, style["Heading3"])) |
---|
| 595 | data.extend(creator.fromStringList(student.history.messages)) |
---|
| 596 | |
---|
| 597 | # Insert content table (optionally on second page) |
---|
[7318] | 598 | if tabledata and tableheader: |
---|
[8141] | 599 | #data.append(PageBreak()) |
---|
| 600 | data.append(Spacer(1, 20)) |
---|
[8112] | 601 | data.append(Paragraph(view.content_title, style["Heading3"])) |
---|
[7304] | 602 | contenttable = render_table_data(tableheader,tabledata) |
---|
[8112] | 603 | data.append(contenttable) |
---|
[7318] | 604 | |
---|
[9010] | 605 | # Insert signatures |
---|
[9555] | 606 | if signatures and not sigs_in_footer: |
---|
[9010] | 607 | data.append(Spacer(1, 20)) |
---|
[9014] | 608 | signaturetable = get_signature_table(signatures) |
---|
[9010] | 609 | data.append(signaturetable) |
---|
| 610 | |
---|
[7150] | 611 | view.response.setHeader( |
---|
| 612 | 'Content-Type', 'application/pdf') |
---|
[8112] | 613 | try: |
---|
| 614 | pdf_stream = creator.create_pdf( |
---|
[8257] | 615 | data, None, doc_title, author=author, footer=footer_text, |
---|
[9555] | 616 | note=note, sigs_in_footer=sigs_in_footer) |
---|
[8112] | 617 | except IOError: |
---|
| 618 | view.flash('Error in image file.') |
---|
| 619 | return view.redirect(view.url(view.context)) |
---|
| 620 | return pdf_stream |
---|
[7620] | 621 | |
---|
[9830] | 622 | def maxCredits(self, studylevel): |
---|
| 623 | """Return maximum credits. |
---|
[9532] | 624 | |
---|
[9830] | 625 | In some universities maximum credits is not constant, it |
---|
| 626 | depends on the student's study level. |
---|
| 627 | """ |
---|
| 628 | return 50 |
---|
| 629 | |
---|
[9532] | 630 | def maxCreditsExceeded(self, studylevel, course): |
---|
[9830] | 631 | max_credits = self.maxCredits(studylevel) |
---|
| 632 | if max_credits and \ |
---|
| 633 | studylevel.total_credits + course.credits > max_credits: |
---|
| 634 | return max_credits |
---|
[9532] | 635 | return 0 |
---|
| 636 | |
---|
[7841] | 637 | VERDICTS_DICT = { |
---|
[8820] | 638 | '0': _('(not yet)'), |
---|
[7841] | 639 | 'A': 'Successful student', |
---|
| 640 | 'B': 'Student with carryover courses', |
---|
| 641 | 'C': 'Student on probation', |
---|
| 642 | } |
---|
[8099] | 643 | |
---|
| 644 | SEPARATORS_DICT = { |
---|
| 645 | } |
---|
[8410] | 646 | |
---|
| 647 | #: A prefix used when generating new student ids. Each student id will |
---|
| 648 | #: start with this string. The default is 'K' for ``Kofa``. |
---|
| 649 | STUDENT_ID_PREFIX = u'K' |
---|