1 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
2 | ## This program is free software; you can redistribute it and/or modify |
---|
3 | ## it under the terms of the GNU General Public License as published by |
---|
4 | ## the Free Software Foundation; either version 2 of the License, or |
---|
5 | ## (at your option) any later version. |
---|
6 | ## |
---|
7 | ## This program is distributed in the hope that it will be useful, |
---|
8 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
9 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
10 | ## GNU General Public License for more details. |
---|
11 | ## |
---|
12 | ## You should have received a copy of the GNU General Public License |
---|
13 | ## along with this program; if not, write to the Free Software |
---|
14 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
15 | ## |
---|
16 | """UI components for students and related components. |
---|
17 | """ |
---|
18 | import grok |
---|
19 | |
---|
20 | from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState |
---|
21 | from zope.component import createObject |
---|
22 | from waeup.sirp.accesscodes import invalidate_accesscode, get_access_code |
---|
23 | from waeup.sirp.accesscodes.workflow import USED |
---|
24 | from waeup.sirp.browser import ( |
---|
25 | WAeUPPage, WAeUPEditFormPage, WAeUPAddFormPage, WAeUPDisplayFormPage) |
---|
26 | from waeup.sirp.browser.breadcrumbs import Breadcrumb |
---|
27 | from waeup.sirp.browser.resources import datepicker, datatable, tabs |
---|
28 | from waeup.sirp.browser.viewlets import ( |
---|
29 | ManageActionButton, PrimaryNavTab, AddActionButton) |
---|
30 | from waeup.sirp.interfaces import IWAeUPObject, IUserAccount |
---|
31 | from waeup.sirp.widgets.datewidget import ( |
---|
32 | FriendlyDateWidget, FriendlyDateDisplayWidget) |
---|
33 | from waeup.sirp.students.interfaces import ( |
---|
34 | IStudentsContainer, IStudent, IStudentClearance, IStudentPasswordSetting, |
---|
35 | IStudentPersonal, IStudentBase, IStudentStudyCourse, IStudentPayments, |
---|
36 | IStudentAccommodation, IStudentClearanceEdit, IStudentStudyLevel, |
---|
37 | ICourseTicket, ICourseTicketAdd, |
---|
38 | ) |
---|
39 | from waeup.sirp.students.catalog import search |
---|
40 | from waeup.sirp.students.workflow import CLEARANCE |
---|
41 | from waeup.sirp.students.studylevel import StudentStudyLevel, CourseTicket |
---|
42 | from waeup.sirp.students.vocabularies import StudyLevelSource |
---|
43 | |
---|
44 | # Save function used for save methods in manager pages |
---|
45 | def msave(view, **data): |
---|
46 | form = view.request.form |
---|
47 | ob_class = view.__implemented__.__name__.replace('waeup.sirp.','') |
---|
48 | changed_fields = view.applyData(view.context, **data) |
---|
49 | # Turn list of lists into single list |
---|
50 | if changed_fields: |
---|
51 | changed_fields = reduce(lambda x,y: x+y, changed_fields.values()) |
---|
52 | fields_string = ' + '.join(changed_fields) |
---|
53 | view.context._p_changed = True |
---|
54 | view.flash('Form has been saved.') |
---|
55 | if fields_string: |
---|
56 | view.context.getStudent().loggerInfo(ob_class, 'saved: % s' % fields_string) |
---|
57 | return |
---|
58 | |
---|
59 | class StudentsTab(PrimaryNavTab): |
---|
60 | """Students tab in primary navigation. |
---|
61 | """ |
---|
62 | |
---|
63 | grok.context(IWAeUPObject) |
---|
64 | grok.order(3) |
---|
65 | grok.require('waeup.viewStudent') |
---|
66 | grok.template('primarynavtab') |
---|
67 | |
---|
68 | pnav = 4 |
---|
69 | tab_title = u'Students' |
---|
70 | |
---|
71 | @property |
---|
72 | def link_target(self): |
---|
73 | return self.view.application_url('students') |
---|
74 | |
---|
75 | class StudentsBreadcrumb(Breadcrumb): |
---|
76 | """A breadcrumb for the students container. |
---|
77 | """ |
---|
78 | grok.context(IStudentsContainer) |
---|
79 | title = u'Students' |
---|
80 | |
---|
81 | class SudyCourseBreadcrumb(Breadcrumb): |
---|
82 | """A breadcrumb for the student study course. |
---|
83 | """ |
---|
84 | grok.context(IStudentStudyCourse) |
---|
85 | title = u'Study Course' |
---|
86 | |
---|
87 | class PaymentsBreadcrumb(Breadcrumb): |
---|
88 | """A breadcrumb for the student payments folder. |
---|
89 | """ |
---|
90 | grok.context(IStudentPayments) |
---|
91 | title = u'Payments' |
---|
92 | |
---|
93 | class AccommodationBreadcrumb(Breadcrumb): |
---|
94 | """A breadcrumb for the student accommodation folder. |
---|
95 | """ |
---|
96 | grok.context(IStudentAccommodation) |
---|
97 | title = u'Accommodation' |
---|
98 | |
---|
99 | class StudyLevelBreadcrumb(Breadcrumb): |
---|
100 | """A breadcrumb for course lists. |
---|
101 | """ |
---|
102 | grok.context(IStudentStudyLevel) |
---|
103 | |
---|
104 | @property |
---|
105 | def title(self): |
---|
106 | return self.context.level_title |
---|
107 | |
---|
108 | class StudentsContainerPage(WAeUPPage): |
---|
109 | """The standard view for student containers. |
---|
110 | """ |
---|
111 | grok.context(IStudentsContainer) |
---|
112 | grok.name('index') |
---|
113 | grok.require('waeup.viewStudent') |
---|
114 | grok.template('containerpage') |
---|
115 | label = 'Student Section' |
---|
116 | title = 'Students' |
---|
117 | pnav = 4 |
---|
118 | |
---|
119 | def update(self, *args, **kw): |
---|
120 | datatable.need() |
---|
121 | form = self.request.form |
---|
122 | self.hitlist = [] |
---|
123 | if 'searchterm' in form and form['searchterm']: |
---|
124 | self.searchterm = form['searchterm'] |
---|
125 | self.searchtype = form['searchtype'] |
---|
126 | elif 'old_searchterm' in form: |
---|
127 | self.searchterm = form['old_searchterm'] |
---|
128 | self.searchtype = form['old_searchtype'] |
---|
129 | else: |
---|
130 | if 'search' in form: |
---|
131 | self.flash('Empty search string.') |
---|
132 | return |
---|
133 | self.hitlist = search(query=self.searchterm, |
---|
134 | searchtype=self.searchtype, view=self) |
---|
135 | if not self.hitlist: |
---|
136 | self.flash('No student found.') |
---|
137 | return |
---|
138 | |
---|
139 | class SetPasswordPage(WAeUPPage): |
---|
140 | grok.context(IWAeUPObject) |
---|
141 | grok.name('setpassword') |
---|
142 | grok.require('waeup.Public') |
---|
143 | grok.template('setpassword') |
---|
144 | title = '' |
---|
145 | label = 'Set password for first-time login' |
---|
146 | ac_prefix = 'PWD' |
---|
147 | pnav = 0 |
---|
148 | |
---|
149 | def update(self, SUBMIT=None): |
---|
150 | self.reg_number = self.request.form.get('reg_number', None) |
---|
151 | self.ac_series = self.request.form.get('ac_series', None) |
---|
152 | self.ac_number = self.request.form.get('ac_number', None) |
---|
153 | |
---|
154 | if SUBMIT is None: |
---|
155 | return |
---|
156 | hitlist = search(query=self.reg_number, |
---|
157 | searchtype='reg_number', view=self) |
---|
158 | if not hitlist: |
---|
159 | self.flash('No student found.') |
---|
160 | return |
---|
161 | if len(hitlist) != 1: # Cannot happen but anyway |
---|
162 | self.flash('More than one student found.') |
---|
163 | return |
---|
164 | student = hitlist[0].context |
---|
165 | self.student_id = student.student_id |
---|
166 | student_pw = student.password |
---|
167 | pin = '%s-%s-%s' % (self.ac_prefix, self.ac_series, self.ac_number) |
---|
168 | code = get_access_code(pin) |
---|
169 | if not code: |
---|
170 | self.flash('Access code is invalid.') |
---|
171 | return |
---|
172 | if student_pw and pin == student.adm_code: |
---|
173 | self.flash('Password has already been set. Your Student Id is %s' |
---|
174 | % self.student_id) |
---|
175 | return |
---|
176 | elif student_pw: |
---|
177 | self.flash('Password has already been set. You are using the wrong Access Code.') |
---|
178 | return |
---|
179 | # Mark pin as used (this also fires a pin related transition) |
---|
180 | # and set student password |
---|
181 | if code.state == USED: |
---|
182 | self.flash('Access code has already been used.') |
---|
183 | return |
---|
184 | else: |
---|
185 | comment = u"AC invalidated for %s" % self.student_id |
---|
186 | # Here we know that the ac is in state initialized so we do not |
---|
187 | # expect an exception |
---|
188 | #import pdb; pdb.set_trace() |
---|
189 | invalidate_accesscode(pin,comment) |
---|
190 | IUserAccount(student).setPassword(self.ac_number) |
---|
191 | student.adm_code = pin |
---|
192 | self.flash('Password has been set. Your Student Id is %s' |
---|
193 | % self.student_id) |
---|
194 | return |
---|
195 | |
---|
196 | class StudentsContainerManageActionButton(ManageActionButton): |
---|
197 | grok.order(1) |
---|
198 | grok.context(IStudentsContainer) |
---|
199 | grok.view(StudentsContainerPage) |
---|
200 | grok.require('waeup.manageStudents') |
---|
201 | text = 'Manage student section' |
---|
202 | |
---|
203 | |
---|
204 | class StudentsContainerManagePage(WAeUPPage): |
---|
205 | """The manage page for student containers. |
---|
206 | """ |
---|
207 | grok.context(IStudentsContainer) |
---|
208 | grok.name('manage') |
---|
209 | grok.require('waeup.manageStudents') |
---|
210 | grok.template('containermanagepage') |
---|
211 | pnav = 4 |
---|
212 | title = 'Manage student section' |
---|
213 | |
---|
214 | @property |
---|
215 | def label(self): |
---|
216 | return self.title |
---|
217 | |
---|
218 | def update(self, *args, **kw): |
---|
219 | datatable.need() |
---|
220 | form = self.request.form |
---|
221 | self.hitlist = [] |
---|
222 | if 'searchterm' in form and form['searchterm']: |
---|
223 | self.searchterm = form['searchterm'] |
---|
224 | self.searchtype = form['searchtype'] |
---|
225 | elif 'old_searchterm' in form: |
---|
226 | self.searchterm = form['old_searchterm'] |
---|
227 | self.searchtype = form['old_searchtype'] |
---|
228 | else: |
---|
229 | if 'search' in form: |
---|
230 | self.flash('Empty search string.') |
---|
231 | return |
---|
232 | if not 'entries' in form: |
---|
233 | self.hitlist = search(query=self.searchterm, |
---|
234 | searchtype=self.searchtype, view=self) |
---|
235 | if not self.hitlist: |
---|
236 | self.flash('No student found.') |
---|
237 | return |
---|
238 | entries = form['entries'] |
---|
239 | if isinstance(entries, basestring): |
---|
240 | entries = [entries] |
---|
241 | deleted = [] |
---|
242 | for entry in entries: |
---|
243 | if 'remove' in form: |
---|
244 | del self.context[entry] |
---|
245 | deleted.append(entry) |
---|
246 | self.hitlist = search(query=self.searchterm, |
---|
247 | searchtype=self.searchtype, view=self) |
---|
248 | if len(deleted): |
---|
249 | self.flash('Successfully removed: %s' % ', '.join(deleted)) |
---|
250 | return |
---|
251 | |
---|
252 | class StudentsContainerAddActionButton(AddActionButton): |
---|
253 | grok.order(1) |
---|
254 | grok.context(IStudentsContainer) |
---|
255 | grok.view(StudentsContainerManagePage) |
---|
256 | grok.require('waeup.manageStudents') |
---|
257 | text = 'Add student' |
---|
258 | target = 'addstudent' |
---|
259 | |
---|
260 | class StudentAddFormPage(WAeUPAddFormPage): |
---|
261 | """Add-form to add a student. |
---|
262 | """ |
---|
263 | grok.context(IStudentsContainer) |
---|
264 | grok.require('waeup.manageStudents') |
---|
265 | grok.name('addstudent') |
---|
266 | grok.template('studentaddpage') |
---|
267 | form_fields = grok.AutoFields(IStudent) |
---|
268 | title = 'Students' |
---|
269 | label = 'Add student' |
---|
270 | pnav = 4 |
---|
271 | |
---|
272 | @grok.action('Create student record') |
---|
273 | def addStudent(self, **data): |
---|
274 | student = createObject(u'waeup.Student') |
---|
275 | self.applyData(student, **data) |
---|
276 | self.context.addStudent(student) |
---|
277 | self.flash('Student record created.') |
---|
278 | self.redirect(self.url(self.context[student.student_id], 'index')) |
---|
279 | return |
---|
280 | |
---|
281 | class StudentBaseDisplayFormPage(WAeUPDisplayFormPage): |
---|
282 | """ Page to display student base data |
---|
283 | """ |
---|
284 | grok.context(IStudent) |
---|
285 | grok.name('index') |
---|
286 | grok.require('waeup.viewStudent') |
---|
287 | grok.template('basepage') |
---|
288 | form_fields = grok.AutoFields(IStudentBase).omit('password') |
---|
289 | pnav = 4 |
---|
290 | title = 'Base Data' |
---|
291 | |
---|
292 | @property |
---|
293 | def label(self): |
---|
294 | return '%s: Base Data' % self.context.name |
---|
295 | |
---|
296 | @property |
---|
297 | def hasPassword(self): |
---|
298 | if self.context.password: |
---|
299 | return 'set' |
---|
300 | return 'unset' |
---|
301 | |
---|
302 | class StudentBaseManageActionButton(ManageActionButton): |
---|
303 | grok.order(1) |
---|
304 | grok.context(IStudent) |
---|
305 | grok.view(StudentBaseDisplayFormPage) |
---|
306 | grok.require('waeup.manageStudents') |
---|
307 | text = 'Manage' |
---|
308 | target = 'edit_base' |
---|
309 | |
---|
310 | class StudentBaseManageFormPage(WAeUPEditFormPage): |
---|
311 | """ View to edit student base data |
---|
312 | """ |
---|
313 | grok.context(IStudent) |
---|
314 | grok.name('edit_base') |
---|
315 | grok.require('waeup.manageStudents') |
---|
316 | form_fields = grok.AutoFields(IStudentBase).omit('student_id') |
---|
317 | grok.template('basemanagepage') |
---|
318 | label = 'Manage base data' |
---|
319 | title = 'Base Data' |
---|
320 | pnav = 4 |
---|
321 | |
---|
322 | def update(self): |
---|
323 | datepicker.need() # Enable jQuery datepicker in date fields. |
---|
324 | super(StudentBaseManageFormPage, self).update() |
---|
325 | self.wf_info = IWorkflowInfo(self.context) |
---|
326 | return |
---|
327 | |
---|
328 | def getTransitions(self): |
---|
329 | """Return a list of dicts of allowed transition ids and titles. |
---|
330 | |
---|
331 | Each list entry provides keys ``name`` and ``title`` for |
---|
332 | internal name and (human readable) title of a single |
---|
333 | transition. |
---|
334 | """ |
---|
335 | allowed_transitions = self.wf_info.getManualTransitions() |
---|
336 | return [dict(name='', title='No transition')] +[ |
---|
337 | dict(name=x, title=y) for x, y in allowed_transitions] |
---|
338 | |
---|
339 | @grok.action('Save') |
---|
340 | def save(self, **data): |
---|
341 | form = self.request.form |
---|
342 | ob_class = self.__implemented__.__name__.replace('waeup.sirp.','') |
---|
343 | password = form.get('password', None) |
---|
344 | password_ctl = form.get('control_password', None) |
---|
345 | if password: |
---|
346 | if (password != password_ctl): |
---|
347 | self.flash('Passwords do not match.') |
---|
348 | else: |
---|
349 | # XXX: This is too early. PW should only be saved if there |
---|
350 | # are no (other) errors left in form. |
---|
351 | IUserAccount(self.context).setPassword(password) |
---|
352 | self.context.loggerInfo(ob_class, 'password changed') |
---|
353 | |
---|
354 | #self.reg_number = form.get('form.reg_number', None) |
---|
355 | #if self.reg_number: |
---|
356 | # hitlist = search(query=self.reg_number,searchtype='reg_number', view=self) |
---|
357 | # if hitlist and hitlist[0].student_id != self.context.student_id: |
---|
358 | # self.flash('Registration number exists.') |
---|
359 | # return |
---|
360 | #self.matric_number = form.get('form.matric_number', None) |
---|
361 | #if self.matric_number: |
---|
362 | # hitlist = search(query=self.matric_number, |
---|
363 | # searchtype='matric_number', view=self) |
---|
364 | # if hitlist and hitlist[0].student_id != self.context.student_id: |
---|
365 | # self.flash('Matriculation number exists.') |
---|
366 | # return |
---|
367 | |
---|
368 | # Turn list of lists into single list |
---|
369 | changed_fields = self.applyData(self.context, **data) |
---|
370 | if changed_fields: |
---|
371 | changed_fields = reduce(lambda x,y: x+y, changed_fields.values()) |
---|
372 | fields_string = ' + '.join(changed_fields) |
---|
373 | self.context._p_changed = True |
---|
374 | if form.has_key('transition') and form['transition']: |
---|
375 | transition_id = form['transition'] |
---|
376 | self.wf_info.fireTransition(transition_id) |
---|
377 | self.flash('Form has been saved.') |
---|
378 | if fields_string: |
---|
379 | self.context.loggerInfo(ob_class, 'saved: % s' % fields_string) |
---|
380 | return |
---|
381 | |
---|
382 | class StudentClearanceDisplayFormPage(WAeUPDisplayFormPage): |
---|
383 | """ Page to display student clearance data |
---|
384 | """ |
---|
385 | grok.context(IStudent) |
---|
386 | grok.name('view_clearance') |
---|
387 | grok.require('waeup.viewStudent') |
---|
388 | form_fields = grok.AutoFields(IStudentClearance).omit('clearance_locked') |
---|
389 | form_fields['date_of_birth'].custom_widget = FriendlyDateDisplayWidget('le') |
---|
390 | title = 'Clearance Data' |
---|
391 | pnav = 4 |
---|
392 | |
---|
393 | @property |
---|
394 | def label(self): |
---|
395 | return '%s: Clearance Data' % self.context.name |
---|
396 | |
---|
397 | class StudentClearanceManageActionButton(ManageActionButton): |
---|
398 | grok.order(1) |
---|
399 | grok.context(IStudent) |
---|
400 | grok.view(StudentClearanceDisplayFormPage) |
---|
401 | grok.require('waeup.manageStudents') |
---|
402 | text = 'Manage' |
---|
403 | target = 'edit_clearance' |
---|
404 | |
---|
405 | class StudentClearanceManageFormPage(WAeUPEditFormPage): |
---|
406 | """ Page to edit student clearance data |
---|
407 | """ |
---|
408 | grok.context(IStudent) |
---|
409 | grok.name('edit_clearance') |
---|
410 | grok.require('waeup.manageStudents') |
---|
411 | form_fields = grok.AutoFields(IStudentClearance) |
---|
412 | label = 'Manage clearance data' |
---|
413 | title = 'Clearance Data' |
---|
414 | pnav = 4 |
---|
415 | |
---|
416 | form_fields['date_of_birth'].custom_widget = FriendlyDateWidget('le-year') |
---|
417 | |
---|
418 | def update(self): |
---|
419 | datepicker.need() # Enable jQuery datepicker in date fields. |
---|
420 | return super(StudentClearanceManageFormPage, self).update() |
---|
421 | |
---|
422 | @grok.action('Save') |
---|
423 | def save(self, **data): |
---|
424 | msave(self, **data) |
---|
425 | return |
---|
426 | |
---|
427 | class StudentPersonalDisplayFormPage(WAeUPDisplayFormPage): |
---|
428 | """ Page to display student personal data |
---|
429 | """ |
---|
430 | grok.context(IStudent) |
---|
431 | grok.name('view_personal') |
---|
432 | grok.require('waeup.viewStudent') |
---|
433 | form_fields = grok.AutoFields(IStudentPersonal) |
---|
434 | title = 'Personal Data' |
---|
435 | pnav = 4 |
---|
436 | |
---|
437 | @property |
---|
438 | def label(self): |
---|
439 | return '%s: Personal Data' % self.context.name |
---|
440 | |
---|
441 | class StudentPersonalManageActionButton(ManageActionButton): |
---|
442 | grok.order(1) |
---|
443 | grok.context(IStudent) |
---|
444 | grok.view(StudentPersonalDisplayFormPage) |
---|
445 | grok.require('waeup.manageStudents') |
---|
446 | text = 'Manage' |
---|
447 | target = 'edit_personal' |
---|
448 | |
---|
449 | class StudentPersonalManageFormPage(WAeUPEditFormPage): |
---|
450 | """ Page to edit student clearance data |
---|
451 | """ |
---|
452 | grok.context(IStudent) |
---|
453 | grok.name('edit_personal') |
---|
454 | grok.require('waeup.viewStudent') |
---|
455 | form_fields = grok.AutoFields(IStudentPersonal) |
---|
456 | label = 'Manage personal data' |
---|
457 | title = 'Personal Data' |
---|
458 | pnav = 4 |
---|
459 | |
---|
460 | @grok.action('Save') |
---|
461 | def save(self, **data): |
---|
462 | msave(self, **data) |
---|
463 | return |
---|
464 | |
---|
465 | class StudyCourseDisplayFormPage(WAeUPDisplayFormPage): |
---|
466 | """ Page to display the student study course data |
---|
467 | """ |
---|
468 | grok.context(IStudentStudyCourse) |
---|
469 | grok.name('index') |
---|
470 | grok.require('waeup.viewStudent') |
---|
471 | form_fields = grok.AutoFields(IStudentStudyCourse) |
---|
472 | grok.template('studycoursepage') |
---|
473 | title = 'Study Course' |
---|
474 | pnav = 4 |
---|
475 | |
---|
476 | @property |
---|
477 | def label(self): |
---|
478 | return '%s: Study Course' % self.context.__parent__.name |
---|
479 | |
---|
480 | class StudyCourseManageActionButton(ManageActionButton): |
---|
481 | grok.order(1) |
---|
482 | grok.context(IStudentStudyCourse) |
---|
483 | grok.view(StudyCourseDisplayFormPage) |
---|
484 | grok.require('waeup.manageStudents') |
---|
485 | text = 'Manage' |
---|
486 | target = 'manage' |
---|
487 | |
---|
488 | class StudyCourseManageFormPage(WAeUPEditFormPage): |
---|
489 | """ Page to edit the student study course data |
---|
490 | """ |
---|
491 | grok.context(IStudentStudyCourse) |
---|
492 | grok.name('manage') |
---|
493 | grok.require('waeup.manageStudents') |
---|
494 | grok.template('studycoursemanagepage') |
---|
495 | form_fields = grok.AutoFields(IStudentStudyCourse) |
---|
496 | title = 'Study Course' |
---|
497 | label = 'Manage study course' |
---|
498 | pnav = 4 |
---|
499 | taboneactions = ['Save','Cancel'] |
---|
500 | tabtwoactions = ['Remove selected levels','Cancel'] |
---|
501 | tabthreeactions = ['Add study level'] |
---|
502 | |
---|
503 | def update(self): |
---|
504 | super(StudyCourseManageFormPage, self).update() |
---|
505 | tabs.need() |
---|
506 | datatable.need() |
---|
507 | return |
---|
508 | |
---|
509 | @grok.action('Save') |
---|
510 | def save(self, **data): |
---|
511 | msave(self, **data) |
---|
512 | return |
---|
513 | |
---|
514 | @property |
---|
515 | def level_dict(self): |
---|
516 | studylevelsource = StudyLevelSource().factory |
---|
517 | for code in studylevelsource.getValues(self.context): |
---|
518 | title = studylevelsource.getTitle(self.context, code) |
---|
519 | yield(dict(code=code, title=title)) |
---|
520 | |
---|
521 | @grok.action('Add study level') |
---|
522 | def addStudyLevel(self, **data): |
---|
523 | level_code = self.request.form.get('addlevel', None) |
---|
524 | studylevel = StudentStudyLevel() |
---|
525 | studylevel.level = int(level_code) |
---|
526 | try: |
---|
527 | self.context.addStudentStudyLevel( |
---|
528 | self.context.certificate,studylevel) |
---|
529 | except KeyError: |
---|
530 | self.flash('This level exists.') |
---|
531 | self.redirect(self.url(self.context, u'@@manage')+'#tab-2') |
---|
532 | return |
---|
533 | |
---|
534 | @grok.action('Remove selected levels') |
---|
535 | def delStudyLevels(self, **data): |
---|
536 | form = self.request.form |
---|
537 | if form.has_key('val_id'): |
---|
538 | child_id = form['val_id'] |
---|
539 | else: |
---|
540 | self.flash('No study level selected.') |
---|
541 | self.redirect(self.url(self.context, '@@manage')+'#tab-2') |
---|
542 | return |
---|
543 | if not isinstance(child_id, list): |
---|
544 | child_id = [child_id] |
---|
545 | deleted = [] |
---|
546 | for id in child_id: |
---|
547 | try: |
---|
548 | del self.context[id] |
---|
549 | deleted.append(id) |
---|
550 | except: |
---|
551 | self.flash('Could not delete %s: %s: %s' % ( |
---|
552 | id, sys.exc_info()[0], sys.exc_info()[1])) |
---|
553 | if len(deleted): |
---|
554 | self.flash('Successfully removed: %s' % ', '.join(deleted)) |
---|
555 | self.redirect(self.url(self.context, u'@@manage')+'#tab-2') |
---|
556 | return |
---|
557 | |
---|
558 | class StudyLevelDisplayFormPage(WAeUPDisplayFormPage): |
---|
559 | """ Page to display student study levels |
---|
560 | """ |
---|
561 | grok.context(IStudentStudyLevel) |
---|
562 | grok.name('index') |
---|
563 | grok.require('waeup.viewStudent') |
---|
564 | form_fields = grok.AutoFields(IStudentStudyLevel) |
---|
565 | grok.template('studylevelpage') |
---|
566 | pnav = 4 |
---|
567 | |
---|
568 | @property |
---|
569 | def title(self): |
---|
570 | return 'Study Level %s' % self.context.level_title |
---|
571 | |
---|
572 | @property |
---|
573 | def label(self): |
---|
574 | return '%s: Study Level %s' % ( |
---|
575 | self.context.getStudent().name,self.context.level_title) |
---|
576 | |
---|
577 | @property |
---|
578 | def total_credits(self): |
---|
579 | total_credits = 0 |
---|
580 | for key, val in self.context.items(): |
---|
581 | total_credits += val.credits |
---|
582 | return total_credits |
---|
583 | |
---|
584 | class StudyLevelManageActionButton(ManageActionButton): |
---|
585 | grok.order(1) |
---|
586 | grok.context(IStudentStudyLevel) |
---|
587 | grok.view(StudyLevelDisplayFormPage) |
---|
588 | grok.require('waeup.manageStudents') |
---|
589 | text = 'Manage' |
---|
590 | target = 'manage' |
---|
591 | |
---|
592 | class StudyLevelManageFormPage(WAeUPEditFormPage): |
---|
593 | """ Page to edit the student study level data |
---|
594 | """ |
---|
595 | grok.context(IStudentStudyLevel) |
---|
596 | grok.name('manage') |
---|
597 | grok.require('waeup.manageStudents') |
---|
598 | grok.template('studylevelmanagepage') |
---|
599 | form_fields = grok.AutoFields(IStudentStudyLevel) |
---|
600 | pnav = 4 |
---|
601 | taboneactions = ['Save','Cancel'] |
---|
602 | tabtwoactions = ['Add course ticket','Remove selected tickets','Cancel'] |
---|
603 | |
---|
604 | def update(self): |
---|
605 | super(StudyLevelManageFormPage, self).update() |
---|
606 | tabs.need() |
---|
607 | datatable.need() |
---|
608 | return |
---|
609 | |
---|
610 | @property |
---|
611 | def title(self): |
---|
612 | return 'Study Level %s' % self.context.level_title |
---|
613 | |
---|
614 | @property |
---|
615 | def label(self): |
---|
616 | return 'Manage study level %s' % self.context.level_title |
---|
617 | |
---|
618 | @grok.action('Save') |
---|
619 | def save(self, **data): |
---|
620 | msave(self, **data) |
---|
621 | return |
---|
622 | |
---|
623 | @grok.action('Add course ticket') |
---|
624 | def addCourseTicket(self, **data): |
---|
625 | self.redirect(self.url(self.context, '@@add')) |
---|
626 | |
---|
627 | @grok.action('Remove selected tickets') |
---|
628 | def delCourseTicket(self, **data): |
---|
629 | form = self.request.form |
---|
630 | if form.has_key('val_id'): |
---|
631 | child_id = form['val_id'] |
---|
632 | else: |
---|
633 | self.flash('No ticket selected.') |
---|
634 | self.redirect(self.url(self.context, '@@manage')+'#tab-2') |
---|
635 | return |
---|
636 | if not isinstance(child_id, list): |
---|
637 | child_id = [child_id] |
---|
638 | deleted = [] |
---|
639 | for id in child_id: |
---|
640 | try: |
---|
641 | del self.context[id] |
---|
642 | deleted.append(id) |
---|
643 | except: |
---|
644 | self.flash('Could not delete %s: %s: %s' % ( |
---|
645 | id, sys.exc_info()[0], sys.exc_info()[1])) |
---|
646 | if len(deleted): |
---|
647 | self.flash('Successfully removed: %s' % ', '.join(deleted)) |
---|
648 | self.redirect(self.url(self.context, u'@@manage')+'#tab-2') |
---|
649 | return |
---|
650 | |
---|
651 | class CourseTicketAddFormPage(WAeUPAddFormPage): |
---|
652 | """Add a department to a faculty. |
---|
653 | """ |
---|
654 | grok.context(IStudentStudyLevel) |
---|
655 | grok.name('add') |
---|
656 | grok.require('waeup.manageStudents') |
---|
657 | label = 'Add course ticket' |
---|
658 | form_fields = grok.AutoFields(ICourseTicketAdd).omit('grade', 'score') |
---|
659 | pnav = 4 |
---|
660 | |
---|
661 | @property |
---|
662 | def title(self): |
---|
663 | return 'Study Level %s' % self.context.level_title |
---|
664 | |
---|
665 | @grok.action('Add course ticket') |
---|
666 | def addCourseTicket(self, **data): |
---|
667 | ticket = CourseTicket() |
---|
668 | course = data['course'] |
---|
669 | ticket.core_or_elective = data['core_or_elective'] |
---|
670 | ticket.automatic = False |
---|
671 | ticket.code = course.code |
---|
672 | ticket.title = course.title |
---|
673 | ticket.faculty = course.__parent__.__parent__.__parent__.title |
---|
674 | ticket.department = course.__parent__.__parent__.title |
---|
675 | ticket.credits = course.credits |
---|
676 | ticket.passmark = course.passmark |
---|
677 | ticket.semester = course.semester |
---|
678 | try: |
---|
679 | self.context.addCourseTicket(ticket) |
---|
680 | except KeyError: |
---|
681 | self.flash('The ticket exists.') |
---|
682 | return |
---|
683 | self.flash('Successfully added %s.' % ticket.code) |
---|
684 | self.redirect(self.url(self.context, u'@@manage')+'#tab-2') |
---|
685 | return |
---|
686 | |
---|
687 | @grok.action('Cancel') |
---|
688 | def cancel(self, **data): |
---|
689 | self.redirect(self.url(self.context)) |
---|
690 | |
---|
691 | class CourseTicketDisplayFormPage(WAeUPDisplayFormPage): |
---|
692 | """ Page to display course tickets |
---|
693 | """ |
---|
694 | grok.context(ICourseTicket) |
---|
695 | grok.name('index') |
---|
696 | grok.require('waeup.viewStudent') |
---|
697 | form_fields = grok.AutoFields(ICourseTicket) |
---|
698 | grok.template('courseticketpage') |
---|
699 | pnav = 4 |
---|
700 | |
---|
701 | @property |
---|
702 | def title(self): |
---|
703 | return 'Course Ticket %s' % self.context.code |
---|
704 | |
---|
705 | @property |
---|
706 | def label(self): |
---|
707 | return '%s: Course Ticket %s' % ( |
---|
708 | self.context.getStudent().name,self.context.code) |
---|
709 | |
---|
710 | class CourseTicketManageActionButton(ManageActionButton): |
---|
711 | grok.order(1) |
---|
712 | grok.context(ICourseTicket) |
---|
713 | grok.view(CourseTicketDisplayFormPage) |
---|
714 | grok.require('waeup.manageStudents') |
---|
715 | text = 'Manage' |
---|
716 | target = 'manage' |
---|
717 | |
---|
718 | class CourseTicketManageFormPage(WAeUPEditFormPage): |
---|
719 | """ Page to manage course tickets |
---|
720 | """ |
---|
721 | grok.context(ICourseTicket) |
---|
722 | grok.name('manage') |
---|
723 | grok.require('waeup.manageStudents') |
---|
724 | form_fields = grok.AutoFields(ICourseTicket) |
---|
725 | grok.template('courseticketmanagepage') |
---|
726 | pnav = 4 |
---|
727 | |
---|
728 | @property |
---|
729 | def title(self): |
---|
730 | return 'Course Ticket %s' % self.context.code |
---|
731 | |
---|
732 | @property |
---|
733 | def label(self): |
---|
734 | return 'Manage course ticket %s' % self.context.code |
---|
735 | |
---|
736 | @grok.action('Save') |
---|
737 | def save(self, **data): |
---|
738 | msave(self, **data) |
---|
739 | return |
---|
740 | |
---|
741 | class PaymentsDisplayFormPage(WAeUPDisplayFormPage): |
---|
742 | """ Page to display the student payments |
---|
743 | """ |
---|
744 | grok.context(IStudentPayments) |
---|
745 | grok.name('index') |
---|
746 | grok.require('waeup.viewStudent') |
---|
747 | form_fields = grok.AutoFields(IStudentPayments) |
---|
748 | #grok.template('paymentspage') |
---|
749 | title = 'Payments' |
---|
750 | pnav = 4 |
---|
751 | |
---|
752 | @property |
---|
753 | def label(self): |
---|
754 | return '%s: Payments' % self.context.__parent__.name |
---|
755 | |
---|
756 | class AccommodationDisplayFormPage(WAeUPDisplayFormPage): |
---|
757 | """ Page to display the student accommodation data |
---|
758 | """ |
---|
759 | grok.context(IStudentAccommodation) |
---|
760 | grok.name('index') |
---|
761 | grok.require('waeup.viewStudent') |
---|
762 | form_fields = grok.AutoFields(IStudentAccommodation) |
---|
763 | #grok.template('accommodationpage') |
---|
764 | title = 'Accommodation' |
---|
765 | pnav = 4 |
---|
766 | |
---|
767 | @property |
---|
768 | def label(self): |
---|
769 | return '%s: Accommodation Data' % self.context.__parent__.name |
---|
770 | |
---|
771 | class StudentHistoryPage(WAeUPPage): |
---|
772 | """ Page to display student clearance data |
---|
773 | """ |
---|
774 | grok.context(IStudent) |
---|
775 | grok.name('history') |
---|
776 | grok.require('waeup.viewStudent') |
---|
777 | grok.template('studenthistory') |
---|
778 | title = 'History' |
---|
779 | pnav = 4 |
---|
780 | |
---|
781 | @property |
---|
782 | def label(self): |
---|
783 | return '%s: History' % self.context.name |
---|
784 | |
---|
785 | # Pages for students only |
---|
786 | |
---|
787 | class StudentBaseEditActionButton(ManageActionButton): |
---|
788 | grok.order(1) |
---|
789 | grok.context(IStudent) |
---|
790 | grok.view(StudentBaseDisplayFormPage) |
---|
791 | grok.require('waeup.handleStudent') |
---|
792 | text = 'Change password' |
---|
793 | target = 'bedit' |
---|
794 | |
---|
795 | class StudentPasswordSetting(grok.Adapter): |
---|
796 | """Adapt IStudent to data needed for password settings. |
---|
797 | |
---|
798 | We provide password getters/setters for the attached context (an |
---|
799 | IStudent object) that cooperate seamless with the usual |
---|
800 | formlib/form techniques. |
---|
801 | """ |
---|
802 | grok.context(IStudent) |
---|
803 | grok.provides(IStudentPasswordSetting) |
---|
804 | |
---|
805 | def __init__(self, context): |
---|
806 | self.name = context.name |
---|
807 | self.password_repeat = context.password |
---|
808 | self.context = context |
---|
809 | return |
---|
810 | |
---|
811 | def getPassword(self): |
---|
812 | return self.context.password |
---|
813 | |
---|
814 | def setPassword(self, password): |
---|
815 | IUserAccount(self.context).setPassword(password) |
---|
816 | return |
---|
817 | |
---|
818 | password = property(getPassword, setPassword) |
---|
819 | |
---|
820 | class StudentBaseEditFormPage(WAeUPEditFormPage): |
---|
821 | """ View to edit student base data by student |
---|
822 | """ |
---|
823 | grok.context(IStudent) |
---|
824 | grok.name('bedit') |
---|
825 | grok.require('waeup.handleStudent') |
---|
826 | #form_fields = grok.AutoFields(IStudentBaseEdit).omit( |
---|
827 | # 'student_id', 'reg_number', 'matric_number') |
---|
828 | form_fields = grok.AutoFields(IStudentPasswordSetting) |
---|
829 | grok.template('baseeditpage') |
---|
830 | label = 'Change password' |
---|
831 | title = 'Base Data' |
---|
832 | pnav = 4 |
---|
833 | |
---|
834 | def update(self): |
---|
835 | super(StudentBaseEditFormPage, self).update() |
---|
836 | self.wf_info = IWorkflowInfo(self.context) |
---|
837 | return |
---|
838 | |
---|
839 | def onFailure(self, action, data, errors): |
---|
840 | new_status = [] |
---|
841 | other_errors = False |
---|
842 | for error in errors: |
---|
843 | msg = getattr(error, 'message', '') |
---|
844 | if isinstance(msg, basestring) and msg != '': |
---|
845 | new_status.append(msg) |
---|
846 | else: |
---|
847 | other_errors = True |
---|
848 | if other_errors: |
---|
849 | if new_status: |
---|
850 | new_status.append('see below for further errors') |
---|
851 | else: |
---|
852 | new_status.append('See below for details.') |
---|
853 | if new_status: |
---|
854 | self.status = u'There were errors: %s' % ', '.join(new_status) |
---|
855 | return |
---|
856 | |
---|
857 | @grok.action('Save', failure=onFailure) |
---|
858 | def save(self, **data): |
---|
859 | self.applyData(self.context, **data) |
---|
860 | self.flash('Form has been saved.') |
---|
861 | return |
---|
862 | |
---|
863 | class StudentClearanceStartActionButton(ManageActionButton): |
---|
864 | grok.order(1) |
---|
865 | grok.context(IStudent) |
---|
866 | grok.view(StudentClearanceDisplayFormPage) |
---|
867 | grok.require('waeup.handleStudent') |
---|
868 | icon = 'actionicon_start.png' |
---|
869 | text = 'Start clearance' |
---|
870 | target = 'start_clearance' |
---|
871 | |
---|
872 | @property |
---|
873 | def target_url(self): |
---|
874 | if self.context.state != 'admitted': |
---|
875 | return '' |
---|
876 | return self.view.url(self.view.context, self.target) |
---|
877 | |
---|
878 | class StartClearancePage(WAeUPPage): |
---|
879 | grok.context(IStudent) |
---|
880 | grok.name('start_clearance') |
---|
881 | grok.require('waeup.handleStudent') |
---|
882 | grok.template('enterpin') |
---|
883 | title = 'Start clearance' |
---|
884 | label = 'Start clearance' |
---|
885 | ac_prefix = 'CLR' |
---|
886 | notice = '' |
---|
887 | pnav = 4 |
---|
888 | buttonname = 'Start clearance now' |
---|
889 | |
---|
890 | def update(self, SUBMIT=None): |
---|
891 | self.ac_series = self.request.form.get('ac_series', None) |
---|
892 | self.ac_number = self.request.form.get('ac_number', None) |
---|
893 | |
---|
894 | if SUBMIT is None: |
---|
895 | return |
---|
896 | pin = '%s-%s-%s' % (self.ac_prefix, self.ac_series, self.ac_number) |
---|
897 | code = get_access_code(pin) |
---|
898 | if not code: |
---|
899 | self.flash('Access code is invalid.') |
---|
900 | return |
---|
901 | # Mark pin as used (this also fires a pin related transition) |
---|
902 | # and fire transition start_clearance |
---|
903 | if code.state == USED: |
---|
904 | self.flash('Access code has already been used.') |
---|
905 | return |
---|
906 | else: |
---|
907 | comment = u"AC invalidated for %s" % self.context.student_id |
---|
908 | # Here we know that the ac is in state initialized so we do not |
---|
909 | # expect an exception |
---|
910 | invalidate_accesscode(pin,comment) |
---|
911 | self.context.clr_code = pin |
---|
912 | IWorkflowInfo(self.context).fireTransition('start_clearance') |
---|
913 | self.flash('Clearance process has been started.') |
---|
914 | self.redirect(self.url(self.context,'cedit')) |
---|
915 | return |
---|
916 | |
---|
917 | class StudentClearanceEditActionButton(ManageActionButton): |
---|
918 | grok.order(1) |
---|
919 | grok.context(IStudent) |
---|
920 | grok.view(StudentClearanceDisplayFormPage) |
---|
921 | grok.require('waeup.handleStudent') |
---|
922 | text = 'Edit' |
---|
923 | target = 'cedit' |
---|
924 | |
---|
925 | @property |
---|
926 | def target_url(self): |
---|
927 | if self.context.clearance_locked: |
---|
928 | return '' |
---|
929 | return self.view.url(self.view.context, self.target) |
---|
930 | |
---|
931 | class StudentClearanceEditFormPage(StudentClearanceManageFormPage): |
---|
932 | """ View to edit student clearance data by student |
---|
933 | """ |
---|
934 | grok.context(IStudent) |
---|
935 | grok.name('cedit') |
---|
936 | grok.require('waeup.handleStudent') |
---|
937 | form_fields = grok.AutoFields( |
---|
938 | IStudentClearanceEdit).omit('clearance_locked') |
---|
939 | label = 'Edit clearance data' |
---|
940 | title = 'Clearance Data' |
---|
941 | pnav = 4 |
---|
942 | form_fields['date_of_birth'].custom_widget = FriendlyDateWidget('le-year') |
---|
943 | |
---|
944 | def emitLockMessage(self): |
---|
945 | self.flash('The requested form is locked (read-only).') |
---|
946 | self.redirect(self.url(self.context)) |
---|
947 | return |
---|
948 | |
---|
949 | def update(self): |
---|
950 | if self.context.clearance_locked: |
---|
951 | self.emitLockMessage() |
---|
952 | return |
---|
953 | datepicker.need() |
---|
954 | return super(StudentClearanceEditFormPage, self).update() |
---|
955 | |
---|
956 | @grok.action('Save') |
---|
957 | def save(self, **data): |
---|
958 | self.applyData(self.context, **data) |
---|
959 | self.flash('Clearance form has been saved.') |
---|
960 | return |
---|
961 | |
---|
962 | @grok.action('Save and request clearance') |
---|
963 | def requestclearance(self, **data): |
---|
964 | self.applyData(self.context, **data) |
---|
965 | self.context._p_changed = True |
---|
966 | #if self.dataNotComplete(): |
---|
967 | # self.flash(self.dataNotComplete()) |
---|
968 | # return |
---|
969 | self.flash('Clearance form has been saved.') |
---|
970 | self.redirect(self.url(self.context,'request_clearance')) |
---|
971 | return |
---|
972 | |
---|
973 | class RequestClearancePage(WAeUPPage): |
---|
974 | grok.context(IStudent) |
---|
975 | grok.name('request_clearance') |
---|
976 | grok.require('waeup.handleStudent') |
---|
977 | grok.template('enterpin') |
---|
978 | title = 'Request clearance' |
---|
979 | label = 'Request clearance' |
---|
980 | notice = 'Enter the CLR access code used for starting clearance.' |
---|
981 | ac_prefix = 'CLR' |
---|
982 | pnav = 4 |
---|
983 | buttonname = 'Request clearance now' |
---|
984 | |
---|
985 | def update(self, SUBMIT=None): |
---|
986 | self.ac_series = self.request.form.get('ac_series', None) |
---|
987 | self.ac_number = self.request.form.get('ac_number', None) |
---|
988 | if SUBMIT is None: |
---|
989 | return |
---|
990 | pin = '%s-%s-%s' % (self.ac_prefix, self.ac_series, self.ac_number) |
---|
991 | if self.context.clr_code != pin: |
---|
992 | self.flash("This isn't your CLR access code.") |
---|
993 | return |
---|
994 | state = IWorkflowState(self.context).getState() |
---|
995 | # This shouldn't happen, but the application officer |
---|
996 | # might have forgotten to lock the form after changing the state |
---|
997 | if state != CLEARANCE: |
---|
998 | self.flash('This form cannot be submitted. Wrong state!') |
---|
999 | return |
---|
1000 | IWorkflowInfo(self.context).fireTransition('request_clearance') |
---|
1001 | self.flash('Clearance has been requested.') |
---|
1002 | self.redirect(self.url(self.context)) |
---|
1003 | return |
---|