[7191] | 1 | ## $Id: utils.py 7256 2011-12-03 05:46:52Z 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 | ## |
---|
[6651] | 18 | """General helper functions for the student section. |
---|
| 19 | """ |
---|
[7150] | 20 | import grok |
---|
[6662] | 21 | from random import SystemRandom as r |
---|
[7256] | 22 | from datetime import datetime |
---|
[7019] | 23 | from reportlab.pdfgen import canvas |
---|
| 24 | from reportlab.lib.units import cm |
---|
| 25 | from reportlab.lib.pagesizes import A4 |
---|
| 26 | from reportlab.lib.styles import getSampleStyleSheet |
---|
| 27 | from reportlab.platypus import (Frame, Paragraph, Image, |
---|
| 28 | Table, Spacer) |
---|
| 29 | from reportlab.platypus.tables import TableStyle |
---|
| 30 | from zope.formlib.form import setUpEditWidgets |
---|
[7150] | 31 | from waeup.sirp.students.interfaces import IStudentsUtils |
---|
[6651] | 32 | |
---|
[7019] | 33 | SLIP_STYLE = TableStyle( |
---|
| 34 | [('VALIGN',(0,0),(-1,-1),'TOP')] |
---|
| 35 | ) |
---|
| 36 | |
---|
[6749] | 37 | def generate_student_id(students,letter): |
---|
[6651] | 38 | if letter == '?': |
---|
[6664] | 39 | letter= r().choice('ABCDEFGHKLMNPQRSTUVWXY') |
---|
| 40 | sid = u"%c%d" % (letter,r().randint(99999,1000000)) |
---|
[6749] | 41 | while sid in students.keys(): |
---|
[6664] | 42 | sid = u"%c%d" % (letter,r().randint(99999,1000000)) |
---|
[6662] | 43 | return sid |
---|
[6742] | 44 | |
---|
| 45 | def set_returning_data(student): |
---|
| 46 | student['studycourse'].current_level += 100 |
---|
| 47 | student['studycourse'].current_session += 1 |
---|
| 48 | verdict = student['studycourse'].current_verdict |
---|
[6804] | 49 | student['studycourse'].current_verdict = '0' |
---|
[6742] | 50 | student['studycourse'].previous_verdict = verdict |
---|
| 51 | return |
---|
[6876] | 52 | |
---|
[7186] | 53 | def set_up_widgets(view, ignore_request=False): |
---|
[7019] | 54 | view.adapters = {} |
---|
| 55 | view.widgets = setUpEditWidgets( |
---|
| 56 | view.form_fields, view.prefix, view.context, view.request, |
---|
| 57 | adapters=view.adapters, for_display=True, |
---|
| 58 | ignore_request=ignore_request |
---|
| 59 | ) |
---|
| 60 | |
---|
[7145] | 61 | def render_student_data(student, studentview): |
---|
[7186] | 62 | set_up_widgets(studentview, ignore_request=True) |
---|
[7019] | 63 | data = [] |
---|
| 64 | style = getSampleStyleSheet() |
---|
| 65 | for widget in studentview.widgets: |
---|
| 66 | if widget.name == 'form.adm_code': |
---|
| 67 | continue |
---|
| 68 | f_label = '<font size=12>%s</font>:' % widget.label.strip() |
---|
| 69 | f_label = Paragraph(f_label, style["Normal"]) |
---|
| 70 | f_text = '<font size=12>%s</font>' % widget() |
---|
| 71 | f_text = Paragraph(f_text, style["Normal"]) |
---|
| 72 | data.append([f_label,f_text]) |
---|
| 73 | table = Table(data,style=SLIP_STYLE) |
---|
| 74 | return table |
---|
| 75 | |
---|
[7150] | 76 | class StudentsUtils(grok.GlobalUtility): |
---|
| 77 | """A collection of methods subject to customization. |
---|
| 78 | """ |
---|
| 79 | grok.implements(IStudentsUtils) |
---|
[7019] | 80 | |
---|
[7186] | 81 | def getPaymentDetails(self,category, student): |
---|
[7150] | 82 | d = {} |
---|
| 83 | d['p_item'] = u'' |
---|
| 84 | d['amount'] = 0 |
---|
| 85 | d['error'] = u'' |
---|
| 86 | d['p_session'] = student['studycourse'].current_session |
---|
| 87 | session = str(d['p_session']) |
---|
| 88 | try: |
---|
| 89 | academic_session = grok.getSite()['configuration'][session] |
---|
| 90 | except KeyError: |
---|
| 91 | d['error'] = u'Session configuration object is not available.' |
---|
| 92 | return d |
---|
| 93 | d['surcharge_1'] = academic_session.surcharge_1 |
---|
| 94 | d['surcharge_2'] = academic_session.surcharge_2 |
---|
| 95 | d['surcharge_3'] = academic_session.surcharge_3 |
---|
| 96 | if category == 'schoolfee': |
---|
| 97 | d['amount'] = academic_session.school_fee_base |
---|
| 98 | d['p_item'] = student['studycourse'].certificate.code |
---|
| 99 | elif category == 'clearance': |
---|
| 100 | d['p_item'] = student['studycourse'].certificate.code |
---|
| 101 | d['amount'] = academic_session.clearance_fee |
---|
| 102 | elif category == 'bed_allocation': |
---|
[7186] | 103 | d['p_item'] = self.getAccommodationDetails(student)['bt'] |
---|
[7150] | 104 | d['amount'] = academic_session.booking_fee |
---|
| 105 | return d |
---|
[7019] | 106 | |
---|
[7186] | 107 | def getAccommodationDetails(self, student): |
---|
[7150] | 108 | d = {} |
---|
| 109 | d['error'] = u'' |
---|
| 110 | site_confoguration = grok.getSite()['configuration'] |
---|
| 111 | d['booking_session'] = site_confoguration.accommodation_session |
---|
| 112 | d['allowed_states'] = site_confoguration.accommodation_states |
---|
| 113 | # Determine bed type |
---|
| 114 | studycourse = student['studycourse'] |
---|
| 115 | entry_session = studycourse.entry_session |
---|
| 116 | current_level = studycourse.current_level |
---|
| 117 | end_level = studycourse.certificate.end_level |
---|
| 118 | if entry_session == grok.getSite()['configuration'].accommodation_session: |
---|
| 119 | bt = 'fr' |
---|
| 120 | elif current_level >= end_level: |
---|
| 121 | bt = 'fi' |
---|
| 122 | else: |
---|
| 123 | bt = 're' |
---|
| 124 | if student.sex == 'f': |
---|
| 125 | sex = 'female' |
---|
| 126 | else: |
---|
| 127 | sex = 'male' |
---|
| 128 | special_handling = 'regular' |
---|
| 129 | d['bt'] = u'%s_%s_%s' % (special_handling,sex,bt) |
---|
| 130 | return d |
---|
[7019] | 131 | |
---|
[7150] | 132 | # In the standard configuration we select the first bed found, |
---|
| 133 | # but can also randomize the selection if we like. |
---|
[7186] | 134 | def selectBed(self, available_beds): |
---|
[7150] | 135 | return available_beds[0] |
---|
| 136 | |
---|
[7186] | 137 | def renderPDF(self, view, subject='', filename='slip.pdf', |
---|
[7150] | 138 | student=None, studentview=None): |
---|
| 139 | # (0,0),(-1,-1) = whole table |
---|
| 140 | # (0,0),(0,-1) = first column |
---|
| 141 | # (-1,0),(-1,-1) = last column |
---|
| 142 | # (0,0),(-1,0) = first row |
---|
| 143 | # (0,-1),(-1,-1) = last row |
---|
| 144 | |
---|
| 145 | pdf = canvas.Canvas(filename,pagesize=A4) |
---|
| 146 | pdf.setTitle(view.label) |
---|
| 147 | pdf.setSubject(subject) |
---|
| 148 | pdf.setAuthor('%s (%s)' % (view.request.principal.title, |
---|
| 149 | view.request.principal.id)) |
---|
| 150 | pdf.setCreator('WAeUP SIRP') |
---|
| 151 | width, height = A4 |
---|
| 152 | style = getSampleStyleSheet() |
---|
| 153 | pdf.line(1*cm,height-(1.8*cm),width-(1*cm),height-(1.8*cm)) |
---|
| 154 | |
---|
| 155 | story = [] |
---|
| 156 | frame_header = Frame(1*cm,1*cm,width-(1.7*cm),height-(1.7*cm)) |
---|
| 157 | header_title = getattr(grok.getSite(), 'name', u'Sample University') |
---|
| 158 | story.append(Paragraph(header_title, style["Heading1"])) |
---|
| 159 | frame_header.addFromList(story,pdf) |
---|
| 160 | |
---|
| 161 | story = [] |
---|
| 162 | frame_body = Frame(1*cm,1*cm,width-(2*cm),height-(3.5*cm)) |
---|
| 163 | story.append(Paragraph(view.label, style["Heading2"])) |
---|
| 164 | #story.append(HRFlowable()) |
---|
| 165 | if student: |
---|
| 166 | story.append(Spacer(1, 18)) |
---|
| 167 | studenttable = render_student_data(student, studentview) |
---|
| 168 | story.append(studenttable) |
---|
[7019] | 169 | story.append(Spacer(1, 18)) |
---|
[7186] | 170 | set_up_widgets(view) |
---|
[7150] | 171 | data = [] |
---|
| 172 | for widget in view.widgets: |
---|
| 173 | f_label = '<font size=12>%s</font>:' % widget.label.strip() |
---|
| 174 | f_label = Paragraph(f_label, style["Normal"]) |
---|
| 175 | f_text = '<font size=12>%s</font>' % widget() |
---|
| 176 | f_text = Paragraph(f_text, style["Normal"]) |
---|
| 177 | data.append([f_label,f_text]) |
---|
| 178 | table = Table(data,style=SLIP_STYLE) |
---|
| 179 | story.append(table) |
---|
| 180 | frame_body.addFromList(story,pdf) |
---|
[7019] | 181 | |
---|
[7150] | 182 | story = [] |
---|
| 183 | frame_footer = Frame(1*cm,0,width-(2*cm),1*cm) |
---|
| 184 | timestamp = datetime.now().strftime("%d/%m/%Y %H:%M:%S") |
---|
| 185 | f_text = '<font size=10>%s</font>' % timestamp |
---|
| 186 | story.append(Paragraph(f_text, style["Normal"])) |
---|
| 187 | frame_footer.addFromList(story,pdf) |
---|
[7019] | 188 | |
---|
[7150] | 189 | view.response.setHeader( |
---|
| 190 | 'Content-Type', 'application/pdf') |
---|
| 191 | return pdf.getpdfdata() |
---|