[5273] | 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 JAMB tables. |
---|
| 23 | """ |
---|
| 24 | import grok |
---|
| 25 | |
---|
| 26 | from waeup.sirp.browser import ( |
---|
| 27 | WAeUPPage, WAeUPEditFormPage, WAeUPAddFormPage, |
---|
| 28 | WAeUPDisplayFormPage, NullValidator) |
---|
[5442] | 29 | from waeup.sirp.browser.pages import LoginPage |
---|
[5320] | 30 | from waeup.sirp.interfaces import IWAeUPObject |
---|
[5273] | 31 | from waeup.sirp.jambtables import JAMBDataTable |
---|
[5325] | 32 | from waeup.sirp.jambtables.util import get_applicant_data |
---|
[5453] | 33 | from waeup.sirp.jambtables.interfaces import ( |
---|
[5484] | 34 | IApplicant, IApplicantContainer, IApplicantPrincipal, IApplicantPDEEditData) |
---|
[5453] | 35 | |
---|
[5273] | 36 | |
---|
| 37 | #from zope.formlib.objectwidget import ObjectWidget |
---|
| 38 | from zope.formlib.sequencewidget import ListSequenceWidget, SequenceDisplayWidget |
---|
| 39 | from zope.formlib.widget import CustomWidgetFactory |
---|
| 40 | from waeup.sirp.jambtables.applicants import ResultEntry |
---|
[5303] | 41 | from waeup.sirp.widgets.objectwidget import ( |
---|
[5301] | 42 | WAeUPObjectWidget, WAeUPObjectDisplayWidget) |
---|
[5303] | 43 | from waeup.sirp.widgets.multilistwidget import ( |
---|
[5273] | 44 | MultiListWidget, MultiListDisplayWidget) |
---|
[5320] | 45 | |
---|
[5273] | 46 | results_widget = CustomWidgetFactory( |
---|
[5301] | 47 | WAeUPObjectWidget, ResultEntry) |
---|
[5273] | 48 | |
---|
| 49 | results_display_widget = CustomWidgetFactory( |
---|
[5301] | 50 | WAeUPObjectDisplayWidget, ResultEntry) |
---|
[5273] | 51 | |
---|
| 52 | #list_results_widget = CustomWidgetFactory( |
---|
| 53 | # ListSequenceWidget, subwidget=results_widget) |
---|
| 54 | |
---|
| 55 | list_results_widget = CustomWidgetFactory( |
---|
| 56 | MultiListWidget, subwidget=results_widget) |
---|
| 57 | |
---|
| 58 | list_results_display_widget = CustomWidgetFactory( |
---|
| 59 | MultiListDisplayWidget, subwidget=results_display_widget) |
---|
| 60 | |
---|
| 61 | class ApplicationsPage(WAeUPPage): |
---|
| 62 | grok.context(IApplicantContainer) |
---|
| 63 | grok.name('index') |
---|
| 64 | title = 'Applications' |
---|
| 65 | pnav = 1 |
---|
| 66 | |
---|
| 67 | def getApplications(self): |
---|
| 68 | """Get a list of all stored applications. |
---|
| 69 | """ |
---|
| 70 | for key, val in self.context.items(): |
---|
| 71 | url = self.url(val) |
---|
| 72 | yield(dict(url=url, name=key)) |
---|
| 73 | |
---|
| 74 | class AddApplicant(WAeUPAddFormPage): |
---|
| 75 | grok.context(IApplicantContainer) |
---|
| 76 | grok.name('add') |
---|
| 77 | form_fields = grok.AutoFields(IApplicant) |
---|
| 78 | form_fields['fst_sit_results'].custom_widget = list_results_widget |
---|
| 79 | label = 'Add Applicant' |
---|
| 80 | title = 'Add Applicant' |
---|
| 81 | pnav = 1 |
---|
| 82 | |
---|
| 83 | @grok.action('Add applicant') |
---|
| 84 | def addApplicant(self, **data): |
---|
| 85 | from waeup.sirp.jambtables.applicants import Applicant |
---|
| 86 | applicant = Applicant() |
---|
| 87 | self.applyData(applicant, **data) |
---|
[5453] | 88 | # XXX: temporarily disabled. |
---|
| 89 | #self.context[applicant.reg_no] = applicant |
---|
| 90 | try: |
---|
| 91 | self.context[applicant.access_code] = applicant |
---|
| 92 | except KeyError: |
---|
| 93 | self.flash('The given access code is already in use!') |
---|
| 94 | return |
---|
[5273] | 95 | self.redirect(self.url(self.context)) |
---|
| 96 | |
---|
| 97 | class DisplayApplicant(WAeUPDisplayFormPage): |
---|
| 98 | grok.context(IApplicant) |
---|
| 99 | grok.name('index') |
---|
| 100 | form_fields = grok.AutoFields(IApplicant) |
---|
| 101 | form_fields['fst_sit_results'].custom_widget = list_results_display_widget |
---|
| 102 | label = 'Applicant' |
---|
| 103 | title = 'Applicant' |
---|
| 104 | pnav = 1 |
---|
| 105 | |
---|
| 106 | class EditApplicant(WAeUPEditFormPage): |
---|
| 107 | grok.context(IApplicant) |
---|
| 108 | grok.name('edit') |
---|
[5484] | 109 | form_fields = grok.AutoFields(IApplicantPDEEditData) |
---|
[5488] | 110 | grok.template('form_edit_pde') |
---|
[5484] | 111 | |
---|
| 112 | @property |
---|
| 113 | def label(self): |
---|
| 114 | # XXX: Use current/upcoming session |
---|
| 115 | return 'Apply for Post UDE Screening Test (2009/2010)' |
---|
[5273] | 116 | title = 'Edit Application' |
---|
| 117 | pnav = 1 |
---|
| 118 | |
---|
| 119 | @grok.action('Save') |
---|
| 120 | def save(self, **data): |
---|
| 121 | self.applyData(self.context, **data) |
---|
| 122 | self.context._p_changed = True |
---|
| 123 | return |
---|
| 124 | |
---|
[5484] | 125 | @grok.action('Final Submit') |
---|
| 126 | def finalsubmit(self, **data): |
---|
[5273] | 127 | self.applyData(self.context, **data) |
---|
[5484] | 128 | self.context._p_changed = True |
---|
| 129 | # XXX: Lock the form for editing... |
---|
[5273] | 130 | return |
---|
| 131 | |
---|
[5442] | 132 | class Login_PDE(LoginPage): |
---|
[5320] | 133 | grok.context(IWAeUPObject) |
---|
| 134 | grok.name('login_pde') |
---|
| 135 | |
---|
| 136 | title = 'PDE Login' |
---|
| 137 | pnav = 1 |
---|
| 138 | |
---|
[5453] | 139 | def update(self): |
---|
[5320] | 140 | """Validate credentials and redirect or show error. |
---|
| 141 | |
---|
[5453] | 142 | The real validation is done by an pluggable authentication |
---|
| 143 | utility (PAU). Here we only check, whether correct credentials |
---|
| 144 | were entered by looking up the principal status of the |
---|
| 145 | request: If the user authenticated successfully, we get an |
---|
| 146 | applicant principal. Otherwise we get the unauthenticated |
---|
| 147 | principal. |
---|
[5320] | 148 | """ |
---|
[5453] | 149 | formfields = ['form.ac_number', 'form.jamb_reg_no', |
---|
| 150 | 'form.ac_series', 'form.prefix',] |
---|
| 151 | |
---|
| 152 | self.reg_no = self.request.form.get('form.jamb_reg_no', '') |
---|
| 153 | self.ac_series = self.request.form.get('form.ac_series', '') |
---|
| 154 | self.ac_number = self.request.form.get('form.ac_number', '') |
---|
| 155 | |
---|
| 156 | for required_field in formfields: |
---|
| 157 | if required_field not in self.request.form.keys(): |
---|
[5320] | 158 | return |
---|
[5453] | 159 | self.reg_no = self.request.form['form.jamb_reg_no'] |
---|
| 160 | self.ac_series = self.request.form['form.ac_series'] |
---|
| 161 | self.ac_number = self.request.form['form.ac_number'] |
---|
| 162 | principal = self.request.principal |
---|
| 163 | if not IApplicantPrincipal.providedBy(principal): |
---|
| 164 | self.flash('You entered invalid credentials') |
---|
[5320] | 165 | return |
---|
[5453] | 166 | if hasattr(principal, 'reg_no'): |
---|
| 167 | if not principal.reg_no is None: |
---|
| 168 | |
---|
| 169 | site = grok.getSite() |
---|
| 170 | applications = site['applications'] |
---|
| 171 | application = applications[principal.reg_no] |
---|
| 172 | self.redirect(self.url(application, '@@edit')) |
---|
[5320] | 173 | return |
---|
| 174 | |
---|
| 175 | def getCurrentSession(self): |
---|
| 176 | """Get the current session. |
---|
| 177 | |
---|
| 178 | XXX: This should be computed or retrieved from elsewhere. |
---|
[5453] | 179 | |
---|
| 180 | `session` here means an academic session, not a browser |
---|
| 181 | session. |
---|
[5320] | 182 | """ |
---|
| 183 | return u'2010/2011' |
---|
| 184 | |
---|
| 185 | def getDeadline(self): |
---|
| 186 | """Get application submission deadline. |
---|
| 187 | |
---|
| 188 | XXX: This should be computed or retrieved from elsewhere. |
---|
| 189 | """ |
---|
| 190 | return u"""Application submission deadline is at Midnight on Friday, |
---|
| 191 | 01. October 2010. No application will be treated |
---|
| 192 | after the deadline.""" |
---|
| 193 | |
---|
| 194 | def getApplicantData(self, reg_no, ac): |
---|
| 195 | """Validate credentials and return applicant data. |
---|
| 196 | |
---|
| 197 | Returns tuple ``(<APPLICANT_ENTRY>, <ACCESSCODE>) on |
---|
| 198 | successful validation and ``None`` else. |
---|
| 199 | |
---|
| 200 | We expect a JAMB registration number and an access code in |
---|
| 201 | format ``PUDE-XXX-XXXXXXXXXX``. |
---|
[5325] | 202 | |
---|
| 203 | See |
---|
| 204 | :func:`waeup.sirp.jambtables.util.get_applicant_data` |
---|
| 205 | for details. |
---|
[5320] | 206 | """ |
---|
[5325] | 207 | return get_applicant_data(reg_no, ac) |
---|