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