1 | ## |
---|
2 | ## browser.py |
---|
3 | ## Login : <uli@pu.smp.net> |
---|
4 | ## Started on Sun Jun 27 11:03:10 2010 Uli Fouquet |
---|
5 | ## $Id$ |
---|
6 | ## |
---|
7 | ## Copyright (C) 2010 Uli Fouquet |
---|
8 | ## This program is free software; you can redistribute it and/or modify |
---|
9 | ## it under the terms of the GNU General Public License as published by |
---|
10 | ## the Free Software Foundation; either version 2 of the License, or |
---|
11 | ## (at your option) any later version. |
---|
12 | ## |
---|
13 | ## This program is distributed in the hope that it will be useful, |
---|
14 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | ## GNU General Public License for more details. |
---|
17 | ## |
---|
18 | ## You should have received a copy of the GNU General Public License |
---|
19 | ## along with this program; if not, write to the Free Software |
---|
20 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
21 | ## |
---|
22 | """UI components for basic applicants and related components. |
---|
23 | """ |
---|
24 | import grok |
---|
25 | |
---|
26 | from datetime import datetime |
---|
27 | from hurry.jquery import jquery |
---|
28 | from hurry.jqueryui import jqueryui |
---|
29 | from zope.component import getUtility, getAllUtilitiesRegisteredFor |
---|
30 | from zope.formlib.widgets import FileWidget, DateWidget |
---|
31 | from zope.securitypolicy.interfaces import IPrincipalRoleManager |
---|
32 | from waeup.sirp.browser import ( |
---|
33 | WAeUPPage, WAeUPEditFormPage, WAeUPAddFormPage, |
---|
34 | WAeUPDisplayFormPage, NullValidator) |
---|
35 | from waeup.sirp.browser.pages import LoginPage |
---|
36 | from waeup.sirp.interfaces import IWAeUPObject |
---|
37 | from waeup.sirp.browser.resources import datepicker, tabs, datatable |
---|
38 | from waeup.sirp.browser.viewlets import ( |
---|
39 | AddActionButton, ManageActionButton, PrimaryNavTab, |
---|
40 | ) |
---|
41 | from waeup.sirp.browser.breadcrumbs import Breadcrumb |
---|
42 | from waeup.sirp.applicants import get_applicant_data, ResultEntry, Applicant |
---|
43 | from waeup.sirp.applicants.interfaces import ( |
---|
44 | IApplicant, IApplicantPrincipal, IApplicantPDEEditData, |
---|
45 | IApplicantsRoot, IApplicantsContainer, IApplicantsContainerProvider, |
---|
46 | ) |
---|
47 | from waeup.sirp.widgets.passportwidget import ( |
---|
48 | PassportWidget, PassportDisplayWidget |
---|
49 | ) |
---|
50 | #from zope.formlib.objectwidget import ObjectWidget |
---|
51 | from zope.formlib.sequencewidget import ListSequenceWidget, SequenceDisplayWidget |
---|
52 | from zope.formlib.widget import CustomWidgetFactory |
---|
53 | from waeup.sirp.utils.helpers import ReST2HTML |
---|
54 | from waeup.sirp.widgets.objectwidget import ( |
---|
55 | WAeUPObjectWidget, WAeUPObjectDisplayWidget) |
---|
56 | from waeup.sirp.widgets.multilistwidget import ( |
---|
57 | MultiListWidget, MultiListDisplayWidget) |
---|
58 | from waeup.sirp.image.browser.widget import ( |
---|
59 | ThumbnailWidget, EncodingImageFileWidget, |
---|
60 | ) |
---|
61 | |
---|
62 | results_widget = CustomWidgetFactory( |
---|
63 | WAeUPObjectWidget, ResultEntry) |
---|
64 | |
---|
65 | results_display_widget = CustomWidgetFactory( |
---|
66 | WAeUPObjectDisplayWidget, ResultEntry) |
---|
67 | |
---|
68 | #list_results_widget = CustomWidgetFactory( |
---|
69 | # ListSequenceWidget, subwidget=results_widget) |
---|
70 | |
---|
71 | list_results_widget = CustomWidgetFactory( |
---|
72 | MultiListWidget, subwidget=results_widget) |
---|
73 | |
---|
74 | list_results_display_widget = CustomWidgetFactory( |
---|
75 | MultiListDisplayWidget, subwidget=results_display_widget) |
---|
76 | |
---|
77 | #: A date widget that renders with CSS class `datepicker` thus |
---|
78 | # enabling : the jQuery datepicker for this field if the form loaded |
---|
79 | # the jQuery code. |
---|
80 | FriendlyDateWidget = CustomWidgetFactory( |
---|
81 | DateWidget, cssClass='datepicker') |
---|
82 | |
---|
83 | class ApplicationsPage(WAeUPPage): |
---|
84 | grok.context(IApplicantsRoot) |
---|
85 | grok.name('index') |
---|
86 | title = 'Applicants' |
---|
87 | pnav = 3 |
---|
88 | |
---|
89 | def update(self): |
---|
90 | super(ApplicationsPage, self).update() |
---|
91 | datatable.need() |
---|
92 | return |
---|
93 | |
---|
94 | def getApplications(self): |
---|
95 | """Get a list of all stored applicant containers. |
---|
96 | """ |
---|
97 | for key, val in self.context.items(): |
---|
98 | url = self.url(val) |
---|
99 | yield(dict(url=url, name=key, container=val)) |
---|
100 | |
---|
101 | class ManageApplicantsRootActionButton(ManageActionButton): |
---|
102 | grok.context(IApplicantsRoot) |
---|
103 | grok.view(ApplicationsPage) |
---|
104 | text = 'Manage applicant containers' |
---|
105 | |
---|
106 | class ApplicantsRootEditPage(WAeUPPage): |
---|
107 | grok.context(IApplicantsRoot) |
---|
108 | grok.name('manage') |
---|
109 | grok.template('applicantsrooteditpage') |
---|
110 | title = 'Edit applicants containers' |
---|
111 | pnav = 3 |
---|
112 | |
---|
113 | def update(self, entries=None, DELETE=None, CANCEL=None): |
---|
114 | if CANCEL is not None: |
---|
115 | self.redirect(self.url(self.context)) |
---|
116 | return |
---|
117 | if DELETE is None: |
---|
118 | return |
---|
119 | if entries is None: |
---|
120 | return |
---|
121 | if not isinstance(entries, list): |
---|
122 | entries = [entries] |
---|
123 | for name in entries: |
---|
124 | del self.context[name] |
---|
125 | self.flash('Deleted "%s"' % name) |
---|
126 | return |
---|
127 | |
---|
128 | def getApplications(self): |
---|
129 | """Get a list of all stored applicant containers. |
---|
130 | """ |
---|
131 | for key, val in self.context.items(): |
---|
132 | url = self.url(val) |
---|
133 | yield(dict(url=url, name=key)) |
---|
134 | |
---|
135 | class AddApplicantsContainerActionButton(AddActionButton): |
---|
136 | grok.context(IApplicantsRoot) |
---|
137 | grok.view(ApplicantsRootEditPage) |
---|
138 | text = 'Add applicants container' |
---|
139 | |
---|
140 | class AddApplicantsContainer(WAeUPPage): |
---|
141 | grok.context(IApplicantsRoot) |
---|
142 | grok.name('add') |
---|
143 | grok.template('addcontainer') |
---|
144 | title = 'Add applicants container' |
---|
145 | pnav = 3 |
---|
146 | |
---|
147 | def update(self, providername=None, name=None, title=None, |
---|
148 | description=None, ADD=None, CANCEL=None): |
---|
149 | if CANCEL is not None: |
---|
150 | self.redirect(self.url(self.context)) |
---|
151 | return |
---|
152 | if ADD is None: |
---|
153 | return |
---|
154 | if not name: |
---|
155 | self.flash('Error: you must give a name') |
---|
156 | return |
---|
157 | if name in self.context.keys(): |
---|
158 | self.flash('A container of the given name already exists') |
---|
159 | return |
---|
160 | # Add new applicants container... |
---|
161 | provider = getUtility(IApplicantsContainerProvider, |
---|
162 | name=providername) |
---|
163 | container = provider.factory() |
---|
164 | if title: |
---|
165 | container.title = title |
---|
166 | if description: |
---|
167 | container.description = description |
---|
168 | self.context[name] = container |
---|
169 | self.flash('Added "%s".' % name) |
---|
170 | self.redirect(self.url(self.context)) |
---|
171 | return |
---|
172 | |
---|
173 | def getContainerProviders(self): |
---|
174 | """Get a list of applicants container providers. |
---|
175 | |
---|
176 | Applicants container providers are named utilities that help |
---|
177 | to create applicants containers of different types |
---|
178 | (JAMB-based, non-JAMB-based, etc.). |
---|
179 | |
---|
180 | The list returned contains dicts:: |
---|
181 | |
---|
182 | {'name': <utility_name>, |
---|
183 | 'provider': <provider instance>} |
---|
184 | |
---|
185 | where `utility_name` is the name under which the respective |
---|
186 | provider utility is registered and `provider` is the real |
---|
187 | provider instance. |
---|
188 | |
---|
189 | The `utility_name` can be used to lookup the utility anew (for |
---|
190 | instance after submitting a form) and the `provider` instance |
---|
191 | can be used to create new instances of the respective |
---|
192 | applicants container type. |
---|
193 | """ |
---|
194 | providers = getAllUtilitiesRegisteredFor(IApplicantsContainerProvider) |
---|
195 | result = [ |
---|
196 | {'name': getattr(x, 'grokcore.component.directive.name'), |
---|
197 | 'provider': x} |
---|
198 | for x in providers |
---|
199 | ] |
---|
200 | return result |
---|
201 | |
---|
202 | class ApplicantsRootBreadcrumb(Breadcrumb): |
---|
203 | """A breadcrumb for applicantsroot. |
---|
204 | """ |
---|
205 | grok.context(IApplicantsRoot) |
---|
206 | title = u'Applicants' |
---|
207 | |
---|
208 | class ApplicantsContainerBreadcrumb(Breadcrumb): |
---|
209 | """A breadcrumb for applicantscontainers. |
---|
210 | """ |
---|
211 | grok.context(IApplicantsContainer) |
---|
212 | |
---|
213 | class ApplicantsTab(PrimaryNavTab): |
---|
214 | """Faculties-tab in primary navigation. |
---|
215 | """ |
---|
216 | grok.context(IWAeUPObject) |
---|
217 | grok.order(3) |
---|
218 | grok.require('waeup.View') |
---|
219 | grok.template('primarynavtab') |
---|
220 | |
---|
221 | pnav = 3 |
---|
222 | tab_title = u'Applicants' |
---|
223 | |
---|
224 | @property |
---|
225 | def link_target(self): |
---|
226 | return self.view.application_url('applicants') |
---|
227 | |
---|
228 | class ApplicantsContainerPage(WAeUPDisplayFormPage): |
---|
229 | """The standard view for regular applicant containers. |
---|
230 | """ |
---|
231 | grok.context(IApplicantsContainer) |
---|
232 | grok.name('index') |
---|
233 | grok.template('applicantscontainerpage') |
---|
234 | pnav = 3 |
---|
235 | |
---|
236 | @property |
---|
237 | def title(self): |
---|
238 | return "Applicants Container: %s" % getattr( |
---|
239 | self.context, '__name__', 'unnamed') |
---|
240 | |
---|
241 | @property |
---|
242 | def label(self): |
---|
243 | return self.title |
---|
244 | |
---|
245 | def descriptionToHTML(self): |
---|
246 | if self.context.description: |
---|
247 | return ReST2HTML(self.context.description) |
---|
248 | else: |
---|
249 | return |
---|
250 | |
---|
251 | class ManageApplicantsContainerActionButton(ManageActionButton): |
---|
252 | grok.context(IApplicantsContainer) |
---|
253 | grok.view(ApplicantsContainerPage) |
---|
254 | text = 'Manage' |
---|
255 | |
---|
256 | |
---|
257 | class ManageApplicantsContainer(WAeUPEditFormPage): |
---|
258 | grok.context(IApplicantsContainer) |
---|
259 | grok.name('manage') |
---|
260 | grok.template('form_manage_applicants_container') |
---|
261 | form_fields = grok.AutoFields(IApplicantsContainer) |
---|
262 | # Use friendlier date widget... |
---|
263 | form_fields['startdate'].custom_widget = FriendlyDateWidget |
---|
264 | form_fields['enddate'].custom_widget = FriendlyDateWidget |
---|
265 | |
---|
266 | @property |
---|
267 | def title(self): |
---|
268 | return "Manage applicants container: %s" % getattr( |
---|
269 | self.context, '__name__', 'unnamed') |
---|
270 | |
---|
271 | @property |
---|
272 | def label(self): |
---|
273 | return self.title |
---|
274 | |
---|
275 | pnav = 3 |
---|
276 | |
---|
277 | def update(self): |
---|
278 | datepicker.need() # Enable jQuery datepicker in date fields. |
---|
279 | tabs.need() |
---|
280 | datatable.need() # Enable jQurey datatables for contents listing |
---|
281 | return super(ManageApplicantsContainer, self).update() |
---|
282 | |
---|
283 | @grok.action('Save') |
---|
284 | def apply(self, **data): |
---|
285 | self.applyData(self.context, **data) |
---|
286 | self.flash('Data saved.') |
---|
287 | return |
---|
288 | |
---|
289 | @grok.action('Back') |
---|
290 | def cancel(self, **data): |
---|
291 | self.redirect(self.url(self.context)) |
---|
292 | return |
---|
293 | |
---|
294 | class LoginApplicant(WAeUPPage): |
---|
295 | grok.context(IApplicantsContainer) |
---|
296 | grok.name('login') |
---|
297 | grok.require('zope.Public') |
---|
298 | |
---|
299 | title = u'Login' |
---|
300 | |
---|
301 | @property |
---|
302 | def label(self): |
---|
303 | return self.title |
---|
304 | |
---|
305 | pnav = 3 |
---|
306 | prefix = u'APP' |
---|
307 | |
---|
308 | def update(self, SUBMIT=None): |
---|
309 | self.ac_series = self.request.form.get('form.ac_series', None) |
---|
310 | self.ac_number = self.request.form.get('form.ac_number', None) |
---|
311 | if SUBMIT is None: |
---|
312 | return |
---|
313 | |
---|
314 | if self.request.principal.id == 'zope.anybody': |
---|
315 | self.flash('Entered credentials are invalid') |
---|
316 | return |
---|
317 | |
---|
318 | if not IApplicantPrincipal.providedBy(self.request.principal): |
---|
319 | # Don't care if user is already authenticated as non-applicant |
---|
320 | return |
---|
321 | |
---|
322 | pin = self.request.principal.access_code |
---|
323 | if pin not in self.context.keys(): |
---|
324 | # Create applicant record |
---|
325 | applicant = Applicant() |
---|
326 | applicant.access_code = pin |
---|
327 | self.context[pin] = applicant |
---|
328 | |
---|
329 | # Assign current principal the owner role on created applicant |
---|
330 | # record |
---|
331 | role_manager = IPrincipalRoleManager(self.context) |
---|
332 | role_manager.assignRoleToPrincipal( |
---|
333 | 'waeup.ApplicationOwner', self.request.principal.id) |
---|
334 | self.redirect(self.url(self.context[pin], 'edit')) |
---|
335 | return |
---|
336 | |
---|
337 | #class AddApplicant(WAeUPAddFormPage): |
---|
338 | # grok.context(IApplicantsContainer) |
---|
339 | # grok.name('add') |
---|
340 | # form_fields = grok.AutoFields(IApplicant) |
---|
341 | # form_fields['fst_sit_results'].custom_widget = list_results_widget |
---|
342 | # form_fields['passport'].custom_widget = EncodingImageFileWidget |
---|
343 | # label = 'Add Applicant' |
---|
344 | # title = 'Add Applicant' |
---|
345 | # pnav = 1 |
---|
346 | # |
---|
347 | # @grok.action('Add applicant') |
---|
348 | # def addApplicant(self, **data): |
---|
349 | # from waeup.sirp.jambtables.applicants import Applicant |
---|
350 | # applicant = Applicant() |
---|
351 | # self.applyData(applicant, **data) |
---|
352 | # # XXX: temporarily disabled. |
---|
353 | # #self.context[applicant.reg_no] = applicant |
---|
354 | # try: |
---|
355 | # self.context[applicant.access_code] = applicant |
---|
356 | # except KeyError: |
---|
357 | # self.flash('The given access code is already in use!') |
---|
358 | # return |
---|
359 | # self.redirect(self.url(self.context)) |
---|
360 | |
---|
361 | class DisplayApplicant(WAeUPDisplayFormPage): |
---|
362 | grok.context(IApplicant) |
---|
363 | grok.name('index') |
---|
364 | grok.require('waeup.viewApplication') |
---|
365 | form_fields = grok.AutoFields(IApplicant).omit('locked') |
---|
366 | form_fields['fst_sit_results'].custom_widget = list_results_display_widget |
---|
367 | form_fields['passport'].custom_widget = ThumbnailWidget |
---|
368 | label = 'Applicant' |
---|
369 | title = 'Applicant' |
---|
370 | pnav = 3 |
---|
371 | |
---|
372 | class EditApplicantStudent(WAeUPEditFormPage): |
---|
373 | """An applicant-centered edit view for applicant data. |
---|
374 | """ |
---|
375 | grok.context(IApplicant) |
---|
376 | grok.name('edit') |
---|
377 | grok.require('waeup.editApplication') |
---|
378 | form_fields = grok.AutoFields(IApplicantPDEEditData).omit('locked') |
---|
379 | form_fields['passport'].custom_widget = EncodingImageFileWidget |
---|
380 | form_fields['date_of_birth'].custom_widget = FriendlyDateWidget |
---|
381 | grok.template('form_edit_pde') |
---|
382 | |
---|
383 | def emitLockMessage(self): |
---|
384 | self.flash('The requested form is locked (read-only)') |
---|
385 | self.redirect(self.url(self.context)) |
---|
386 | return |
---|
387 | |
---|
388 | def update(self): |
---|
389 | if self.context.locked: |
---|
390 | self.emitLockMessage() |
---|
391 | return |
---|
392 | datepicker.need() # Enable jQuery datepicker in date fields. |
---|
393 | super(EditApplicantStudent, self).update() |
---|
394 | return |
---|
395 | |
---|
396 | def filteredWidgets(self): |
---|
397 | for widget in self.widgets: |
---|
398 | if widget.name == 'form.confirm_passport': |
---|
399 | continue |
---|
400 | yield widget |
---|
401 | |
---|
402 | @property |
---|
403 | def label(self): |
---|
404 | # XXX: Use current/upcoming session |
---|
405 | return 'Apply for Post UDE Screening Test (2009/2010)' |
---|
406 | title = 'Edit Application' |
---|
407 | pnav = 3 |
---|
408 | |
---|
409 | @grok.action('Save') |
---|
410 | def save(self, **data): |
---|
411 | if self.context.locked: |
---|
412 | self.emitLockMessage() |
---|
413 | return |
---|
414 | self.applyData(self.context, **data) |
---|
415 | self.context._p_changed = True |
---|
416 | return |
---|
417 | |
---|
418 | @grok.action('Final Submit') |
---|
419 | def finalsubmit(self, **data): |
---|
420 | if self.context.locked: |
---|
421 | self.emitLockMessage() |
---|
422 | return |
---|
423 | self.applyData(self.context, **data) |
---|
424 | self.context._p_changed = True |
---|
425 | if not self.dataComplete(): |
---|
426 | self.flash('Data yet not complete.') |
---|
427 | return |
---|
428 | self.context.locked = True |
---|
429 | return |
---|
430 | |
---|
431 | def dataComplete(self): |
---|
432 | if context.confirm_passport is not True: |
---|
433 | return False |
---|
434 | if len(self.errors) > 0: |
---|
435 | return False |
---|
436 | return True |
---|
437 | |
---|
438 | class EditApplicantFull(WAeUPEditFormPage): |
---|
439 | """A full edit view for applicant data. |
---|
440 | |
---|
441 | This one is meant to be used by officers only. |
---|
442 | """ |
---|
443 | grok.context(IApplicant) |
---|
444 | grok.name('edit_full') |
---|
445 | grok.require('waeup.editFullApplication') |
---|
446 | form_fields = grok.AutoFields(IApplicantPDEEditData).omit('locked') |
---|
447 | form_fields['passport'].custom_widget = EncodingImageFileWidget |
---|
448 | form_fields['date_of_birth'].custom_widget = FriendlyDateWidget |
---|
449 | grok.template('form_edit_full') |
---|
450 | |
---|
451 | def update(self): |
---|
452 | if self.context.locked: |
---|
453 | self.emitLockMessage() |
---|
454 | return |
---|
455 | datepicker.need() # Enable jQuery datepicker in date fields. |
---|
456 | super(EditApplicantFull, self).update() |
---|
457 | return |
---|
458 | |
---|
459 | def filteredWidgets(self): |
---|
460 | for widget in self.widgets: |
---|
461 | if widget.name == 'form.confirm_passport': |
---|
462 | continue |
---|
463 | yield widget |
---|
464 | |
---|
465 | @property |
---|
466 | def label(self): |
---|
467 | return 'Application for %s' % self.context.access_code |
---|
468 | title = 'Edit Application' |
---|
469 | pnav = 3 |
---|
470 | |
---|
471 | @grok.action('Save') |
---|
472 | def save(self, **data): |
---|
473 | self.applyData(self.context, **data) |
---|
474 | self.context._p_changed = True |
---|
475 | return |
---|