1 | ## $Id: browser.py 7681 2012-02-22 21:14:09Z 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 | ## |
---|
18 | """UI components for students and related components. |
---|
19 | """ |
---|
20 | import sys |
---|
21 | import grok |
---|
22 | from urllib import urlencode |
---|
23 | from time import time |
---|
24 | from datetime import datetime |
---|
25 | from zope.event import notify |
---|
26 | from zope.catalog.interfaces import ICatalog |
---|
27 | from zope.component import queryUtility, getUtility, createObject |
---|
28 | from zope.formlib.textwidgets import BytesDisplayWidget |
---|
29 | from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState |
---|
30 | from waeup.sirp.accesscodes import ( |
---|
31 | invalidate_accesscode, get_access_code, create_accesscode) |
---|
32 | from waeup.sirp.accesscodes.workflow import USED |
---|
33 | from waeup.sirp.browser import ( |
---|
34 | SIRPPage, SIRPEditFormPage, SIRPAddFormPage, SIRPDisplayFormPage, |
---|
35 | ContactAdminForm, SIRPForm) |
---|
36 | from waeup.sirp.browser.interfaces import ICaptchaManager |
---|
37 | from waeup.sirp.browser.breadcrumbs import Breadcrumb |
---|
38 | from waeup.sirp.browser.resources import datepicker, datatable, tabs, warning |
---|
39 | from waeup.sirp.browser.layout import jsaction, action, UtilityView |
---|
40 | from waeup.sirp.interfaces import ( |
---|
41 | ISIRPObject, IUserAccount, IExtFileStore, IPasswordValidator, IContactForm, |
---|
42 | ISIRPUtils, IUniversity) |
---|
43 | from waeup.sirp.widgets.datewidget import ( |
---|
44 | FriendlyDateWidget, FriendlyDateDisplayWidget, |
---|
45 | FriendlyDatetimeDisplayWidget) |
---|
46 | from waeup.sirp.students.interfaces import ( |
---|
47 | IStudentsContainer, IStudent, IStudentClearance, |
---|
48 | IStudentPersonal, IStudentBase, IStudentStudyCourse, |
---|
49 | IStudentAccommodation, IStudentStudyLevel, |
---|
50 | ICourseTicket, ICourseTicketAdd, IStudentPaymentsContainer, |
---|
51 | IStudentOnlinePayment, IBedTicket, IStudentsUtils, IStudentChangePassword |
---|
52 | ) |
---|
53 | from waeup.sirp.students.catalog import search |
---|
54 | from waeup.sirp.students.workflow import (ADMITTED, PAID, |
---|
55 | CLEARANCE, REQUESTED, RETURNING, CLEARED, REGISTERED, VALIDATED) |
---|
56 | from waeup.sirp.students.studylevel import StudentStudyLevel, CourseTicket |
---|
57 | from waeup.sirp.students.vocabularies import StudyLevelSource |
---|
58 | from waeup.sirp.browser.resources import toggleall |
---|
59 | from waeup.sirp.hostels.hostel import NOT_OCCUPIED |
---|
60 | from waeup.sirp.utils.helpers import get_current_principal |
---|
61 | |
---|
62 | def write_log_message(view, message): |
---|
63 | ob_class = view.__implemented__.__name__.replace('waeup.sirp.','') |
---|
64 | view.context.getStudent().loggerInfo(ob_class, message) |
---|
65 | return |
---|
66 | |
---|
67 | # Save function used for save methods in pages |
---|
68 | def msave(view, **data): |
---|
69 | changed_fields = view.applyData(view.context, **data) |
---|
70 | # Turn list of lists into single list |
---|
71 | if changed_fields: |
---|
72 | changed_fields = reduce(lambda x,y: x+y, changed_fields.values()) |
---|
73 | # Inform catalog if certificate has changed |
---|
74 | # (applyData does this only for the context) |
---|
75 | if 'certificate' in changed_fields: |
---|
76 | notify(grok.ObjectModifiedEvent(view.context.getStudent())) |
---|
77 | fields_string = ' + '.join(changed_fields) |
---|
78 | view.flash('Form has been saved.') |
---|
79 | if fields_string: |
---|
80 | write_log_message(view, 'saved: %s' % fields_string) |
---|
81 | return |
---|
82 | |
---|
83 | def emit_lock_message(view): |
---|
84 | """Flash a lock message. |
---|
85 | """ |
---|
86 | view.flash('The requested form is locked (read-only).') |
---|
87 | view.redirect(view.url(view.context)) |
---|
88 | return |
---|
89 | |
---|
90 | class StudentsBreadcrumb(Breadcrumb): |
---|
91 | """A breadcrumb for the students container. |
---|
92 | """ |
---|
93 | grok.context(IStudentsContainer) |
---|
94 | title = 'Students' |
---|
95 | |
---|
96 | @property |
---|
97 | def target(self): |
---|
98 | user = get_current_principal() |
---|
99 | if getattr(user, 'user_type', None) == 'student': |
---|
100 | return None |
---|
101 | return self.viewname |
---|
102 | |
---|
103 | class StudentBreadcrumb(Breadcrumb): |
---|
104 | """A breadcrumb for the student container. |
---|
105 | """ |
---|
106 | grok.context(IStudent) |
---|
107 | |
---|
108 | def title(self): |
---|
109 | return self.context.display_fullname |
---|
110 | |
---|
111 | class SudyCourseBreadcrumb(Breadcrumb): |
---|
112 | """A breadcrumb for the student study course. |
---|
113 | """ |
---|
114 | grok.context(IStudentStudyCourse) |
---|
115 | title = 'Study Course' |
---|
116 | |
---|
117 | class PaymentsBreadcrumb(Breadcrumb): |
---|
118 | """A breadcrumb for the student payments folder. |
---|
119 | """ |
---|
120 | grok.context(IStudentPaymentsContainer) |
---|
121 | title = 'Payments' |
---|
122 | |
---|
123 | class OnlinePaymentBreadcrumb(Breadcrumb): |
---|
124 | """A breadcrumb for payments. |
---|
125 | """ |
---|
126 | grok.context(IStudentOnlinePayment) |
---|
127 | |
---|
128 | @property |
---|
129 | def title(self): |
---|
130 | return self.context.p_id |
---|
131 | |
---|
132 | class AccommodationBreadcrumb(Breadcrumb): |
---|
133 | """A breadcrumb for the student accommodation folder. |
---|
134 | """ |
---|
135 | grok.context(IStudentAccommodation) |
---|
136 | title = 'Accommodation' |
---|
137 | |
---|
138 | class BedTicketBreadcrumb(Breadcrumb): |
---|
139 | """A breadcrumb for bed tickets. |
---|
140 | """ |
---|
141 | grok.context(IBedTicket) |
---|
142 | |
---|
143 | @property |
---|
144 | def title(self): |
---|
145 | return 'Bed Ticket %s' % self.context.getSessionString() |
---|
146 | |
---|
147 | class StudyLevelBreadcrumb(Breadcrumb): |
---|
148 | """A breadcrumb for course lists. |
---|
149 | """ |
---|
150 | grok.context(IStudentStudyLevel) |
---|
151 | |
---|
152 | @property |
---|
153 | def title(self): |
---|
154 | return self.context.level_title |
---|
155 | |
---|
156 | class StudentsContainerPage(SIRPPage): |
---|
157 | """The standard view for student containers. |
---|
158 | """ |
---|
159 | grok.context(IStudentsContainer) |
---|
160 | grok.name('index') |
---|
161 | grok.require('waeup.viewStudentsContainer') |
---|
162 | grok.template('containerpage') |
---|
163 | label = 'Student Section' |
---|
164 | pnav = 4 |
---|
165 | |
---|
166 | def update(self, *args, **kw): |
---|
167 | datatable.need() |
---|
168 | form = self.request.form |
---|
169 | self.hitlist = [] |
---|
170 | if 'searchterm' in form and form['searchterm']: |
---|
171 | self.searchterm = form['searchterm'] |
---|
172 | self.searchtype = form['searchtype'] |
---|
173 | elif 'old_searchterm' in form: |
---|
174 | self.searchterm = form['old_searchterm'] |
---|
175 | self.searchtype = form['old_searchtype'] |
---|
176 | else: |
---|
177 | if 'search' in form: |
---|
178 | self.flash('Empty search string.') |
---|
179 | return |
---|
180 | if self.searchtype == 'current_session': |
---|
181 | self.searchterm = int(self.searchterm) |
---|
182 | self.hitlist = search(query=self.searchterm, |
---|
183 | searchtype=self.searchtype, view=self) |
---|
184 | if not self.hitlist: |
---|
185 | self.flash('No student found.') |
---|
186 | return |
---|
187 | |
---|
188 | class StudentsContainerManagePage(SIRPPage): |
---|
189 | """The manage page for student containers. |
---|
190 | """ |
---|
191 | grok.context(IStudentsContainer) |
---|
192 | grok.name('manage') |
---|
193 | grok.require('waeup.manageStudent') |
---|
194 | grok.template('containermanagepage') |
---|
195 | pnav = 4 |
---|
196 | label = 'Manage student section' |
---|
197 | |
---|
198 | def update(self, *args, **kw): |
---|
199 | datatable.need() |
---|
200 | toggleall.need() |
---|
201 | warning.need() |
---|
202 | form = self.request.form |
---|
203 | self.hitlist = [] |
---|
204 | if 'searchterm' in form and form['searchterm']: |
---|
205 | self.searchterm = form['searchterm'] |
---|
206 | self.searchtype = form['searchtype'] |
---|
207 | elif 'old_searchterm' in form: |
---|
208 | self.searchterm = form['old_searchterm'] |
---|
209 | self.searchtype = form['old_searchtype'] |
---|
210 | else: |
---|
211 | if 'search' in form: |
---|
212 | self.flash('Empty search string.') |
---|
213 | return |
---|
214 | if not 'entries' in form: |
---|
215 | self.hitlist = search(query=self.searchterm, |
---|
216 | searchtype=self.searchtype, view=self) |
---|
217 | if not self.hitlist: |
---|
218 | self.flash('No student found.') |
---|
219 | if 'remove' in form: |
---|
220 | self.flash('No item selected.') |
---|
221 | return |
---|
222 | entries = form['entries'] |
---|
223 | if isinstance(entries, basestring): |
---|
224 | entries = [entries] |
---|
225 | deleted = [] |
---|
226 | for entry in entries: |
---|
227 | if 'remove' in form: |
---|
228 | del self.context[entry] |
---|
229 | deleted.append(entry) |
---|
230 | self.hitlist = search(query=self.searchterm, |
---|
231 | searchtype=self.searchtype, view=self) |
---|
232 | if len(deleted): |
---|
233 | self.flash('Successfully removed: %s' % ', '.join(deleted)) |
---|
234 | return |
---|
235 | |
---|
236 | class StudentAddFormPage(SIRPAddFormPage): |
---|
237 | """Add-form to add a student. |
---|
238 | """ |
---|
239 | grok.context(IStudentsContainer) |
---|
240 | grok.require('waeup.manageStudent') |
---|
241 | grok.name('addstudent') |
---|
242 | form_fields = grok.AutoFields(IStudent).select( |
---|
243 | 'firstname', 'middlename', 'lastname', 'reg_number') |
---|
244 | label = 'Add student' |
---|
245 | pnav = 4 |
---|
246 | |
---|
247 | @action('Create student record', style='primary') |
---|
248 | def addStudent(self, **data): |
---|
249 | student = createObject(u'waeup.Student') |
---|
250 | self.applyData(student, **data) |
---|
251 | self.context.addStudent(student) |
---|
252 | self.flash('Student record created.') |
---|
253 | self.redirect(self.url(self.context[student.student_id], 'index')) |
---|
254 | return |
---|
255 | |
---|
256 | class StudentBaseDisplayFormPage(SIRPDisplayFormPage): |
---|
257 | """ Page to display student base data |
---|
258 | """ |
---|
259 | grok.context(IStudent) |
---|
260 | grok.name('index') |
---|
261 | grok.require('waeup.viewStudent') |
---|
262 | grok.template('basepage') |
---|
263 | form_fields = grok.AutoFields(IStudentBase).omit('password') |
---|
264 | pnav = 4 |
---|
265 | |
---|
266 | @property |
---|
267 | def label(self): |
---|
268 | return '%s: Base Data' % self.context.display_fullname |
---|
269 | |
---|
270 | @property |
---|
271 | def hasPassword(self): |
---|
272 | if self.context.password: |
---|
273 | return 'set' |
---|
274 | return 'unset' |
---|
275 | |
---|
276 | class ContactStudentForm(ContactAdminForm): |
---|
277 | grok.context(IStudent) |
---|
278 | grok.name('contactstudent') |
---|
279 | grok.require('waeup.viewStudent') |
---|
280 | pnav = 4 |
---|
281 | form_fields = grok.AutoFields(IContactForm).select('subject', 'body') |
---|
282 | |
---|
283 | def update(self, subject=u''): |
---|
284 | self.form_fields.get('subject').field.default = subject |
---|
285 | self.subject = subject |
---|
286 | return |
---|
287 | |
---|
288 | def label(self): |
---|
289 | return u'Send message to %s' % self.context.display_fullname |
---|
290 | |
---|
291 | @action('Send message now', style='primary') |
---|
292 | def send(self, *args, **data): |
---|
293 | try: |
---|
294 | email = self.request.principal.email |
---|
295 | except AttributeError: |
---|
296 | email = self.config.email_admin |
---|
297 | usertype = getattr(self.request.principal, |
---|
298 | 'user_type', 'system').title() |
---|
299 | sirp_utils = getUtility(ISIRPUtils) |
---|
300 | success = sirp_utils.sendContactForm( |
---|
301 | self.request.principal.title,email, |
---|
302 | self.context.display_fullname,self.context.email, |
---|
303 | self.request.principal.id,usertype, |
---|
304 | self.config.name, |
---|
305 | data['body'],data['subject']) |
---|
306 | if success: |
---|
307 | self.flash('Your message has been sent.') |
---|
308 | else: |
---|
309 | self.flash('An smtp server error occurred.') |
---|
310 | return |
---|
311 | |
---|
312 | class StudentBaseManageFormPage(SIRPEditFormPage): |
---|
313 | """ View to manage student base data |
---|
314 | """ |
---|
315 | grok.context(IStudent) |
---|
316 | grok.name('manage_base') |
---|
317 | grok.require('waeup.manageStudent') |
---|
318 | form_fields = grok.AutoFields(IStudentBase).omit('student_id') |
---|
319 | grok.template('basemanagepage') |
---|
320 | label = 'Manage base data' |
---|
321 | pnav = 4 |
---|
322 | |
---|
323 | def update(self): |
---|
324 | datepicker.need() # Enable jQuery datepicker in date fields. |
---|
325 | tabs.need() |
---|
326 | self.tab1 = self.tab2 = '' |
---|
327 | qs = self.request.get('QUERY_STRING', '') |
---|
328 | if not qs: |
---|
329 | qs = 'tab1' |
---|
330 | setattr(self, qs, 'active') |
---|
331 | super(StudentBaseManageFormPage, self).update() |
---|
332 | self.wf_info = IWorkflowInfo(self.context) |
---|
333 | return |
---|
334 | |
---|
335 | def getTransitions(self): |
---|
336 | """Return a list of dicts of allowed transition ids and titles. |
---|
337 | |
---|
338 | Each list entry provides keys ``name`` and ``title`` for |
---|
339 | internal name and (human readable) title of a single |
---|
340 | transition. |
---|
341 | """ |
---|
342 | allowed_transitions = self.wf_info.getManualTransitions() |
---|
343 | return [dict(name='', title='No transition')] +[ |
---|
344 | dict(name=x, title=y) for x, y in allowed_transitions] |
---|
345 | |
---|
346 | @action('Save', style='primary') |
---|
347 | def save(self, **data): |
---|
348 | form = self.request.form |
---|
349 | password = form.get('password', None) |
---|
350 | password_ctl = form.get('control_password', None) |
---|
351 | if password: |
---|
352 | validator = getUtility(IPasswordValidator) |
---|
353 | errors = validator.validate_password(password, password_ctl) |
---|
354 | if errors: |
---|
355 | self.flash( ' '.join(errors)) |
---|
356 | return |
---|
357 | changed_fields = self.applyData(self.context, **data) |
---|
358 | # Turn list of lists into single list |
---|
359 | if changed_fields: |
---|
360 | changed_fields = reduce(lambda x,y: x+y, changed_fields.values()) |
---|
361 | else: |
---|
362 | changed_fields = [] |
---|
363 | if password: |
---|
364 | # Now we know that the form has no errors and can set password ... |
---|
365 | IUserAccount(self.context).setPassword(password) |
---|
366 | changed_fields.append('password') |
---|
367 | # ... and execute transition |
---|
368 | if form.has_key('transition') and form['transition']: |
---|
369 | transition_id = form['transition'] |
---|
370 | self.wf_info.fireTransition(transition_id) |
---|
371 | fields_string = ' + '.join(changed_fields) |
---|
372 | self.flash('Form has been saved.') |
---|
373 | if fields_string: |
---|
374 | write_log_message(self, 'saved: % s' % fields_string) |
---|
375 | return |
---|
376 | |
---|
377 | class StudentClearanceDisplayFormPage(SIRPDisplayFormPage): |
---|
378 | """ Page to display student clearance data |
---|
379 | """ |
---|
380 | grok.context(IStudent) |
---|
381 | grok.name('view_clearance') |
---|
382 | grok.require('waeup.viewStudent') |
---|
383 | form_fields = grok.AutoFields(IStudentClearance).omit('clearance_locked') |
---|
384 | form_fields['date_of_birth'].custom_widget = FriendlyDateDisplayWidget('le') |
---|
385 | pnav = 4 |
---|
386 | |
---|
387 | @property |
---|
388 | def label(self): |
---|
389 | return '%s: Clearance Data' % self.context.display_fullname |
---|
390 | |
---|
391 | class ExportPDFClearanceSlipPage(grok.View): |
---|
392 | """Deliver a PDF slip of the context. |
---|
393 | """ |
---|
394 | grok.context(IStudent) |
---|
395 | grok.name('clearance.pdf') |
---|
396 | grok.require('waeup.viewStudent') |
---|
397 | form_fields = grok.AutoFields(IStudentClearance).omit('clearance_locked') |
---|
398 | form_fields['date_of_birth'].custom_widget = FriendlyDateDisplayWidget('le') |
---|
399 | prefix = 'form' |
---|
400 | title = 'Clearance Data' |
---|
401 | |
---|
402 | @property |
---|
403 | def label(self): |
---|
404 | return 'Clearance Slip of %s' % self.context.display_fullname |
---|
405 | |
---|
406 | def render(self): |
---|
407 | studentview = StudentBaseDisplayFormPage(self.context.getStudent(), |
---|
408 | self.request) |
---|
409 | students_utils = getUtility(IStudentsUtils) |
---|
410 | return students_utils.renderPDF( |
---|
411 | self, 'clearance.pdf', |
---|
412 | self.context.getStudent(), studentview) |
---|
413 | |
---|
414 | class StudentClearanceManageFormPage(SIRPEditFormPage): |
---|
415 | """ Page to edit student clearance data |
---|
416 | """ |
---|
417 | grok.context(IStudent) |
---|
418 | grok.name('edit_clearance') |
---|
419 | grok.require('waeup.manageStudent') |
---|
420 | grok.template('clearanceeditpage') |
---|
421 | form_fields = grok.AutoFields(IStudentClearance) |
---|
422 | label = 'Manage clearance data' |
---|
423 | pnav = 4 |
---|
424 | form_fields['date_of_birth'].custom_widget = FriendlyDateWidget('le-year') |
---|
425 | |
---|
426 | def update(self): |
---|
427 | datepicker.need() # Enable jQuery datepicker in date fields. |
---|
428 | tabs.need() |
---|
429 | self.tab1 = self.tab2 = '' |
---|
430 | qs = self.request.get('QUERY_STRING', '') |
---|
431 | if not qs: |
---|
432 | qs = 'tab1' |
---|
433 | setattr(self, qs, 'active') |
---|
434 | return super(StudentClearanceManageFormPage, self).update() |
---|
435 | |
---|
436 | @action('Save', style='primary') |
---|
437 | def save(self, **data): |
---|
438 | msave(self, **data) |
---|
439 | return |
---|
440 | |
---|
441 | class StudentClearPage(UtilityView, grok.View): |
---|
442 | """ Clear student by clearance officer |
---|
443 | """ |
---|
444 | grok.context(IStudent) |
---|
445 | grok.name('clear') |
---|
446 | grok.require('waeup.clearStudent') |
---|
447 | |
---|
448 | def update(self): |
---|
449 | if self.context.state == REQUESTED: |
---|
450 | IWorkflowInfo(self.context).fireTransition('clear') |
---|
451 | self.flash('Student has been cleared.') |
---|
452 | else: |
---|
453 | self.flash('Student is in the wrong state.') |
---|
454 | self.redirect(self.url(self.context,'view_clearance')) |
---|
455 | return |
---|
456 | |
---|
457 | def render(self): |
---|
458 | return |
---|
459 | |
---|
460 | class StudentRejectClearancePage(UtilityView, grok.View): |
---|
461 | """ Reject clearance by clearance officers |
---|
462 | """ |
---|
463 | grok.context(IStudent) |
---|
464 | grok.name('reject_clearance') |
---|
465 | grok.require('waeup.clearStudent') |
---|
466 | |
---|
467 | def update(self): |
---|
468 | if self.context.state == CLEARED: |
---|
469 | IWorkflowInfo(self.context).fireTransition('reset4') |
---|
470 | message = 'Clearance has been annulled' |
---|
471 | self.flash(message) |
---|
472 | elif self.context.state == REQUESTED: |
---|
473 | IWorkflowInfo(self.context).fireTransition('reset3') |
---|
474 | message = 'Clearance request has been rejected' |
---|
475 | self.flash(message) |
---|
476 | else: |
---|
477 | self.flash('Student is in the wrong state.') |
---|
478 | self.redirect(self.url(self.context,'view_clearance')) |
---|
479 | return |
---|
480 | args = {'subject':message} |
---|
481 | self.redirect(self.url(self.context) + |
---|
482 | '/contactstudent?%s' % urlencode(args)) |
---|
483 | return |
---|
484 | |
---|
485 | def render(self): |
---|
486 | return |
---|
487 | |
---|
488 | class StudentPersonalDisplayFormPage(SIRPDisplayFormPage): |
---|
489 | """ Page to display student personal data |
---|
490 | """ |
---|
491 | grok.context(IStudent) |
---|
492 | grok.name('view_personal') |
---|
493 | grok.require('waeup.viewStudent') |
---|
494 | form_fields = grok.AutoFields(IStudentPersonal) |
---|
495 | form_fields['perm_address'].custom_widget = BytesDisplayWidget |
---|
496 | pnav = 4 |
---|
497 | |
---|
498 | @property |
---|
499 | def label(self): |
---|
500 | return '%s: Personal Data' % self.context.display_fullname |
---|
501 | |
---|
502 | class StudentPersonalManageFormPage(SIRPEditFormPage): |
---|
503 | """ Page to edit student clearance data |
---|
504 | """ |
---|
505 | grok.context(IStudent) |
---|
506 | grok.name('edit_personal') |
---|
507 | grok.require('waeup.viewStudent') |
---|
508 | form_fields = grok.AutoFields(IStudentPersonal) |
---|
509 | label = 'Manage personal data' |
---|
510 | pnav = 4 |
---|
511 | |
---|
512 | @action('Save', style='primary') |
---|
513 | def save(self, **data): |
---|
514 | msave(self, **data) |
---|
515 | return |
---|
516 | |
---|
517 | class StudyCourseDisplayFormPage(SIRPDisplayFormPage): |
---|
518 | """ Page to display the student study course data |
---|
519 | """ |
---|
520 | grok.context(IStudentStudyCourse) |
---|
521 | grok.name('index') |
---|
522 | grok.require('waeup.viewStudent') |
---|
523 | form_fields = grok.AutoFields(IStudentStudyCourse) |
---|
524 | grok.template('studycoursepage') |
---|
525 | pnav = 4 |
---|
526 | |
---|
527 | @property |
---|
528 | def label(self): |
---|
529 | return '%s: Study Course' % self.context.__parent__.display_fullname |
---|
530 | |
---|
531 | @property |
---|
532 | def current_mode(self): |
---|
533 | if self.context.certificate is not None: |
---|
534 | studymodes_dict = getUtility(ISIRPUtils).getStudyModesDict() |
---|
535 | return studymodes_dict[self.context.certificate.study_mode] |
---|
536 | return |
---|
537 | |
---|
538 | @property |
---|
539 | def department(self): |
---|
540 | if self.context.certificate is not None: |
---|
541 | return self.context.certificate.__parent__.__parent__ |
---|
542 | return |
---|
543 | |
---|
544 | @property |
---|
545 | def faculty(self): |
---|
546 | if self.context.certificate is not None: |
---|
547 | return self.context.certificate.__parent__.__parent__.__parent__ |
---|
548 | return |
---|
549 | |
---|
550 | class StudyCourseManageFormPage(SIRPEditFormPage): |
---|
551 | """ Page to edit the student study course data |
---|
552 | """ |
---|
553 | grok.context(IStudentStudyCourse) |
---|
554 | grok.name('manage') |
---|
555 | grok.require('waeup.manageStudent') |
---|
556 | grok.template('studycoursemanagepage') |
---|
557 | form_fields = grok.AutoFields(IStudentStudyCourse) |
---|
558 | label = 'Manage study course' |
---|
559 | pnav = 4 |
---|
560 | taboneactions = ['Save','Cancel'] |
---|
561 | tabtwoactions = ['Remove selected levels','Cancel'] |
---|
562 | tabthreeactions = ['Add study level'] |
---|
563 | |
---|
564 | def update(self): |
---|
565 | super(StudyCourseManageFormPage, self).update() |
---|
566 | tabs.need() |
---|
567 | self.tab1 = self.tab2 = '' |
---|
568 | qs = self.request.get('QUERY_STRING', '') |
---|
569 | if not qs: |
---|
570 | qs = 'tab1' |
---|
571 | setattr(self, qs, 'active') |
---|
572 | warning.need() |
---|
573 | datatable.need() |
---|
574 | return |
---|
575 | |
---|
576 | @action('Save', style='primary') |
---|
577 | def save(self, **data): |
---|
578 | msave(self, **data) |
---|
579 | return |
---|
580 | |
---|
581 | @property |
---|
582 | def level_dict(self): |
---|
583 | studylevelsource = StudyLevelSource().factory |
---|
584 | for code in studylevelsource.getValues(self.context): |
---|
585 | title = studylevelsource.getTitle(self.context, code) |
---|
586 | yield(dict(code=code, title=title)) |
---|
587 | |
---|
588 | @action('Add study level') |
---|
589 | def addStudyLevel(self, **data): |
---|
590 | level_code = self.request.form.get('addlevel', None) |
---|
591 | studylevel = StudentStudyLevel() |
---|
592 | studylevel.level = int(level_code) |
---|
593 | try: |
---|
594 | self.context.addStudentStudyLevel( |
---|
595 | self.context.certificate,studylevel) |
---|
596 | self.flash('Study level has been added.') |
---|
597 | except KeyError: |
---|
598 | self.flash('This level exists.') |
---|
599 | self.redirect(self.url(self.context, u'@@manage')+'?tab2') |
---|
600 | return |
---|
601 | |
---|
602 | @jsaction('Remove selected levels') |
---|
603 | def delStudyLevels(self, **data): |
---|
604 | form = self.request.form |
---|
605 | if form.has_key('val_id'): |
---|
606 | child_id = form['val_id'] |
---|
607 | else: |
---|
608 | self.flash('No study level selected.') |
---|
609 | self.redirect(self.url(self.context, '@@manage')+'?tab2') |
---|
610 | return |
---|
611 | if not isinstance(child_id, list): |
---|
612 | child_id = [child_id] |
---|
613 | deleted = [] |
---|
614 | for id in child_id: |
---|
615 | try: |
---|
616 | del self.context[id] |
---|
617 | deleted.append(id) |
---|
618 | except: |
---|
619 | self.flash('Could not delete %s: %s: %s' % ( |
---|
620 | id, sys.exc_info()[0], sys.exc_info()[1])) |
---|
621 | if len(deleted): |
---|
622 | self.flash('Successfully removed: %s' % ', '.join(deleted)) |
---|
623 | self.redirect(self.url(self.context, u'@@manage')+'?tab2') |
---|
624 | return |
---|
625 | |
---|
626 | class StudyLevelDisplayFormPage(SIRPDisplayFormPage): |
---|
627 | """ Page to display student study levels |
---|
628 | """ |
---|
629 | grok.context(IStudentStudyLevel) |
---|
630 | grok.name('index') |
---|
631 | grok.require('waeup.viewStudent') |
---|
632 | form_fields = grok.AutoFields(IStudentStudyLevel) |
---|
633 | grok.template('studylevelpage') |
---|
634 | pnav = 4 |
---|
635 | |
---|
636 | def update(self): |
---|
637 | super(StudyLevelDisplayFormPage, self).update() |
---|
638 | datatable.need() |
---|
639 | return |
---|
640 | |
---|
641 | @property |
---|
642 | def label(self): |
---|
643 | return '%s: Study Level %s' % ( |
---|
644 | self.context.getStudent().display_fullname,self.context.level_title) |
---|
645 | |
---|
646 | @property |
---|
647 | def total_credits(self): |
---|
648 | total_credits = 0 |
---|
649 | for key, val in self.context.items(): |
---|
650 | total_credits += val.credits |
---|
651 | return total_credits |
---|
652 | |
---|
653 | class ExportPDFCourseRegistrationSlipPage(UtilityView, grok.View): |
---|
654 | """Deliver a PDF slip of the context. |
---|
655 | """ |
---|
656 | grok.context(IStudentStudyLevel) |
---|
657 | grok.name('course_registration.pdf') |
---|
658 | grok.require('waeup.viewStudent') |
---|
659 | form_fields = grok.AutoFields(IStudentStudyLevel) |
---|
660 | prefix = 'form' |
---|
661 | title = 'Level Data' |
---|
662 | content_title = 'Course List' |
---|
663 | |
---|
664 | @property |
---|
665 | def label(self): |
---|
666 | return 'Course Registration Slip %s' % self.context.level_title |
---|
667 | |
---|
668 | def render(self): |
---|
669 | studentview = StudentBaseDisplayFormPage(self.context.getStudent(), |
---|
670 | self.request) |
---|
671 | students_utils = getUtility(IStudentsUtils) |
---|
672 | tabledata = sorted(self.context.values(), |
---|
673 | key=lambda value: str(value.semester) + value.code) |
---|
674 | return students_utils.renderPDF( |
---|
675 | self, 'course_registration.pdf', |
---|
676 | self.context.getStudent(), studentview, |
---|
677 | tableheader=[('Sem.','semester', 1.5),('Code','code', 2.5), |
---|
678 | ('Title','title', 5), |
---|
679 | ('Dept.','dcode', 1.5), ('Faculty','fcode', 1.5), |
---|
680 | ('Cred.', 'credits', 1.5), |
---|
681 | ('Mand.', 'mandatory', 1.5), |
---|
682 | ('Score', 'score', 1.5),('Auto', 'automatic', 1.5) |
---|
683 | ], |
---|
684 | tabledata=tabledata) |
---|
685 | |
---|
686 | class StudyLevelManageFormPage(SIRPEditFormPage): |
---|
687 | """ Page to edit the student study level data |
---|
688 | """ |
---|
689 | grok.context(IStudentStudyLevel) |
---|
690 | grok.name('manage') |
---|
691 | grok.require('waeup.manageStudent') |
---|
692 | grok.template('studylevelmanagepage') |
---|
693 | form_fields = grok.AutoFields(IStudentStudyLevel) |
---|
694 | pnav = 4 |
---|
695 | taboneactions = ['Save','Cancel'] |
---|
696 | tabtwoactions = ['Add course ticket','Remove selected tickets','Cancel'] |
---|
697 | |
---|
698 | def update(self): |
---|
699 | super(StudyLevelManageFormPage, self).update() |
---|
700 | tabs.need() |
---|
701 | self.tab1 = self.tab2 = '' |
---|
702 | qs = self.request.get('QUERY_STRING', '') |
---|
703 | if not qs: |
---|
704 | qs = 'tab1' |
---|
705 | setattr(self, qs, 'active') |
---|
706 | warning.need() |
---|
707 | datatable.need() |
---|
708 | return |
---|
709 | |
---|
710 | @property |
---|
711 | def label(self): |
---|
712 | return 'Manage study level %s' % self.context.level_title |
---|
713 | |
---|
714 | @action('Save', style='primary') |
---|
715 | def save(self, **data): |
---|
716 | msave(self, **data) |
---|
717 | return |
---|
718 | |
---|
719 | @action('Add course ticket') |
---|
720 | def addCourseTicket(self, **data): |
---|
721 | self.redirect(self.url(self.context, '@@add')) |
---|
722 | |
---|
723 | @jsaction('Remove selected tickets') |
---|
724 | def delCourseTicket(self, **data): |
---|
725 | form = self.request.form |
---|
726 | if form.has_key('val_id'): |
---|
727 | child_id = form['val_id'] |
---|
728 | else: |
---|
729 | self.flash('No ticket selected.') |
---|
730 | self.redirect(self.url(self.context, '@@manage')+'?tab2') |
---|
731 | return |
---|
732 | if not isinstance(child_id, list): |
---|
733 | child_id = [child_id] |
---|
734 | deleted = [] |
---|
735 | for id in child_id: |
---|
736 | try: |
---|
737 | del self.context[id] |
---|
738 | deleted.append(id) |
---|
739 | except: |
---|
740 | self.flash('Could not delete %s: %s: %s' % ( |
---|
741 | id, sys.exc_info()[0], sys.exc_info()[1])) |
---|
742 | if len(deleted): |
---|
743 | self.flash('Successfully removed: %s' % ', '.join(deleted)) |
---|
744 | self.redirect(self.url(self.context, u'@@manage')+'?tab2') |
---|
745 | return |
---|
746 | |
---|
747 | class ValidateCoursesPage(UtilityView, grok.View): |
---|
748 | """ Validate course list by course adviser |
---|
749 | """ |
---|
750 | grok.context(IStudentStudyLevel) |
---|
751 | grok.name('validate_courses') |
---|
752 | grok.require('waeup.validateStudent') |
---|
753 | |
---|
754 | def update(self): |
---|
755 | if str(self.context.__parent__.current_level) != self.context.__name__: |
---|
756 | self.flash('This level does not correspond current level.') |
---|
757 | elif self.context.getStudent().state == REGISTERED: |
---|
758 | IWorkflowInfo(self.context.getStudent()).fireTransition( |
---|
759 | 'validate_courses') |
---|
760 | self.flash('Course list has been validated.') |
---|
761 | else: |
---|
762 | self.flash('Student is in the wrong state.') |
---|
763 | self.redirect(self.url(self.context)) |
---|
764 | return |
---|
765 | |
---|
766 | def render(self): |
---|
767 | return |
---|
768 | |
---|
769 | class RejectCoursesPage(UtilityView, grok.View): |
---|
770 | """ Reject course list by course adviser |
---|
771 | """ |
---|
772 | grok.context(IStudentStudyLevel) |
---|
773 | grok.name('reject_courses') |
---|
774 | grok.require('waeup.validateStudent') |
---|
775 | |
---|
776 | def update(self): |
---|
777 | if str(self.context.__parent__.current_level) != self.context.__name__: |
---|
778 | self.flash('This level does not correspond current level.') |
---|
779 | self.redirect(self.url(self.context)) |
---|
780 | return |
---|
781 | elif self.context.getStudent().state == VALIDATED: |
---|
782 | IWorkflowInfo(self.context.getStudent()).fireTransition('reset8') |
---|
783 | message = 'Course list request has been annulled' |
---|
784 | self.flash(message) |
---|
785 | elif self.context.getStudent().state == REGISTERED: |
---|
786 | IWorkflowInfo(self.context.getStudent()).fireTransition('reset7') |
---|
787 | message = 'Course list request has been rejected' |
---|
788 | self.flash(message) |
---|
789 | else: |
---|
790 | self.flash('Student is in the wrong state.') |
---|
791 | self.redirect(self.url(self.context)) |
---|
792 | return |
---|
793 | args = {'subject':message} |
---|
794 | self.redirect(self.url(self.context.getStudent()) + |
---|
795 | '/contactstudent?%s' % urlencode(args)) |
---|
796 | return |
---|
797 | |
---|
798 | def render(self): |
---|
799 | return |
---|
800 | |
---|
801 | class CourseTicketAddFormPage(SIRPAddFormPage): |
---|
802 | """Add a course ticket. |
---|
803 | """ |
---|
804 | grok.context(IStudentStudyLevel) |
---|
805 | grok.name('add') |
---|
806 | grok.require('waeup.manageStudent') |
---|
807 | label = 'Add course ticket' |
---|
808 | form_fields = grok.AutoFields(ICourseTicketAdd).omit( |
---|
809 | 'grade', 'score', 'automatic', 'carry_over') |
---|
810 | pnav = 4 |
---|
811 | |
---|
812 | @action('Add course ticket') |
---|
813 | def addCourseTicket(self, **data): |
---|
814 | ticket = CourseTicket() |
---|
815 | course = data['course'] |
---|
816 | ticket.automatic = False |
---|
817 | ticket.code = course.code |
---|
818 | ticket.title = course.title |
---|
819 | ticket.fcode = course.__parent__.__parent__.__parent__.code |
---|
820 | ticket.dcode = course.__parent__.__parent__.code |
---|
821 | ticket.credits = course.credits |
---|
822 | ticket.passmark = course.passmark |
---|
823 | ticket.semester = course.semester |
---|
824 | try: |
---|
825 | self.context.addCourseTicket(ticket) |
---|
826 | except KeyError: |
---|
827 | self.flash('The ticket exists.') |
---|
828 | return |
---|
829 | self.flash('Successfully added %s.' % ticket.code) |
---|
830 | self.redirect(self.url(self.context, u'@@manage')+'?tab2') |
---|
831 | return |
---|
832 | |
---|
833 | @action('Cancel') |
---|
834 | def cancel(self, **data): |
---|
835 | self.redirect(self.url(self.context)) |
---|
836 | |
---|
837 | class CourseTicketDisplayFormPage(SIRPDisplayFormPage): |
---|
838 | """ Page to display course tickets |
---|
839 | """ |
---|
840 | grok.context(ICourseTicket) |
---|
841 | grok.name('index') |
---|
842 | grok.require('waeup.viewStudent') |
---|
843 | form_fields = grok.AutoFields(ICourseTicket) |
---|
844 | grok.template('courseticketpage') |
---|
845 | pnav = 4 |
---|
846 | |
---|
847 | @property |
---|
848 | def label(self): |
---|
849 | return '%s: Course Ticket %s' % ( |
---|
850 | self.context.getStudent().display_fullname,self.context.code) |
---|
851 | |
---|
852 | class CourseTicketManageFormPage(SIRPEditFormPage): |
---|
853 | """ Page to manage course tickets |
---|
854 | """ |
---|
855 | grok.context(ICourseTicket) |
---|
856 | grok.name('manage') |
---|
857 | grok.require('waeup.manageStudent') |
---|
858 | form_fields = grok.AutoFields(ICourseTicket) |
---|
859 | grok.template('courseticketmanagepage') |
---|
860 | pnav = 4 |
---|
861 | |
---|
862 | @property |
---|
863 | def label(self): |
---|
864 | return 'Manage course ticket %s' % self.context.code |
---|
865 | |
---|
866 | @action('Save', style='primary') |
---|
867 | def save(self, **data): |
---|
868 | msave(self, **data) |
---|
869 | return |
---|
870 | |
---|
871 | class PaymentsManageFormPage(SIRPEditFormPage): |
---|
872 | """ Page to manage the student payments |
---|
873 | |
---|
874 | This manage form page is for both students and students officers. |
---|
875 | """ |
---|
876 | grok.context(IStudentPaymentsContainer) |
---|
877 | grok.name('index') |
---|
878 | grok.require('waeup.payStudent') |
---|
879 | form_fields = grok.AutoFields(IStudentPaymentsContainer) |
---|
880 | grok.template('paymentsmanagepage') |
---|
881 | pnav = 4 |
---|
882 | |
---|
883 | def unremovable(self, ticket): |
---|
884 | usertype = getattr(self.request.principal, 'user_type', None) |
---|
885 | if not usertype: |
---|
886 | return False |
---|
887 | return (self.request.principal.user_type == 'student' and ticket.r_code) |
---|
888 | |
---|
889 | @property |
---|
890 | def label(self): |
---|
891 | return '%s: Payments' % self.context.__parent__.display_fullname |
---|
892 | |
---|
893 | def update(self): |
---|
894 | super(PaymentsManageFormPage, self).update() |
---|
895 | datatable.need() |
---|
896 | warning.need() |
---|
897 | return |
---|
898 | |
---|
899 | @jsaction('Remove selected tickets') |
---|
900 | def delPaymentTicket(self, **data): |
---|
901 | form = self.request.form |
---|
902 | if form.has_key('val_id'): |
---|
903 | child_id = form['val_id'] |
---|
904 | else: |
---|
905 | self.flash('No payment selected.') |
---|
906 | self.redirect(self.url(self.context)) |
---|
907 | return |
---|
908 | if not isinstance(child_id, list): |
---|
909 | child_id = [child_id] |
---|
910 | deleted = [] |
---|
911 | for id in child_id: |
---|
912 | # Students are not allowed to remove used payment tickets |
---|
913 | if not self.unremovable(self.context[id]): |
---|
914 | try: |
---|
915 | del self.context[id] |
---|
916 | deleted.append(id) |
---|
917 | except: |
---|
918 | self.flash('Could not delete %s: %s: %s' % ( |
---|
919 | id, sys.exc_info()[0], sys.exc_info()[1])) |
---|
920 | if len(deleted): |
---|
921 | self.flash('Successfully removed: %s' % ', '.join(deleted)) |
---|
922 | write_log_message(self,'removed: % s' % ', '.join(deleted)) |
---|
923 | self.redirect(self.url(self.context)) |
---|
924 | return |
---|
925 | |
---|
926 | @action('Add online payment ticket') |
---|
927 | def addPaymentTicket(self, **data): |
---|
928 | self.redirect(self.url(self.context, '@@addop')) |
---|
929 | |
---|
930 | class OnlinePaymentAddFormPage(SIRPAddFormPage): |
---|
931 | """ Page to add an online payment ticket |
---|
932 | """ |
---|
933 | grok.context(IStudentPaymentsContainer) |
---|
934 | grok.name('addop') |
---|
935 | grok.require('waeup.payStudent') |
---|
936 | form_fields = grok.AutoFields(IStudentOnlinePayment).select( |
---|
937 | 'p_category') |
---|
938 | label = 'Add online payment' |
---|
939 | pnav = 4 |
---|
940 | |
---|
941 | @action('Create ticket', style='primary') |
---|
942 | def createTicket(self, **data): |
---|
943 | p_category = data['p_category'] |
---|
944 | student = self.context.__parent__ |
---|
945 | if p_category == 'bed_allocation' and student[ |
---|
946 | 'studycourse'].current_session != grok.getSite()[ |
---|
947 | 'configuration'].accommodation_session: |
---|
948 | self.flash( |
---|
949 | 'Your current session does not match ' |
---|
950 | 'accommodation session.') |
---|
951 | self.redirect(self.url(self.context)) |
---|
952 | return |
---|
953 | students_utils = getUtility(IStudentsUtils) |
---|
954 | pay_details = students_utils.getPaymentDetails( |
---|
955 | p_category,student) |
---|
956 | if pay_details['error']: |
---|
957 | self.flash(pay_details['error']) |
---|
958 | self.redirect(self.url(self.context)) |
---|
959 | return |
---|
960 | p_item = pay_details['p_item'] |
---|
961 | p_session = pay_details['p_session'] |
---|
962 | for key in self.context.keys(): |
---|
963 | ticket = self.context[key] |
---|
964 | if ticket.p_state == 'paid' and\ |
---|
965 | ticket.p_category == p_category and \ |
---|
966 | ticket.p_item == p_item and \ |
---|
967 | ticket.p_session == p_session: |
---|
968 | self.flash( |
---|
969 | 'This type of payment has already been made.') |
---|
970 | self.redirect(self.url(self.context)) |
---|
971 | return |
---|
972 | payment = createObject(u'waeup.StudentOnlinePayment') |
---|
973 | self.applyData(payment, **data) |
---|
974 | timestamp = "%d" % int(time()*1000) |
---|
975 | payment.p_id = "p%s" % timestamp |
---|
976 | payment.p_item = p_item |
---|
977 | payment.p_session = p_session |
---|
978 | payment.amount_auth = pay_details['amount'] |
---|
979 | payment.surcharge_1 = pay_details['surcharge_1'] |
---|
980 | payment.surcharge_2 = pay_details['surcharge_2'] |
---|
981 | payment.surcharge_3 = pay_details['surcharge_3'] |
---|
982 | self.context[payment.p_id] = payment |
---|
983 | self.flash('Payment ticket created.') |
---|
984 | self.redirect(self.url(self.context)) |
---|
985 | return |
---|
986 | |
---|
987 | class OnlinePaymentDisplayFormPage(SIRPDisplayFormPage): |
---|
988 | """ Page to view an online payment ticket |
---|
989 | """ |
---|
990 | grok.context(IStudentOnlinePayment) |
---|
991 | grok.name('index') |
---|
992 | grok.require('waeup.viewStudent') |
---|
993 | form_fields = grok.AutoFields(IStudentOnlinePayment) |
---|
994 | form_fields[ |
---|
995 | 'creation_date'].custom_widget = FriendlyDatetimeDisplayWidget('le') |
---|
996 | form_fields[ |
---|
997 | 'payment_date'].custom_widget = FriendlyDatetimeDisplayWidget('le') |
---|
998 | pnav = 4 |
---|
999 | |
---|
1000 | @property |
---|
1001 | def label(self): |
---|
1002 | return '%s: Online Payment Ticket %s' % ( |
---|
1003 | self.context.getStudent().display_fullname,self.context.p_id) |
---|
1004 | |
---|
1005 | class OnlinePaymentCallbackPage(UtilityView, grok.View): |
---|
1006 | """ Callback view |
---|
1007 | """ |
---|
1008 | grok.context(IStudentOnlinePayment) |
---|
1009 | grok.name('callback') |
---|
1010 | grok.require('waeup.payStudent') |
---|
1011 | |
---|
1012 | # This update method simulates a valid callback und must be |
---|
1013 | # specified in the customization package. The parameters must be taken |
---|
1014 | # from the incoming request. |
---|
1015 | def update(self): |
---|
1016 | if self.context.p_state == 'paid': |
---|
1017 | self.flash('This ticket has already been paid.') |
---|
1018 | return |
---|
1019 | student = self.context.getStudent() |
---|
1020 | write_log_message(self,'valid callback: %s' % self.context.p_id) |
---|
1021 | self.context.r_amount_approved = self.context.amount_auth |
---|
1022 | self.context.r_card_num = u'0000' |
---|
1023 | self.context.r_code = u'00' |
---|
1024 | self.context.p_state = 'paid' |
---|
1025 | self.context.payment_date = datetime.now() |
---|
1026 | if self.context.p_category == 'clearance': |
---|
1027 | # Create CLR access code |
---|
1028 | pin, error = create_accesscode('CLR',0,student.student_id) |
---|
1029 | if error: |
---|
1030 | self.flash('Valid callback received. ' + error) |
---|
1031 | return |
---|
1032 | self.context.ac = pin |
---|
1033 | elif self.context.p_category == 'schoolfee': |
---|
1034 | # Create SFE access code |
---|
1035 | pin, error = create_accesscode('SFE',0,student.student_id) |
---|
1036 | if error: |
---|
1037 | self.flash('Valid callback received. ' + error) |
---|
1038 | return |
---|
1039 | self.context.ac = pin |
---|
1040 | elif self.context.p_category == 'bed_allocation': |
---|
1041 | # Create HOS access code |
---|
1042 | pin, error = create_accesscode('HOS',0,student.student_id) |
---|
1043 | if error: |
---|
1044 | self.flash('Valid callback received. ' + error) |
---|
1045 | return |
---|
1046 | self.context.ac = pin |
---|
1047 | self.flash('Valid callback received.') |
---|
1048 | return |
---|
1049 | |
---|
1050 | def render(self): |
---|
1051 | self.redirect(self.url(self.context, '@@index')) |
---|
1052 | return |
---|
1053 | |
---|
1054 | class ExportPDFPaymentSlipPage(UtilityView, grok.View): |
---|
1055 | """Deliver a PDF slip of the context. |
---|
1056 | """ |
---|
1057 | grok.context(IStudentOnlinePayment) |
---|
1058 | grok.name('payment_receipt.pdf') |
---|
1059 | grok.require('waeup.viewStudent') |
---|
1060 | form_fields = grok.AutoFields(IStudentOnlinePayment) |
---|
1061 | form_fields['creation_date'].custom_widget = FriendlyDateDisplayWidget('le') |
---|
1062 | form_fields['payment_date'].custom_widget = FriendlyDateDisplayWidget('le') |
---|
1063 | prefix = 'form' |
---|
1064 | title = 'Payment Data' |
---|
1065 | |
---|
1066 | @property |
---|
1067 | def label(self): |
---|
1068 | return 'Online Payment Receipt %s' % self.context.p_id |
---|
1069 | |
---|
1070 | def render(self): |
---|
1071 | if self.context.p_state != 'paid': |
---|
1072 | self.flash('Ticket not yet paid.') |
---|
1073 | self.redirect(self.url(self.context)) |
---|
1074 | return |
---|
1075 | studentview = StudentBaseDisplayFormPage(self.context.getStudent(), |
---|
1076 | self.request) |
---|
1077 | students_utils = getUtility(IStudentsUtils) |
---|
1078 | return students_utils.renderPDF(self, 'payment_receipt.pdf', |
---|
1079 | self.context.getStudent(), studentview) |
---|
1080 | |
---|
1081 | |
---|
1082 | class AccommodationManageFormPage(SIRPEditFormPage): |
---|
1083 | """ Page to manage bed tickets. |
---|
1084 | |
---|
1085 | This manage form page is for both students and students officers. |
---|
1086 | """ |
---|
1087 | grok.context(IStudentAccommodation) |
---|
1088 | grok.name('index') |
---|
1089 | grok.require('waeup.handleAccommodation') |
---|
1090 | form_fields = grok.AutoFields(IStudentAccommodation) |
---|
1091 | grok.template('accommodationmanagepage') |
---|
1092 | pnav = 4 |
---|
1093 | officers_only_actions = ['Remove selected'] |
---|
1094 | |
---|
1095 | @property |
---|
1096 | def label(self): |
---|
1097 | return '%s: Accommodation' % self.context.__parent__.display_fullname |
---|
1098 | |
---|
1099 | def update(self): |
---|
1100 | super(AccommodationManageFormPage, self).update() |
---|
1101 | datatable.need() |
---|
1102 | warning.need() |
---|
1103 | return |
---|
1104 | |
---|
1105 | @jsaction('Remove selected') |
---|
1106 | def delBedTickets(self, **data): |
---|
1107 | if getattr(self.request.principal, 'user_type', None) == 'student': |
---|
1108 | self.flash('You are not allowed to remove bed tickets.') |
---|
1109 | self.redirect(self.url(self.context)) |
---|
1110 | return |
---|
1111 | form = self.request.form |
---|
1112 | if form.has_key('val_id'): |
---|
1113 | child_id = form['val_id'] |
---|
1114 | else: |
---|
1115 | self.flash('No bed ticket selected.') |
---|
1116 | self.redirect(self.url(self.context)) |
---|
1117 | return |
---|
1118 | if not isinstance(child_id, list): |
---|
1119 | child_id = [child_id] |
---|
1120 | deleted = [] |
---|
1121 | for id in child_id: |
---|
1122 | del self.context[id] |
---|
1123 | deleted.append(id) |
---|
1124 | if len(deleted): |
---|
1125 | self.flash('Successfully removed: %s' % ', '.join(deleted)) |
---|
1126 | write_log_message(self,'removed: % s' % ', '.join(deleted)) |
---|
1127 | self.redirect(self.url(self.context)) |
---|
1128 | return |
---|
1129 | |
---|
1130 | @property |
---|
1131 | def selected_actions(self): |
---|
1132 | if getattr(self.request.principal, 'user_type', None) == 'student': |
---|
1133 | return [action for action in self.actions |
---|
1134 | if not action.label in self.officers_only_actions] |
---|
1135 | return self.actions |
---|
1136 | |
---|
1137 | class BedTicketAddPage(SIRPPage): |
---|
1138 | """ Page to add an online payment ticket |
---|
1139 | """ |
---|
1140 | grok.context(IStudentAccommodation) |
---|
1141 | grok.name('add') |
---|
1142 | grok.require('waeup.handleAccommodation') |
---|
1143 | grok.template('enterpin') |
---|
1144 | ac_prefix = 'HOS' |
---|
1145 | label = 'Add bed ticket' |
---|
1146 | pnav = 4 |
---|
1147 | buttonname = 'Create bed ticket' |
---|
1148 | notice = '' |
---|
1149 | |
---|
1150 | def update(self, SUBMIT=None): |
---|
1151 | student = self.context.getStudent() |
---|
1152 | students_utils = getUtility(IStudentsUtils) |
---|
1153 | acc_details = students_utils.getAccommodationDetails(student) |
---|
1154 | if not acc_details: |
---|
1155 | self.flash("Your data are incomplete.") |
---|
1156 | self.redirect(self.url(self.context)) |
---|
1157 | return |
---|
1158 | if not student.state in acc_details['allowed_states']: |
---|
1159 | self.flash("You are in the wrong registration state.") |
---|
1160 | self.redirect(self.url(self.context)) |
---|
1161 | return |
---|
1162 | if student['studycourse'].current_session != acc_details[ |
---|
1163 | 'booking_session']: |
---|
1164 | self.flash( |
---|
1165 | 'Your current session does not match accommodation session.') |
---|
1166 | self.redirect(self.url(self.context)) |
---|
1167 | return |
---|
1168 | if str(acc_details['booking_session']) in self.context.keys(): |
---|
1169 | self.flash( |
---|
1170 | 'You already booked a bed space in current ' |
---|
1171 | 'accommodation session.') |
---|
1172 | self.redirect(self.url(self.context)) |
---|
1173 | return |
---|
1174 | self.ac_series = self.request.form.get('ac_series', None) |
---|
1175 | self.ac_number = self.request.form.get('ac_number', None) |
---|
1176 | if SUBMIT is None: |
---|
1177 | return |
---|
1178 | pin = '%s-%s-%s' % (self.ac_prefix, self.ac_series, self.ac_number) |
---|
1179 | code = get_access_code(pin) |
---|
1180 | if not code: |
---|
1181 | self.flash('Activation code is invalid.') |
---|
1182 | return |
---|
1183 | # Search and book bed |
---|
1184 | cat = queryUtility(ICatalog, name='beds_catalog', default=None) |
---|
1185 | entries = cat.searchResults( |
---|
1186 | owner=(student.student_id,student.student_id)) |
---|
1187 | if len(entries): |
---|
1188 | # If bed space has bee manually allocated use this bed |
---|
1189 | bed = [entry for entry in entries][0] |
---|
1190 | else: |
---|
1191 | # else search for other available beds |
---|
1192 | entries = cat.searchResults( |
---|
1193 | bed_type=(acc_details['bt'],acc_details['bt'])) |
---|
1194 | available_beds = [ |
---|
1195 | entry for entry in entries if entry.owner == NOT_OCCUPIED] |
---|
1196 | if available_beds: |
---|
1197 | students_utils = getUtility(IStudentsUtils) |
---|
1198 | bed = students_utils.selectBed(available_beds) |
---|
1199 | bed.bookBed(student.student_id) |
---|
1200 | else: |
---|
1201 | self.flash('There is no free bed in your category %s.' |
---|
1202 | % acc_details['bt']) |
---|
1203 | return |
---|
1204 | # Mark pin as used (this also fires a pin related transition) |
---|
1205 | if code.state == USED: |
---|
1206 | self.flash('Activation code has already been used.') |
---|
1207 | return |
---|
1208 | else: |
---|
1209 | comment = u"AC invalidated for %s" % ( |
---|
1210 | self.context.getStudent().student_id,) |
---|
1211 | # Here we know that the ac is in state initialized so we do not |
---|
1212 | # expect an exception, but the owner might be different |
---|
1213 | if not invalidate_accesscode( |
---|
1214 | pin,comment,self.context.getStudent().student_id): |
---|
1215 | self.flash('You are not the owner of this access code.') |
---|
1216 | return |
---|
1217 | # Create bed ticket |
---|
1218 | bedticket = createObject(u'waeup.BedTicket') |
---|
1219 | bedticket.booking_code = pin |
---|
1220 | bedticket.booking_session = acc_details['booking_session'] |
---|
1221 | bedticket.bed_type = acc_details['bt'] |
---|
1222 | bedticket.bed = bed |
---|
1223 | hall_title = bed.__parent__.hostel_name |
---|
1224 | coordinates = bed.getBedCoordinates()[1:] |
---|
1225 | block, room_nr, bed_nr = coordinates |
---|
1226 | bedticket.bed_coordinates = '%s, Block %s, Room %s, Bed %s (%s)' % ( |
---|
1227 | hall_title, block, room_nr, bed_nr, bed.bed_type) |
---|
1228 | key = str(acc_details['booking_session']) |
---|
1229 | self.context[key] = bedticket |
---|
1230 | self.flash('Bed ticket created and bed booked: %s' |
---|
1231 | % bedticket.bed_coordinates) |
---|
1232 | self.redirect(self.url(self.context)) |
---|
1233 | return |
---|
1234 | |
---|
1235 | class BedTicketDisplayFormPage(SIRPDisplayFormPage): |
---|
1236 | """ Page to display bed tickets |
---|
1237 | """ |
---|
1238 | grok.context(IBedTicket) |
---|
1239 | grok.name('index') |
---|
1240 | grok.require('waeup.handleAccommodation') |
---|
1241 | form_fields = grok.AutoFields(IBedTicket) |
---|
1242 | form_fields[ |
---|
1243 | 'booking_date'].custom_widget = FriendlyDatetimeDisplayWidget('le') |
---|
1244 | pnav = 4 |
---|
1245 | |
---|
1246 | @property |
---|
1247 | def label(self): |
---|
1248 | return 'Bed Ticket for Session %s' % self.context.getSessionString() |
---|
1249 | |
---|
1250 | class ExportPDFBedTicketSlipPage(UtilityView, grok.View): |
---|
1251 | """Deliver a PDF slip of the context. |
---|
1252 | """ |
---|
1253 | grok.context(IBedTicket) |
---|
1254 | grok.name('bed_allocation.pdf') |
---|
1255 | grok.require('waeup.handleAccommodation') |
---|
1256 | form_fields = grok.AutoFields(IBedTicket) |
---|
1257 | form_fields['booking_date'].custom_widget = FriendlyDateDisplayWidget('le') |
---|
1258 | prefix = 'form' |
---|
1259 | title = 'Bed Allocation Data' |
---|
1260 | |
---|
1261 | @property |
---|
1262 | def label(self): |
---|
1263 | return 'Bed Allocation: %s' % self.context.bed_coordinates |
---|
1264 | |
---|
1265 | def render(self): |
---|
1266 | studentview = StudentBaseDisplayFormPage(self.context.getStudent(), |
---|
1267 | self.request) |
---|
1268 | students_utils = getUtility(IStudentsUtils) |
---|
1269 | return students_utils.renderPDF( |
---|
1270 | self, 'bed_allocation.pdf', |
---|
1271 | self.context.getStudent(), studentview) |
---|
1272 | |
---|
1273 | class BedTicketRelocationPage(UtilityView, grok.View): |
---|
1274 | """ Callback view |
---|
1275 | """ |
---|
1276 | grok.context(IBedTicket) |
---|
1277 | grok.name('relocate') |
---|
1278 | grok.require('waeup.manageHostels') |
---|
1279 | |
---|
1280 | # Relocate student if student parameters have changed or the bed_type |
---|
1281 | # of the bed has changed |
---|
1282 | def update(self): |
---|
1283 | student = self.context.getStudent() |
---|
1284 | students_utils = getUtility(IStudentsUtils) |
---|
1285 | acc_details = students_utils.getAccommodationDetails(student) |
---|
1286 | if self.context.bed != None and \ |
---|
1287 | 'reserved' in self.context.bed.bed_type: |
---|
1288 | self.flash("Students in reserved beds can't be relocated.") |
---|
1289 | self.redirect(self.url(self.context)) |
---|
1290 | return |
---|
1291 | if acc_details['bt'] == self.context.bed_type and \ |
---|
1292 | self.context.bed != None and \ |
---|
1293 | self.context.bed.bed_type == self.context.bed_type: |
---|
1294 | self.flash("Student can't be relocated.") |
---|
1295 | self.redirect(self.url(self.context)) |
---|
1296 | return |
---|
1297 | # Search a bed |
---|
1298 | cat = queryUtility(ICatalog, name='beds_catalog', default=None) |
---|
1299 | entries = cat.searchResults( |
---|
1300 | owner=(student.student_id,student.student_id)) |
---|
1301 | if len(entries) and self.context.bed == None: |
---|
1302 | # If booking has been cancelled but other bed space has been |
---|
1303 | # manually allocated after cancellation use this bed |
---|
1304 | new_bed = [entry for entry in entries][0] |
---|
1305 | else: |
---|
1306 | # Search for other available beds |
---|
1307 | entries = cat.searchResults( |
---|
1308 | bed_type=(acc_details['bt'],acc_details['bt'])) |
---|
1309 | available_beds = [ |
---|
1310 | entry for entry in entries if entry.owner == NOT_OCCUPIED] |
---|
1311 | if available_beds: |
---|
1312 | students_utils = getUtility(IStudentsUtils) |
---|
1313 | new_bed = students_utils.selectBed(available_beds) |
---|
1314 | new_bed.bookBed(student.student_id) |
---|
1315 | else: |
---|
1316 | self.flash('There is no free bed in your category %s.' |
---|
1317 | % acc_details['bt']) |
---|
1318 | self.redirect(self.url(self.context)) |
---|
1319 | return |
---|
1320 | # Release old bed if exists |
---|
1321 | if self.context.bed != None: |
---|
1322 | self.context.bed.owner = NOT_OCCUPIED |
---|
1323 | notify(grok.ObjectModifiedEvent(self.context.bed)) |
---|
1324 | # Alocate new bed |
---|
1325 | self.context.bed_type = acc_details['bt'] |
---|
1326 | self.context.bed = new_bed |
---|
1327 | hall_title = new_bed.__parent__.hostel_name |
---|
1328 | coordinates = new_bed.getBedCoordinates()[1:] |
---|
1329 | block, room_nr, bed_nr = coordinates |
---|
1330 | self.context.bed_coordinates = '%s, Block %s, Room %s, Bed %s (%s)' % ( |
---|
1331 | hall_title, block, room_nr, bed_nr, new_bed.bed_type) |
---|
1332 | self.flash('Student relocated: %s' % self.context.bed_coordinates) |
---|
1333 | self.redirect(self.url(self.context)) |
---|
1334 | return |
---|
1335 | |
---|
1336 | def render(self): |
---|
1337 | return |
---|
1338 | |
---|
1339 | class StudentHistoryPage(SIRPPage): |
---|
1340 | """ Page to display student clearance data |
---|
1341 | """ |
---|
1342 | grok.context(IStudent) |
---|
1343 | grok.name('history') |
---|
1344 | grok.require('waeup.viewStudent') |
---|
1345 | grok.template('studenthistory') |
---|
1346 | pnav = 4 |
---|
1347 | |
---|
1348 | @property |
---|
1349 | def label(self): |
---|
1350 | return '%s: History' % self.context.display_fullname |
---|
1351 | |
---|
1352 | # Pages for students only |
---|
1353 | |
---|
1354 | class StudentBaseEditFormPage(SIRPEditFormPage): |
---|
1355 | """ View to edit student base data |
---|
1356 | """ |
---|
1357 | grok.context(IStudent) |
---|
1358 | grok.name('edit_base') |
---|
1359 | grok.require('waeup.handleStudent') |
---|
1360 | form_fields = grok.AutoFields(IStudentBase).select( |
---|
1361 | 'email', 'phone') |
---|
1362 | label = 'Edit base data' |
---|
1363 | pnav = 4 |
---|
1364 | |
---|
1365 | @action('Save', style='primary') |
---|
1366 | def save(self, **data): |
---|
1367 | msave(self, **data) |
---|
1368 | return |
---|
1369 | |
---|
1370 | class StudentChangePasswordPage(SIRPEditFormPage): |
---|
1371 | """ View to manage student base data |
---|
1372 | """ |
---|
1373 | grok.context(IStudent) |
---|
1374 | grok.name('change_password') |
---|
1375 | grok.require('waeup.handleStudent') |
---|
1376 | grok.template('change_password') |
---|
1377 | label = 'Change password' |
---|
1378 | pnav = 4 |
---|
1379 | |
---|
1380 | @action('Save', style='primary') |
---|
1381 | def save(self, **data): |
---|
1382 | form = self.request.form |
---|
1383 | password = form.get('change_password', None) |
---|
1384 | password_ctl = form.get('change_password_repeat', None) |
---|
1385 | if password: |
---|
1386 | validator = getUtility(IPasswordValidator) |
---|
1387 | errors = validator.validate_password(password, password_ctl) |
---|
1388 | if not errors: |
---|
1389 | IUserAccount(self.context).setPassword(password) |
---|
1390 | write_log_message(self, 'saved: password') |
---|
1391 | self.flash('Password changed.') |
---|
1392 | else: |
---|
1393 | self.flash( ' '.join(errors)) |
---|
1394 | return |
---|
1395 | |
---|
1396 | class StudentFilesUploadPage(SIRPPage): |
---|
1397 | """ View to upload files by student |
---|
1398 | """ |
---|
1399 | grok.context(IStudent) |
---|
1400 | grok.name('change_portrait') |
---|
1401 | grok.require('waeup.uploadStudentFile') |
---|
1402 | grok.template('filesuploadpage') |
---|
1403 | label = 'Upload portrait' |
---|
1404 | pnav = 4 |
---|
1405 | |
---|
1406 | def update(self): |
---|
1407 | if self.context.getStudent().state != ADMITTED: |
---|
1408 | emit_lock_message(self) |
---|
1409 | return |
---|
1410 | super(StudentFilesUploadPage, self).update() |
---|
1411 | return |
---|
1412 | |
---|
1413 | class StartClearancePage(SIRPPage): |
---|
1414 | grok.context(IStudent) |
---|
1415 | grok.name('start_clearance') |
---|
1416 | grok.require('waeup.handleStudent') |
---|
1417 | grok.template('enterpin') |
---|
1418 | label = 'Start clearance' |
---|
1419 | ac_prefix = 'CLR' |
---|
1420 | notice = '' |
---|
1421 | pnav = 4 |
---|
1422 | buttonname = 'Start clearance now' |
---|
1423 | |
---|
1424 | @property |
---|
1425 | def all_required_fields_filled(self): |
---|
1426 | if self.context.email and self.context.phone: |
---|
1427 | return True |
---|
1428 | return False |
---|
1429 | |
---|
1430 | @property |
---|
1431 | def portrait_uploaded(self): |
---|
1432 | store = getUtility(IExtFileStore) |
---|
1433 | if store.getFileByContext(self.context, attr=u'passport.jpg'): |
---|
1434 | return True |
---|
1435 | return False |
---|
1436 | |
---|
1437 | def update(self, SUBMIT=None): |
---|
1438 | if not self.context.state == ADMITTED: |
---|
1439 | self.flash("Wrong state.") |
---|
1440 | self.redirect(self.url(self.context)) |
---|
1441 | return |
---|
1442 | if not self.portrait_uploaded: |
---|
1443 | self.flash("No portrait uploaded.") |
---|
1444 | self.redirect(self.url(self.context, 'change_portrait')) |
---|
1445 | return |
---|
1446 | if not self.all_required_fields_filled: |
---|
1447 | self.flash("Not all required fields filled.") |
---|
1448 | self.redirect(self.url(self.context, 'edit_base')) |
---|
1449 | return |
---|
1450 | self.ac_series = self.request.form.get('ac_series', None) |
---|
1451 | self.ac_number = self.request.form.get('ac_number', None) |
---|
1452 | |
---|
1453 | if SUBMIT is None: |
---|
1454 | return |
---|
1455 | pin = '%s-%s-%s' % (self.ac_prefix, self.ac_series, self.ac_number) |
---|
1456 | code = get_access_code(pin) |
---|
1457 | if not code: |
---|
1458 | self.flash('Activation code is invalid.') |
---|
1459 | return |
---|
1460 | # Mark pin as used (this also fires a pin related transition) |
---|
1461 | # and fire transition start_clearance |
---|
1462 | if code.state == USED: |
---|
1463 | self.flash('Activation code has already been used.') |
---|
1464 | return |
---|
1465 | else: |
---|
1466 | comment = u"AC invalidated for %s" % self.context.student_id |
---|
1467 | # Here we know that the ac is in state initialized so we do not |
---|
1468 | # expect an exception, but the owner might be different |
---|
1469 | if not invalidate_accesscode(pin,comment,self.context.student_id): |
---|
1470 | self.flash('You are not the owner of this access code.') |
---|
1471 | return |
---|
1472 | self.context.clr_code = pin |
---|
1473 | IWorkflowInfo(self.context).fireTransition('start_clearance') |
---|
1474 | self.flash('Clearance process has been started.') |
---|
1475 | self.redirect(self.url(self.context,'cedit')) |
---|
1476 | return |
---|
1477 | |
---|
1478 | class StudentClearanceEditFormPage(StudentClearanceManageFormPage): |
---|
1479 | """ View to edit student clearance data by student |
---|
1480 | """ |
---|
1481 | grok.context(IStudent) |
---|
1482 | grok.name('cedit') |
---|
1483 | grok.require('waeup.handleStudent') |
---|
1484 | form_fields = grok.AutoFields( |
---|
1485 | IStudentClearance).omit('clearance_locked') |
---|
1486 | label = 'Edit clearance data' |
---|
1487 | form_fields['date_of_birth'].custom_widget = FriendlyDateWidget('le-year') |
---|
1488 | |
---|
1489 | def update(self): |
---|
1490 | if self.context.clearance_locked: |
---|
1491 | emit_lock_message(self) |
---|
1492 | return |
---|
1493 | return super(StudentClearanceEditFormPage, self).update() |
---|
1494 | |
---|
1495 | @action('Save', style='primary') |
---|
1496 | def save(self, **data): |
---|
1497 | self.applyData(self.context, **data) |
---|
1498 | self.flash('Clearance form has been saved.') |
---|
1499 | return |
---|
1500 | |
---|
1501 | def dataNotComplete(self): |
---|
1502 | """To be implemented in the customization package. |
---|
1503 | """ |
---|
1504 | return False |
---|
1505 | |
---|
1506 | @action('Save and request clearance', style='primary') |
---|
1507 | def requestClearance(self, **data): |
---|
1508 | self.applyData(self.context, **data) |
---|
1509 | if self.dataNotComplete(): |
---|
1510 | self.flash(self.dataNotComplete()) |
---|
1511 | return |
---|
1512 | self.flash('Clearance form has been saved.') |
---|
1513 | self.redirect(self.url(self.context,'request_clearance')) |
---|
1514 | return |
---|
1515 | |
---|
1516 | class RequestClearancePage(SIRPPage): |
---|
1517 | grok.context(IStudent) |
---|
1518 | grok.name('request_clearance') |
---|
1519 | grok.require('waeup.handleStudent') |
---|
1520 | grok.template('enterpin') |
---|
1521 | label = 'Request clearance' |
---|
1522 | notice = 'Enter the CLR access code used for starting clearance.' |
---|
1523 | ac_prefix = 'CLR' |
---|
1524 | pnav = 4 |
---|
1525 | buttonname = 'Request clearance now' |
---|
1526 | |
---|
1527 | def update(self, SUBMIT=None): |
---|
1528 | self.ac_series = self.request.form.get('ac_series', None) |
---|
1529 | self.ac_number = self.request.form.get('ac_number', None) |
---|
1530 | if SUBMIT is None: |
---|
1531 | return |
---|
1532 | pin = '%s-%s-%s' % (self.ac_prefix, self.ac_series, self.ac_number) |
---|
1533 | if self.context.clr_code != pin: |
---|
1534 | self.flash("This isn't your CLR access code.") |
---|
1535 | return |
---|
1536 | state = IWorkflowState(self.context).getState() |
---|
1537 | # This shouldn't happen, but the application officer |
---|
1538 | # might have forgotten to lock the form after changing the state |
---|
1539 | if state != CLEARANCE: |
---|
1540 | self.flash('This form cannot be submitted. Wrong state!') |
---|
1541 | return |
---|
1542 | IWorkflowInfo(self.context).fireTransition('request_clearance') |
---|
1543 | self.flash('Clearance has been requested.') |
---|
1544 | self.redirect(self.url(self.context)) |
---|
1545 | return |
---|
1546 | |
---|
1547 | class StartCourseRegistrationPage(SIRPPage): |
---|
1548 | grok.context(IStudentStudyCourse) |
---|
1549 | grok.name('start_course_registration') |
---|
1550 | grok.require('waeup.handleStudent') |
---|
1551 | grok.template('enterpin') |
---|
1552 | label = 'Start course registration' |
---|
1553 | ac_prefix = 'SFE' |
---|
1554 | notice = '' |
---|
1555 | pnav = 4 |
---|
1556 | buttonname = 'Start course registration now' |
---|
1557 | |
---|
1558 | def update(self, SUBMIT=None): |
---|
1559 | if not self.context.getStudent().state in (CLEARED,RETURNING): |
---|
1560 | self.flash("Wrong state.") |
---|
1561 | self.redirect(self.url(self.context)) |
---|
1562 | return |
---|
1563 | self.ac_series = self.request.form.get('ac_series', None) |
---|
1564 | self.ac_number = self.request.form.get('ac_number', None) |
---|
1565 | |
---|
1566 | if SUBMIT is None: |
---|
1567 | return |
---|
1568 | pin = '%s-%s-%s' % (self.ac_prefix, self.ac_series, self.ac_number) |
---|
1569 | code = get_access_code(pin) |
---|
1570 | if not code: |
---|
1571 | self.flash('Activation code is invalid.') |
---|
1572 | return |
---|
1573 | # Mark pin as used (this also fires a pin related transition) |
---|
1574 | # and fire transition start_clearance |
---|
1575 | if code.state == USED: |
---|
1576 | self.flash('Activation code has already been used.') |
---|
1577 | return |
---|
1578 | else: |
---|
1579 | comment = u"AC invalidated for %s" % ( |
---|
1580 | self.context.getStudent().student_id,) |
---|
1581 | # Here we know that the ac is in state initialized so we do not |
---|
1582 | # expect an exception, but the owner might be different |
---|
1583 | if not invalidate_accesscode( |
---|
1584 | pin,comment,self.context.getStudent().student_id): |
---|
1585 | self.flash('You are not the owner of this access code.') |
---|
1586 | return |
---|
1587 | if self.context.getStudent().state == CLEARED: |
---|
1588 | IWorkflowInfo(self.context.getStudent()).fireTransition( |
---|
1589 | 'pay_first_school_fee') |
---|
1590 | elif self.context.getStudent().state == RETURNING: |
---|
1591 | IWorkflowInfo(self.context.getStudent()).fireTransition( |
---|
1592 | 'pay_school_fee') |
---|
1593 | self.flash('Course registration has been started.') |
---|
1594 | self.redirect(self.url(self.context)) |
---|
1595 | return |
---|
1596 | |
---|
1597 | class AddStudyLevelFormPage(SIRPEditFormPage): |
---|
1598 | """ Page for students to add current study levels |
---|
1599 | """ |
---|
1600 | grok.context(IStudentStudyCourse) |
---|
1601 | grok.name('add') |
---|
1602 | grok.require('waeup.handleStudent') |
---|
1603 | grok.template('studyleveladdpage') |
---|
1604 | form_fields = grok.AutoFields(IStudentStudyCourse) |
---|
1605 | pnav = 4 |
---|
1606 | |
---|
1607 | @property |
---|
1608 | def label(self): |
---|
1609 | studylevelsource = StudyLevelSource().factory |
---|
1610 | code = self.context.current_level |
---|
1611 | title = studylevelsource.getTitle(self.context, code) |
---|
1612 | return 'Add current level %s' % title |
---|
1613 | |
---|
1614 | def update(self): |
---|
1615 | if self.context.getStudent().state != PAID: |
---|
1616 | emit_lock_message(self) |
---|
1617 | return |
---|
1618 | super(AddStudyLevelFormPage, self).update() |
---|
1619 | return |
---|
1620 | |
---|
1621 | @action('Create course list now', style='primary') |
---|
1622 | def addStudyLevel(self, **data): |
---|
1623 | studylevel = StudentStudyLevel() |
---|
1624 | studylevel.level = self.context.current_level |
---|
1625 | studylevel.level_session = self.context.current_session |
---|
1626 | try: |
---|
1627 | self.context.addStudentStudyLevel( |
---|
1628 | self.context.certificate,studylevel) |
---|
1629 | except KeyError: |
---|
1630 | self.flash('This level exists.') |
---|
1631 | self.redirect(self.url(self.context)) |
---|
1632 | return |
---|
1633 | |
---|
1634 | class StudyLevelEditFormPage(SIRPEditFormPage): |
---|
1635 | """ Page to edit the student study level data by students |
---|
1636 | """ |
---|
1637 | grok.context(IStudentStudyLevel) |
---|
1638 | grok.name('edit') |
---|
1639 | grok.require('waeup.handleStudent') |
---|
1640 | grok.template('studyleveleditpage') |
---|
1641 | form_fields = grok.AutoFields(IStudentStudyLevel).omit( |
---|
1642 | 'level_session', 'level_verdict') |
---|
1643 | pnav = 4 |
---|
1644 | |
---|
1645 | def update(self): |
---|
1646 | if self.context.getStudent().state != PAID: |
---|
1647 | emit_lock_message(self) |
---|
1648 | return |
---|
1649 | super(StudyLevelEditFormPage, self).update() |
---|
1650 | datatable.need() |
---|
1651 | warning.need() |
---|
1652 | return |
---|
1653 | |
---|
1654 | @property |
---|
1655 | def label(self): |
---|
1656 | return 'Add and remove course tickets of study level %s' % ( |
---|
1657 | self.context.level_title,) |
---|
1658 | |
---|
1659 | @property |
---|
1660 | def total_credits(self): |
---|
1661 | total_credits = 0 |
---|
1662 | for key, val in self.context.items(): |
---|
1663 | total_credits += val.credits |
---|
1664 | return total_credits |
---|
1665 | |
---|
1666 | @action('Add course ticket') |
---|
1667 | def addCourseTicket(self, **data): |
---|
1668 | self.redirect(self.url(self.context, 'ctadd')) |
---|
1669 | |
---|
1670 | @jsaction('Remove selected tickets') |
---|
1671 | def delCourseTicket(self, **data): |
---|
1672 | form = self.request.form |
---|
1673 | if form.has_key('val_id'): |
---|
1674 | child_id = form['val_id'] |
---|
1675 | else: |
---|
1676 | self.flash('No ticket selected.') |
---|
1677 | self.redirect(self.url(self.context, '@@edit')) |
---|
1678 | return |
---|
1679 | if not isinstance(child_id, list): |
---|
1680 | child_id = [child_id] |
---|
1681 | deleted = [] |
---|
1682 | for id in child_id: |
---|
1683 | # Students are not allowed to remove core tickets |
---|
1684 | if not self.context[id].mandatory: |
---|
1685 | try: |
---|
1686 | del self.context[id] |
---|
1687 | deleted.append(id) |
---|
1688 | except: |
---|
1689 | self.flash('Could not delete %s: %s: %s' % ( |
---|
1690 | id, sys.exc_info()[0], sys.exc_info()[1])) |
---|
1691 | if len(deleted): |
---|
1692 | self.flash('Successfully removed: %s' % ', '.join(deleted)) |
---|
1693 | self.redirect(self.url(self.context, u'@@edit')) |
---|
1694 | return |
---|
1695 | |
---|
1696 | @action('Register course list', style='primary') |
---|
1697 | def RegisterCourses(self, **data): |
---|
1698 | IWorkflowInfo(self.context.getStudent()).fireTransition( |
---|
1699 | 'register_courses') |
---|
1700 | self.flash('Course list has been registered.') |
---|
1701 | self.redirect(self.url(self.context)) |
---|
1702 | return |
---|
1703 | |
---|
1704 | class CourseTicketAddFormPage2(CourseTicketAddFormPage): |
---|
1705 | """Add a course ticket by student. |
---|
1706 | """ |
---|
1707 | grok.name('ctadd') |
---|
1708 | grok.require('waeup.handleStudent') |
---|
1709 | form_fields = grok.AutoFields(ICourseTicketAdd).omit( |
---|
1710 | 'grade', 'score', 'mandatory', 'automatic', 'carry_over') |
---|
1711 | |
---|
1712 | def update(self): |
---|
1713 | if self.context.getStudent().state != PAID: |
---|
1714 | emit_lock_message(self) |
---|
1715 | return |
---|
1716 | super(CourseTicketAddFormPage2, self).update() |
---|
1717 | return |
---|
1718 | |
---|
1719 | @action('Add course ticket') |
---|
1720 | def addCourseTicket(self, **data): |
---|
1721 | # Safety belt |
---|
1722 | if self.context.getStudent().state != PAID: |
---|
1723 | return |
---|
1724 | ticket = CourseTicket() |
---|
1725 | course = data['course'] |
---|
1726 | for name in ['code', 'title', 'credits', 'passmark', 'semester']: |
---|
1727 | setattr(ticket, name, getattr(course, name)) |
---|
1728 | ticket.automatic = False |
---|
1729 | try: |
---|
1730 | self.context.addCourseTicket(ticket) |
---|
1731 | except KeyError: |
---|
1732 | self.flash('The ticket exists.') |
---|
1733 | return |
---|
1734 | self.flash('Successfully added %s.' % ticket.code) |
---|
1735 | self.redirect(self.url(self.context, u'@@edit')) |
---|
1736 | return |
---|
1737 | |
---|
1738 | class ChangePasswordRequestPage(SIRPForm): |
---|
1739 | """Captcha'd page for students to request a password change. |
---|
1740 | """ |
---|
1741 | grok.context(IUniversity) |
---|
1742 | grok.name('changepw') |
---|
1743 | grok.require('waeup.Anonymous') |
---|
1744 | grok.template('changepw') |
---|
1745 | label = 'Change my password' |
---|
1746 | form_fields = grok.AutoFields(IStudentChangePassword) |
---|
1747 | |
---|
1748 | def update(self): |
---|
1749 | # Handle captcha |
---|
1750 | self.captcha = getUtility(ICaptchaManager).getCaptcha() |
---|
1751 | self.captcha_result = self.captcha.verify(self.request) |
---|
1752 | self.captcha_code = self.captcha.display(self.captcha_result.error_code) |
---|
1753 | return |
---|
1754 | |
---|
1755 | @action('Get new login credentials', style='primary') |
---|
1756 | def request(self, **data): |
---|
1757 | if not self.captcha_result.is_valid: |
---|
1758 | # Captcha will display error messages automatically. |
---|
1759 | # No need to flash something. |
---|
1760 | return |
---|
1761 | # Search student |
---|
1762 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
1763 | reg_number = data['reg_number'] |
---|
1764 | email = data['email'] |
---|
1765 | results = cat.searchResults( |
---|
1766 | reg_number=(reg_number, reg_number), |
---|
1767 | email=(email,email)) |
---|
1768 | if len(results) == 0: |
---|
1769 | self.flash('No student record found.') |
---|
1770 | return |
---|
1771 | student = list(results)[0] |
---|
1772 | # Change password |
---|
1773 | sirp_utils = getUtility(ISIRPUtils) |
---|
1774 | pwd = sirp_utils.genPassword() |
---|
1775 | IUserAccount(student).setPassword(pwd) |
---|
1776 | # Send email with new redentials |
---|
1777 | msg = 'You have successfully changed your password for the' |
---|
1778 | login_url = self.url(grok.getSite(), 'login') |
---|
1779 | success = sirp_utils.sendCredentials( |
---|
1780 | IUserAccount(student),pwd,login_url,msg) |
---|
1781 | if success: |
---|
1782 | self.flash('An email with your user name and password ' + |
---|
1783 | 'has been sent to %s.' % email) |
---|
1784 | else: |
---|
1785 | self.flash('An smtp server error occurred.') |
---|
1786 | return |
---|
1787 | |
---|
1788 | class SetPasswordPage(SIRPPage): |
---|
1789 | grok.context(ISIRPObject) |
---|
1790 | grok.name('setpassword') |
---|
1791 | grok.require('waeup.Anonymous') |
---|
1792 | grok.template('setpassword') |
---|
1793 | label = 'Set password for first-time login' |
---|
1794 | ac_prefix = 'PWD' |
---|
1795 | pnav = 0 |
---|
1796 | |
---|
1797 | def update(self, SUBMIT=None): |
---|
1798 | self.reg_number = self.request.form.get('reg_number', None) |
---|
1799 | self.ac_series = self.request.form.get('ac_series', None) |
---|
1800 | self.ac_number = self.request.form.get('ac_number', None) |
---|
1801 | |
---|
1802 | if SUBMIT is None: |
---|
1803 | return |
---|
1804 | hitlist = search(query=self.reg_number, |
---|
1805 | searchtype='reg_number', view=self) |
---|
1806 | if not hitlist: |
---|
1807 | self.flash('No student found.') |
---|
1808 | return |
---|
1809 | if len(hitlist) != 1: # Cannot happen but anyway |
---|
1810 | self.flash('More than one student found.') |
---|
1811 | return |
---|
1812 | student = hitlist[0].context |
---|
1813 | self.student_id = student.student_id |
---|
1814 | student_pw = student.password |
---|
1815 | pin = '%s-%s-%s' % (self.ac_prefix, self.ac_series, self.ac_number) |
---|
1816 | code = get_access_code(pin) |
---|
1817 | if not code: |
---|
1818 | self.flash('Access code is invalid.') |
---|
1819 | return |
---|
1820 | if student_pw and pin == student.adm_code: |
---|
1821 | self.flash('Password has already been set. Your Student Id is %s' |
---|
1822 | % self.student_id) |
---|
1823 | return |
---|
1824 | elif student_pw: |
---|
1825 | self.flash( |
---|
1826 | 'Password has already been set. You are using the ' |
---|
1827 | 'wrong Access Code.') |
---|
1828 | return |
---|
1829 | # Mark pin as used (this also fires a pin related transition) |
---|
1830 | # and set student password |
---|
1831 | if code.state == USED: |
---|
1832 | self.flash('Access code has already been used.') |
---|
1833 | return |
---|
1834 | else: |
---|
1835 | comment = u"AC invalidated for %s" % self.student_id |
---|
1836 | # Here we know that the ac is in state initialized so we do not |
---|
1837 | # expect an exception |
---|
1838 | invalidate_accesscode(pin,comment) |
---|
1839 | IUserAccount(student).setPassword(self.ac_number) |
---|
1840 | student.adm_code = pin |
---|
1841 | self.flash('Password has been set. Your Student Id is %s' |
---|
1842 | % self.student_id) |
---|
1843 | return |
---|