1 | ## $Id: browser.py 15470 2019-06-21 08:09:17Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2012 Uli Fouquet & Henrik Bettermann |
---|
4 | ## This program is free software; you can redistribute it and/or modify |
---|
5 | ## it under the terms of the GNU General Public License as published by |
---|
6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
7 | ## (at your option) any later version. |
---|
8 | ## |
---|
9 | ## This program is distributed in the hope that it will be useful, |
---|
10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | ## GNU General Public License for more details. |
---|
13 | ## |
---|
14 | ## You should have received a copy of the GNU General Public License |
---|
15 | ## along with this program; if not, write to the Free Software |
---|
16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | ## |
---|
18 | import grok |
---|
19 | from zope.formlib.textwidgets import BytesDisplayWidget |
---|
20 | from zope.component import getUtility |
---|
21 | from zope.security import checkPermission |
---|
22 | from zope.i18n import translate |
---|
23 | from datetime import datetime |
---|
24 | from waeup.kofa.widgets.datewidget import FriendlyDatetimeDisplayWidget |
---|
25 | from waeup.kofa.interfaces import IExtFileStore, IObjectHistory, IKofaUtils |
---|
26 | from waeup.kofa.browser.layout import action, UtilityView |
---|
27 | from waeup.kofa.utils.helpers import get_current_principal, to_timezone |
---|
28 | from waeup.kofa.students.browser import ( |
---|
29 | StudentPersonalDisplayFormPage, StudentPersonalManageFormPage, |
---|
30 | StudentClearanceManageFormPage, StudentClearanceEditFormPage, |
---|
31 | StudentClearanceDisplayFormPage, OnlinePaymentFakeApproveView, |
---|
32 | ExportPDFClearanceSlip, StudentBaseManageFormPage, |
---|
33 | StudentBaseDisplayFormPage, |
---|
34 | StudentBasePDFFormPage, |
---|
35 | StudentBaseEditFormPage, StudentPersonalEditFormPage, |
---|
36 | OnlinePaymentDisplayFormPage, OnlinePaymentAddFormPage, |
---|
37 | OnlinePaymentBreadcrumb, ExportPDFPaymentSlip, |
---|
38 | ExportPDFCourseRegistrationSlip, |
---|
39 | ExportPDFBedTicketSlip, |
---|
40 | StudentFilesUploadPage, emit_lock_message, |
---|
41 | AccommodationManageFormPage) |
---|
42 | from waeup.kofa.students.interfaces import IStudentsUtils |
---|
43 | from waeup.kofa.students.viewlets import ( |
---|
44 | PaymentReceiptActionButton, StudentPassportActionButton) |
---|
45 | from kofacustom.nigeria.students.interfaces import ( |
---|
46 | INigeriaStudentBase, INigeriaStudent, INigeriaStudentPersonal, |
---|
47 | INigeriaStudentPersonalEdit, |
---|
48 | INigeriaUGStudentClearance,INigeriaPGStudentClearance, |
---|
49 | INigeriaStudentOnlinePayment |
---|
50 | ) |
---|
51 | from waeup.kofa.students.workflow import ADMITTED |
---|
52 | from kofacustom.nigeria.interfaces import MessageFactory as _ |
---|
53 | |
---|
54 | class NigeriaOnlinePaymentBreadcrumb(OnlinePaymentBreadcrumb): |
---|
55 | """A breadcrumb for payments. |
---|
56 | """ |
---|
57 | grok.context(INigeriaStudentOnlinePayment) |
---|
58 | |
---|
59 | class PaymentReceiptActionButton(PaymentReceiptActionButton): |
---|
60 | grok.order(4) |
---|
61 | grok.context(INigeriaStudentOnlinePayment) |
---|
62 | |
---|
63 | class NigeriaStudentBaseDisplayFormPage(StudentBaseDisplayFormPage): |
---|
64 | """ Page to display student base data |
---|
65 | """ |
---|
66 | form_fields = grok.AutoFields(INigeriaStudentBase).omit( |
---|
67 | 'password', 'suspended', 'suspended_comment', |
---|
68 | 'flash_notice', 'provisionally_cleared') |
---|
69 | form_fields[ |
---|
70 | 'financial_clearance_date'].custom_widget = FriendlyDatetimeDisplayWidget('le') |
---|
71 | |
---|
72 | class NigeriaStudentBaseManageFormPage(StudentBaseManageFormPage): |
---|
73 | """ View to manage student base data |
---|
74 | """ |
---|
75 | form_fields = grok.AutoFields(INigeriaStudentBase).omit( |
---|
76 | 'student_id', 'adm_code', 'suspended', |
---|
77 | 'financially_cleared_by', 'financial_clearance_date') |
---|
78 | |
---|
79 | class NigeriaStudentBaseEditFormPage(StudentBaseEditFormPage): |
---|
80 | """ View to edit student base data |
---|
81 | """ |
---|
82 | form_fields = grok.AutoFields(INigeriaStudentBase).select( |
---|
83 | 'email', 'phone') |
---|
84 | |
---|
85 | class NigeriaStudentPersonalDisplayFormPage(StudentPersonalDisplayFormPage): |
---|
86 | """ Page to display student personal data |
---|
87 | """ |
---|
88 | form_fields = grok.AutoFields(INigeriaStudentPersonal) |
---|
89 | form_fields['perm_address'].custom_widget = BytesDisplayWidget |
---|
90 | form_fields['next_kin_address'].custom_widget = BytesDisplayWidget |
---|
91 | form_fields[ |
---|
92 | 'personal_updated'].custom_widget = FriendlyDatetimeDisplayWidget('le') |
---|
93 | |
---|
94 | class NigeriaStudentPersonalEditFormPage(StudentPersonalEditFormPage): |
---|
95 | """ Page to edit personal data |
---|
96 | """ |
---|
97 | form_fields = grok.AutoFields(INigeriaStudentPersonalEdit).omit('personal_updated') |
---|
98 | |
---|
99 | class NigeriaStudentPersonalManageFormPage(StudentPersonalManageFormPage): |
---|
100 | """ Page to edit personal data |
---|
101 | """ |
---|
102 | form_fields = grok.AutoFields(INigeriaStudentPersonal) |
---|
103 | form_fields['personal_updated'].for_display = True |
---|
104 | form_fields[ |
---|
105 | 'personal_updated'].custom_widget = FriendlyDatetimeDisplayWidget('le') |
---|
106 | |
---|
107 | class NigeriaStudentClearanceDisplayFormPage(StudentClearanceDisplayFormPage): |
---|
108 | """ Page to display student clearance data |
---|
109 | """ |
---|
110 | |
---|
111 | @property |
---|
112 | def form_fields(self): |
---|
113 | if self.context.is_postgrad: |
---|
114 | form_fields = grok.AutoFields( |
---|
115 | INigeriaPGStudentClearance).omit('clearance_locked') |
---|
116 | else: |
---|
117 | form_fields = grok.AutoFields( |
---|
118 | INigeriaUGStudentClearance).omit('clearance_locked') |
---|
119 | if not getattr(self.context, 'officer_comment'): |
---|
120 | form_fields = form_fields.omit('officer_comment') |
---|
121 | else: |
---|
122 | form_fields['officer_comment'].custom_widget = BytesDisplayWidget |
---|
123 | return form_fields |
---|
124 | |
---|
125 | class NigeriaExportPDFClearanceSlip(ExportPDFClearanceSlip): |
---|
126 | """Deliver a PDF slip of the context. |
---|
127 | """ |
---|
128 | omit_fields = ('password', 'suspended', 'suspended_comment', |
---|
129 | 'phone', 'adm_code', 'email', 'date_of_birth', 'current_level', |
---|
130 | 'flash_notice') |
---|
131 | |
---|
132 | @property |
---|
133 | def form_fields(self): |
---|
134 | if self.context.is_postgrad: |
---|
135 | form_fields = grok.AutoFields( |
---|
136 | INigeriaPGStudentClearance).omit('clearance_locked') |
---|
137 | else: |
---|
138 | form_fields = grok.AutoFields( |
---|
139 | INigeriaUGStudentClearance).omit('clearance_locked') |
---|
140 | if not getattr(self.context, 'officer_comment'): |
---|
141 | form_fields = form_fields.omit('officer_comment') |
---|
142 | return form_fields |
---|
143 | |
---|
144 | class NigeriaStudentClearanceManageFormPage(StudentClearanceManageFormPage): |
---|
145 | """ Page to edit student clearance data |
---|
146 | """ |
---|
147 | |
---|
148 | @property |
---|
149 | def form_fields(self): |
---|
150 | if self.context.is_postgrad: |
---|
151 | form_fields = grok.AutoFields( |
---|
152 | INigeriaPGStudentClearance).omit('clr_code') |
---|
153 | else: |
---|
154 | form_fields = grok.AutoFields( |
---|
155 | INigeriaUGStudentClearance).omit('clr_code') |
---|
156 | return form_fields |
---|
157 | |
---|
158 | class NigeriaStudentClearanceEditFormPage(StudentClearanceEditFormPage): |
---|
159 | """ View to edit student clearance data by student |
---|
160 | """ |
---|
161 | |
---|
162 | @property |
---|
163 | def form_fields(self): |
---|
164 | if self.context.is_postgrad: |
---|
165 | form_fields = grok.AutoFields(INigeriaPGStudentClearance).omit( |
---|
166 | 'clearance_locked', 'nysc_location', 'clr_code', 'officer_comment', |
---|
167 | 'physical_clearance_date') |
---|
168 | else: |
---|
169 | form_fields = grok.AutoFields(INigeriaUGStudentClearance).omit( |
---|
170 | 'clearance_locked', 'clr_code', 'officer_comment', |
---|
171 | 'physical_clearance_date') |
---|
172 | return form_fields |
---|
173 | |
---|
174 | class NigeriaExportPDFCourseRegistrationSlip(ExportPDFCourseRegistrationSlip): |
---|
175 | """Deliver a PDF slip of the context. |
---|
176 | """ |
---|
177 | omit_fields = ('password', 'suspended', 'suspended_comment', |
---|
178 | 'phone', 'adm_code', 'sex', 'email', 'date_of_birth', 'current_level', |
---|
179 | 'flash_notice') |
---|
180 | |
---|
181 | class NigeriaOnlinePaymentDisplayFormPage(OnlinePaymentDisplayFormPage): |
---|
182 | """ Page to view an online payment ticket |
---|
183 | """ |
---|
184 | grok.context(INigeriaStudentOnlinePayment) |
---|
185 | form_fields = grok.AutoFields(INigeriaStudentOnlinePayment).omit( |
---|
186 | 'provider_amt', 'gateway_amt', 'thirdparty_amt', 'p_item') |
---|
187 | form_fields[ |
---|
188 | 'creation_date'].custom_widget = FriendlyDatetimeDisplayWidget('le') |
---|
189 | form_fields[ |
---|
190 | 'payment_date'].custom_widget = FriendlyDatetimeDisplayWidget('le') |
---|
191 | |
---|
192 | class NigeriaOnlinePaymentAddFormPage(OnlinePaymentAddFormPage): |
---|
193 | """ Page to add an online payment ticket |
---|
194 | """ |
---|
195 | form_fields = grok.AutoFields(INigeriaStudentOnlinePayment).select( |
---|
196 | 'p_category') |
---|
197 | |
---|
198 | class NigeriaOnlinePaymentFakeApproveView(OnlinePaymentFakeApproveView): |
---|
199 | """ Disable payment approval view for students. |
---|
200 | |
---|
201 | This view is used for browser tests only and |
---|
202 | has to be neutralized here! |
---|
203 | """ |
---|
204 | grok.name('fake_approve') |
---|
205 | grok.require('waeup.managePortal') |
---|
206 | |
---|
207 | def update(self): |
---|
208 | return |
---|
209 | |
---|
210 | class NigeriaExportPDFPaymentSlip(ExportPDFPaymentSlip): |
---|
211 | """Deliver a PDF slip of the context. |
---|
212 | """ |
---|
213 | grok.context(INigeriaStudentOnlinePayment) |
---|
214 | form_fields = grok.AutoFields(INigeriaStudentOnlinePayment).omit( |
---|
215 | 'provider_amt', 'gateway_amt', 'thirdparty_amt', 'p_item', |
---|
216 | 'p_split_data') |
---|
217 | form_fields['creation_date'].custom_widget = FriendlyDatetimeDisplayWidget('le') |
---|
218 | form_fields['payment_date'].custom_widget = FriendlyDatetimeDisplayWidget('le') |
---|
219 | omit_fields = ('password', 'suspended', 'suspended_comment', 'phone', |
---|
220 | 'adm_code', 'sex', 'email', 'date_of_birth', 'current_level', |
---|
221 | 'flash_notice') |
---|
222 | |
---|
223 | class NigeriaAccommodationManageFormPage(AccommodationManageFormPage): |
---|
224 | """ Page to manage bed tickets. |
---|
225 | This manage form page is for both students and students officers. |
---|
226 | """ |
---|
227 | with_hostel_selection = False |
---|
228 | |
---|
229 | class NigeriaExportPDFBedTicketSlip(ExportPDFBedTicketSlip): |
---|
230 | """Deliver a PDF slip of the context. |
---|
231 | """ |
---|
232 | omit_fields = ('password', 'suspended', 'suspended_comment', |
---|
233 | 'phone', 'adm_code', 'email', 'date_of_birth', 'current_level', |
---|
234 | 'flash_notice') |
---|
235 | |
---|
236 | class StudentPassportActionButton(StudentPassportActionButton): |
---|
237 | |
---|
238 | @property |
---|
239 | def target_url(self): |
---|
240 | # Passport pictures must not be editable if application slip |
---|
241 | # exists. |
---|
242 | slip = getUtility(IExtFileStore).getFileByContext( |
---|
243 | self.context, 'application_slip') |
---|
244 | PORTRAIT_CHANGE_STATES = getUtility(IStudentsUtils).PORTRAIT_CHANGE_STATES |
---|
245 | if self.context.state not in PORTRAIT_CHANGE_STATES or slip is not None: |
---|
246 | return '' |
---|
247 | return self.view.url(self.view.context, self.target) |
---|
248 | |
---|
249 | class NigeriaStudentFilesUploadPage(StudentFilesUploadPage): |
---|
250 | """ View to upload passport picture. |
---|
251 | |
---|
252 | Students are not allowed to change the picture if they |
---|
253 | passed the regular Kofa application. |
---|
254 | """ |
---|
255 | |
---|
256 | def update(self): |
---|
257 | # Passport pictures must not be editable if application slip |
---|
258 | # exists. |
---|
259 | slip = getUtility(IExtFileStore).getFileByContext( |
---|
260 | self.context, 'application_slip') |
---|
261 | PORTRAIT_CHANGE_STATES = getUtility(IStudentsUtils).PORTRAIT_CHANGE_STATES |
---|
262 | if self.context.state not in PORTRAIT_CHANGE_STATES or slip is not None: |
---|
263 | emit_lock_message(self) |
---|
264 | return |
---|
265 | super(StudentFilesUploadPage, self).update() |
---|
266 | return |
---|
267 | |
---|
268 | class ClearStudentFinancially(UtilityView, grok.View): |
---|
269 | """ Clear student financially by financial clearance officer |
---|
270 | """ |
---|
271 | grok.context(INigeriaStudent) |
---|
272 | grok.name('clear_financially') |
---|
273 | grok.require('waeup.clearStudentFinancially') |
---|
274 | |
---|
275 | def update(self): |
---|
276 | if self.context.financially_cleared_by: |
---|
277 | self.flash(_('This student has already been financially cleared.'), |
---|
278 | type="danger") |
---|
279 | self.redirect(self.url(self.context)) |
---|
280 | return |
---|
281 | user = get_current_principal() |
---|
282 | if user is None: |
---|
283 | usertitle = 'system' |
---|
284 | else: |
---|
285 | usertitle = getattr(user, 'public_name', None) |
---|
286 | if not usertitle: |
---|
287 | usertitle = user.title |
---|
288 | self.context.financially_cleared_by = usertitle |
---|
289 | self.context.financial_clearance_date = datetime.utcnow() |
---|
290 | self.context.writeLogMessage(self,'financially cleared') |
---|
291 | history = IObjectHistory(self.context) |
---|
292 | history.addMessage('Financially cleared') |
---|
293 | self.flash(_('Student has been financially cleared.')) |
---|
294 | self.redirect(self.url(self.context)) |
---|
295 | return |
---|
296 | |
---|
297 | def render(self): |
---|
298 | return |
---|
299 | |
---|
300 | class WithdrawFinancialClearance(UtilityView, grok.View): |
---|
301 | """ Withdraw financial clearance by financial clearance officer |
---|
302 | """ |
---|
303 | grok.context(INigeriaStudent) |
---|
304 | grok.name('withdraw_financial_clearance') |
---|
305 | grok.require('waeup.clearStudentFinancially') |
---|
306 | |
---|
307 | def update(self): |
---|
308 | if not self.context.financially_cleared_by: |
---|
309 | self.flash(_('This student has not yet been financially cleared.'), |
---|
310 | type="danger") |
---|
311 | self.redirect(self.url(self.context)) |
---|
312 | return |
---|
313 | self.context.financially_cleared_by = None |
---|
314 | self.context.financial_clearance_date = None |
---|
315 | self.context.writeLogMessage(self,'financial clearance withdrawn') |
---|
316 | history = IObjectHistory(self.context) |
---|
317 | history.addMessage('Financial clearance withdrawn') |
---|
318 | self.flash(_('Financial clearance withdrawn.')) |
---|
319 | self.redirect(self.url(self.context)) |
---|
320 | return |
---|
321 | |
---|
322 | def render(self): |
---|
323 | return |
---|
324 | |
---|
325 | cleared_note = """ |
---|
326 | <br /><br /><br /> |
---|
327 | <strong>Financially cleared on %s by %s.</strong> |
---|
328 | |
---|
329 | """ |
---|
330 | |
---|
331 | class NigeriaExportPDFFinancialClearancePage(UtilityView, grok.View): |
---|
332 | """Deliver a PDF financial clearance slip. |
---|
333 | """ |
---|
334 | grok.context(INigeriaStudent) |
---|
335 | grok.name('fee_payment_history.pdf') |
---|
336 | grok.require('waeup.viewStudent') |
---|
337 | prefix = 'form' |
---|
338 | |
---|
339 | omit_fields = ( |
---|
340 | 'suspended', 'phone', |
---|
341 | 'adm_code', 'suspended_comment', |
---|
342 | 'date_of_birth', 'current_level', |
---|
343 | 'flash_notice') |
---|
344 | |
---|
345 | form_fields = None |
---|
346 | |
---|
347 | @property |
---|
348 | def label(self): |
---|
349 | portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
350 | return translate(_('Fee Payment History for'), |
---|
351 | 'waeup.kofa', target_language=portal_language) \ |
---|
352 | + ' %s' % self.context.display_fullname |
---|
353 | |
---|
354 | def _sigsInFooter(self): |
---|
355 | if not checkPermission('waeup.clearStudentFinancially', self.context): |
---|
356 | return () |
---|
357 | return (_('Date, Checking Officer Signature'), |
---|
358 | _('Date, Approving Officer Signature'), |
---|
359 | ) |
---|
360 | |
---|
361 | @property |
---|
362 | def note(self): |
---|
363 | if self.context.financially_cleared_by: |
---|
364 | tz = getUtility(IKofaUtils).tzinfo |
---|
365 | try: |
---|
366 | timestamp = to_timezone( |
---|
367 | self.context.financial_clearance_date, tz).strftime( |
---|
368 | "%Y-%m-%d %H:%M:%S") |
---|
369 | except ValueError: |
---|
370 | return |
---|
371 | return cleared_note % ( |
---|
372 | timestamp, self.context.financially_cleared_by) |
---|
373 | return |
---|
374 | |
---|
375 | @property |
---|
376 | def tabletitle(self): |
---|
377 | portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
378 | tabletitle = [] |
---|
379 | tabletitle.append(translate(_('Successful Payments'), 'waeup.kofa', |
---|
380 | target_language=portal_language)) |
---|
381 | return tabletitle |
---|
382 | |
---|
383 | def render(self): |
---|
384 | portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
385 | P_ID = translate(_('Payment Id'), 'waeup.kofa', target_language=portal_language) |
---|
386 | #CD = translate(_('Creation Date'), 'waeup.kofa', target_language=portal_language) |
---|
387 | PD = translate(_('Payment Date'), 'waeup.kofa', target_language=portal_language) |
---|
388 | CAT = translate(_('Payment Category'), 'waeup.kofa', target_language=portal_language) |
---|
389 | ITEM = translate(_('Payment Item'), 'waeup.kofa', target_language=portal_language) |
---|
390 | AMT = translate(_('Amount (Naira)'), 'waeup.kofa', target_language=portal_language) |
---|
391 | SSS = translate(_('Payment Session'), 'waeup.kofa', target_language=portal_language) |
---|
392 | studentview = StudentBasePDFFormPage(self.context.student, |
---|
393 | self.request, self.omit_fields) |
---|
394 | students_utils = getUtility(IStudentsUtils) |
---|
395 | |
---|
396 | tabledata = [] |
---|
397 | tableheader = [] |
---|
398 | tabledata.append(sorted( |
---|
399 | [value for value in self.context['payments'].values() |
---|
400 | if value.p_state in ('paid', 'waived')], key=lambda value: value.p_session)) |
---|
401 | tableheader.append([(P_ID,'p_id', 4.2), |
---|
402 | #(CD,'creation_date', 3), |
---|
403 | (PD,'formatted_p_date', 3), |
---|
404 | (CAT,'category', 3), |
---|
405 | (ITEM, 'p_item', 3), |
---|
406 | (AMT, 'amount_auth', 2), |
---|
407 | (SSS, 'p_session', 2), |
---|
408 | ]) |
---|
409 | return students_utils.renderPDF( |
---|
410 | self, 'financial_clearance_slip.pdf', |
---|
411 | self.context.student, studentview, |
---|
412 | tableheader=tableheader, |
---|
413 | tabledata=tabledata, |
---|
414 | signatures=None, |
---|
415 | sigs_in_footer=self._sigsInFooter(), |
---|
416 | omit_fields=self.omit_fields, |
---|
417 | note=self.note |
---|
418 | ) |
---|