[5273] | 1 | ## $Id: browser.py 7380 2011-12-18 15:26:41Z uli $ |
---|
[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 |
---|
[7240] | 25 | from zope.component import getUtility, createObject |
---|
[6358] | 26 | from zope.formlib.form import setUpEditWidgets |
---|
[7322] | 27 | from hurry.workflow.interfaces import ( |
---|
| 28 | IWorkflowInfo, IWorkflowState, InvalidTransitionError) |
---|
[6357] | 29 | from reportlab.pdfgen import canvas |
---|
[6364] | 30 | from reportlab.lib.units import cm |
---|
[6390] | 31 | from reportlab.lib.pagesizes import A4 |
---|
[6364] | 32 | from reportlab.lib.styles import getSampleStyleSheet |
---|
| 33 | from reportlab.platypus import (Frame, Paragraph, Image, |
---|
| 34 | Table, Spacer) |
---|
| 35 | from reportlab.platypus.tables import TableStyle |
---|
[7363] | 36 | from waeup.sirp.applicants.interfaces import ( |
---|
| 37 | IApplicant, IApplicantEdit, IApplicantsRoot, |
---|
| 38 | IApplicantsContainer, IApplicantsContainerAdd, application_types_vocab, |
---|
| 39 | MAX_UPLOAD_SIZE, IApplicantOnlinePayment, |
---|
| 40 | ) |
---|
| 41 | from waeup.sirp.applicants.workflow import INITIALIZED, STARTED, PAID, SUBMITTED |
---|
[5273] | 42 | from waeup.sirp.browser import ( |
---|
[7363] | 43 | SIRPPage, SIRPEditFormPage, SIRPAddFormPage, SIRPDisplayFormPage, |
---|
| 44 | DEFAULT_PASSPORT_IMAGE_PATH) |
---|
| 45 | from waeup.sirp.browser.interfaces import ICaptchaManager |
---|
[6081] | 46 | from waeup.sirp.browser.breadcrumbs import Breadcrumb |
---|
[7332] | 47 | from waeup.sirp.browser.layout import NullValidator, jsaction, JSAction |
---|
[6321] | 48 | from waeup.sirp.browser.pages import add_local_role, del_local_roles |
---|
[7330] | 49 | from waeup.sirp.browser.resources import datepicker, tabs, datatable, warning |
---|
[7255] | 50 | from waeup.sirp.browser.viewlets import ManageActionButton, PrimaryNavTab |
---|
[7063] | 51 | from waeup.sirp.interfaces import ( |
---|
[7321] | 52 | ISIRPObject, ILocalRolesAssignable, IExtFileStore, |
---|
[7365] | 53 | IFileStoreNameChooser, IPasswordValidator, IUserAccount, ISIRPUtils) |
---|
[6321] | 54 | from waeup.sirp.permissions import get_users_with_local_roles |
---|
[7363] | 55 | from waeup.sirp.students.viewlets import PrimaryStudentNavTab |
---|
| 56 | from waeup.sirp.students.interfaces import IStudentsUtils |
---|
[6254] | 57 | from waeup.sirp.university.interfaces import ICertificate |
---|
[7081] | 58 | from waeup.sirp.utils.helpers import string_from_bytes, file_size |
---|
[6054] | 59 | from waeup.sirp.widgets.datewidget import ( |
---|
[7250] | 60 | FriendlyDateWidget, FriendlyDateDisplayWidget, |
---|
| 61 | FriendlyDatetimeDisplayWidget) |
---|
[7363] | 62 | from waeup.sirp.widgets.phonewidget import PhoneWidget |
---|
[6084] | 63 | from waeup.sirp.widgets.restwidget import ReSTDisplayWidget |
---|
[5320] | 64 | |
---|
[7321] | 65 | grok.context(ISIRPObject) # Make ISIRPObject the default context |
---|
[5273] | 66 | |
---|
[7321] | 67 | class ApplicantsRootPage(SIRPPage): |
---|
[5822] | 68 | grok.context(IApplicantsRoot) |
---|
| 69 | grok.name('index') |
---|
[6153] | 70 | grok.require('waeup.Public') |
---|
[5822] | 71 | title = 'Applicants' |
---|
[6069] | 72 | label = 'Application Section' |
---|
[5843] | 73 | pnav = 3 |
---|
[6012] | 74 | |
---|
| 75 | def update(self): |
---|
[6067] | 76 | super(ApplicantsRootPage, self).update() |
---|
[6012] | 77 | datatable.need() |
---|
| 78 | return |
---|
| 79 | |
---|
[5828] | 80 | class ManageApplicantsRootActionButton(ManageActionButton): |
---|
| 81 | grok.context(IApplicantsRoot) |
---|
[6067] | 82 | grok.view(ApplicantsRootPage) |
---|
[7136] | 83 | grok.require('waeup.manageApplication') |
---|
[6069] | 84 | text = 'Manage application section' |
---|
[5828] | 85 | |
---|
[7321] | 86 | class ApplicantsRootManageFormPage(SIRPEditFormPage): |
---|
[5828] | 87 | grok.context(IApplicantsRoot) |
---|
| 88 | grok.name('manage') |
---|
[6107] | 89 | grok.template('applicantsrootmanagepage') |
---|
[6069] | 90 | title = 'Applicants' |
---|
| 91 | label = 'Manage application section' |
---|
[5843] | 92 | pnav = 3 |
---|
[7136] | 93 | grok.require('waeup.manageApplication') |
---|
[6069] | 94 | taboneactions = ['Add applicants container', 'Remove selected','Cancel'] |
---|
[6184] | 95 | tabtwoactions1 = ['Remove selected local roles'] |
---|
| 96 | tabtwoactions2 = ['Add local role'] |
---|
[6069] | 97 | subunits = 'Applicants Containers' |
---|
[6078] | 98 | |
---|
[6069] | 99 | def update(self): |
---|
| 100 | tabs.need() |
---|
[6108] | 101 | datatable.need() |
---|
[7330] | 102 | warning.need() |
---|
[6069] | 103 | return super(ApplicantsRootManageFormPage, self).update() |
---|
[5828] | 104 | |
---|
[6184] | 105 | def getLocalRoles(self): |
---|
| 106 | roles = ILocalRolesAssignable(self.context) |
---|
| 107 | return roles() |
---|
| 108 | |
---|
| 109 | def getUsers(self): |
---|
| 110 | """Get a list of all users. |
---|
| 111 | """ |
---|
| 112 | for key, val in grok.getSite()['users'].items(): |
---|
| 113 | url = self.url(val) |
---|
| 114 | yield(dict(url=url, name=key, val=val)) |
---|
| 115 | |
---|
| 116 | def getUsersWithLocalRoles(self): |
---|
| 117 | return get_users_with_local_roles(self.context) |
---|
| 118 | |
---|
[7330] | 119 | @jsaction('Remove selected') |
---|
[6069] | 120 | def delApplicantsContainers(self, **data): |
---|
| 121 | form = self.request.form |
---|
| 122 | child_id = form['val_id'] |
---|
| 123 | if not isinstance(child_id, list): |
---|
| 124 | child_id = [child_id] |
---|
| 125 | deleted = [] |
---|
| 126 | for id in child_id: |
---|
| 127 | try: |
---|
| 128 | del self.context[id] |
---|
| 129 | deleted.append(id) |
---|
| 130 | except: |
---|
| 131 | self.flash('Could not delete %s: %s: %s' % ( |
---|
| 132 | id, sys.exc_info()[0], sys.exc_info()[1])) |
---|
| 133 | if len(deleted): |
---|
| 134 | self.flash('Successfully removed: %s' % ', '.join(deleted)) |
---|
[6078] | 135 | self.redirect(self.url(self.context, '@@manage')+'#tab-1') |
---|
| 136 | return |
---|
[5828] | 137 | |
---|
[6069] | 138 | @grok.action('Add applicants container', validator=NullValidator) |
---|
| 139 | def addApplicantsContainer(self, **data): |
---|
| 140 | self.redirect(self.url(self.context, '@@add')) |
---|
[6078] | 141 | return |
---|
| 142 | |
---|
[6069] | 143 | @grok.action('Cancel', validator=NullValidator) |
---|
| 144 | def cancel(self, **data): |
---|
| 145 | self.redirect(self.url(self.context)) |
---|
[6078] | 146 | return |
---|
| 147 | |
---|
[6184] | 148 | @grok.action('Add local role', validator=NullValidator) |
---|
| 149 | def addLocalRole(self, **data): |
---|
| 150 | return add_local_role(self,2, **data) |
---|
| 151 | |
---|
| 152 | @grok.action('Remove selected local roles') |
---|
| 153 | def delLocalRoles(self, **data): |
---|
| 154 | return del_local_roles(self,2,**data) |
---|
| 155 | |
---|
[7321] | 156 | class ApplicantsContainerAddFormPage(SIRPAddFormPage): |
---|
[5822] | 157 | grok.context(IApplicantsRoot) |
---|
[7136] | 158 | grok.require('waeup.manageApplication') |
---|
[5822] | 159 | grok.name('add') |
---|
[6107] | 160 | grok.template('applicantscontaineraddpage') |
---|
[6069] | 161 | title = 'Applicants' |
---|
| 162 | label = 'Add applicants container' |
---|
[5843] | 163 | pnav = 3 |
---|
[6078] | 164 | |
---|
[6103] | 165 | form_fields = grok.AutoFields( |
---|
| 166 | IApplicantsContainerAdd).omit('code').omit('title') |
---|
[6083] | 167 | form_fields['startdate'].custom_widget = FriendlyDateWidget('le') |
---|
| 168 | form_fields['enddate'].custom_widget = FriendlyDateWidget('le') |
---|
[6078] | 169 | |
---|
[6083] | 170 | def update(self): |
---|
| 171 | datepicker.need() # Enable jQuery datepicker in date fields. |
---|
| 172 | return super(ApplicantsContainerAddFormPage, self).update() |
---|
| 173 | |
---|
[6069] | 174 | @grok.action('Add applicants container') |
---|
| 175 | def addApplicantsContainer(self, **data): |
---|
[6103] | 176 | year = data['year'] |
---|
| 177 | code = u'%s%s' % (data['prefix'], year) |
---|
| 178 | prefix = application_types_vocab.getTerm(data['prefix']) |
---|
| 179 | title = u'%s %s/%s' % (prefix.title, year, year + 1) |
---|
[6087] | 180 | if code in self.context.keys(): |
---|
[6105] | 181 | self.flash( |
---|
| 182 | 'An applicants container for the same application ' |
---|
| 183 | 'type and entrance year exists already in the database.') |
---|
[5822] | 184 | return |
---|
| 185 | # Add new applicants container... |
---|
[6083] | 186 | provider = data['provider'][1] |
---|
[5822] | 187 | container = provider.factory() |
---|
[6069] | 188 | self.applyData(container, **data) |
---|
[6087] | 189 | container.code = code |
---|
| 190 | container.title = title |
---|
| 191 | self.context[code] = container |
---|
[6105] | 192 | self.flash('Added: "%s".' % code) |
---|
[6069] | 193 | self.redirect(self.url(self.context, u'@@manage')+'#tab-1') |
---|
[5822] | 194 | return |
---|
[6078] | 195 | |
---|
[6103] | 196 | @grok.action('Cancel', validator=NullValidator) |
---|
[6069] | 197 | def cancel(self, **data): |
---|
[6103] | 198 | self.redirect(self.url(self.context, '@@manage') + '#tab-1') |
---|
[6078] | 199 | |
---|
[5845] | 200 | class ApplicantsRootBreadcrumb(Breadcrumb): |
---|
| 201 | """A breadcrumb for applicantsroot. |
---|
| 202 | """ |
---|
| 203 | grok.context(IApplicantsRoot) |
---|
[6654] | 204 | title = u'Applicants' |
---|
[6078] | 205 | |
---|
[5845] | 206 | class ApplicantsContainerBreadcrumb(Breadcrumb): |
---|
| 207 | """A breadcrumb for applicantscontainers. |
---|
| 208 | """ |
---|
| 209 | grok.context(IApplicantsContainer) |
---|
[6319] | 210 | |
---|
[6153] | 211 | class ApplicantBreadcrumb(Breadcrumb): |
---|
| 212 | """A breadcrumb for applicants. |
---|
| 213 | """ |
---|
| 214 | grok.context(IApplicant) |
---|
[6319] | 215 | |
---|
[6153] | 216 | @property |
---|
| 217 | def title(self): |
---|
| 218 | """Get a title for a context. |
---|
| 219 | """ |
---|
[7240] | 220 | return self.context.application_number |
---|
[5828] | 221 | |
---|
[7250] | 222 | class OnlinePaymentBreadcrumb(Breadcrumb): |
---|
| 223 | """A breadcrumb for payments. |
---|
| 224 | """ |
---|
| 225 | grok.context(IApplicantOnlinePayment) |
---|
| 226 | |
---|
| 227 | @property |
---|
| 228 | def title(self): |
---|
| 229 | return self.context.p_id |
---|
| 230 | |
---|
[7184] | 231 | class ApplicantsAuthTab(PrimaryNavTab): |
---|
[6153] | 232 | """Applicants tab in primary navigation. |
---|
[5828] | 233 | """ |
---|
[7321] | 234 | grok.context(ISIRPObject) |
---|
[5828] | 235 | grok.order(3) |
---|
[7250] | 236 | grok.require('waeup.viewApplicantsTab') |
---|
[5843] | 237 | pnav = 3 |
---|
[5828] | 238 | tab_title = u'Applicants' |
---|
| 239 | |
---|
| 240 | @property |
---|
| 241 | def link_target(self): |
---|
| 242 | return self.view.application_url('applicants') |
---|
| 243 | |
---|
[7184] | 244 | class ApplicantsAnonTab(ApplicantsAuthTab): |
---|
| 245 | """Applicants tab in primary navigation. |
---|
| 246 | |
---|
| 247 | Display tab only for anonymous. Authenticated users can call the |
---|
| 248 | form from the user navigation bar. |
---|
| 249 | """ |
---|
| 250 | grok.require('waeup.Anonymous') |
---|
| 251 | tab_title = u'Application' |
---|
| 252 | |
---|
| 253 | # Also zope.manager has role Anonymous. |
---|
[7243] | 254 | # To avoid displaying this tab, we have to check the principal id too. |
---|
| 255 | @property |
---|
| 256 | def link_target(self): |
---|
| 257 | if self.request.principal.id == 'zope.anybody': |
---|
| 258 | return self.view.application_url('applicants') |
---|
| 259 | return |
---|
[7184] | 260 | |
---|
[7240] | 261 | class MyApplicationDataTab(PrimaryStudentNavTab): |
---|
| 262 | """MyData-tab in primary navigation. |
---|
| 263 | """ |
---|
| 264 | grok.order(3) |
---|
| 265 | grok.require('waeup.viewMyApplicationDataTab') |
---|
| 266 | pnav = 3 |
---|
| 267 | tab_title = u'My Data' |
---|
| 268 | |
---|
| 269 | @property |
---|
| 270 | def link_target(self): |
---|
| 271 | try: |
---|
| 272 | container, application_number = self.request.principal.id.split('_') |
---|
| 273 | except ValueError: |
---|
| 274 | return |
---|
| 275 | rel_link = '/applicants/%s/%s' % (container, application_number) |
---|
| 276 | return self.view.application_url() + rel_link |
---|
| 277 | |
---|
[7321] | 278 | class ApplicantsContainerPage(SIRPDisplayFormPage): |
---|
[5830] | 279 | """The standard view for regular applicant containers. |
---|
| 280 | """ |
---|
| 281 | grok.context(IApplicantsContainer) |
---|
| 282 | grok.name('index') |
---|
[6153] | 283 | grok.require('waeup.Public') |
---|
[6029] | 284 | grok.template('applicantscontainerpage') |
---|
[5850] | 285 | pnav = 3 |
---|
[6053] | 286 | |
---|
[6105] | 287 | form_fields = grok.AutoFields(IApplicantsContainer).omit('title') |
---|
[6054] | 288 | form_fields['startdate'].custom_widget = FriendlyDateDisplayWidget('le') |
---|
| 289 | form_fields['enddate'].custom_widget = FriendlyDateDisplayWidget('le') |
---|
[6084] | 290 | form_fields['description'].custom_widget = ReSTDisplayWidget |
---|
[6053] | 291 | |
---|
[5837] | 292 | @property |
---|
| 293 | def title(self): |
---|
[6087] | 294 | return "Applicants Container: %s" % self.context.title |
---|
[5837] | 295 | |
---|
| 296 | @property |
---|
| 297 | def label(self): |
---|
[6087] | 298 | return self.context.title |
---|
[5830] | 299 | |
---|
[6107] | 300 | class ApplicantsContainerManageActionButton(ManageActionButton): |
---|
[6336] | 301 | grok.order(1) |
---|
[5832] | 302 | grok.context(IApplicantsContainer) |
---|
| 303 | grok.view(ApplicantsContainerPage) |
---|
[7136] | 304 | grok.require('waeup.manageApplication') |
---|
[6070] | 305 | text = 'Manage applicants container' |
---|
[5832] | 306 | |
---|
[7368] | 307 | class ApplicantRegisterActionButton(ManageActionButton): |
---|
| 308 | grok.order(2) |
---|
| 309 | grok.context(IApplicantsContainer) |
---|
| 310 | grok.view(ApplicantsContainerPage) |
---|
| 311 | grok.require('waeup.Anonymous') |
---|
| 312 | icon = 'actionicon_login.png' |
---|
| 313 | text = 'Register for application' |
---|
| 314 | target = 'register' |
---|
| 315 | |
---|
[7321] | 316 | class ApplicantsContainerManageFormPage(SIRPEditFormPage): |
---|
[5837] | 317 | grok.context(IApplicantsContainer) |
---|
[5850] | 318 | grok.name('manage') |
---|
[6107] | 319 | grok.template('applicantscontainermanagepage') |
---|
[6105] | 320 | form_fields = grok.AutoFields(IApplicantsContainer).omit('title') |
---|
| 321 | taboneactions = ['Save','Cancel'] |
---|
| 322 | tabtwoactions = ['Add applicant', 'Remove selected','Cancel'] |
---|
[6184] | 323 | tabthreeactions1 = ['Remove selected local roles'] |
---|
| 324 | tabthreeactions2 = ['Add local role'] |
---|
[5844] | 325 | # Use friendlier date widget... |
---|
[6054] | 326 | form_fields['startdate'].custom_widget = FriendlyDateWidget('le') |
---|
| 327 | form_fields['enddate'].custom_widget = FriendlyDateWidget('le') |
---|
[7136] | 328 | grok.require('waeup.manageApplication') |
---|
[5850] | 329 | |
---|
| 330 | @property |
---|
| 331 | def title(self): |
---|
[6087] | 332 | return "Applicants Container: %s" % self.context.title |
---|
[6078] | 333 | |
---|
[5850] | 334 | @property |
---|
| 335 | def label(self): |
---|
[6087] | 336 | return 'Manage applicants container' |
---|
[5850] | 337 | |
---|
[5845] | 338 | pnav = 3 |
---|
[5837] | 339 | |
---|
| 340 | def update(self): |
---|
[5850] | 341 | datepicker.need() # Enable jQuery datepicker in date fields. |
---|
[5982] | 342 | tabs.need() |
---|
[7330] | 343 | warning.need() |
---|
[6015] | 344 | datatable.need() # Enable jQurey datatables for contents listing |
---|
[6107] | 345 | return super(ApplicantsContainerManageFormPage, self).update() |
---|
[5837] | 346 | |
---|
[6184] | 347 | def getLocalRoles(self): |
---|
| 348 | roles = ILocalRolesAssignable(self.context) |
---|
| 349 | return roles() |
---|
| 350 | |
---|
| 351 | def getUsers(self): |
---|
| 352 | """Get a list of all users. |
---|
| 353 | """ |
---|
| 354 | for key, val in grok.getSite()['users'].items(): |
---|
| 355 | url = self.url(val) |
---|
| 356 | yield(dict(url=url, name=key, val=val)) |
---|
| 357 | |
---|
| 358 | def getUsersWithLocalRoles(self): |
---|
| 359 | return get_users_with_local_roles(self.context) |
---|
| 360 | |
---|
[5850] | 361 | @grok.action('Save') |
---|
[5837] | 362 | def apply(self, **data): |
---|
| 363 | self.applyData(self.context, **data) |
---|
| 364 | self.flash('Data saved.') |
---|
| 365 | return |
---|
[6078] | 366 | |
---|
[7330] | 367 | @jsaction('Remove selected') |
---|
[6105] | 368 | def delApplicant(self, **data): |
---|
[6189] | 369 | form = self.request.form |
---|
| 370 | if form.has_key('val_id'): |
---|
| 371 | child_id = form['val_id'] |
---|
| 372 | else: |
---|
| 373 | self.flash('No applicant selected!') |
---|
| 374 | self.redirect(self.url(self.context, '@@manage')+'#tab-2') |
---|
| 375 | return |
---|
| 376 | if not isinstance(child_id, list): |
---|
| 377 | child_id = [child_id] |
---|
| 378 | deleted = [] |
---|
| 379 | for id in child_id: |
---|
| 380 | try: |
---|
| 381 | del self.context[id] |
---|
| 382 | deleted.append(id) |
---|
| 383 | except: |
---|
| 384 | self.flash('Could not delete %s: %s: %s' % ( |
---|
| 385 | id, sys.exc_info()[0], sys.exc_info()[1])) |
---|
| 386 | if len(deleted): |
---|
| 387 | self.flash('Successfully removed: %s' % ', '.join(deleted)) |
---|
| 388 | self.redirect(self.url(self.context, u'@@manage')+'#tab-2') |
---|
| 389 | return |
---|
[6105] | 390 | |
---|
| 391 | @grok.action('Add applicant', validator=NullValidator) |
---|
| 392 | def addApplicant(self, **data): |
---|
[6327] | 393 | self.redirect(self.url(self.context, 'addapplicant')) |
---|
| 394 | return |
---|
[6105] | 395 | |
---|
| 396 | @grok.action('Cancel', validator=NullValidator) |
---|
[5837] | 397 | def cancel(self, **data): |
---|
| 398 | self.redirect(self.url(self.context)) |
---|
| 399 | return |
---|
[5886] | 400 | |
---|
[6184] | 401 | @grok.action('Add local role', validator=NullValidator) |
---|
| 402 | def addLocalRole(self, **data): |
---|
| 403 | return add_local_role(self,3, **data) |
---|
[6105] | 404 | |
---|
[6184] | 405 | @grok.action('Remove selected local roles') |
---|
| 406 | def delLocalRoles(self, **data): |
---|
| 407 | return del_local_roles(self,3,**data) |
---|
| 408 | |
---|
[7321] | 409 | class ApplicantAddFormPage(SIRPAddFormPage): |
---|
[6622] | 410 | """Add-form to add an applicant. |
---|
[6327] | 411 | """ |
---|
| 412 | grok.context(IApplicantsContainer) |
---|
[7136] | 413 | grok.require('waeup.manageApplication') |
---|
[6327] | 414 | grok.name('addapplicant') |
---|
[7240] | 415 | #grok.template('applicantaddpage') |
---|
| 416 | form_fields = grok.AutoFields(IApplicant).select( |
---|
[7356] | 417 | 'firstname', 'middlename', 'lastname', |
---|
[7240] | 418 | 'email', 'phone') |
---|
[6327] | 419 | label = 'Add applicant' |
---|
| 420 | pnav = 3 |
---|
| 421 | |
---|
| 422 | @property |
---|
| 423 | def title(self): |
---|
| 424 | return "Applicants Container: %s" % self.context.title |
---|
| 425 | |
---|
| 426 | @grok.action('Create application record') |
---|
| 427 | def addApplicant(self, **data): |
---|
[7260] | 428 | applicant = createObject(u'waeup.Applicant') |
---|
[7240] | 429 | self.applyData(applicant, **data) |
---|
| 430 | self.context.addApplicant(applicant) |
---|
| 431 | self.flash('Applicant record created.') |
---|
[7363] | 432 | self.redirect( |
---|
| 433 | self.url(self.context[applicant.application_number], 'index')) |
---|
[6327] | 434 | return |
---|
| 435 | |
---|
[7321] | 436 | class ApplicantDisplayFormPage(SIRPDisplayFormPage): |
---|
[5273] | 437 | grok.context(IApplicant) |
---|
| 438 | grok.name('index') |
---|
[7113] | 439 | grok.require('waeup.viewApplication') |
---|
[7200] | 440 | grok.template('applicantdisplaypage') |
---|
[6320] | 441 | form_fields = grok.AutoFields(IApplicant).omit( |
---|
[7347] | 442 | 'locked', 'course_admitted', 'password') |
---|
[6054] | 443 | form_fields['date_of_birth'].custom_widget = FriendlyDateDisplayWidget('le') |
---|
[5273] | 444 | label = 'Applicant' |
---|
[5843] | 445 | pnav = 3 |
---|
[5273] | 446 | |
---|
[7063] | 447 | def update(self): |
---|
| 448 | self.passport_url = self.url(self.context, 'passport.jpg') |
---|
[7240] | 449 | # Mark application as started if applicant logs in for the first time |
---|
[7272] | 450 | usertype = getattr(self.request.principal, 'user_type', None) |
---|
| 451 | if usertype == 'applicant' and \ |
---|
| 452 | IWorkflowState(self.context).getState() == INITIALIZED: |
---|
[7240] | 453 | IWorkflowInfo(self.context).fireTransition('start') |
---|
[7063] | 454 | return |
---|
| 455 | |
---|
[6196] | 456 | @property |
---|
[7240] | 457 | def hasPassword(self): |
---|
| 458 | if self.context.password: |
---|
| 459 | return 'set' |
---|
| 460 | return 'unset' |
---|
| 461 | |
---|
| 462 | @property |
---|
[6196] | 463 | def title(self): |
---|
[7240] | 464 | return 'Application Record %s' % self.context.application_number |
---|
[6196] | 465 | |
---|
| 466 | @property |
---|
| 467 | def label(self): |
---|
| 468 | container_title = self.context.__parent__.title |
---|
[7240] | 469 | return '%s Application Record %s' % ( |
---|
| 470 | container_title, self.context.application_number) |
---|
[6196] | 471 | |
---|
[7347] | 472 | def getCourseAdmitted(self): |
---|
| 473 | """Return link, title and code in html format to the certificate |
---|
| 474 | admitted. |
---|
| 475 | """ |
---|
| 476 | course_admitted = self.context.course_admitted |
---|
[7351] | 477 | if getattr(course_admitted, '__parent__',None): |
---|
[7347] | 478 | url = self.url(course_admitted) |
---|
| 479 | title = course_admitted.title |
---|
| 480 | code = course_admitted.code |
---|
| 481 | return '<a href="%s">%s - %s</a>' %(url,code,title) |
---|
| 482 | return '' |
---|
[6254] | 483 | |
---|
[7259] | 484 | class ApplicantBaseDisplayFormPage(ApplicantDisplayFormPage): |
---|
| 485 | grok.context(IApplicant) |
---|
| 486 | grok.name('base') |
---|
| 487 | form_fields = grok.AutoFields(IApplicant).select( |
---|
| 488 | 'applicant_id', 'firstname', 'lastname','email', 'course1') |
---|
| 489 | |
---|
[7341] | 490 | class CreateStudentPage(grok.View): |
---|
| 491 | """Create a student object from applicatnt data |
---|
| 492 | and copy applicant object. |
---|
| 493 | """ |
---|
| 494 | grok.context(IApplicant) |
---|
| 495 | grok.name('createstudent') |
---|
| 496 | grok.require('waeup.manageStudent') |
---|
| 497 | |
---|
| 498 | def update(self): |
---|
| 499 | msg = self.context.createStudent()[1] |
---|
| 500 | self.flash(msg) |
---|
| 501 | self.redirect(self.url(self.context)) |
---|
| 502 | return |
---|
| 503 | |
---|
| 504 | def render(self): |
---|
| 505 | return |
---|
| 506 | |
---|
[7250] | 507 | class AcceptanceFeePaymentAddPage(grok.View): |
---|
| 508 | """ Page to add an online payment ticket |
---|
| 509 | """ |
---|
| 510 | grok.context(IApplicant) |
---|
| 511 | grok.name('addafp') |
---|
| 512 | grok.require('waeup.payApplicant') |
---|
| 513 | |
---|
| 514 | def update(self): |
---|
| 515 | p_category = 'acceptance' |
---|
| 516 | d = {} |
---|
| 517 | session = str(self.context.__parent__.year) |
---|
| 518 | try: |
---|
| 519 | academic_session = grok.getSite()['configuration'][session] |
---|
| 520 | except KeyError: |
---|
| 521 | self.flash('Session configuration object is not available.') |
---|
| 522 | return |
---|
| 523 | timestamp = "%d" % int(time()*1000) |
---|
| 524 | for key in self.context.keys(): |
---|
| 525 | ticket = self.context[key] |
---|
| 526 | if ticket.p_state == 'paid': |
---|
| 527 | self.flash( |
---|
| 528 | 'This type of payment has already been made.') |
---|
| 529 | self.redirect(self.url(self.context)) |
---|
| 530 | return |
---|
| 531 | payment = createObject(u'waeup.ApplicantOnlinePayment') |
---|
| 532 | payment.p_id = "p%s" % timestamp |
---|
| 533 | payment.p_item = self.context.__parent__.title |
---|
| 534 | payment.p_year = self.context.__parent__.year |
---|
| 535 | payment.p_category = p_category |
---|
| 536 | payment.amount_auth = academic_session.acceptance_fee |
---|
| 537 | payment.surcharge_1 = academic_session.surcharge_1 |
---|
| 538 | payment.surcharge_2 = academic_session.surcharge_2 |
---|
| 539 | payment.surcharge_3 = academic_session.surcharge_3 |
---|
| 540 | self.context[payment.p_id] = payment |
---|
| 541 | self.flash('Payment ticket created.') |
---|
| 542 | return |
---|
| 543 | |
---|
| 544 | def render(self): |
---|
| 545 | usertype = getattr(self.request.principal, 'user_type', None) |
---|
| 546 | if usertype == 'applicant': |
---|
| 547 | self.redirect(self.url(self.context, '@@edit')) |
---|
| 548 | return |
---|
| 549 | self.redirect(self.url(self.context, '@@manage')) |
---|
| 550 | return |
---|
| 551 | |
---|
| 552 | |
---|
[7321] | 553 | class OnlinePaymentDisplayFormPage(SIRPDisplayFormPage): |
---|
[7250] | 554 | """ Page to view an online payment ticket |
---|
| 555 | """ |
---|
| 556 | grok.context(IApplicantOnlinePayment) |
---|
| 557 | grok.name('index') |
---|
| 558 | grok.require('waeup.viewApplication') |
---|
| 559 | form_fields = grok.AutoFields(IApplicantOnlinePayment) |
---|
[7363] | 560 | form_fields[ |
---|
| 561 | 'creation_date'].custom_widget = FriendlyDatetimeDisplayWidget('le') |
---|
| 562 | form_fields[ |
---|
| 563 | 'payment_date'].custom_widget = FriendlyDatetimeDisplayWidget('le') |
---|
[7250] | 564 | pnav = 3 |
---|
| 565 | |
---|
| 566 | @property |
---|
| 567 | def title(self): |
---|
| 568 | return 'Online Payment Ticket %s' % self.context.p_id |
---|
| 569 | |
---|
| 570 | @property |
---|
| 571 | def label(self): |
---|
| 572 | return '%s: Online Payment Ticket %s' % ( |
---|
[7364] | 573 | self.context.__parent__.display_fullname,self.context.p_id) |
---|
[7250] | 574 | |
---|
| 575 | class PaymentReceiptActionButton(ManageActionButton): |
---|
| 576 | grok.order(1) |
---|
| 577 | grok.context(IApplicantOnlinePayment) |
---|
| 578 | grok.view(OnlinePaymentDisplayFormPage) |
---|
| 579 | grok.require('waeup.viewApplication') |
---|
| 580 | icon = 'actionicon_pdf.png' |
---|
| 581 | text = 'Download payment receipt' |
---|
| 582 | target = 'payment_receipt.pdf' |
---|
| 583 | |
---|
| 584 | @property |
---|
| 585 | def target_url(self): |
---|
| 586 | if self.context.p_state != 'paid': |
---|
| 587 | return '' |
---|
| 588 | return self.view.url(self.view.context, self.target) |
---|
| 589 | |
---|
| 590 | class RequestCallbackActionButton(ManageActionButton): |
---|
| 591 | grok.order(2) |
---|
| 592 | grok.context(IApplicantOnlinePayment) |
---|
| 593 | grok.view(OnlinePaymentDisplayFormPage) |
---|
| 594 | grok.require('waeup.payApplicant') |
---|
| 595 | icon = 'actionicon_call.png' |
---|
| 596 | text = 'Request callback' |
---|
| 597 | target = 'callback' |
---|
| 598 | |
---|
| 599 | @property |
---|
| 600 | def target_url(self): |
---|
| 601 | if self.context.p_state != 'unpaid': |
---|
| 602 | return '' |
---|
| 603 | return self.view.url(self.view.context, self.target) |
---|
| 604 | |
---|
| 605 | class OnlinePaymentCallbackPage(grok.View): |
---|
| 606 | """ Callback view |
---|
| 607 | """ |
---|
| 608 | grok.context(IApplicantOnlinePayment) |
---|
| 609 | grok.name('callback') |
---|
| 610 | grok.require('waeup.payApplicant') |
---|
| 611 | |
---|
| 612 | # This update method simulates a valid callback und must be |
---|
| 613 | # specified in the customization package. The parameters must be taken |
---|
| 614 | # from the incoming request. |
---|
| 615 | def update(self): |
---|
[7322] | 616 | self.wf_info = IWorkflowInfo(self.context.__parent__) |
---|
| 617 | try: |
---|
| 618 | self.wf_info.fireTransition('pay') |
---|
| 619 | except InvalidTransitionError: |
---|
| 620 | self.flash('Error: %s' % sys.exc_info()[1]) |
---|
[7250] | 621 | return |
---|
| 622 | self.context.r_amount_approved = self.context.amount_auth |
---|
| 623 | self.context.r_card_num = u'0000' |
---|
| 624 | self.context.r_code = u'00' |
---|
| 625 | self.context.p_state = 'paid' |
---|
| 626 | self.context.payment_date = datetime.now() |
---|
| 627 | ob_class = self.__implemented__.__name__.replace('waeup.sirp.','') |
---|
| 628 | self.context.__parent__.loggerInfo( |
---|
| 629 | ob_class, 'valid callback: %s' % self.context.p_id) |
---|
| 630 | self.flash('Valid callback received.') |
---|
| 631 | return |
---|
| 632 | |
---|
| 633 | def render(self): |
---|
| 634 | self.redirect(self.url(self.context, '@@index')) |
---|
| 635 | return |
---|
| 636 | |
---|
| 637 | class ExportPDFPaymentSlipPage(grok.View): |
---|
| 638 | """Deliver a PDF slip of the context. |
---|
| 639 | """ |
---|
| 640 | grok.context(IApplicantOnlinePayment) |
---|
| 641 | grok.name('payment_receipt.pdf') |
---|
| 642 | grok.require('waeup.viewApplication') |
---|
| 643 | form_fields = grok.AutoFields(IApplicantOnlinePayment) |
---|
| 644 | form_fields['creation_date'].custom_widget = FriendlyDateDisplayWidget('le') |
---|
| 645 | form_fields['payment_date'].custom_widget = FriendlyDateDisplayWidget('le') |
---|
| 646 | prefix = 'form' |
---|
[7318] | 647 | title = 'Payment Data' |
---|
[7250] | 648 | |
---|
| 649 | @property |
---|
| 650 | def label(self): |
---|
| 651 | return 'Online Payment Receipt %s' % self.context.p_id |
---|
| 652 | |
---|
| 653 | def render(self): |
---|
| 654 | if self.context.p_state != 'paid': |
---|
| 655 | self.flash('Ticket not yet paid.') |
---|
| 656 | self.redirect(self.url(self.context)) |
---|
| 657 | return |
---|
[7259] | 658 | applicantview = ApplicantBaseDisplayFormPage(self.context.__parent__, |
---|
[7250] | 659 | self.request) |
---|
| 660 | students_utils = getUtility(IStudentsUtils) |
---|
[7318] | 661 | return students_utils.renderPDF(self,'payment_receipt.pdf', |
---|
[7250] | 662 | self.context.__parent__, applicantview) |
---|
| 663 | |
---|
[6358] | 664 | class PDFActionButton(ManageActionButton): |
---|
| 665 | grok.context(IApplicant) |
---|
[7136] | 666 | grok.require('waeup.viewApplication') |
---|
[6358] | 667 | icon = 'actionicon_pdf.png' |
---|
[6367] | 668 | text = 'Download application slip' |
---|
[6358] | 669 | target = 'application_slip.pdf' |
---|
| 670 | |
---|
| 671 | class ExportPDFPage(grok.View): |
---|
| 672 | """Deliver a PDF slip of the context. |
---|
| 673 | """ |
---|
| 674 | grok.context(IApplicant) |
---|
| 675 | grok.name('application_slip.pdf') |
---|
[7136] | 676 | grok.require('waeup.viewApplication') |
---|
[6358] | 677 | form_fields = grok.AutoFields(IApplicant).omit( |
---|
[7347] | 678 | 'locked', 'course_admitted') |
---|
[6358] | 679 | form_fields['date_of_birth'].custom_widget = FriendlyDateDisplayWidget('le') |
---|
| 680 | prefix = 'form' |
---|
| 681 | |
---|
[6363] | 682 | @property |
---|
| 683 | def label(self): |
---|
| 684 | container_title = self.context.__parent__.title |
---|
[7240] | 685 | return '%s Application Record %s' % ( |
---|
| 686 | container_title, self.context.application_number) |
---|
[6363] | 687 | |
---|
[7347] | 688 | def getCourseAdmitted(self): |
---|
| 689 | """Return title and code in html format to the certificate |
---|
| 690 | admitted. |
---|
| 691 | """ |
---|
| 692 | course_admitted = self.context.course_admitted |
---|
[7351] | 693 | #if ICertificate.providedBy(course_admitted): |
---|
| 694 | if getattr(course_admitted, '__parent__',None): |
---|
[7347] | 695 | title = course_admitted.title |
---|
| 696 | code = course_admitted.code |
---|
| 697 | return '%s - %s' %(code,title) |
---|
| 698 | return '' |
---|
[6358] | 699 | |
---|
| 700 | def setUpWidgets(self, ignore_request=False): |
---|
| 701 | self.adapters = {} |
---|
| 702 | self.widgets = setUpEditWidgets( |
---|
| 703 | self.form_fields, self.prefix, self.context, self.request, |
---|
| 704 | adapters=self.adapters, for_display=True, |
---|
| 705 | ignore_request=ignore_request |
---|
| 706 | ) |
---|
| 707 | |
---|
| 708 | def render(self): |
---|
[7276] | 709 | # To recall the table coordinate system: |
---|
| 710 | # (0,0),(-1,-1) = whole table |
---|
| 711 | # (0,0),(0,-1) = first column |
---|
| 712 | # (-1,0),(-1,-1) = last column |
---|
| 713 | # (0,0),(-1,0) = first row |
---|
| 714 | # (0,-1),(-1,-1) = last row |
---|
| 715 | |
---|
[6364] | 716 | SLIP_STYLE = TableStyle( |
---|
| 717 | [('VALIGN',(0,0),(-1,-1),'TOP')] |
---|
| 718 | ) |
---|
[6358] | 719 | |
---|
| 720 | pdf = canvas.Canvas('application_slip.pdf',pagesize=A4) |
---|
[6364] | 721 | pdf.setTitle(self.label) |
---|
| 722 | pdf.setSubject('Application') |
---|
| 723 | pdf.setAuthor('%s (%s)' % (self.request.principal.title, |
---|
| 724 | self.request.principal.id)) |
---|
[7321] | 725 | pdf.setCreator('SIRP SIRP') |
---|
[6358] | 726 | width, height = A4 |
---|
| 727 | style = getSampleStyleSheet() |
---|
[6365] | 728 | pdf.line(1*cm,height-(1.8*cm),width-(1*cm),height-(1.8*cm)) |
---|
[6363] | 729 | |
---|
[6358] | 730 | story = [] |
---|
[6365] | 731 | frame_header = Frame(1*cm,1*cm,width-(1.7*cm),height-(1.7*cm)) |
---|
[6363] | 732 | header_title = getattr(grok.getSite(), 'name', u'Sample University') |
---|
| 733 | story.append(Paragraph(header_title, style["Heading1"])) |
---|
| 734 | frame_header.addFromList(story,pdf) |
---|
| 735 | |
---|
| 736 | story = [] |
---|
[6365] | 737 | frame_body = Frame(1*cm,1*cm,width-(2*cm),height-(3.5*cm)) |
---|
[6364] | 738 | story.append(Paragraph(self.label, style["Heading2"])) |
---|
| 739 | story.append(Spacer(1, 18)) |
---|
| 740 | for msg in self.context.history.messages: |
---|
| 741 | f_msg = '<font face="Courier" size=10>%s</font>' % msg |
---|
| 742 | story.append(Paragraph(f_msg, style["Normal"])) |
---|
[6363] | 743 | story.append(Spacer(1, 24)) |
---|
[7276] | 744 | # Setup table data |
---|
| 745 | data = [] |
---|
| 746 | # Insert passport photograph |
---|
[7063] | 747 | img = getUtility(IExtFileStore).getFileByContext(self.context) |
---|
| 748 | if img is None: |
---|
[7089] | 749 | img = open(DEFAULT_PASSPORT_IMAGE_PATH, 'rb') |
---|
[7063] | 750 | doc_img = Image(img.name, width=4*cm, height=3*cm, kind='bound') |
---|
[7276] | 751 | data.append([doc_img]) |
---|
| 752 | data.append([Spacer(1, 18)]) |
---|
| 753 | # Render widget fields |
---|
[6358] | 754 | self.setUpWidgets() |
---|
| 755 | for widget in self.widgets: |
---|
[6462] | 756 | f_label = '<font size=12>%s</font>:' % widget.label.strip() |
---|
[6363] | 757 | f_label = Paragraph(f_label, style["Normal"]) |
---|
[7063] | 758 | f_text = '<font size=12>%s</font>' % widget() |
---|
| 759 | f_text = Paragraph(f_text, style["Normal"]) |
---|
| 760 | data.append([f_label,f_text]) |
---|
[7347] | 761 | course_admitted = self.getCourseAdmitted() |
---|
| 762 | f_label = '<font size=12>Admitted Course of Study:</font>' |
---|
| 763 | f_text = '<font size=12>%s</font>' % course_admitted |
---|
[6363] | 764 | f_label = Paragraph(f_label, style["Normal"]) |
---|
| 765 | f_text = Paragraph(f_text, style["Normal"]) |
---|
| 766 | data.append([f_label,f_text]) |
---|
[7341] | 767 | |
---|
[7347] | 768 | course_admitted = self.context.course_admitted |
---|
[7351] | 769 | if getattr(course_admitted, '__parent__',None): |
---|
[7347] | 770 | f_label = '<font size=12>Department:</font>' |
---|
| 771 | f_text = '<font size=12>%s</font>' % ( |
---|
| 772 | course_admitted.__parent__.__parent__.longtitle()) |
---|
| 773 | f_label = Paragraph(f_label, style["Normal"]) |
---|
| 774 | f_text = Paragraph(f_text, style["Normal"]) |
---|
| 775 | data.append([f_label,f_text]) |
---|
[7341] | 776 | |
---|
[7347] | 777 | f_label = '<font size=12>Faculty:</font>' |
---|
| 778 | f_text = '<font size=12>%s</font>' % ( |
---|
| 779 | course_admitted.__parent__.__parent__.__parent__.longtitle()) |
---|
| 780 | f_label = Paragraph(f_label, style["Normal"]) |
---|
| 781 | f_text = Paragraph(f_text, style["Normal"]) |
---|
| 782 | data.append([f_label,f_text]) |
---|
| 783 | |
---|
[7276] | 784 | # Create table |
---|
[6364] | 785 | table = Table(data,style=SLIP_STYLE) |
---|
[6363] | 786 | story.append(table) |
---|
| 787 | frame_body.addFromList(story,pdf) |
---|
| 788 | |
---|
[6364] | 789 | story = [] |
---|
[6365] | 790 | frame_footer = Frame(1*cm,0,width-(2*cm),1*cm) |
---|
[6364] | 791 | timestamp = datetime.now().strftime("%d/%m/%Y %H:%M:%S") |
---|
| 792 | f_text = '<font size=10>%s</font>' % timestamp |
---|
| 793 | story.append(Paragraph(f_text, style["Normal"])) |
---|
| 794 | frame_footer.addFromList(story,pdf) |
---|
| 795 | |
---|
[6358] | 796 | self.response.setHeader( |
---|
| 797 | 'Content-Type', 'application/pdf') |
---|
| 798 | return pdf.getpdfdata() |
---|
| 799 | |
---|
[6383] | 800 | class ApplicantManageActionButton(ManageActionButton): |
---|
[6198] | 801 | grok.context(IApplicant) |
---|
[7200] | 802 | grok.view(ApplicantDisplayFormPage) |
---|
[7136] | 803 | grok.require('waeup.manageApplication') |
---|
[6383] | 804 | text = 'Manage application record' |
---|
[7200] | 805 | target = 'manage' |
---|
[6198] | 806 | |
---|
[7240] | 807 | class ApplicantEditActionButton(ManageActionButton): |
---|
| 808 | grok.context(IApplicant) |
---|
| 809 | grok.view(ApplicantDisplayFormPage) |
---|
| 810 | grok.require('waeup.handleApplication') |
---|
| 811 | text = 'Edit application record' |
---|
| 812 | target ='edit' |
---|
[7081] | 813 | |
---|
[7240] | 814 | @property |
---|
| 815 | def target_url(self): |
---|
| 816 | """Get a URL to the target... |
---|
| 817 | """ |
---|
| 818 | if self.context.locked: |
---|
| 819 | return |
---|
| 820 | return self.view.url(self.view.context, self.target) |
---|
| 821 | |
---|
[7081] | 822 | def handle_img_upload(upload, context, view): |
---|
[7063] | 823 | """Handle upload of applicant image. |
---|
[7081] | 824 | |
---|
| 825 | Returns `True` in case of success or `False`. |
---|
| 826 | |
---|
| 827 | Please note that file pointer passed in (`upload`) most probably |
---|
| 828 | points to end of file when leaving this function. |
---|
[7063] | 829 | """ |
---|
[7081] | 830 | size = file_size(upload) |
---|
| 831 | if size > MAX_UPLOAD_SIZE: |
---|
| 832 | view.flash('Uploaded image is too big!') |
---|
| 833 | return False |
---|
[7247] | 834 | dummy, ext = os.path.splitext(upload.filename) |
---|
| 835 | ext.lower() |
---|
| 836 | if ext != '.jpg': |
---|
| 837 | view.flash('jpg file extension expected.') |
---|
| 838 | return False |
---|
[7081] | 839 | upload.seek(0) # file pointer moved when determining size |
---|
[7063] | 840 | store = getUtility(IExtFileStore) |
---|
| 841 | file_id = IFileStoreNameChooser(context).chooseName() |
---|
| 842 | store.createFile(file_id, upload) |
---|
[7081] | 843 | return True |
---|
[7063] | 844 | |
---|
[7321] | 845 | class ApplicantManageFormPage(SIRPEditFormPage): |
---|
[6196] | 846 | """A full edit view for applicant data. |
---|
| 847 | """ |
---|
| 848 | grok.context(IApplicant) |
---|
[7200] | 849 | grok.name('manage') |
---|
[7136] | 850 | grok.require('waeup.manageApplication') |
---|
[6476] | 851 | form_fields = grok.AutoFields(IApplicant) |
---|
[6196] | 852 | form_fields['date_of_birth'].custom_widget = FriendlyDateWidget('le-year') |
---|
[7351] | 853 | form_fields['student_id'].for_display = True |
---|
[7378] | 854 | form_fields['applicant_id'].for_display = True |
---|
[7200] | 855 | grok.template('applicanteditpage') |
---|
[6322] | 856 | manage_applications = True |
---|
[6196] | 857 | pnav = 3 |
---|
[7250] | 858 | display_actions = [['Save', 'Final Submit'], |
---|
| 859 | ['Add online payment ticket','Remove selected tickets']] |
---|
[6196] | 860 | |
---|
| 861 | def update(self): |
---|
| 862 | datepicker.need() # Enable jQuery datepicker in date fields. |
---|
[7330] | 863 | warning.need() |
---|
[7200] | 864 | super(ApplicantManageFormPage, self).update() |
---|
[6353] | 865 | self.wf_info = IWorkflowInfo(self.context) |
---|
[7081] | 866 | self.max_upload_size = string_from_bytes(MAX_UPLOAD_SIZE) |
---|
[7084] | 867 | self.passport_changed = None |
---|
[6598] | 868 | upload = self.request.form.get('form.passport', None) |
---|
| 869 | if upload: |
---|
| 870 | # We got a fresh upload |
---|
[7084] | 871 | self.passport_changed = handle_img_upload( |
---|
| 872 | upload, self.context, self) |
---|
[6196] | 873 | return |
---|
| 874 | |
---|
| 875 | @property |
---|
| 876 | def title(self): |
---|
[7240] | 877 | return 'Application Record %s' % self.context.application_number |
---|
[6196] | 878 | |
---|
| 879 | @property |
---|
| 880 | def label(self): |
---|
| 881 | container_title = self.context.__parent__.title |
---|
[7240] | 882 | return '%s Application Form %s' % ( |
---|
| 883 | container_title, self.context.application_number) |
---|
[6196] | 884 | |
---|
[6303] | 885 | def getTransitions(self): |
---|
[6351] | 886 | """Return a list of dicts of allowed transition ids and titles. |
---|
[6353] | 887 | |
---|
| 888 | Each list entry provides keys ``name`` and ``title`` for |
---|
| 889 | internal name and (human readable) title of a single |
---|
| 890 | transition. |
---|
[6349] | 891 | """ |
---|
[6353] | 892 | allowed_transitions = self.wf_info.getManualTransitions() |
---|
[6355] | 893 | return [dict(name='', title='No transition')] +[ |
---|
| 894 | dict(name=x, title=y) for x, y in allowed_transitions] |
---|
[6303] | 895 | |
---|
[6196] | 896 | @grok.action('Save') |
---|
| 897 | def save(self, **data): |
---|
[7240] | 898 | form = self.request.form |
---|
| 899 | password = form.get('password', None) |
---|
| 900 | password_ctl = form.get('control_password', None) |
---|
| 901 | if password: |
---|
| 902 | validator = getUtility(IPasswordValidator) |
---|
| 903 | errors = validator.validate_password(password, password_ctl) |
---|
| 904 | if errors: |
---|
| 905 | self.flash( ' '.join(errors)) |
---|
| 906 | return |
---|
[7084] | 907 | if self.passport_changed is False: # False is not None! |
---|
| 908 | return # error during image upload. Ignore other values |
---|
[6475] | 909 | changed_fields = self.applyData(self.context, **data) |
---|
[7199] | 910 | # Turn list of lists into single list |
---|
| 911 | if changed_fields: |
---|
| 912 | changed_fields = reduce(lambda x,y: x+y, changed_fields.values()) |
---|
[7240] | 913 | else: |
---|
| 914 | changed_fields = [] |
---|
| 915 | if self.passport_changed: |
---|
| 916 | changed_fields.append('passport') |
---|
| 917 | if password: |
---|
| 918 | # Now we know that the form has no errors and can set password ... |
---|
| 919 | IUserAccount(self.context).setPassword(password) |
---|
| 920 | changed_fields.append('password') |
---|
[7199] | 921 | fields_string = ' + '.join(changed_fields) |
---|
[7085] | 922 | trans_id = form.get('transition', None) |
---|
| 923 | if trans_id: |
---|
| 924 | self.wf_info.fireTransition(trans_id) |
---|
[6196] | 925 | self.flash('Form has been saved.') |
---|
[6475] | 926 | ob_class = self.__implemented__.__name__.replace('waeup.sirp.','') |
---|
[6644] | 927 | if fields_string: |
---|
| 928 | self.context.loggerInfo(ob_class, 'saved: % s' % fields_string) |
---|
[6196] | 929 | return |
---|
| 930 | |
---|
[7250] | 931 | def unremovable(self, ticket): |
---|
[7330] | 932 | return False |
---|
[7250] | 933 | |
---|
| 934 | # This method is also used by the ApplicantEditFormPage |
---|
| 935 | def delPaymentTickets(self, **data): |
---|
| 936 | form = self.request.form |
---|
| 937 | if form.has_key('val_id'): |
---|
| 938 | child_id = form['val_id'] |
---|
| 939 | else: |
---|
| 940 | self.flash('No payment selected.') |
---|
| 941 | self.redirect(self.url(self.context)) |
---|
| 942 | return |
---|
| 943 | if not isinstance(child_id, list): |
---|
| 944 | child_id = [child_id] |
---|
| 945 | deleted = [] |
---|
| 946 | for id in child_id: |
---|
| 947 | # Applicants are not allowed to remove used payment tickets |
---|
| 948 | if not self.unremovable(self.context[id]): |
---|
| 949 | try: |
---|
| 950 | del self.context[id] |
---|
| 951 | deleted.append(id) |
---|
| 952 | except: |
---|
| 953 | self.flash('Could not delete %s: %s: %s' % ( |
---|
| 954 | id, sys.exc_info()[0], sys.exc_info()[1])) |
---|
| 955 | if len(deleted): |
---|
| 956 | self.flash('Successfully removed: %s' % ', '.join(deleted)) |
---|
| 957 | ob_class = self.__implemented__.__name__.replace('waeup.sirp.','') |
---|
[7363] | 958 | self.context.loggerInfo( |
---|
| 959 | ob_class, 'removed: % s' % ', '.join(deleted)) |
---|
[7250] | 960 | return |
---|
| 961 | |
---|
[7252] | 962 | # We explicitely want the forms to be validated before payment tickets |
---|
| 963 | # can be created. If no validation is requested, use |
---|
| 964 | # 'validator=NullValidator' in the grok.action directive |
---|
[7250] | 965 | @grok.action('Add online payment ticket') |
---|
| 966 | def addPaymentTicket(self, **data): |
---|
| 967 | self.redirect(self.url(self.context, '@@addafp')) |
---|
[7252] | 968 | return |
---|
[7250] | 969 | |
---|
[7330] | 970 | @jsaction('Remove selected tickets') |
---|
[7250] | 971 | def removePaymentTickets(self, **data): |
---|
| 972 | self.delPaymentTickets(**data) |
---|
| 973 | self.redirect(self.url(self.context) + '/@@manage') |
---|
| 974 | return |
---|
| 975 | |
---|
[7200] | 976 | class ApplicantEditFormPage(ApplicantManageFormPage): |
---|
[5982] | 977 | """An applicant-centered edit view for applicant data. |
---|
| 978 | """ |
---|
[6196] | 979 | grok.context(IApplicantEdit) |
---|
[5273] | 980 | grok.name('edit') |
---|
[6198] | 981 | grok.require('waeup.handleApplication') |
---|
[6459] | 982 | form_fields = grok.AutoFields(IApplicantEdit).omit( |
---|
[6476] | 983 | 'locked', 'course_admitted', 'student_id', |
---|
[7378] | 984 | 'screening_score', 'reg_number' |
---|
[6459] | 985 | ) |
---|
[6054] | 986 | form_fields['date_of_birth'].custom_widget = FriendlyDateWidget('le-year') |
---|
[7363] | 987 | #form_fields['phone'].custom_widget = PhoneWidget |
---|
[7200] | 988 | grok.template('applicanteditpage') |
---|
[6322] | 989 | manage_applications = False |
---|
[6465] | 990 | title = u'Your Application Form' |
---|
[5484] | 991 | |
---|
[7250] | 992 | @property |
---|
| 993 | def display_actions(self): |
---|
| 994 | state = IWorkflowState(self.context).getState() |
---|
| 995 | if state == INITIALIZED: |
---|
| 996 | actions = [[],[]] |
---|
| 997 | elif state == STARTED: |
---|
| 998 | actions = [['Save'], |
---|
| 999 | ['Add online payment ticket','Remove selected tickets']] |
---|
| 1000 | elif state == PAID: |
---|
| 1001 | actions = [['Save', 'Final Submit'], |
---|
| 1002 | ['Remove selected tickets']] |
---|
[7351] | 1003 | else: |
---|
[7250] | 1004 | actions = [[],[]] |
---|
| 1005 | return actions |
---|
| 1006 | |
---|
[7330] | 1007 | def unremovable(self, ticket): |
---|
| 1008 | state = IWorkflowState(self.context).getState() |
---|
| 1009 | return ticket.r_code or state in (INITIALIZED, SUBMITTED) |
---|
| 1010 | |
---|
[7145] | 1011 | def emit_lock_message(self): |
---|
[6105] | 1012 | self.flash('The requested form is locked (read-only).') |
---|
[5941] | 1013 | self.redirect(self.url(self.context)) |
---|
| 1014 | return |
---|
[6078] | 1015 | |
---|
[5686] | 1016 | def update(self): |
---|
[5941] | 1017 | if self.context.locked: |
---|
[7145] | 1018 | self.emit_lock_message() |
---|
[5941] | 1019 | return |
---|
[7200] | 1020 | super(ApplicantEditFormPage, self).update() |
---|
[5686] | 1021 | return |
---|
[5952] | 1022 | |
---|
[6196] | 1023 | def dataNotComplete(self): |
---|
[7252] | 1024 | store = getUtility(IExtFileStore) |
---|
| 1025 | if not store.getFileByContext(self.context, attr=u'passport.jpg'): |
---|
| 1026 | return 'No passport picture uploaded.' |
---|
[6322] | 1027 | if not self.request.form.get('confirm_passport', False): |
---|
[7252] | 1028 | return 'Passport picture confirmation box not ticked.' |
---|
[6196] | 1029 | return False |
---|
[5952] | 1030 | |
---|
[7252] | 1031 | # We explicitely want the forms to be validated before payment tickets |
---|
| 1032 | # can be created. If no validation is requested, use |
---|
| 1033 | # 'validator=NullValidator' in the grok.action directive |
---|
[7250] | 1034 | @grok.action('Add online payment ticket') |
---|
| 1035 | def addPaymentTicket(self, **data): |
---|
| 1036 | self.redirect(self.url(self.context, '@@addafp')) |
---|
[7252] | 1037 | return |
---|
[7250] | 1038 | |
---|
[7330] | 1039 | @jsaction('Remove selected tickets') |
---|
[7250] | 1040 | def removePaymentTickets(self, **data): |
---|
| 1041 | self.delPaymentTickets(**data) |
---|
| 1042 | self.redirect(self.url(self.context) + '/@@edit') |
---|
| 1043 | return |
---|
| 1044 | |
---|
[5273] | 1045 | @grok.action('Save') |
---|
| 1046 | def save(self, **data): |
---|
[7084] | 1047 | if self.passport_changed is False: # False is not None! |
---|
| 1048 | return # error during image upload. Ignore other values |
---|
[5273] | 1049 | self.applyData(self.context, **data) |
---|
[6196] | 1050 | self.flash('Form has been saved.') |
---|
[5273] | 1051 | return |
---|
| 1052 | |
---|
[5484] | 1053 | @grok.action('Final Submit') |
---|
| 1054 | def finalsubmit(self, **data): |
---|
[7084] | 1055 | if self.passport_changed is False: # False is not None! |
---|
| 1056 | return # error during image upload. Ignore other values |
---|
[6196] | 1057 | if self.dataNotComplete(): |
---|
| 1058 | self.flash(self.dataNotComplete()) |
---|
[5941] | 1059 | return |
---|
[7252] | 1060 | self.applyData(self.context, **data) |
---|
[6303] | 1061 | state = IWorkflowState(self.context).getState() |
---|
[6322] | 1062 | # This shouldn't happen, but the application officer |
---|
| 1063 | # might have forgotten to lock the form after changing the state |
---|
[7250] | 1064 | if state != PAID: |
---|
[6322] | 1065 | self.flash('This form cannot be submitted. Wrong state!') |
---|
[6303] | 1066 | return |
---|
| 1067 | IWorkflowInfo(self.context).fireTransition('submit') |
---|
[6476] | 1068 | self.context.application_date = datetime.now() |
---|
[5941] | 1069 | self.context.locked = True |
---|
[6196] | 1070 | self.flash('Form has been submitted.') |
---|
| 1071 | self.redirect(self.url(self.context)) |
---|
[5273] | 1072 | return |
---|
[5941] | 1073 | |
---|
[6367] | 1074 | class ApplicantViewActionButton(ManageActionButton): |
---|
| 1075 | grok.context(IApplicant) |
---|
[7200] | 1076 | grok.view(ApplicantManageFormPage) |
---|
[7240] | 1077 | grok.require('waeup.viewApplication') |
---|
[6383] | 1078 | icon = 'actionicon_view.png' |
---|
[6367] | 1079 | text = 'View application record' |
---|
[6598] | 1080 | target = 'index' |
---|
[7063] | 1081 | |
---|
| 1082 | class PassportImage(grok.View): |
---|
| 1083 | """Renders the passport image for applicants. |
---|
| 1084 | """ |
---|
| 1085 | grok.name('passport.jpg') |
---|
| 1086 | grok.context(IApplicant) |
---|
[7113] | 1087 | grok.require('waeup.viewApplication') |
---|
[7063] | 1088 | |
---|
| 1089 | def render(self): |
---|
| 1090 | # A filename chooser turns a context into a filename suitable |
---|
| 1091 | # for file storage. |
---|
| 1092 | image = getUtility(IExtFileStore).getFileByContext(self.context) |
---|
| 1093 | self.response.setHeader( |
---|
| 1094 | 'Content-Type', 'image/jpeg') |
---|
| 1095 | if image is None: |
---|
| 1096 | # show placeholder image |
---|
[7089] | 1097 | return open(DEFAULT_PASSPORT_IMAGE_PATH, 'rb').read() |
---|
[7063] | 1098 | return image |
---|
[7363] | 1099 | |
---|
| 1100 | class ApplicantRegistrationPage(SIRPAddFormPage): |
---|
| 1101 | """Captcha'd registration page for applicants. |
---|
| 1102 | """ |
---|
| 1103 | grok.context(IApplicantsContainer) |
---|
| 1104 | grok.name('register') |
---|
[7373] | 1105 | grok.require('waeup.Anonymous') |
---|
[7363] | 1106 | grok.template('applicantregister') |
---|
| 1107 | form_fields = grok.AutoFields(IApplicantEdit).select( |
---|
| 1108 | 'firstname', 'middlename', 'lastname', 'email', 'phone') |
---|
| 1109 | form_fields['phone'].custom_widget = PhoneWidget |
---|
| 1110 | |
---|
[7368] | 1111 | @property |
---|
| 1112 | def title(self): |
---|
| 1113 | return "Applicants Container: %s" % self.context.title |
---|
| 1114 | |
---|
| 1115 | @property |
---|
| 1116 | def label(self): |
---|
| 1117 | return "Register for %s Application" % self.context.title |
---|
| 1118 | |
---|
[7363] | 1119 | def update(self): |
---|
[7368] | 1120 | # Check if application has started ... |
---|
| 1121 | if not self.context.startdate or self.context.startdate > date.today(): |
---|
| 1122 | self.flash('Application has not yet started.') |
---|
| 1123 | self.redirect(self.url(self.context)) |
---|
| 1124 | return |
---|
| 1125 | # ... or ended |
---|
| 1126 | if not self.context.enddate or self.context.enddate < date.today(): |
---|
| 1127 | self.flash('Application has ended.') |
---|
| 1128 | self.redirect(self.url(self.context)) |
---|
| 1129 | return |
---|
| 1130 | # Handle captcha |
---|
[7363] | 1131 | self.captcha = getUtility(ICaptchaManager).getCaptcha() |
---|
| 1132 | self.captcha_result = self.captcha.verify(self.request) |
---|
| 1133 | self.captcha_code = self.captcha.display(self.captcha_result.error_code) |
---|
| 1134 | return |
---|
| 1135 | |
---|
[7380] | 1136 | @grok.action('Get login credentials') |
---|
[7363] | 1137 | def register(self, **data): |
---|
| 1138 | if not self.captcha_result.is_valid: |
---|
| 1139 | # captcha will display error messages automatically. |
---|
| 1140 | # No need to flash something. |
---|
| 1141 | return |
---|
[7365] | 1142 | # Add applicant and create password |
---|
[7363] | 1143 | applicant = createObject('waeup.Applicant') |
---|
| 1144 | self.applyData(applicant, **data) |
---|
| 1145 | self.context.addApplicant(applicant) |
---|
[7380] | 1146 | password = getUtility(ISIRPUtils).genPassword() |
---|
| 1147 | IUserAccount(applicant).setPassword(password) |
---|
[7365] | 1148 | # Send email with credentials |
---|
[7380] | 1149 | if self.sendCredentials(applicant, password): |
---|
| 1150 | self.redirect(self.url(self.context, 'registration_complete', |
---|
| 1151 | data = dict(email=applicant.email))) |
---|
| 1152 | return |
---|
| 1153 | else: |
---|
| 1154 | self.flash('Email could not been sent. Please retry later.') |
---|
| 1155 | return |
---|
| 1156 | |
---|
| 1157 | def sendCredentials(self, applicant, password): |
---|
| 1158 | """Send credentials as email. |
---|
| 1159 | |
---|
| 1160 | Input is the applicant for which credentials are sent and the |
---|
| 1161 | password. |
---|
| 1162 | |
---|
| 1163 | Returns True or False to indicate successful operation. |
---|
| 1164 | """ |
---|
| 1165 | sirp_utils = getUtility(ISIRPUtils) |
---|
[7365] | 1166 | username = applicant.applicant_id |
---|
| 1167 | fullname = applicant.display_fullname |
---|
| 1168 | subject = 'Your SIRP credentials' |
---|
[7380] | 1169 | msg = 'You have successfully been registered for the' |
---|
[7365] | 1170 | email_to = applicant.email |
---|
| 1171 | login_url = self.url(grok.getSite(), 'login') |
---|
| 1172 | success = sirp_utils.sendPassword(fullname,msg,username, |
---|
[7380] | 1173 | password,login_url,email_to,subject) |
---|
| 1174 | return success |
---|
| 1175 | |
---|
| 1176 | class ApplicantRegistrationEmailSent(SIRPPage): |
---|
| 1177 | """Landing page after successful registration. |
---|
| 1178 | """ |
---|
| 1179 | grok.name('registration_complete') |
---|
| 1180 | grok.require('waeup.Public') |
---|
| 1181 | grok.template('applicantregemailsent') |
---|
| 1182 | title = 'Registration Completed' |
---|
| 1183 | label = 'Your registration was successful' |
---|
| 1184 | |
---|
| 1185 | def update(self, email=None): |
---|
| 1186 | self.email = email |
---|
| 1187 | return |
---|