[5273] | 1 | ## $Id: browser.py 8042 2012-04-04 21:28:16Z henrik $ |
---|
[6078] | 2 | ## |
---|
[7192] | 3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
[5273] | 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. |
---|
[6078] | 8 | ## |
---|
[5273] | 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. |
---|
[6078] | 13 | ## |
---|
[5273] | 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 | ## |
---|
[5824] | 18 | """UI components for basic applicants and related components. |
---|
[5273] | 19 | """ |
---|
[7063] | 20 | import os |
---|
[6082] | 21 | import sys |
---|
[5273] | 22 | import grok |
---|
[7250] | 23 | from time import time |
---|
[7370] | 24 | from datetime import datetime, date |
---|
[8042] | 25 | from zope.event import notify |
---|
[7392] | 26 | from zope.component import getUtility, createObject, getAdapter |
---|
[8033] | 27 | from zope.catalog.interfaces import ICatalog |
---|
[7714] | 28 | from zope.i18n import translate |
---|
[7322] | 29 | from hurry.workflow.interfaces import ( |
---|
| 30 | IWorkflowInfo, IWorkflowState, InvalidTransitionError) |
---|
[7811] | 31 | from waeup.kofa.applicants.interfaces import ( |
---|
[7363] | 32 | IApplicant, IApplicantEdit, IApplicantsRoot, |
---|
[7683] | 33 | IApplicantsContainer, IApplicantsContainerAdd, |
---|
[8033] | 34 | MAX_UPLOAD_SIZE, IApplicantOnlinePayment, IApplicantsUtils, |
---|
[8037] | 35 | IApplicantRegisterUpdate |
---|
[7363] | 36 | ) |
---|
[7811] | 37 | from waeup.kofa.applicants.workflow import INITIALIZED, STARTED, PAID, SUBMITTED |
---|
| 38 | from waeup.kofa.browser import ( |
---|
[7819] | 39 | KofaPage, KofaEditFormPage, KofaAddFormPage, KofaDisplayFormPage, |
---|
[7363] | 40 | DEFAULT_PASSPORT_IMAGE_PATH) |
---|
[7811] | 41 | from waeup.kofa.browser.interfaces import ICaptchaManager |
---|
| 42 | from waeup.kofa.browser.breadcrumbs import Breadcrumb |
---|
| 43 | from waeup.kofa.browser.layout import ( |
---|
[7459] | 44 | NullValidator, jsaction, action, UtilityView) |
---|
[7811] | 45 | from waeup.kofa.browser.pages import add_local_role, del_local_roles |
---|
| 46 | from waeup.kofa.browser.resources import datepicker, tabs, datatable, warning |
---|
| 47 | from waeup.kofa.interfaces import ( |
---|
[7819] | 48 | IKofaObject, ILocalRolesAssignable, IExtFileStore, IPDF, |
---|
| 49 | IFileStoreNameChooser, IPasswordValidator, IUserAccount, IKofaUtils) |
---|
[7811] | 50 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
| 51 | from waeup.kofa.permissions import get_users_with_local_roles |
---|
| 52 | from waeup.kofa.students.interfaces import IStudentsUtils |
---|
| 53 | from waeup.kofa.utils.helpers import string_from_bytes, file_size |
---|
| 54 | from waeup.kofa.widgets.datewidget import ( |
---|
[7250] | 55 | FriendlyDateWidget, FriendlyDateDisplayWidget, |
---|
| 56 | FriendlyDatetimeDisplayWidget) |
---|
[7811] | 57 | from waeup.kofa.widgets.phonewidget import PhoneWidget |
---|
| 58 | from waeup.kofa.widgets.restwidget import ReSTDisplayWidget |
---|
[5320] | 59 | |
---|
[7819] | 60 | grok.context(IKofaObject) # Make IKofaObject the default context |
---|
[5273] | 61 | |
---|
[7819] | 62 | class ApplicantsRootPage(KofaPage): |
---|
[5822] | 63 | grok.context(IApplicantsRoot) |
---|
| 64 | grok.name('index') |
---|
[6153] | 65 | grok.require('waeup.Public') |
---|
[7710] | 66 | label = _('Application Section') |
---|
[5843] | 67 | pnav = 3 |
---|
[6012] | 68 | |
---|
| 69 | def update(self): |
---|
[6067] | 70 | super(ApplicantsRootPage, self).update() |
---|
[7493] | 71 | #datatable.need() |
---|
[6012] | 72 | return |
---|
| 73 | |
---|
[7819] | 74 | class ApplicantsRootManageFormPage(KofaEditFormPage): |
---|
[5828] | 75 | grok.context(IApplicantsRoot) |
---|
| 76 | grok.name('manage') |
---|
[6107] | 77 | grok.template('applicantsrootmanagepage') |
---|
[7710] | 78 | label = _('Manage application section') |
---|
[5843] | 79 | pnav = 3 |
---|
[7136] | 80 | grok.require('waeup.manageApplication') |
---|
[7710] | 81 | taboneactions = [_('Add applicants container'), _('Remove selected'), |
---|
| 82 | _('Cancel')] |
---|
| 83 | tabtwoactions1 = [_('Remove selected local roles')] |
---|
| 84 | tabtwoactions2 = [_('Add local role')] |
---|
| 85 | subunits = _('Applicants Containers') |
---|
[6078] | 86 | |
---|
[6069] | 87 | def update(self): |
---|
| 88 | tabs.need() |
---|
[6108] | 89 | datatable.need() |
---|
[7330] | 90 | warning.need() |
---|
[6069] | 91 | return super(ApplicantsRootManageFormPage, self).update() |
---|
[5828] | 92 | |
---|
[6184] | 93 | def getLocalRoles(self): |
---|
| 94 | roles = ILocalRolesAssignable(self.context) |
---|
| 95 | return roles() |
---|
| 96 | |
---|
| 97 | def getUsers(self): |
---|
| 98 | """Get a list of all users. |
---|
| 99 | """ |
---|
| 100 | for key, val in grok.getSite()['users'].items(): |
---|
| 101 | url = self.url(val) |
---|
| 102 | yield(dict(url=url, name=key, val=val)) |
---|
| 103 | |
---|
| 104 | def getUsersWithLocalRoles(self): |
---|
| 105 | return get_users_with_local_roles(self.context) |
---|
| 106 | |
---|
[7710] | 107 | @jsaction(_('Remove selected')) |
---|
[6069] | 108 | def delApplicantsContainers(self, **data): |
---|
| 109 | form = self.request.form |
---|
| 110 | child_id = form['val_id'] |
---|
| 111 | if not isinstance(child_id, list): |
---|
| 112 | child_id = [child_id] |
---|
| 113 | deleted = [] |
---|
| 114 | for id in child_id: |
---|
| 115 | try: |
---|
| 116 | del self.context[id] |
---|
| 117 | deleted.append(id) |
---|
| 118 | except: |
---|
[7710] | 119 | self.flash(_('Could not delete:') + ' %s: %s: %s' % ( |
---|
[6069] | 120 | id, sys.exc_info()[0], sys.exc_info()[1])) |
---|
| 121 | if len(deleted): |
---|
[7738] | 122 | self.flash(_('Successfully removed: ${a}', |
---|
| 123 | mapping = {'a':', '.join(deleted)})) |
---|
[7484] | 124 | self.redirect(self.url(self.context, '@@manage')) |
---|
[6078] | 125 | return |
---|
[5828] | 126 | |
---|
[7710] | 127 | @action(_('Add applicants container'), validator=NullValidator) |
---|
[6069] | 128 | def addApplicantsContainer(self, **data): |
---|
| 129 | self.redirect(self.url(self.context, '@@add')) |
---|
[6078] | 130 | return |
---|
| 131 | |
---|
[7710] | 132 | @action(_('Cancel'), validator=NullValidator) |
---|
[6069] | 133 | def cancel(self, **data): |
---|
| 134 | self.redirect(self.url(self.context)) |
---|
[6078] | 135 | return |
---|
| 136 | |
---|
[7710] | 137 | @action(_('Add local role'), validator=NullValidator) |
---|
[6184] | 138 | def addLocalRole(self, **data): |
---|
[7484] | 139 | return add_local_role(self,3, **data) |
---|
[6184] | 140 | |
---|
[7710] | 141 | @action(_('Remove selected local roles')) |
---|
[6184] | 142 | def delLocalRoles(self, **data): |
---|
[7484] | 143 | return del_local_roles(self,3,**data) |
---|
[6184] | 144 | |
---|
[7819] | 145 | class ApplicantsContainerAddFormPage(KofaAddFormPage): |
---|
[5822] | 146 | grok.context(IApplicantsRoot) |
---|
[7136] | 147 | grok.require('waeup.manageApplication') |
---|
[5822] | 148 | grok.name('add') |
---|
[6107] | 149 | grok.template('applicantscontaineraddpage') |
---|
[7710] | 150 | label = _('Add applicants container') |
---|
[5843] | 151 | pnav = 3 |
---|
[6078] | 152 | |
---|
[6103] | 153 | form_fields = grok.AutoFields( |
---|
[7903] | 154 | IApplicantsContainerAdd).omit('code').omit('title') |
---|
[6083] | 155 | form_fields['startdate'].custom_widget = FriendlyDateWidget('le') |
---|
| 156 | form_fields['enddate'].custom_widget = FriendlyDateWidget('le') |
---|
[6078] | 157 | |
---|
[6083] | 158 | def update(self): |
---|
| 159 | datepicker.need() # Enable jQuery datepicker in date fields. |
---|
| 160 | return super(ApplicantsContainerAddFormPage, self).update() |
---|
| 161 | |
---|
[7710] | 162 | @action(_('Add applicants container')) |
---|
[6069] | 163 | def addApplicantsContainer(self, **data): |
---|
[6103] | 164 | year = data['year'] |
---|
| 165 | code = u'%s%s' % (data['prefix'], year) |
---|
[7844] | 166 | appcats_dict = getUtility(IApplicantsUtils).APP_TYPES_DICT |
---|
[7685] | 167 | title = appcats_dict[data['prefix']][0] |
---|
| 168 | title = u'%s %s/%s' % (title, year, year + 1) |
---|
[6087] | 169 | if code in self.context.keys(): |
---|
[6105] | 170 | self.flash( |
---|
[7710] | 171 | _('An applicants container for the same application type and entrance year exists already in the database.')) |
---|
[5822] | 172 | return |
---|
| 173 | # Add new applicants container... |
---|
[8009] | 174 | container = createObject(u'waeup.ApplicantsContainer') |
---|
[6069] | 175 | self.applyData(container, **data) |
---|
[6087] | 176 | container.code = code |
---|
| 177 | container.title = title |
---|
| 178 | self.context[code] = container |
---|
[7710] | 179 | self.flash(_('Added:') + ' "%s".' % code) |
---|
[7484] | 180 | self.redirect(self.url(self.context, u'@@manage')) |
---|
[5822] | 181 | return |
---|
[6078] | 182 | |
---|
[7710] | 183 | @action(_('Cancel'), validator=NullValidator) |
---|
[6069] | 184 | def cancel(self, **data): |
---|
[7484] | 185 | self.redirect(self.url(self.context, '@@manage')) |
---|
[6078] | 186 | |
---|
[5845] | 187 | class ApplicantsRootBreadcrumb(Breadcrumb): |
---|
| 188 | """A breadcrumb for applicantsroot. |
---|
| 189 | """ |
---|
| 190 | grok.context(IApplicantsRoot) |
---|
[7710] | 191 | title = _(u'Applicants') |
---|
[6078] | 192 | |
---|
[5845] | 193 | class ApplicantsContainerBreadcrumb(Breadcrumb): |
---|
| 194 | """A breadcrumb for applicantscontainers. |
---|
| 195 | """ |
---|
| 196 | grok.context(IApplicantsContainer) |
---|
[6319] | 197 | |
---|
[6153] | 198 | class ApplicantBreadcrumb(Breadcrumb): |
---|
| 199 | """A breadcrumb for applicants. |
---|
| 200 | """ |
---|
| 201 | grok.context(IApplicant) |
---|
[6319] | 202 | |
---|
[6153] | 203 | @property |
---|
| 204 | def title(self): |
---|
| 205 | """Get a title for a context. |
---|
| 206 | """ |
---|
[7240] | 207 | return self.context.application_number |
---|
[5828] | 208 | |
---|
[7250] | 209 | class OnlinePaymentBreadcrumb(Breadcrumb): |
---|
| 210 | """A breadcrumb for payments. |
---|
| 211 | """ |
---|
| 212 | grok.context(IApplicantOnlinePayment) |
---|
| 213 | |
---|
| 214 | @property |
---|
| 215 | def title(self): |
---|
| 216 | return self.context.p_id |
---|
| 217 | |
---|
[7819] | 218 | class ApplicantsContainerPage(KofaDisplayFormPage): |
---|
[5830] | 219 | """The standard view for regular applicant containers. |
---|
| 220 | """ |
---|
| 221 | grok.context(IApplicantsContainer) |
---|
| 222 | grok.name('index') |
---|
[6153] | 223 | grok.require('waeup.Public') |
---|
[6029] | 224 | grok.template('applicantscontainerpage') |
---|
[5850] | 225 | pnav = 3 |
---|
[6053] | 226 | |
---|
[7903] | 227 | form_fields = grok.AutoFields(IApplicantsContainer).omit('title') |
---|
[6054] | 228 | form_fields['startdate'].custom_widget = FriendlyDateDisplayWidget('le') |
---|
| 229 | form_fields['enddate'].custom_widget = FriendlyDateDisplayWidget('le') |
---|
[6084] | 230 | form_fields['description'].custom_widget = ReSTDisplayWidget |
---|
[6053] | 231 | |
---|
[5837] | 232 | @property |
---|
[7708] | 233 | def introduction(self): |
---|
[7833] | 234 | # Here we know that the cookie has been set |
---|
| 235 | lang = self.request.cookies.get('kofa.language') |
---|
[7708] | 236 | html = self.context.description_dict.get(lang,'') |
---|
| 237 | if html =='': |
---|
[7833] | 238 | portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
[7708] | 239 | html = self.context.description_dict.get(portal_language,'') |
---|
| 240 | if html =='': |
---|
| 241 | return '' |
---|
| 242 | else: |
---|
| 243 | return html |
---|
| 244 | |
---|
| 245 | @property |
---|
[7467] | 246 | def label(self): |
---|
[7493] | 247 | return "%s" % self.context.title |
---|
[5837] | 248 | |
---|
[7819] | 249 | class ApplicantsContainerManageFormPage(KofaEditFormPage): |
---|
[5837] | 250 | grok.context(IApplicantsContainer) |
---|
[5850] | 251 | grok.name('manage') |
---|
[6107] | 252 | grok.template('applicantscontainermanagepage') |
---|
[7903] | 253 | form_fields = grok.AutoFields(IApplicantsContainer).omit('title') |
---|
[7710] | 254 | taboneactions = [_('Save'),_('Cancel')] |
---|
| 255 | tabtwoactions = [_('Add applicant'), _('Remove selected'),_('Cancel')] |
---|
| 256 | tabthreeactions1 = [_('Remove selected local roles')] |
---|
| 257 | tabthreeactions2 = [_('Add local role')] |
---|
[5844] | 258 | # Use friendlier date widget... |
---|
[6054] | 259 | form_fields['startdate'].custom_widget = FriendlyDateWidget('le') |
---|
| 260 | form_fields['enddate'].custom_widget = FriendlyDateWidget('le') |
---|
[7136] | 261 | grok.require('waeup.manageApplication') |
---|
[5850] | 262 | |
---|
| 263 | @property |
---|
| 264 | def label(self): |
---|
[7710] | 265 | return _('Manage applicants container') |
---|
[5850] | 266 | |
---|
[5845] | 267 | pnav = 3 |
---|
[5837] | 268 | |
---|
| 269 | def update(self): |
---|
[5850] | 270 | datepicker.need() # Enable jQuery datepicker in date fields. |
---|
[5982] | 271 | tabs.need() |
---|
[7484] | 272 | self.tab1 = self.tab2 = self.tab3 = '' |
---|
| 273 | qs = self.request.get('QUERY_STRING', '') |
---|
| 274 | if not qs: |
---|
| 275 | qs = 'tab1' |
---|
| 276 | setattr(self, qs, 'active') |
---|
[7330] | 277 | warning.need() |
---|
[6015] | 278 | datatable.need() # Enable jQurey datatables for contents listing |
---|
[6107] | 279 | return super(ApplicantsContainerManageFormPage, self).update() |
---|
[5837] | 280 | |
---|
[6184] | 281 | def getLocalRoles(self): |
---|
| 282 | roles = ILocalRolesAssignable(self.context) |
---|
| 283 | return roles() |
---|
| 284 | |
---|
| 285 | def getUsers(self): |
---|
| 286 | """Get a list of all users. |
---|
| 287 | """ |
---|
| 288 | for key, val in grok.getSite()['users'].items(): |
---|
| 289 | url = self.url(val) |
---|
| 290 | yield(dict(url=url, name=key, val=val)) |
---|
| 291 | |
---|
| 292 | def getUsersWithLocalRoles(self): |
---|
| 293 | return get_users_with_local_roles(self.context) |
---|
| 294 | |
---|
[7708] | 295 | def _description(self): |
---|
| 296 | view = ApplicantsContainerPage( |
---|
| 297 | self.context,self.request) |
---|
| 298 | view.setUpWidgets() |
---|
| 299 | return view.widgets['description']() |
---|
| 300 | |
---|
[7714] | 301 | @action(_('Save'), style='primary') |
---|
[7489] | 302 | def save(self, **data): |
---|
[5837] | 303 | self.applyData(self.context, **data) |
---|
[7708] | 304 | self.context.description_dict = self._description() |
---|
[7710] | 305 | self.flash(_('Form has been saved.')) |
---|
[5837] | 306 | return |
---|
[6078] | 307 | |
---|
[7710] | 308 | @jsaction(_('Remove selected')) |
---|
[6105] | 309 | def delApplicant(self, **data): |
---|
[6189] | 310 | form = self.request.form |
---|
| 311 | if form.has_key('val_id'): |
---|
| 312 | child_id = form['val_id'] |
---|
| 313 | else: |
---|
[7710] | 314 | self.flash(_('No applicant selected!')) |
---|
[7484] | 315 | self.redirect(self.url(self.context, '@@manage')+'?tab2') |
---|
[6189] | 316 | return |
---|
| 317 | if not isinstance(child_id, list): |
---|
| 318 | child_id = [child_id] |
---|
| 319 | deleted = [] |
---|
| 320 | for id in child_id: |
---|
| 321 | try: |
---|
| 322 | del self.context[id] |
---|
| 323 | deleted.append(id) |
---|
| 324 | except: |
---|
[7710] | 325 | self.flash(_('Could not delete:') + ' %s: %s: %s' % ( |
---|
[6189] | 326 | id, sys.exc_info()[0], sys.exc_info()[1])) |
---|
| 327 | if len(deleted): |
---|
[7741] | 328 | self.flash(_('Successfully removed: ${a}', |
---|
[7738] | 329 | mapping = {'a':', '.join(deleted)})) |
---|
[7484] | 330 | self.redirect(self.url(self.context, u'@@manage')+'?tab2') |
---|
[6189] | 331 | return |
---|
[6105] | 332 | |
---|
[7710] | 333 | @action(_('Add applicant'), validator=NullValidator) |
---|
[6105] | 334 | def addApplicant(self, **data): |
---|
[6327] | 335 | self.redirect(self.url(self.context, 'addapplicant')) |
---|
| 336 | return |
---|
[6105] | 337 | |
---|
[7710] | 338 | @action(_('Cancel'), validator=NullValidator) |
---|
[5837] | 339 | def cancel(self, **data): |
---|
| 340 | self.redirect(self.url(self.context)) |
---|
| 341 | return |
---|
[5886] | 342 | |
---|
[7710] | 343 | @action(_('Add local role'), validator=NullValidator) |
---|
[6184] | 344 | def addLocalRole(self, **data): |
---|
| 345 | return add_local_role(self,3, **data) |
---|
[6105] | 346 | |
---|
[7710] | 347 | @action(_('Remove selected local roles')) |
---|
[6184] | 348 | def delLocalRoles(self, **data): |
---|
| 349 | return del_local_roles(self,3,**data) |
---|
| 350 | |
---|
[7819] | 351 | class ApplicantAddFormPage(KofaAddFormPage): |
---|
[6622] | 352 | """Add-form to add an applicant. |
---|
[6327] | 353 | """ |
---|
| 354 | grok.context(IApplicantsContainer) |
---|
[7136] | 355 | grok.require('waeup.manageApplication') |
---|
[6327] | 356 | grok.name('addapplicant') |
---|
[7240] | 357 | #grok.template('applicantaddpage') |
---|
| 358 | form_fields = grok.AutoFields(IApplicant).select( |
---|
[7356] | 359 | 'firstname', 'middlename', 'lastname', |
---|
[7240] | 360 | 'email', 'phone') |
---|
[7868] | 361 | form_fields['phone'].custom_widget = PhoneWidget |
---|
[7714] | 362 | label = _('Add applicant') |
---|
[6327] | 363 | pnav = 3 |
---|
| 364 | |
---|
[7714] | 365 | @action(_('Create application record')) |
---|
[6327] | 366 | def addApplicant(self, **data): |
---|
[8008] | 367 | applicant = createObject(u'waeup.Applicant') |
---|
[7240] | 368 | self.applyData(applicant, **data) |
---|
| 369 | self.context.addApplicant(applicant) |
---|
[7714] | 370 | self.flash(_('Applicant record created.')) |
---|
[7363] | 371 | self.redirect( |
---|
| 372 | self.url(self.context[applicant.application_number], 'index')) |
---|
[6327] | 373 | return |
---|
| 374 | |
---|
[7819] | 375 | class ApplicantDisplayFormPage(KofaDisplayFormPage): |
---|
[8014] | 376 | """A display view for applicant data. |
---|
| 377 | """ |
---|
[5273] | 378 | grok.context(IApplicant) |
---|
| 379 | grok.name('index') |
---|
[7113] | 380 | grok.require('waeup.viewApplication') |
---|
[7200] | 381 | grok.template('applicantdisplaypage') |
---|
[6320] | 382 | form_fields = grok.AutoFields(IApplicant).omit( |
---|
[7347] | 383 | 'locked', 'course_admitted', 'password') |
---|
[6054] | 384 | form_fields['date_of_birth'].custom_widget = FriendlyDateDisplayWidget('le') |
---|
[7714] | 385 | label = _('Applicant') |
---|
[5843] | 386 | pnav = 3 |
---|
[5273] | 387 | |
---|
[7063] | 388 | def update(self): |
---|
| 389 | self.passport_url = self.url(self.context, 'passport.jpg') |
---|
[7240] | 390 | # Mark application as started if applicant logs in for the first time |
---|
[7272] | 391 | usertype = getattr(self.request.principal, 'user_type', None) |
---|
| 392 | if usertype == 'applicant' and \ |
---|
| 393 | IWorkflowState(self.context).getState() == INITIALIZED: |
---|
[7240] | 394 | IWorkflowInfo(self.context).fireTransition('start') |
---|
[7063] | 395 | return |
---|
| 396 | |
---|
[6196] | 397 | @property |
---|
[7240] | 398 | def hasPassword(self): |
---|
| 399 | if self.context.password: |
---|
[7714] | 400 | return _('set') |
---|
| 401 | return _('unset') |
---|
[7240] | 402 | |
---|
| 403 | @property |
---|
[6196] | 404 | def label(self): |
---|
| 405 | container_title = self.context.__parent__.title |
---|
[7714] | 406 | return _('${a} Application Record ${b}', mapping = { |
---|
| 407 | 'a':container_title, 'b':self.context.application_number}) |
---|
[6196] | 408 | |
---|
[7347] | 409 | def getCourseAdmitted(self): |
---|
| 410 | """Return link, title and code in html format to the certificate |
---|
| 411 | admitted. |
---|
| 412 | """ |
---|
| 413 | course_admitted = self.context.course_admitted |
---|
[7351] | 414 | if getattr(course_admitted, '__parent__',None): |
---|
[7347] | 415 | url = self.url(course_admitted) |
---|
| 416 | title = course_admitted.title |
---|
| 417 | code = course_admitted.code |
---|
| 418 | return '<a href="%s">%s - %s</a>' %(url,code,title) |
---|
| 419 | return '' |
---|
[6254] | 420 | |
---|
[7259] | 421 | class ApplicantBaseDisplayFormPage(ApplicantDisplayFormPage): |
---|
| 422 | grok.context(IApplicant) |
---|
| 423 | grok.name('base') |
---|
| 424 | form_fields = grok.AutoFields(IApplicant).select( |
---|
| 425 | 'applicant_id', 'firstname', 'lastname','email', 'course1') |
---|
| 426 | |
---|
[7459] | 427 | class CreateStudentPage(UtilityView, grok.View): |
---|
[7341] | 428 | """Create a student object from applicatnt data |
---|
| 429 | and copy applicant object. |
---|
| 430 | """ |
---|
| 431 | grok.context(IApplicant) |
---|
| 432 | grok.name('createstudent') |
---|
| 433 | grok.require('waeup.manageStudent') |
---|
| 434 | |
---|
| 435 | def update(self): |
---|
| 436 | msg = self.context.createStudent()[1] |
---|
| 437 | self.flash(msg) |
---|
| 438 | self.redirect(self.url(self.context)) |
---|
| 439 | return |
---|
| 440 | |
---|
| 441 | def render(self): |
---|
| 442 | return |
---|
| 443 | |
---|
[7459] | 444 | class AcceptanceFeePaymentAddPage(UtilityView, grok.View): |
---|
[7250] | 445 | """ Page to add an online payment ticket |
---|
| 446 | """ |
---|
| 447 | grok.context(IApplicant) |
---|
| 448 | grok.name('addafp') |
---|
| 449 | grok.require('waeup.payApplicant') |
---|
| 450 | |
---|
| 451 | def update(self): |
---|
| 452 | p_category = 'acceptance' |
---|
| 453 | session = str(self.context.__parent__.year) |
---|
| 454 | try: |
---|
| 455 | academic_session = grok.getSite()['configuration'][session] |
---|
| 456 | except KeyError: |
---|
[7714] | 457 | self.flash(_('Session configuration object is not available.')) |
---|
[7250] | 458 | return |
---|
| 459 | timestamp = "%d" % int(time()*1000) |
---|
| 460 | for key in self.context.keys(): |
---|
| 461 | ticket = self.context[key] |
---|
| 462 | if ticket.p_state == 'paid': |
---|
| 463 | self.flash( |
---|
[7714] | 464 | _('This type of payment has already been made.')) |
---|
[7250] | 465 | self.redirect(self.url(self.context)) |
---|
| 466 | return |
---|
| 467 | payment = createObject(u'waeup.ApplicantOnlinePayment') |
---|
| 468 | payment.p_id = "p%s" % timestamp |
---|
| 469 | payment.p_item = self.context.__parent__.title |
---|
| 470 | payment.p_year = self.context.__parent__.year |
---|
| 471 | payment.p_category = p_category |
---|
| 472 | payment.amount_auth = academic_session.acceptance_fee |
---|
| 473 | payment.surcharge_1 = academic_session.surcharge_1 |
---|
| 474 | payment.surcharge_2 = academic_session.surcharge_2 |
---|
| 475 | payment.surcharge_3 = academic_session.surcharge_3 |
---|
| 476 | self.context[payment.p_id] = payment |
---|
[7714] | 477 | self.flash(_('Payment ticket created.')) |
---|
[7250] | 478 | return |
---|
| 479 | |
---|
| 480 | def render(self): |
---|
| 481 | usertype = getattr(self.request.principal, 'user_type', None) |
---|
| 482 | if usertype == 'applicant': |
---|
| 483 | self.redirect(self.url(self.context, '@@edit')) |
---|
| 484 | return |
---|
| 485 | self.redirect(self.url(self.context, '@@manage')) |
---|
| 486 | return |
---|
| 487 | |
---|
| 488 | |
---|
[7819] | 489 | class OnlinePaymentDisplayFormPage(KofaDisplayFormPage): |
---|
[7250] | 490 | """ Page to view an online payment ticket |
---|
| 491 | """ |
---|
| 492 | grok.context(IApplicantOnlinePayment) |
---|
| 493 | grok.name('index') |
---|
| 494 | grok.require('waeup.viewApplication') |
---|
| 495 | form_fields = grok.AutoFields(IApplicantOnlinePayment) |
---|
[7363] | 496 | form_fields[ |
---|
| 497 | 'creation_date'].custom_widget = FriendlyDatetimeDisplayWidget('le') |
---|
| 498 | form_fields[ |
---|
| 499 | 'payment_date'].custom_widget = FriendlyDatetimeDisplayWidget('le') |
---|
[7250] | 500 | pnav = 3 |
---|
| 501 | |
---|
| 502 | @property |
---|
| 503 | def label(self): |
---|
[7714] | 504 | return _('${a}: Online Payment Ticket ${b}', mapping = { |
---|
| 505 | 'a':self.context.__parent__.display_fullname, 'b':self.context.p_id}) |
---|
[7250] | 506 | |
---|
[7459] | 507 | class OnlinePaymentCallbackPage(UtilityView, grok.View): |
---|
[7250] | 508 | """ Callback view |
---|
| 509 | """ |
---|
| 510 | grok.context(IApplicantOnlinePayment) |
---|
[7997] | 511 | grok.name('simulate_callback') |
---|
[7250] | 512 | grok.require('waeup.payApplicant') |
---|
| 513 | |
---|
| 514 | # This update method simulates a valid callback und must be |
---|
[7997] | 515 | # neutralized in the customization package. |
---|
[7250] | 516 | def update(self): |
---|
[7322] | 517 | self.wf_info = IWorkflowInfo(self.context.__parent__) |
---|
| 518 | try: |
---|
| 519 | self.wf_info.fireTransition('pay') |
---|
| 520 | except InvalidTransitionError: |
---|
| 521 | self.flash('Error: %s' % sys.exc_info()[1]) |
---|
[7250] | 522 | return |
---|
| 523 | self.context.r_amount_approved = self.context.amount_auth |
---|
| 524 | self.context.r_card_num = u'0000' |
---|
| 525 | self.context.r_code = u'00' |
---|
| 526 | self.context.p_state = 'paid' |
---|
| 527 | self.context.payment_date = datetime.now() |
---|
[7811] | 528 | ob_class = self.__implemented__.__name__.replace('waeup.kofa.','') |
---|
[7250] | 529 | self.context.__parent__.loggerInfo( |
---|
| 530 | ob_class, 'valid callback: %s' % self.context.p_id) |
---|
[7714] | 531 | self.flash(_('Valid callback received.')) |
---|
[7250] | 532 | return |
---|
| 533 | |
---|
| 534 | def render(self): |
---|
| 535 | self.redirect(self.url(self.context, '@@index')) |
---|
| 536 | return |
---|
| 537 | |
---|
[7459] | 538 | class ExportPDFPaymentSlipPage(UtilityView, grok.View): |
---|
[7250] | 539 | """Deliver a PDF slip of the context. |
---|
| 540 | """ |
---|
| 541 | grok.context(IApplicantOnlinePayment) |
---|
| 542 | grok.name('payment_receipt.pdf') |
---|
| 543 | grok.require('waeup.viewApplication') |
---|
| 544 | form_fields = grok.AutoFields(IApplicantOnlinePayment) |
---|
| 545 | form_fields['creation_date'].custom_widget = FriendlyDateDisplayWidget('le') |
---|
| 546 | form_fields['payment_date'].custom_widget = FriendlyDateDisplayWidget('le') |
---|
| 547 | prefix = 'form' |
---|
| 548 | |
---|
| 549 | @property |
---|
[7714] | 550 | def title(self): |
---|
[7819] | 551 | portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
[7811] | 552 | return translate(_('Payment Data'), 'waeup.kofa', |
---|
[7714] | 553 | target_language=portal_language) |
---|
| 554 | |
---|
| 555 | @property |
---|
[7250] | 556 | def label(self): |
---|
[7819] | 557 | portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
[7714] | 558 | return translate(_('Online Payment Receipt'), |
---|
[7811] | 559 | 'waeup.kofa', target_language=portal_language) \ |
---|
[7714] | 560 | + ' %s' % self.context.p_id |
---|
[7250] | 561 | |
---|
| 562 | def render(self): |
---|
| 563 | if self.context.p_state != 'paid': |
---|
[7714] | 564 | self.flash(_('Ticket not yet paid.')) |
---|
[7250] | 565 | self.redirect(self.url(self.context)) |
---|
| 566 | return |
---|
[7259] | 567 | applicantview = ApplicantBaseDisplayFormPage(self.context.__parent__, |
---|
[7250] | 568 | self.request) |
---|
| 569 | students_utils = getUtility(IStudentsUtils) |
---|
[7318] | 570 | return students_utils.renderPDF(self,'payment_receipt.pdf', |
---|
[7250] | 571 | self.context.__parent__, applicantview) |
---|
| 572 | |
---|
[7459] | 573 | class ExportPDFPage(UtilityView, grok.View): |
---|
[6358] | 574 | """Deliver a PDF slip of the context. |
---|
| 575 | """ |
---|
| 576 | grok.context(IApplicant) |
---|
| 577 | grok.name('application_slip.pdf') |
---|
[7136] | 578 | grok.require('waeup.viewApplication') |
---|
[6358] | 579 | prefix = 'form' |
---|
| 580 | |
---|
| 581 | def render(self): |
---|
[7392] | 582 | pdfstream = getAdapter(self.context, IPDF, name='application_slip')( |
---|
| 583 | view=self) |
---|
[6358] | 584 | self.response.setHeader( |
---|
| 585 | 'Content-Type', 'application/pdf') |
---|
[7392] | 586 | return pdfstream |
---|
[6358] | 587 | |
---|
[7081] | 588 | def handle_img_upload(upload, context, view): |
---|
[7063] | 589 | """Handle upload of applicant image. |
---|
[7081] | 590 | |
---|
| 591 | Returns `True` in case of success or `False`. |
---|
| 592 | |
---|
| 593 | Please note that file pointer passed in (`upload`) most probably |
---|
| 594 | points to end of file when leaving this function. |
---|
[7063] | 595 | """ |
---|
[7081] | 596 | size = file_size(upload) |
---|
| 597 | if size > MAX_UPLOAD_SIZE: |
---|
[7714] | 598 | view.flash(_('Uploaded image is too big!')) |
---|
[7081] | 599 | return False |
---|
[7247] | 600 | dummy, ext = os.path.splitext(upload.filename) |
---|
| 601 | ext.lower() |
---|
| 602 | if ext != '.jpg': |
---|
[7714] | 603 | view.flash(_('jpg file extension expected.')) |
---|
[7247] | 604 | return False |
---|
[7081] | 605 | upload.seek(0) # file pointer moved when determining size |
---|
[7063] | 606 | store = getUtility(IExtFileStore) |
---|
| 607 | file_id = IFileStoreNameChooser(context).chooseName() |
---|
| 608 | store.createFile(file_id, upload) |
---|
[7081] | 609 | return True |
---|
[7063] | 610 | |
---|
[7819] | 611 | class ApplicantManageFormPage(KofaEditFormPage): |
---|
[6196] | 612 | """A full edit view for applicant data. |
---|
| 613 | """ |
---|
| 614 | grok.context(IApplicant) |
---|
[7200] | 615 | grok.name('manage') |
---|
[7136] | 616 | grok.require('waeup.manageApplication') |
---|
[6476] | 617 | form_fields = grok.AutoFields(IApplicant) |
---|
[6196] | 618 | form_fields['date_of_birth'].custom_widget = FriendlyDateWidget('le-year') |
---|
[7351] | 619 | form_fields['student_id'].for_display = True |
---|
[7378] | 620 | form_fields['applicant_id'].for_display = True |
---|
[7423] | 621 | form_fields['phone'].custom_widget = PhoneWidget |
---|
[7200] | 622 | grok.template('applicanteditpage') |
---|
[6322] | 623 | manage_applications = True |
---|
[6196] | 624 | pnav = 3 |
---|
[7714] | 625 | display_actions = [[_('Save'), _('Final Submit')], |
---|
| 626 | [_('Add online payment ticket'),_('Remove selected tickets')]] |
---|
[6196] | 627 | |
---|
| 628 | def update(self): |
---|
| 629 | datepicker.need() # Enable jQuery datepicker in date fields. |
---|
[7330] | 630 | warning.need() |
---|
[7200] | 631 | super(ApplicantManageFormPage, self).update() |
---|
[6353] | 632 | self.wf_info = IWorkflowInfo(self.context) |
---|
[7081] | 633 | self.max_upload_size = string_from_bytes(MAX_UPLOAD_SIZE) |
---|
[7084] | 634 | self.passport_changed = None |
---|
[6598] | 635 | upload = self.request.form.get('form.passport', None) |
---|
| 636 | if upload: |
---|
| 637 | # We got a fresh upload |
---|
[7084] | 638 | self.passport_changed = handle_img_upload( |
---|
| 639 | upload, self.context, self) |
---|
[6196] | 640 | return |
---|
| 641 | |
---|
| 642 | @property |
---|
| 643 | def label(self): |
---|
| 644 | container_title = self.context.__parent__.title |
---|
[7714] | 645 | return _('${a} Application Form ${b}', mapping = { |
---|
| 646 | 'a':container_title, 'b':self.context.application_number}) |
---|
[6196] | 647 | |
---|
[6303] | 648 | def getTransitions(self): |
---|
[6351] | 649 | """Return a list of dicts of allowed transition ids and titles. |
---|
[6353] | 650 | |
---|
| 651 | Each list entry provides keys ``name`` and ``title`` for |
---|
| 652 | internal name and (human readable) title of a single |
---|
| 653 | transition. |
---|
[6349] | 654 | """ |
---|
[6353] | 655 | allowed_transitions = self.wf_info.getManualTransitions() |
---|
[7687] | 656 | return [dict(name='', title=_('No transition'))] +[ |
---|
[6355] | 657 | dict(name=x, title=y) for x, y in allowed_transitions] |
---|
[6303] | 658 | |
---|
[7714] | 659 | @action(_('Save'), style='primary') |
---|
[6196] | 660 | def save(self, **data): |
---|
[7240] | 661 | form = self.request.form |
---|
| 662 | password = form.get('password', None) |
---|
| 663 | password_ctl = form.get('control_password', None) |
---|
| 664 | if password: |
---|
| 665 | validator = getUtility(IPasswordValidator) |
---|
| 666 | errors = validator.validate_password(password, password_ctl) |
---|
| 667 | if errors: |
---|
| 668 | self.flash( ' '.join(errors)) |
---|
| 669 | return |
---|
[7084] | 670 | if self.passport_changed is False: # False is not None! |
---|
| 671 | return # error during image upload. Ignore other values |
---|
[6475] | 672 | changed_fields = self.applyData(self.context, **data) |
---|
[7199] | 673 | # Turn list of lists into single list |
---|
| 674 | if changed_fields: |
---|
| 675 | changed_fields = reduce(lambda x,y: x+y, changed_fields.values()) |
---|
[7240] | 676 | else: |
---|
| 677 | changed_fields = [] |
---|
| 678 | if self.passport_changed: |
---|
| 679 | changed_fields.append('passport') |
---|
| 680 | if password: |
---|
| 681 | # Now we know that the form has no errors and can set password ... |
---|
| 682 | IUserAccount(self.context).setPassword(password) |
---|
| 683 | changed_fields.append('password') |
---|
[7199] | 684 | fields_string = ' + '.join(changed_fields) |
---|
[7085] | 685 | trans_id = form.get('transition', None) |
---|
| 686 | if trans_id: |
---|
| 687 | self.wf_info.fireTransition(trans_id) |
---|
[7714] | 688 | self.flash(_('Form has been saved.')) |
---|
[7811] | 689 | ob_class = self.__implemented__.__name__.replace('waeup.kofa.','') |
---|
[6644] | 690 | if fields_string: |
---|
| 691 | self.context.loggerInfo(ob_class, 'saved: % s' % fields_string) |
---|
[6196] | 692 | return |
---|
| 693 | |
---|
[7250] | 694 | def unremovable(self, ticket): |
---|
[7330] | 695 | return False |
---|
[7250] | 696 | |
---|
| 697 | # This method is also used by the ApplicantEditFormPage |
---|
| 698 | def delPaymentTickets(self, **data): |
---|
| 699 | form = self.request.form |
---|
| 700 | if form.has_key('val_id'): |
---|
| 701 | child_id = form['val_id'] |
---|
| 702 | else: |
---|
[7714] | 703 | self.flash(_('No payment selected.')) |
---|
[7250] | 704 | self.redirect(self.url(self.context)) |
---|
| 705 | return |
---|
| 706 | if not isinstance(child_id, list): |
---|
| 707 | child_id = [child_id] |
---|
| 708 | deleted = [] |
---|
| 709 | for id in child_id: |
---|
| 710 | # Applicants are not allowed to remove used payment tickets |
---|
| 711 | if not self.unremovable(self.context[id]): |
---|
| 712 | try: |
---|
| 713 | del self.context[id] |
---|
| 714 | deleted.append(id) |
---|
| 715 | except: |
---|
[7714] | 716 | self.flash(_('Could not delete:') + ' %s: %s: %s' % ( |
---|
[7250] | 717 | id, sys.exc_info()[0], sys.exc_info()[1])) |
---|
| 718 | if len(deleted): |
---|
[7741] | 719 | self.flash(_('Successfully removed: ${a}', |
---|
[7738] | 720 | mapping = {'a':', '.join(deleted)})) |
---|
[7811] | 721 | ob_class = self.__implemented__.__name__.replace('waeup.kofa.','') |
---|
[7363] | 722 | self.context.loggerInfo( |
---|
| 723 | ob_class, 'removed: % s' % ', '.join(deleted)) |
---|
[7250] | 724 | return |
---|
| 725 | |
---|
[7252] | 726 | # We explicitely want the forms to be validated before payment tickets |
---|
| 727 | # can be created. If no validation is requested, use |
---|
[7459] | 728 | # 'validator=NullValidator' in the action directive |
---|
[7714] | 729 | @action(_('Add online payment ticket')) |
---|
[7250] | 730 | def addPaymentTicket(self, **data): |
---|
| 731 | self.redirect(self.url(self.context, '@@addafp')) |
---|
[7252] | 732 | return |
---|
[7250] | 733 | |
---|
[7714] | 734 | @jsaction(_('Remove selected tickets')) |
---|
[7250] | 735 | def removePaymentTickets(self, **data): |
---|
| 736 | self.delPaymentTickets(**data) |
---|
| 737 | self.redirect(self.url(self.context) + '/@@manage') |
---|
| 738 | return |
---|
| 739 | |
---|
[7200] | 740 | class ApplicantEditFormPage(ApplicantManageFormPage): |
---|
[5982] | 741 | """An applicant-centered edit view for applicant data. |
---|
| 742 | """ |
---|
[6196] | 743 | grok.context(IApplicantEdit) |
---|
[5273] | 744 | grok.name('edit') |
---|
[6198] | 745 | grok.require('waeup.handleApplication') |
---|
[6459] | 746 | form_fields = grok.AutoFields(IApplicantEdit).omit( |
---|
[6476] | 747 | 'locked', 'course_admitted', 'student_id', |
---|
[8039] | 748 | 'screening_score', |
---|
[6459] | 749 | ) |
---|
[6054] | 750 | form_fields['date_of_birth'].custom_widget = FriendlyDateWidget('le-year') |
---|
[7433] | 751 | form_fields['phone'].custom_widget = PhoneWidget |
---|
[7459] | 752 | form_fields['applicant_id'].for_display = True |
---|
[8039] | 753 | form_fields['reg_number'].for_display = True |
---|
[7200] | 754 | grok.template('applicanteditpage') |
---|
[6322] | 755 | manage_applications = False |
---|
[5484] | 756 | |
---|
[7250] | 757 | @property |
---|
| 758 | def display_actions(self): |
---|
| 759 | state = IWorkflowState(self.context).getState() |
---|
| 760 | if state == INITIALIZED: |
---|
| 761 | actions = [[],[]] |
---|
| 762 | elif state == STARTED: |
---|
[7714] | 763 | actions = [[_('Save')], |
---|
| 764 | [_('Add online payment ticket'),_('Remove selected tickets')]] |
---|
[7250] | 765 | elif state == PAID: |
---|
[7714] | 766 | actions = [[_('Save'), _('Final Submit')], |
---|
| 767 | [_('Remove selected tickets')]] |
---|
[7351] | 768 | else: |
---|
[7250] | 769 | actions = [[],[]] |
---|
| 770 | return actions |
---|
| 771 | |
---|
[7330] | 772 | def unremovable(self, ticket): |
---|
| 773 | state = IWorkflowState(self.context).getState() |
---|
| 774 | return ticket.r_code or state in (INITIALIZED, SUBMITTED) |
---|
| 775 | |
---|
[7145] | 776 | def emit_lock_message(self): |
---|
[7714] | 777 | self.flash(_('The requested form is locked (read-only).')) |
---|
[5941] | 778 | self.redirect(self.url(self.context)) |
---|
| 779 | return |
---|
[6078] | 780 | |
---|
[5686] | 781 | def update(self): |
---|
[5941] | 782 | if self.context.locked: |
---|
[7145] | 783 | self.emit_lock_message() |
---|
[5941] | 784 | return |
---|
[7200] | 785 | super(ApplicantEditFormPage, self).update() |
---|
[5686] | 786 | return |
---|
[5952] | 787 | |
---|
[6196] | 788 | def dataNotComplete(self): |
---|
[7252] | 789 | store = getUtility(IExtFileStore) |
---|
| 790 | if not store.getFileByContext(self.context, attr=u'passport.jpg'): |
---|
[7714] | 791 | return _('No passport picture uploaded.') |
---|
[6322] | 792 | if not self.request.form.get('confirm_passport', False): |
---|
[7714] | 793 | return _('Passport picture confirmation box not ticked.') |
---|
[6196] | 794 | return False |
---|
[5952] | 795 | |
---|
[7252] | 796 | # We explicitely want the forms to be validated before payment tickets |
---|
| 797 | # can be created. If no validation is requested, use |
---|
[7459] | 798 | # 'validator=NullValidator' in the action directive |
---|
[7714] | 799 | @action(_('Add online payment ticket')) |
---|
[7250] | 800 | def addPaymentTicket(self, **data): |
---|
| 801 | self.redirect(self.url(self.context, '@@addafp')) |
---|
[7252] | 802 | return |
---|
[7250] | 803 | |
---|
[7714] | 804 | @jsaction(_('Remove selected tickets')) |
---|
[7250] | 805 | def removePaymentTickets(self, **data): |
---|
| 806 | self.delPaymentTickets(**data) |
---|
| 807 | self.redirect(self.url(self.context) + '/@@edit') |
---|
| 808 | return |
---|
| 809 | |
---|
[7996] | 810 | @action(_('Save'), style='primary') |
---|
[5273] | 811 | def save(self, **data): |
---|
[7084] | 812 | if self.passport_changed is False: # False is not None! |
---|
| 813 | return # error during image upload. Ignore other values |
---|
[5273] | 814 | self.applyData(self.context, **data) |
---|
[6196] | 815 | self.flash('Form has been saved.') |
---|
[5273] | 816 | return |
---|
| 817 | |
---|
[7714] | 818 | @action(_('Final Submit')) |
---|
[5484] | 819 | def finalsubmit(self, **data): |
---|
[7084] | 820 | if self.passport_changed is False: # False is not None! |
---|
| 821 | return # error during image upload. Ignore other values |
---|
[6196] | 822 | if self.dataNotComplete(): |
---|
| 823 | self.flash(self.dataNotComplete()) |
---|
[5941] | 824 | return |
---|
[7252] | 825 | self.applyData(self.context, **data) |
---|
[6303] | 826 | state = IWorkflowState(self.context).getState() |
---|
[6322] | 827 | # This shouldn't happen, but the application officer |
---|
| 828 | # might have forgotten to lock the form after changing the state |
---|
[7250] | 829 | if state != PAID: |
---|
[7714] | 830 | self.flash(_('This form cannot be submitted. Wrong state!')) |
---|
[6303] | 831 | return |
---|
| 832 | IWorkflowInfo(self.context).fireTransition('submit') |
---|
[6476] | 833 | self.context.application_date = datetime.now() |
---|
[5941] | 834 | self.context.locked = True |
---|
[7714] | 835 | self.flash(_('Form has been submitted.')) |
---|
[6196] | 836 | self.redirect(self.url(self.context)) |
---|
[5273] | 837 | return |
---|
[5941] | 838 | |
---|
[7063] | 839 | class PassportImage(grok.View): |
---|
| 840 | """Renders the passport image for applicants. |
---|
| 841 | """ |
---|
| 842 | grok.name('passport.jpg') |
---|
| 843 | grok.context(IApplicant) |
---|
[7113] | 844 | grok.require('waeup.viewApplication') |
---|
[7063] | 845 | |
---|
| 846 | def render(self): |
---|
| 847 | # A filename chooser turns a context into a filename suitable |
---|
| 848 | # for file storage. |
---|
| 849 | image = getUtility(IExtFileStore).getFileByContext(self.context) |
---|
| 850 | self.response.setHeader( |
---|
| 851 | 'Content-Type', 'image/jpeg') |
---|
| 852 | if image is None: |
---|
| 853 | # show placeholder image |
---|
[7089] | 854 | return open(DEFAULT_PASSPORT_IMAGE_PATH, 'rb').read() |
---|
[7063] | 855 | return image |
---|
[7363] | 856 | |
---|
[7819] | 857 | class ApplicantRegistrationPage(KofaAddFormPage): |
---|
[7363] | 858 | """Captcha'd registration page for applicants. |
---|
| 859 | """ |
---|
| 860 | grok.context(IApplicantsContainer) |
---|
| 861 | grok.name('register') |
---|
[7373] | 862 | grok.require('waeup.Anonymous') |
---|
[7363] | 863 | grok.template('applicantregister') |
---|
| 864 | |
---|
[7368] | 865 | @property |
---|
[8033] | 866 | def form_fields(self): |
---|
| 867 | form_fields = None |
---|
| 868 | if self.context.mode == 'create': |
---|
| 869 | form_fields = grok.AutoFields(IApplicantEdit).select( |
---|
| 870 | 'firstname', 'middlename', 'lastname', 'email', 'phone') |
---|
| 871 | form_fields['phone'].custom_widget = PhoneWidget |
---|
| 872 | elif self.context.mode == 'update': |
---|
[8037] | 873 | form_fields = grok.AutoFields(IApplicantRegisterUpdate).select( |
---|
| 874 | 'firstname','reg_number','email') |
---|
[8033] | 875 | return form_fields |
---|
| 876 | |
---|
| 877 | @property |
---|
[7368] | 878 | def label(self): |
---|
[7714] | 879 | return _('Register for ${a} Application', |
---|
| 880 | mapping = {'a':self.context.title}) |
---|
[7368] | 881 | |
---|
[7363] | 882 | def update(self): |
---|
[7368] | 883 | # Check if application has started ... |
---|
| 884 | if not self.context.startdate or self.context.startdate > date.today(): |
---|
[7714] | 885 | self.flash(_('Application has not yet started.')) |
---|
[7368] | 886 | self.redirect(self.url(self.context)) |
---|
| 887 | return |
---|
| 888 | # ... or ended |
---|
| 889 | if not self.context.enddate or self.context.enddate < date.today(): |
---|
[7714] | 890 | self.flash(_('Application has ended.')) |
---|
[7368] | 891 | self.redirect(self.url(self.context)) |
---|
| 892 | return |
---|
| 893 | # Handle captcha |
---|
[7363] | 894 | self.captcha = getUtility(ICaptchaManager).getCaptcha() |
---|
| 895 | self.captcha_result = self.captcha.verify(self.request) |
---|
| 896 | self.captcha_code = self.captcha.display(self.captcha_result.error_code) |
---|
| 897 | return |
---|
| 898 | |
---|
[7714] | 899 | @action(_('Get login credentials'), style='primary') |
---|
[7363] | 900 | def register(self, **data): |
---|
| 901 | if not self.captcha_result.is_valid: |
---|
[8037] | 902 | # Captcha will display error messages automatically. |
---|
[7363] | 903 | # No need to flash something. |
---|
| 904 | return |
---|
[8033] | 905 | if self.context.mode == 'create': |
---|
| 906 | # Add applicant |
---|
| 907 | applicant = createObject(u'waeup.Applicant') |
---|
| 908 | self.applyData(applicant, **data) |
---|
| 909 | self.context.addApplicant(applicant) |
---|
[8042] | 910 | applicant.reg_number = applicant.applicant_id |
---|
| 911 | notify(grok.ObjectModifiedEvent(applicant)) |
---|
[8033] | 912 | elif self.context.mode == 'update': |
---|
| 913 | # Update applicant |
---|
[8037] | 914 | reg_number = data.get('reg_number','') |
---|
| 915 | firstname = data.get('firstname','') |
---|
[8033] | 916 | cat = getUtility(ICatalog, name='applicants_catalog') |
---|
| 917 | results = list( |
---|
| 918 | cat.searchResults(reg_number=(reg_number, reg_number))) |
---|
| 919 | if results: |
---|
| 920 | applicant = results[0] |
---|
[8042] | 921 | if getattr(applicant,'firstname',None) is None: |
---|
[8037] | 922 | self.flash(_('An error occurred.')) |
---|
| 923 | return |
---|
| 924 | elif applicant.firstname.lower() != firstname.lower(): |
---|
[8042] | 925 | # Don't tell the truth here. Anonymous must not |
---|
| 926 | # know that a record was found and only the firstname |
---|
| 927 | # verification failed. |
---|
[8037] | 928 | self.flash(_('No application record found.')) |
---|
| 929 | return |
---|
[8042] | 930 | elif applicant.password is not None: |
---|
| 931 | self.flash(_('Your password has already been set. ' |
---|
| 932 | 'Please proceed to the login page.')) |
---|
| 933 | return |
---|
| 934 | # Store email address but nothing else. |
---|
[8033] | 935 | applicant.email = data['email'] |
---|
[8042] | 936 | notify(grok.ObjectModifiedEvent(applicant)) |
---|
[8033] | 937 | else: |
---|
[8042] | 938 | # No record found, this is the truth. |
---|
[8033] | 939 | self.flash(_('No application record found.')) |
---|
| 940 | return |
---|
| 941 | else: |
---|
[8042] | 942 | # Does not happen but anyway ... |
---|
[8033] | 943 | return |
---|
[7819] | 944 | kofa_utils = getUtility(IKofaUtils) |
---|
[7811] | 945 | password = kofa_utils.genPassword() |
---|
[7380] | 946 | IUserAccount(applicant).setPassword(password) |
---|
[7365] | 947 | # Send email with credentials |
---|
[7399] | 948 | login_url = self.url(grok.getSite(), 'login') |
---|
[7714] | 949 | msg = _('You have successfully been registered for the') |
---|
[7811] | 950 | if kofa_utils.sendCredentials(IUserAccount(applicant), |
---|
[7407] | 951 | password, login_url, msg): |
---|
[7380] | 952 | self.redirect(self.url(self.context, 'registration_complete', |
---|
| 953 | data = dict(email=applicant.email))) |
---|
| 954 | return |
---|
| 955 | else: |
---|
[7714] | 956 | self.flash(_('Email could not been sent. Please retry later.')) |
---|
[7380] | 957 | return |
---|
| 958 | |
---|
[7819] | 959 | class ApplicantRegistrationEmailSent(KofaPage): |
---|
[7380] | 960 | """Landing page after successful registration. |
---|
| 961 | """ |
---|
| 962 | grok.name('registration_complete') |
---|
| 963 | grok.require('waeup.Public') |
---|
| 964 | grok.template('applicantregemailsent') |
---|
[7714] | 965 | label = _('Your registration was successful.') |
---|
[7380] | 966 | |
---|
| 967 | def update(self, email=None): |
---|
| 968 | self.email = email |
---|
| 969 | return |
---|