[5273] | 1 | ## $Id: browser.py 7685 2012-02-23 08:44:57Z 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 |
---|
[7392] | 25 | from zope.component import getUtility, createObject, getAdapter |
---|
[6358] | 26 | from zope.formlib.form import setUpEditWidgets |
---|
[7322] | 27 | from hurry.workflow.interfaces import ( |
---|
| 28 | IWorkflowInfo, IWorkflowState, InvalidTransitionError) |
---|
[7363] | 29 | from waeup.sirp.applicants.interfaces import ( |
---|
| 30 | IApplicant, IApplicantEdit, IApplicantsRoot, |
---|
[7683] | 31 | IApplicantsContainer, IApplicantsContainerAdd, |
---|
| 32 | MAX_UPLOAD_SIZE, IApplicantOnlinePayment, IApplicantsUtils |
---|
[7363] | 33 | ) |
---|
| 34 | from waeup.sirp.applicants.workflow import INITIALIZED, STARTED, PAID, SUBMITTED |
---|
[5273] | 35 | from waeup.sirp.browser import ( |
---|
[7363] | 36 | SIRPPage, SIRPEditFormPage, SIRPAddFormPage, SIRPDisplayFormPage, |
---|
| 37 | DEFAULT_PASSPORT_IMAGE_PATH) |
---|
| 38 | from waeup.sirp.browser.interfaces import ICaptchaManager |
---|
[6081] | 39 | from waeup.sirp.browser.breadcrumbs import Breadcrumb |
---|
[7459] | 40 | from waeup.sirp.browser.layout import ( |
---|
| 41 | NullValidator, jsaction, action, UtilityView) |
---|
[6321] | 42 | from waeup.sirp.browser.pages import add_local_role, del_local_roles |
---|
[7330] | 43 | from waeup.sirp.browser.resources import datepicker, tabs, datatable, warning |
---|
[7063] | 44 | from waeup.sirp.interfaces import ( |
---|
[7392] | 45 | ISIRPObject, ILocalRolesAssignable, IExtFileStore, IPDF, |
---|
[7365] | 46 | IFileStoreNameChooser, IPasswordValidator, IUserAccount, ISIRPUtils) |
---|
[6321] | 47 | from waeup.sirp.permissions import get_users_with_local_roles |
---|
[7363] | 48 | from waeup.sirp.students.interfaces import IStudentsUtils |
---|
[7081] | 49 | from waeup.sirp.utils.helpers import string_from_bytes, file_size |
---|
[6054] | 50 | from waeup.sirp.widgets.datewidget import ( |
---|
[7250] | 51 | FriendlyDateWidget, FriendlyDateDisplayWidget, |
---|
| 52 | FriendlyDatetimeDisplayWidget) |
---|
[7363] | 53 | from waeup.sirp.widgets.phonewidget import PhoneWidget |
---|
[6084] | 54 | from waeup.sirp.widgets.restwidget import ReSTDisplayWidget |
---|
[5320] | 55 | |
---|
[7321] | 56 | grok.context(ISIRPObject) # Make ISIRPObject the default context |
---|
[5273] | 57 | |
---|
[7321] | 58 | class ApplicantsRootPage(SIRPPage): |
---|
[5822] | 59 | grok.context(IApplicantsRoot) |
---|
| 60 | grok.name('index') |
---|
[6153] | 61 | grok.require('waeup.Public') |
---|
[6069] | 62 | label = 'Application Section' |
---|
[5843] | 63 | pnav = 3 |
---|
[6012] | 64 | |
---|
| 65 | def update(self): |
---|
[6067] | 66 | super(ApplicantsRootPage, self).update() |
---|
[7493] | 67 | #datatable.need() |
---|
[6012] | 68 | return |
---|
| 69 | |
---|
[7321] | 70 | class ApplicantsRootManageFormPage(SIRPEditFormPage): |
---|
[5828] | 71 | grok.context(IApplicantsRoot) |
---|
| 72 | grok.name('manage') |
---|
[6107] | 73 | grok.template('applicantsrootmanagepage') |
---|
[6069] | 74 | label = 'Manage application section' |
---|
[5843] | 75 | pnav = 3 |
---|
[7136] | 76 | grok.require('waeup.manageApplication') |
---|
[6069] | 77 | taboneactions = ['Add applicants container', 'Remove selected','Cancel'] |
---|
[6184] | 78 | tabtwoactions1 = ['Remove selected local roles'] |
---|
| 79 | tabtwoactions2 = ['Add local role'] |
---|
[6069] | 80 | subunits = 'Applicants Containers' |
---|
[6078] | 81 | |
---|
[6069] | 82 | def update(self): |
---|
| 83 | tabs.need() |
---|
[6108] | 84 | datatable.need() |
---|
[7330] | 85 | warning.need() |
---|
[6069] | 86 | return super(ApplicantsRootManageFormPage, self).update() |
---|
[5828] | 87 | |
---|
[6184] | 88 | def getLocalRoles(self): |
---|
| 89 | roles = ILocalRolesAssignable(self.context) |
---|
| 90 | return roles() |
---|
| 91 | |
---|
| 92 | def getUsers(self): |
---|
| 93 | """Get a list of all users. |
---|
| 94 | """ |
---|
| 95 | for key, val in grok.getSite()['users'].items(): |
---|
| 96 | url = self.url(val) |
---|
| 97 | yield(dict(url=url, name=key, val=val)) |
---|
| 98 | |
---|
| 99 | def getUsersWithLocalRoles(self): |
---|
| 100 | return get_users_with_local_roles(self.context) |
---|
| 101 | |
---|
[7330] | 102 | @jsaction('Remove selected') |
---|
[6069] | 103 | def delApplicantsContainers(self, **data): |
---|
| 104 | form = self.request.form |
---|
| 105 | child_id = form['val_id'] |
---|
| 106 | if not isinstance(child_id, list): |
---|
| 107 | child_id = [child_id] |
---|
| 108 | deleted = [] |
---|
| 109 | for id in child_id: |
---|
| 110 | try: |
---|
| 111 | del self.context[id] |
---|
| 112 | deleted.append(id) |
---|
| 113 | except: |
---|
| 114 | self.flash('Could not delete %s: %s: %s' % ( |
---|
| 115 | id, sys.exc_info()[0], sys.exc_info()[1])) |
---|
| 116 | if len(deleted): |
---|
| 117 | self.flash('Successfully removed: %s' % ', '.join(deleted)) |
---|
[7484] | 118 | self.redirect(self.url(self.context, '@@manage')) |
---|
[6078] | 119 | return |
---|
[5828] | 120 | |
---|
[7459] | 121 | @action('Add applicants container', validator=NullValidator) |
---|
[6069] | 122 | def addApplicantsContainer(self, **data): |
---|
| 123 | self.redirect(self.url(self.context, '@@add')) |
---|
[6078] | 124 | return |
---|
| 125 | |
---|
[7459] | 126 | @action('Cancel', validator=NullValidator) |
---|
[6069] | 127 | def cancel(self, **data): |
---|
| 128 | self.redirect(self.url(self.context)) |
---|
[6078] | 129 | return |
---|
| 130 | |
---|
[7459] | 131 | @action('Add local role', validator=NullValidator) |
---|
[6184] | 132 | def addLocalRole(self, **data): |
---|
[7484] | 133 | return add_local_role(self,3, **data) |
---|
[6184] | 134 | |
---|
[7459] | 135 | @action('Remove selected local roles') |
---|
[6184] | 136 | def delLocalRoles(self, **data): |
---|
[7484] | 137 | return del_local_roles(self,3,**data) |
---|
[6184] | 138 | |
---|
[7321] | 139 | class ApplicantsContainerAddFormPage(SIRPAddFormPage): |
---|
[5822] | 140 | grok.context(IApplicantsRoot) |
---|
[7136] | 141 | grok.require('waeup.manageApplication') |
---|
[5822] | 142 | grok.name('add') |
---|
[6107] | 143 | grok.template('applicantscontaineraddpage') |
---|
[6069] | 144 | label = 'Add applicants container' |
---|
[5843] | 145 | pnav = 3 |
---|
[6078] | 146 | |
---|
[6103] | 147 | form_fields = grok.AutoFields( |
---|
| 148 | IApplicantsContainerAdd).omit('code').omit('title') |
---|
[6083] | 149 | form_fields['startdate'].custom_widget = FriendlyDateWidget('le') |
---|
| 150 | form_fields['enddate'].custom_widget = FriendlyDateWidget('le') |
---|
[6078] | 151 | |
---|
[6083] | 152 | def update(self): |
---|
| 153 | datepicker.need() # Enable jQuery datepicker in date fields. |
---|
| 154 | return super(ApplicantsContainerAddFormPage, self).update() |
---|
| 155 | |
---|
[7459] | 156 | @action('Add applicants container') |
---|
[6069] | 157 | def addApplicantsContainer(self, **data): |
---|
[6103] | 158 | year = data['year'] |
---|
| 159 | code = u'%s%s' % (data['prefix'], year) |
---|
[7683] | 160 | appcats_dict = getUtility(IApplicantsUtils).getApplicationTypeDict() |
---|
[7685] | 161 | title = appcats_dict[data['prefix']][0] |
---|
| 162 | title = u'%s %s/%s' % (title, year, year + 1) |
---|
[6087] | 163 | if code in self.context.keys(): |
---|
[6105] | 164 | self.flash( |
---|
| 165 | 'An applicants container for the same application ' |
---|
| 166 | 'type and entrance year exists already in the database.') |
---|
[5822] | 167 | return |
---|
| 168 | # Add new applicants container... |
---|
[6083] | 169 | provider = data['provider'][1] |
---|
[5822] | 170 | container = provider.factory() |
---|
[6069] | 171 | self.applyData(container, **data) |
---|
[6087] | 172 | container.code = code |
---|
| 173 | container.title = title |
---|
| 174 | self.context[code] = container |
---|
[6105] | 175 | self.flash('Added: "%s".' % code) |
---|
[7484] | 176 | self.redirect(self.url(self.context, u'@@manage')) |
---|
[5822] | 177 | return |
---|
[6078] | 178 | |
---|
[7459] | 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) |
---|
[6654] | 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 | |
---|
[7321] | 214 | class ApplicantsContainerPage(SIRPDisplayFormPage): |
---|
[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 | |
---|
[6105] | 223 | form_fields = grok.AutoFields(IApplicantsContainer).omit('title') |
---|
[6054] | 224 | form_fields['startdate'].custom_widget = FriendlyDateDisplayWidget('le') |
---|
| 225 | form_fields['enddate'].custom_widget = FriendlyDateDisplayWidget('le') |
---|
[6084] | 226 | form_fields['description'].custom_widget = ReSTDisplayWidget |
---|
[6053] | 227 | |
---|
[5837] | 228 | @property |
---|
[7467] | 229 | def label(self): |
---|
[7493] | 230 | return "%s" % self.context.title |
---|
[5837] | 231 | |
---|
[7321] | 232 | class ApplicantsContainerManageFormPage(SIRPEditFormPage): |
---|
[5837] | 233 | grok.context(IApplicantsContainer) |
---|
[5850] | 234 | grok.name('manage') |
---|
[6107] | 235 | grok.template('applicantscontainermanagepage') |
---|
[6105] | 236 | form_fields = grok.AutoFields(IApplicantsContainer).omit('title') |
---|
| 237 | taboneactions = ['Save','Cancel'] |
---|
| 238 | tabtwoactions = ['Add applicant', 'Remove selected','Cancel'] |
---|
[6184] | 239 | tabthreeactions1 = ['Remove selected local roles'] |
---|
| 240 | tabthreeactions2 = ['Add local role'] |
---|
[5844] | 241 | # Use friendlier date widget... |
---|
[6054] | 242 | form_fields['startdate'].custom_widget = FriendlyDateWidget('le') |
---|
| 243 | form_fields['enddate'].custom_widget = FriendlyDateWidget('le') |
---|
[7136] | 244 | grok.require('waeup.manageApplication') |
---|
[5850] | 245 | |
---|
| 246 | @property |
---|
| 247 | def label(self): |
---|
[6087] | 248 | return 'Manage applicants container' |
---|
[5850] | 249 | |
---|
[5845] | 250 | pnav = 3 |
---|
[5837] | 251 | |
---|
| 252 | def update(self): |
---|
[5850] | 253 | datepicker.need() # Enable jQuery datepicker in date fields. |
---|
[5982] | 254 | tabs.need() |
---|
[7484] | 255 | self.tab1 = self.tab2 = self.tab3 = '' |
---|
| 256 | qs = self.request.get('QUERY_STRING', '') |
---|
| 257 | if not qs: |
---|
| 258 | qs = 'tab1' |
---|
| 259 | setattr(self, qs, 'active') |
---|
[7330] | 260 | warning.need() |
---|
[6015] | 261 | datatable.need() # Enable jQurey datatables for contents listing |
---|
[6107] | 262 | return super(ApplicantsContainerManageFormPage, self).update() |
---|
[5837] | 263 | |
---|
[6184] | 264 | def getLocalRoles(self): |
---|
| 265 | roles = ILocalRolesAssignable(self.context) |
---|
| 266 | return roles() |
---|
| 267 | |
---|
| 268 | def getUsers(self): |
---|
| 269 | """Get a list of all users. |
---|
| 270 | """ |
---|
| 271 | for key, val in grok.getSite()['users'].items(): |
---|
| 272 | url = self.url(val) |
---|
| 273 | yield(dict(url=url, name=key, val=val)) |
---|
| 274 | |
---|
| 275 | def getUsersWithLocalRoles(self): |
---|
| 276 | return get_users_with_local_roles(self.context) |
---|
| 277 | |
---|
[7489] | 278 | @action('Save', style='primary') |
---|
| 279 | def save(self, **data): |
---|
[5837] | 280 | self.applyData(self.context, **data) |
---|
[7489] | 281 | self.flash('Form has been saved.') |
---|
[5837] | 282 | return |
---|
[6078] | 283 | |
---|
[7330] | 284 | @jsaction('Remove selected') |
---|
[6105] | 285 | def delApplicant(self, **data): |
---|
[6189] | 286 | form = self.request.form |
---|
| 287 | if form.has_key('val_id'): |
---|
| 288 | child_id = form['val_id'] |
---|
| 289 | else: |
---|
| 290 | self.flash('No applicant selected!') |
---|
[7484] | 291 | self.redirect(self.url(self.context, '@@manage')+'?tab2') |
---|
[6189] | 292 | return |
---|
| 293 | if not isinstance(child_id, list): |
---|
| 294 | child_id = [child_id] |
---|
| 295 | deleted = [] |
---|
| 296 | for id in child_id: |
---|
| 297 | try: |
---|
| 298 | del self.context[id] |
---|
| 299 | deleted.append(id) |
---|
| 300 | except: |
---|
| 301 | self.flash('Could not delete %s: %s: %s' % ( |
---|
| 302 | id, sys.exc_info()[0], sys.exc_info()[1])) |
---|
| 303 | if len(deleted): |
---|
| 304 | self.flash('Successfully removed: %s' % ', '.join(deleted)) |
---|
[7484] | 305 | self.redirect(self.url(self.context, u'@@manage')+'?tab2') |
---|
[6189] | 306 | return |
---|
[6105] | 307 | |
---|
[7459] | 308 | @action('Add applicant', validator=NullValidator) |
---|
[6105] | 309 | def addApplicant(self, **data): |
---|
[6327] | 310 | self.redirect(self.url(self.context, 'addapplicant')) |
---|
| 311 | return |
---|
[6105] | 312 | |
---|
[7459] | 313 | @action('Cancel', validator=NullValidator) |
---|
[5837] | 314 | def cancel(self, **data): |
---|
| 315 | self.redirect(self.url(self.context)) |
---|
| 316 | return |
---|
[5886] | 317 | |
---|
[7459] | 318 | @action('Add local role', validator=NullValidator) |
---|
[6184] | 319 | def addLocalRole(self, **data): |
---|
| 320 | return add_local_role(self,3, **data) |
---|
[6105] | 321 | |
---|
[7459] | 322 | @action('Remove selected local roles') |
---|
[6184] | 323 | def delLocalRoles(self, **data): |
---|
| 324 | return del_local_roles(self,3,**data) |
---|
| 325 | |
---|
[7321] | 326 | class ApplicantAddFormPage(SIRPAddFormPage): |
---|
[6622] | 327 | """Add-form to add an applicant. |
---|
[6327] | 328 | """ |
---|
| 329 | grok.context(IApplicantsContainer) |
---|
[7136] | 330 | grok.require('waeup.manageApplication') |
---|
[6327] | 331 | grok.name('addapplicant') |
---|
[7240] | 332 | #grok.template('applicantaddpage') |
---|
| 333 | form_fields = grok.AutoFields(IApplicant).select( |
---|
[7356] | 334 | 'firstname', 'middlename', 'lastname', |
---|
[7240] | 335 | 'email', 'phone') |
---|
[6327] | 336 | label = 'Add applicant' |
---|
| 337 | pnav = 3 |
---|
| 338 | |
---|
[7459] | 339 | @action('Create application record') |
---|
[6327] | 340 | def addApplicant(self, **data): |
---|
[7260] | 341 | applicant = createObject(u'waeup.Applicant') |
---|
[7240] | 342 | self.applyData(applicant, **data) |
---|
| 343 | self.context.addApplicant(applicant) |
---|
| 344 | self.flash('Applicant record created.') |
---|
[7363] | 345 | self.redirect( |
---|
| 346 | self.url(self.context[applicant.application_number], 'index')) |
---|
[6327] | 347 | return |
---|
| 348 | |
---|
[7321] | 349 | class ApplicantDisplayFormPage(SIRPDisplayFormPage): |
---|
[5273] | 350 | grok.context(IApplicant) |
---|
| 351 | grok.name('index') |
---|
[7113] | 352 | grok.require('waeup.viewApplication') |
---|
[7200] | 353 | grok.template('applicantdisplaypage') |
---|
[6320] | 354 | form_fields = grok.AutoFields(IApplicant).omit( |
---|
[7347] | 355 | 'locked', 'course_admitted', 'password') |
---|
[6054] | 356 | form_fields['date_of_birth'].custom_widget = FriendlyDateDisplayWidget('le') |
---|
[5273] | 357 | label = 'Applicant' |
---|
[5843] | 358 | pnav = 3 |
---|
[5273] | 359 | |
---|
[7063] | 360 | def update(self): |
---|
| 361 | self.passport_url = self.url(self.context, 'passport.jpg') |
---|
[7240] | 362 | # Mark application as started if applicant logs in for the first time |
---|
[7272] | 363 | usertype = getattr(self.request.principal, 'user_type', None) |
---|
| 364 | if usertype == 'applicant' and \ |
---|
| 365 | IWorkflowState(self.context).getState() == INITIALIZED: |
---|
[7240] | 366 | IWorkflowInfo(self.context).fireTransition('start') |
---|
[7063] | 367 | return |
---|
| 368 | |
---|
[6196] | 369 | @property |
---|
[7240] | 370 | def hasPassword(self): |
---|
| 371 | if self.context.password: |
---|
| 372 | return 'set' |
---|
| 373 | return 'unset' |
---|
| 374 | |
---|
| 375 | @property |
---|
[6196] | 376 | def label(self): |
---|
| 377 | container_title = self.context.__parent__.title |
---|
[7240] | 378 | return '%s Application Record %s' % ( |
---|
| 379 | container_title, self.context.application_number) |
---|
[6196] | 380 | |
---|
[7347] | 381 | def getCourseAdmitted(self): |
---|
| 382 | """Return link, title and code in html format to the certificate |
---|
| 383 | admitted. |
---|
| 384 | """ |
---|
| 385 | course_admitted = self.context.course_admitted |
---|
[7351] | 386 | if getattr(course_admitted, '__parent__',None): |
---|
[7347] | 387 | url = self.url(course_admitted) |
---|
| 388 | title = course_admitted.title |
---|
| 389 | code = course_admitted.code |
---|
| 390 | return '<a href="%s">%s - %s</a>' %(url,code,title) |
---|
| 391 | return '' |
---|
[6254] | 392 | |
---|
[7259] | 393 | class ApplicantBaseDisplayFormPage(ApplicantDisplayFormPage): |
---|
| 394 | grok.context(IApplicant) |
---|
| 395 | grok.name('base') |
---|
| 396 | form_fields = grok.AutoFields(IApplicant).select( |
---|
| 397 | 'applicant_id', 'firstname', 'lastname','email', 'course1') |
---|
| 398 | |
---|
[7459] | 399 | class CreateStudentPage(UtilityView, grok.View): |
---|
[7341] | 400 | """Create a student object from applicatnt data |
---|
| 401 | and copy applicant object. |
---|
| 402 | """ |
---|
| 403 | grok.context(IApplicant) |
---|
| 404 | grok.name('createstudent') |
---|
| 405 | grok.require('waeup.manageStudent') |
---|
| 406 | |
---|
| 407 | def update(self): |
---|
| 408 | msg = self.context.createStudent()[1] |
---|
| 409 | self.flash(msg) |
---|
| 410 | self.redirect(self.url(self.context)) |
---|
| 411 | return |
---|
| 412 | |
---|
| 413 | def render(self): |
---|
| 414 | return |
---|
| 415 | |
---|
[7459] | 416 | class AcceptanceFeePaymentAddPage(UtilityView, grok.View): |
---|
[7250] | 417 | """ Page to add an online payment ticket |
---|
| 418 | """ |
---|
| 419 | grok.context(IApplicant) |
---|
| 420 | grok.name('addafp') |
---|
| 421 | grok.require('waeup.payApplicant') |
---|
| 422 | |
---|
| 423 | def update(self): |
---|
| 424 | p_category = 'acceptance' |
---|
| 425 | session = str(self.context.__parent__.year) |
---|
| 426 | try: |
---|
| 427 | academic_session = grok.getSite()['configuration'][session] |
---|
| 428 | except KeyError: |
---|
| 429 | self.flash('Session configuration object is not available.') |
---|
| 430 | return |
---|
| 431 | timestamp = "%d" % int(time()*1000) |
---|
| 432 | for key in self.context.keys(): |
---|
| 433 | ticket = self.context[key] |
---|
| 434 | if ticket.p_state == 'paid': |
---|
| 435 | self.flash( |
---|
| 436 | 'This type of payment has already been made.') |
---|
| 437 | self.redirect(self.url(self.context)) |
---|
| 438 | return |
---|
| 439 | payment = createObject(u'waeup.ApplicantOnlinePayment') |
---|
| 440 | payment.p_id = "p%s" % timestamp |
---|
| 441 | payment.p_item = self.context.__parent__.title |
---|
| 442 | payment.p_year = self.context.__parent__.year |
---|
| 443 | payment.p_category = p_category |
---|
| 444 | payment.amount_auth = academic_session.acceptance_fee |
---|
| 445 | payment.surcharge_1 = academic_session.surcharge_1 |
---|
| 446 | payment.surcharge_2 = academic_session.surcharge_2 |
---|
| 447 | payment.surcharge_3 = academic_session.surcharge_3 |
---|
| 448 | self.context[payment.p_id] = payment |
---|
| 449 | self.flash('Payment ticket created.') |
---|
| 450 | return |
---|
| 451 | |
---|
| 452 | def render(self): |
---|
| 453 | usertype = getattr(self.request.principal, 'user_type', None) |
---|
| 454 | if usertype == 'applicant': |
---|
| 455 | self.redirect(self.url(self.context, '@@edit')) |
---|
| 456 | return |
---|
| 457 | self.redirect(self.url(self.context, '@@manage')) |
---|
| 458 | return |
---|
| 459 | |
---|
| 460 | |
---|
[7321] | 461 | class OnlinePaymentDisplayFormPage(SIRPDisplayFormPage): |
---|
[7250] | 462 | """ Page to view an online payment ticket |
---|
| 463 | """ |
---|
| 464 | grok.context(IApplicantOnlinePayment) |
---|
| 465 | grok.name('index') |
---|
| 466 | grok.require('waeup.viewApplication') |
---|
| 467 | form_fields = grok.AutoFields(IApplicantOnlinePayment) |
---|
[7363] | 468 | form_fields[ |
---|
| 469 | 'creation_date'].custom_widget = FriendlyDatetimeDisplayWidget('le') |
---|
| 470 | form_fields[ |
---|
| 471 | 'payment_date'].custom_widget = FriendlyDatetimeDisplayWidget('le') |
---|
[7250] | 472 | pnav = 3 |
---|
| 473 | |
---|
| 474 | @property |
---|
| 475 | def label(self): |
---|
| 476 | return '%s: Online Payment Ticket %s' % ( |
---|
[7364] | 477 | self.context.__parent__.display_fullname,self.context.p_id) |
---|
[7250] | 478 | |
---|
[7459] | 479 | class OnlinePaymentCallbackPage(UtilityView, grok.View): |
---|
[7250] | 480 | """ Callback view |
---|
| 481 | """ |
---|
| 482 | grok.context(IApplicantOnlinePayment) |
---|
| 483 | grok.name('callback') |
---|
| 484 | grok.require('waeup.payApplicant') |
---|
| 485 | |
---|
| 486 | # This update method simulates a valid callback und must be |
---|
| 487 | # specified in the customization package. The parameters must be taken |
---|
| 488 | # from the incoming request. |
---|
| 489 | def update(self): |
---|
[7322] | 490 | self.wf_info = IWorkflowInfo(self.context.__parent__) |
---|
| 491 | try: |
---|
| 492 | self.wf_info.fireTransition('pay') |
---|
| 493 | except InvalidTransitionError: |
---|
| 494 | self.flash('Error: %s' % sys.exc_info()[1]) |
---|
[7250] | 495 | return |
---|
| 496 | self.context.r_amount_approved = self.context.amount_auth |
---|
| 497 | self.context.r_card_num = u'0000' |
---|
| 498 | self.context.r_code = u'00' |
---|
| 499 | self.context.p_state = 'paid' |
---|
| 500 | self.context.payment_date = datetime.now() |
---|
| 501 | ob_class = self.__implemented__.__name__.replace('waeup.sirp.','') |
---|
| 502 | self.context.__parent__.loggerInfo( |
---|
| 503 | ob_class, 'valid callback: %s' % self.context.p_id) |
---|
| 504 | self.flash('Valid callback received.') |
---|
| 505 | return |
---|
| 506 | |
---|
| 507 | def render(self): |
---|
| 508 | self.redirect(self.url(self.context, '@@index')) |
---|
| 509 | return |
---|
| 510 | |
---|
[7459] | 511 | class ExportPDFPaymentSlipPage(UtilityView, grok.View): |
---|
[7250] | 512 | """Deliver a PDF slip of the context. |
---|
| 513 | """ |
---|
| 514 | grok.context(IApplicantOnlinePayment) |
---|
| 515 | grok.name('payment_receipt.pdf') |
---|
| 516 | grok.require('waeup.viewApplication') |
---|
| 517 | form_fields = grok.AutoFields(IApplicantOnlinePayment) |
---|
| 518 | form_fields['creation_date'].custom_widget = FriendlyDateDisplayWidget('le') |
---|
| 519 | form_fields['payment_date'].custom_widget = FriendlyDateDisplayWidget('le') |
---|
| 520 | prefix = 'form' |
---|
[7318] | 521 | title = 'Payment Data' |
---|
[7250] | 522 | |
---|
| 523 | @property |
---|
| 524 | def label(self): |
---|
| 525 | return 'Online Payment Receipt %s' % self.context.p_id |
---|
| 526 | |
---|
| 527 | def render(self): |
---|
| 528 | if self.context.p_state != 'paid': |
---|
| 529 | self.flash('Ticket not yet paid.') |
---|
| 530 | self.redirect(self.url(self.context)) |
---|
| 531 | return |
---|
[7259] | 532 | applicantview = ApplicantBaseDisplayFormPage(self.context.__parent__, |
---|
[7250] | 533 | self.request) |
---|
| 534 | students_utils = getUtility(IStudentsUtils) |
---|
[7318] | 535 | return students_utils.renderPDF(self,'payment_receipt.pdf', |
---|
[7250] | 536 | self.context.__parent__, applicantview) |
---|
| 537 | |
---|
[7459] | 538 | class ExportPDFPage(UtilityView, grok.View): |
---|
[6358] | 539 | """Deliver a PDF slip of the context. |
---|
| 540 | """ |
---|
| 541 | grok.context(IApplicant) |
---|
| 542 | grok.name('application_slip.pdf') |
---|
[7136] | 543 | grok.require('waeup.viewApplication') |
---|
[6358] | 544 | form_fields = grok.AutoFields(IApplicant).omit( |
---|
[7347] | 545 | 'locked', 'course_admitted') |
---|
[6358] | 546 | form_fields['date_of_birth'].custom_widget = FriendlyDateDisplayWidget('le') |
---|
| 547 | prefix = 'form' |
---|
| 548 | |
---|
[6363] | 549 | @property |
---|
| 550 | def label(self): |
---|
| 551 | container_title = self.context.__parent__.title |
---|
[7240] | 552 | return '%s Application Record %s' % ( |
---|
| 553 | container_title, self.context.application_number) |
---|
[6363] | 554 | |
---|
[7347] | 555 | def getCourseAdmitted(self): |
---|
| 556 | """Return title and code in html format to the certificate |
---|
| 557 | admitted. |
---|
| 558 | """ |
---|
| 559 | course_admitted = self.context.course_admitted |
---|
[7351] | 560 | #if ICertificate.providedBy(course_admitted): |
---|
| 561 | if getattr(course_admitted, '__parent__',None): |
---|
[7347] | 562 | title = course_admitted.title |
---|
| 563 | code = course_admitted.code |
---|
| 564 | return '%s - %s' %(code,title) |
---|
| 565 | return '' |
---|
[6358] | 566 | |
---|
| 567 | def setUpWidgets(self, ignore_request=False): |
---|
| 568 | self.adapters = {} |
---|
| 569 | self.widgets = setUpEditWidgets( |
---|
| 570 | self.form_fields, self.prefix, self.context, self.request, |
---|
| 571 | adapters=self.adapters, for_display=True, |
---|
| 572 | ignore_request=ignore_request |
---|
| 573 | ) |
---|
| 574 | |
---|
| 575 | def render(self): |
---|
[7392] | 576 | pdfstream = getAdapter(self.context, IPDF, name='application_slip')( |
---|
| 577 | view=self) |
---|
[6358] | 578 | self.response.setHeader( |
---|
| 579 | 'Content-Type', 'application/pdf') |
---|
[7392] | 580 | return pdfstream |
---|
[6358] | 581 | |
---|
[7081] | 582 | def handle_img_upload(upload, context, view): |
---|
[7063] | 583 | """Handle upload of applicant image. |
---|
[7081] | 584 | |
---|
| 585 | Returns `True` in case of success or `False`. |
---|
| 586 | |
---|
| 587 | Please note that file pointer passed in (`upload`) most probably |
---|
| 588 | points to end of file when leaving this function. |
---|
[7063] | 589 | """ |
---|
[7081] | 590 | size = file_size(upload) |
---|
| 591 | if size > MAX_UPLOAD_SIZE: |
---|
| 592 | view.flash('Uploaded image is too big!') |
---|
| 593 | return False |
---|
[7247] | 594 | dummy, ext = os.path.splitext(upload.filename) |
---|
| 595 | ext.lower() |
---|
| 596 | if ext != '.jpg': |
---|
| 597 | view.flash('jpg file extension expected.') |
---|
| 598 | return False |
---|
[7081] | 599 | upload.seek(0) # file pointer moved when determining size |
---|
[7063] | 600 | store = getUtility(IExtFileStore) |
---|
| 601 | file_id = IFileStoreNameChooser(context).chooseName() |
---|
| 602 | store.createFile(file_id, upload) |
---|
[7081] | 603 | return True |
---|
[7063] | 604 | |
---|
[7321] | 605 | class ApplicantManageFormPage(SIRPEditFormPage): |
---|
[6196] | 606 | """A full edit view for applicant data. |
---|
| 607 | """ |
---|
| 608 | grok.context(IApplicant) |
---|
[7200] | 609 | grok.name('manage') |
---|
[7136] | 610 | grok.require('waeup.manageApplication') |
---|
[6476] | 611 | form_fields = grok.AutoFields(IApplicant) |
---|
[6196] | 612 | form_fields['date_of_birth'].custom_widget = FriendlyDateWidget('le-year') |
---|
[7351] | 613 | form_fields['student_id'].for_display = True |
---|
[7378] | 614 | form_fields['applicant_id'].for_display = True |
---|
[7423] | 615 | form_fields['phone'].custom_widget = PhoneWidget |
---|
[7200] | 616 | grok.template('applicanteditpage') |
---|
[6322] | 617 | manage_applications = True |
---|
[6196] | 618 | pnav = 3 |
---|
[7250] | 619 | display_actions = [['Save', 'Final Submit'], |
---|
| 620 | ['Add online payment ticket','Remove selected tickets']] |
---|
[6196] | 621 | |
---|
| 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 |
---|
[7240] | 639 | return '%s Application Form %s' % ( |
---|
| 640 | container_title, 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() |
---|
[6355] | 650 | return [dict(name='', title='No transition')] +[ |
---|
| 651 | dict(name=x, title=y) for x, y in allowed_transitions] |
---|
[6303] | 652 | |
---|
[7489] | 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) |
---|
[6196] | 682 | self.flash('Form has been saved.') |
---|
[6475] | 683 | ob_class = self.__implemented__.__name__.replace('waeup.sirp.','') |
---|
[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: |
---|
| 697 | self.flash('No payment selected.') |
---|
| 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: |
---|
| 710 | self.flash('Could not delete %s: %s: %s' % ( |
---|
| 711 | id, sys.exc_info()[0], sys.exc_info()[1])) |
---|
| 712 | if len(deleted): |
---|
| 713 | self.flash('Successfully removed: %s' % ', '.join(deleted)) |
---|
| 714 | ob_class = self.__implemented__.__name__.replace('waeup.sirp.','') |
---|
[7363] | 715 | self.context.loggerInfo( |
---|
| 716 | ob_class, 'removed: % s' % ', '.join(deleted)) |
---|
[7250] | 717 | return |
---|
| 718 | |
---|
[7252] | 719 | # We explicitely want the forms to be validated before payment tickets |
---|
| 720 | # can be created. If no validation is requested, use |
---|
[7459] | 721 | # 'validator=NullValidator' in the action directive |
---|
| 722 | @action('Add online payment ticket') |
---|
[7250] | 723 | def addPaymentTicket(self, **data): |
---|
| 724 | self.redirect(self.url(self.context, '@@addafp')) |
---|
[7252] | 725 | return |
---|
[7250] | 726 | |
---|
[7330] | 727 | @jsaction('Remove selected tickets') |
---|
[7250] | 728 | def removePaymentTickets(self, **data): |
---|
| 729 | self.delPaymentTickets(**data) |
---|
| 730 | self.redirect(self.url(self.context) + '/@@manage') |
---|
| 731 | return |
---|
| 732 | |
---|
[7200] | 733 | class ApplicantEditFormPage(ApplicantManageFormPage): |
---|
[5982] | 734 | """An applicant-centered edit view for applicant data. |
---|
| 735 | """ |
---|
[6196] | 736 | grok.context(IApplicantEdit) |
---|
[5273] | 737 | grok.name('edit') |
---|
[6198] | 738 | grok.require('waeup.handleApplication') |
---|
[6459] | 739 | form_fields = grok.AutoFields(IApplicantEdit).omit( |
---|
[6476] | 740 | 'locked', 'course_admitted', 'student_id', |
---|
[7378] | 741 | 'screening_score', 'reg_number' |
---|
[6459] | 742 | ) |
---|
[6054] | 743 | form_fields['date_of_birth'].custom_widget = FriendlyDateWidget('le-year') |
---|
[7433] | 744 | form_fields['phone'].custom_widget = PhoneWidget |
---|
[7459] | 745 | form_fields['applicant_id'].for_display = True |
---|
[7200] | 746 | grok.template('applicanteditpage') |
---|
[6322] | 747 | manage_applications = False |
---|
[5484] | 748 | |
---|
[7250] | 749 | @property |
---|
| 750 | def display_actions(self): |
---|
| 751 | state = IWorkflowState(self.context).getState() |
---|
| 752 | if state == INITIALIZED: |
---|
| 753 | actions = [[],[]] |
---|
| 754 | elif state == STARTED: |
---|
| 755 | actions = [['Save'], |
---|
| 756 | ['Add online payment ticket','Remove selected tickets']] |
---|
| 757 | elif state == PAID: |
---|
| 758 | actions = [['Save', 'Final Submit'], |
---|
| 759 | ['Remove selected tickets']] |
---|
[7351] | 760 | else: |
---|
[7250] | 761 | actions = [[],[]] |
---|
| 762 | return actions |
---|
| 763 | |
---|
[7330] | 764 | def unremovable(self, ticket): |
---|
| 765 | state = IWorkflowState(self.context).getState() |
---|
| 766 | return ticket.r_code or state in (INITIALIZED, SUBMITTED) |
---|
| 767 | |
---|
[7145] | 768 | def emit_lock_message(self): |
---|
[6105] | 769 | self.flash('The requested form is locked (read-only).') |
---|
[5941] | 770 | self.redirect(self.url(self.context)) |
---|
| 771 | return |
---|
[6078] | 772 | |
---|
[5686] | 773 | def update(self): |
---|
[5941] | 774 | if self.context.locked: |
---|
[7145] | 775 | self.emit_lock_message() |
---|
[5941] | 776 | return |
---|
[7200] | 777 | super(ApplicantEditFormPage, self).update() |
---|
[5686] | 778 | return |
---|
[5952] | 779 | |
---|
[6196] | 780 | def dataNotComplete(self): |
---|
[7252] | 781 | store = getUtility(IExtFileStore) |
---|
| 782 | if not store.getFileByContext(self.context, attr=u'passport.jpg'): |
---|
| 783 | return 'No passport picture uploaded.' |
---|
[6322] | 784 | if not self.request.form.get('confirm_passport', False): |
---|
[7252] | 785 | return 'Passport picture confirmation box not ticked.' |
---|
[6196] | 786 | return False |
---|
[5952] | 787 | |
---|
[7252] | 788 | # We explicitely want the forms to be validated before payment tickets |
---|
| 789 | # can be created. If no validation is requested, use |
---|
[7459] | 790 | # 'validator=NullValidator' in the action directive |
---|
| 791 | @action('Add online payment ticket') |
---|
[7250] | 792 | def addPaymentTicket(self, **data): |
---|
| 793 | self.redirect(self.url(self.context, '@@addafp')) |
---|
[7252] | 794 | return |
---|
[7250] | 795 | |
---|
[7330] | 796 | @jsaction('Remove selected tickets') |
---|
[7250] | 797 | def removePaymentTickets(self, **data): |
---|
| 798 | self.delPaymentTickets(**data) |
---|
| 799 | self.redirect(self.url(self.context) + '/@@edit') |
---|
| 800 | return |
---|
| 801 | |
---|
[7459] | 802 | @action('Save') |
---|
[5273] | 803 | def save(self, **data): |
---|
[7084] | 804 | if self.passport_changed is False: # False is not None! |
---|
| 805 | return # error during image upload. Ignore other values |
---|
[5273] | 806 | self.applyData(self.context, **data) |
---|
[6196] | 807 | self.flash('Form has been saved.') |
---|
[5273] | 808 | return |
---|
| 809 | |
---|
[7459] | 810 | @action('Final Submit') |
---|
[5484] | 811 | def finalsubmit(self, **data): |
---|
[7084] | 812 | if self.passport_changed is False: # False is not None! |
---|
| 813 | return # error during image upload. Ignore other values |
---|
[6196] | 814 | if self.dataNotComplete(): |
---|
| 815 | self.flash(self.dataNotComplete()) |
---|
[5941] | 816 | return |
---|
[7252] | 817 | self.applyData(self.context, **data) |
---|
[6303] | 818 | state = IWorkflowState(self.context).getState() |
---|
[6322] | 819 | # This shouldn't happen, but the application officer |
---|
| 820 | # might have forgotten to lock the form after changing the state |
---|
[7250] | 821 | if state != PAID: |
---|
[6322] | 822 | self.flash('This form cannot be submitted. Wrong state!') |
---|
[6303] | 823 | return |
---|
| 824 | IWorkflowInfo(self.context).fireTransition('submit') |
---|
[6476] | 825 | self.context.application_date = datetime.now() |
---|
[5941] | 826 | self.context.locked = True |
---|
[6196] | 827 | self.flash('Form has been submitted.') |
---|
| 828 | self.redirect(self.url(self.context)) |
---|
[5273] | 829 | return |
---|
[5941] | 830 | |
---|
[7063] | 831 | class PassportImage(grok.View): |
---|
| 832 | """Renders the passport image for applicants. |
---|
| 833 | """ |
---|
| 834 | grok.name('passport.jpg') |
---|
| 835 | grok.context(IApplicant) |
---|
[7113] | 836 | grok.require('waeup.viewApplication') |
---|
[7063] | 837 | |
---|
| 838 | def render(self): |
---|
| 839 | # A filename chooser turns a context into a filename suitable |
---|
| 840 | # for file storage. |
---|
| 841 | image = getUtility(IExtFileStore).getFileByContext(self.context) |
---|
| 842 | self.response.setHeader( |
---|
| 843 | 'Content-Type', 'image/jpeg') |
---|
| 844 | if image is None: |
---|
| 845 | # show placeholder image |
---|
[7089] | 846 | return open(DEFAULT_PASSPORT_IMAGE_PATH, 'rb').read() |
---|
[7063] | 847 | return image |
---|
[7363] | 848 | |
---|
| 849 | class ApplicantRegistrationPage(SIRPAddFormPage): |
---|
| 850 | """Captcha'd registration page for applicants. |
---|
| 851 | """ |
---|
| 852 | grok.context(IApplicantsContainer) |
---|
| 853 | grok.name('register') |
---|
[7373] | 854 | grok.require('waeup.Anonymous') |
---|
[7363] | 855 | grok.template('applicantregister') |
---|
| 856 | form_fields = grok.AutoFields(IApplicantEdit).select( |
---|
| 857 | 'firstname', 'middlename', 'lastname', 'email', 'phone') |
---|
| 858 | form_fields['phone'].custom_widget = PhoneWidget |
---|
| 859 | |
---|
[7368] | 860 | @property |
---|
| 861 | def label(self): |
---|
| 862 | return "Register for %s Application" % self.context.title |
---|
| 863 | |
---|
[7363] | 864 | def update(self): |
---|
[7368] | 865 | # Check if application has started ... |
---|
| 866 | if not self.context.startdate or self.context.startdate > date.today(): |
---|
| 867 | self.flash('Application has not yet started.') |
---|
| 868 | self.redirect(self.url(self.context)) |
---|
| 869 | return |
---|
| 870 | # ... or ended |
---|
| 871 | if not self.context.enddate or self.context.enddate < date.today(): |
---|
| 872 | self.flash('Application has ended.') |
---|
| 873 | self.redirect(self.url(self.context)) |
---|
| 874 | return |
---|
| 875 | # Handle captcha |
---|
[7363] | 876 | self.captcha = getUtility(ICaptchaManager).getCaptcha() |
---|
| 877 | self.captcha_result = self.captcha.verify(self.request) |
---|
| 878 | self.captcha_code = self.captcha.display(self.captcha_result.error_code) |
---|
| 879 | return |
---|
| 880 | |
---|
[7494] | 881 | @action('Get login credentials', style='primary') |
---|
[7363] | 882 | def register(self, **data): |
---|
| 883 | if not self.captcha_result.is_valid: |
---|
| 884 | # captcha will display error messages automatically. |
---|
| 885 | # No need to flash something. |
---|
| 886 | return |
---|
[7365] | 887 | # Add applicant and create password |
---|
[7363] | 888 | applicant = createObject('waeup.Applicant') |
---|
| 889 | self.applyData(applicant, **data) |
---|
| 890 | self.context.addApplicant(applicant) |
---|
[7399] | 891 | sirp_utils = getUtility(ISIRPUtils) |
---|
| 892 | password = sirp_utils.genPassword() |
---|
[7380] | 893 | IUserAccount(applicant).setPassword(password) |
---|
[7365] | 894 | # Send email with credentials |
---|
[7399] | 895 | login_url = self.url(grok.getSite(), 'login') |
---|
[7407] | 896 | msg = 'You have successfully been registered for the' |
---|
| 897 | if sirp_utils.sendCredentials(IUserAccount(applicant), |
---|
| 898 | password, login_url, msg): |
---|
[7380] | 899 | self.redirect(self.url(self.context, 'registration_complete', |
---|
| 900 | data = dict(email=applicant.email))) |
---|
| 901 | return |
---|
| 902 | else: |
---|
| 903 | self.flash('Email could not been sent. Please retry later.') |
---|
| 904 | return |
---|
| 905 | |
---|
| 906 | class ApplicantRegistrationEmailSent(SIRPPage): |
---|
| 907 | """Landing page after successful registration. |
---|
| 908 | """ |
---|
| 909 | grok.name('registration_complete') |
---|
| 910 | grok.require('waeup.Public') |
---|
| 911 | grok.template('applicantregemailsent') |
---|
| 912 | label = 'Your registration was successful' |
---|
| 913 | |
---|
| 914 | def update(self, email=None): |
---|
| 915 | self.email = email |
---|
| 916 | return |
---|