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