[5273] | 1 | ## $Id: browser.py 7363 2011-12-16 13:50:40Z 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 |
---|
[7255] | 24 | from datetime import datetime |
---|
[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, |
---|
[7240] | 53 | IFileStoreNameChooser, IPasswordValidator, IUserAccount) |
---|
[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 | |
---|
[7321] | 307 | class ApplicantsContainerManageFormPage(SIRPEditFormPage): |
---|
[5837] | 308 | grok.context(IApplicantsContainer) |
---|
[5850] | 309 | grok.name('manage') |
---|
[6107] | 310 | grok.template('applicantscontainermanagepage') |
---|
[6105] | 311 | form_fields = grok.AutoFields(IApplicantsContainer).omit('title') |
---|
| 312 | taboneactions = ['Save','Cancel'] |
---|
| 313 | tabtwoactions = ['Add applicant', 'Remove selected','Cancel'] |
---|
[6184] | 314 | tabthreeactions1 = ['Remove selected local roles'] |
---|
| 315 | tabthreeactions2 = ['Add local role'] |
---|
[5844] | 316 | # Use friendlier date widget... |
---|
[6054] | 317 | form_fields['startdate'].custom_widget = FriendlyDateWidget('le') |
---|
| 318 | form_fields['enddate'].custom_widget = FriendlyDateWidget('le') |
---|
[7136] | 319 | grok.require('waeup.manageApplication') |
---|
[5850] | 320 | |
---|
| 321 | @property |
---|
| 322 | def title(self): |
---|
[6087] | 323 | return "Applicants Container: %s" % self.context.title |
---|
[6078] | 324 | |
---|
[5850] | 325 | @property |
---|
| 326 | def label(self): |
---|
[6087] | 327 | return 'Manage applicants container' |
---|
[5850] | 328 | |
---|
[5845] | 329 | pnav = 3 |
---|
[5837] | 330 | |
---|
| 331 | def update(self): |
---|
[5850] | 332 | datepicker.need() # Enable jQuery datepicker in date fields. |
---|
[5982] | 333 | tabs.need() |
---|
[7330] | 334 | warning.need() |
---|
[6015] | 335 | datatable.need() # Enable jQurey datatables for contents listing |
---|
[6107] | 336 | return super(ApplicantsContainerManageFormPage, self).update() |
---|
[5837] | 337 | |
---|
[6184] | 338 | def getLocalRoles(self): |
---|
| 339 | roles = ILocalRolesAssignable(self.context) |
---|
| 340 | return roles() |
---|
| 341 | |
---|
| 342 | def getUsers(self): |
---|
| 343 | """Get a list of all users. |
---|
| 344 | """ |
---|
| 345 | for key, val in grok.getSite()['users'].items(): |
---|
| 346 | url = self.url(val) |
---|
| 347 | yield(dict(url=url, name=key, val=val)) |
---|
| 348 | |
---|
| 349 | def getUsersWithLocalRoles(self): |
---|
| 350 | return get_users_with_local_roles(self.context) |
---|
| 351 | |
---|
[5850] | 352 | @grok.action('Save') |
---|
[5837] | 353 | def apply(self, **data): |
---|
| 354 | self.applyData(self.context, **data) |
---|
| 355 | self.flash('Data saved.') |
---|
| 356 | return |
---|
[6078] | 357 | |
---|
[7330] | 358 | @jsaction('Remove selected') |
---|
[6105] | 359 | def delApplicant(self, **data): |
---|
[6189] | 360 | form = self.request.form |
---|
| 361 | if form.has_key('val_id'): |
---|
| 362 | child_id = form['val_id'] |
---|
| 363 | else: |
---|
| 364 | self.flash('No applicant selected!') |
---|
| 365 | self.redirect(self.url(self.context, '@@manage')+'#tab-2') |
---|
| 366 | return |
---|
| 367 | if not isinstance(child_id, list): |
---|
| 368 | child_id = [child_id] |
---|
| 369 | deleted = [] |
---|
| 370 | for id in child_id: |
---|
| 371 | try: |
---|
| 372 | del self.context[id] |
---|
| 373 | deleted.append(id) |
---|
| 374 | except: |
---|
| 375 | self.flash('Could not delete %s: %s: %s' % ( |
---|
| 376 | id, sys.exc_info()[0], sys.exc_info()[1])) |
---|
| 377 | if len(deleted): |
---|
| 378 | self.flash('Successfully removed: %s' % ', '.join(deleted)) |
---|
| 379 | self.redirect(self.url(self.context, u'@@manage')+'#tab-2') |
---|
| 380 | return |
---|
[6105] | 381 | |
---|
| 382 | @grok.action('Add applicant', validator=NullValidator) |
---|
| 383 | def addApplicant(self, **data): |
---|
[6327] | 384 | self.redirect(self.url(self.context, 'addapplicant')) |
---|
| 385 | return |
---|
[6105] | 386 | |
---|
| 387 | @grok.action('Cancel', validator=NullValidator) |
---|
[5837] | 388 | def cancel(self, **data): |
---|
| 389 | self.redirect(self.url(self.context)) |
---|
| 390 | return |
---|
[5886] | 391 | |
---|
[6184] | 392 | @grok.action('Add local role', validator=NullValidator) |
---|
| 393 | def addLocalRole(self, **data): |
---|
| 394 | return add_local_role(self,3, **data) |
---|
[6105] | 395 | |
---|
[6184] | 396 | @grok.action('Remove selected local roles') |
---|
| 397 | def delLocalRoles(self, **data): |
---|
| 398 | return del_local_roles(self,3,**data) |
---|
| 399 | |
---|
[7321] | 400 | class ApplicantAddFormPage(SIRPAddFormPage): |
---|
[6622] | 401 | """Add-form to add an applicant. |
---|
[6327] | 402 | """ |
---|
| 403 | grok.context(IApplicantsContainer) |
---|
[7136] | 404 | grok.require('waeup.manageApplication') |
---|
[6327] | 405 | grok.name('addapplicant') |
---|
[7240] | 406 | #grok.template('applicantaddpage') |
---|
| 407 | form_fields = grok.AutoFields(IApplicant).select( |
---|
[7356] | 408 | 'firstname', 'middlename', 'lastname', |
---|
[7240] | 409 | 'email', 'phone') |
---|
[6327] | 410 | title = 'Applicants' |
---|
| 411 | label = 'Add applicant' |
---|
| 412 | pnav = 3 |
---|
| 413 | |
---|
| 414 | @property |
---|
| 415 | def title(self): |
---|
| 416 | return "Applicants Container: %s" % self.context.title |
---|
| 417 | |
---|
| 418 | @grok.action('Create application record') |
---|
| 419 | def addApplicant(self, **data): |
---|
[7260] | 420 | applicant = createObject(u'waeup.Applicant') |
---|
[7240] | 421 | self.applyData(applicant, **data) |
---|
| 422 | self.context.addApplicant(applicant) |
---|
| 423 | self.flash('Applicant record created.') |
---|
[7363] | 424 | self.redirect( |
---|
| 425 | self.url(self.context[applicant.application_number], 'index')) |
---|
[6327] | 426 | return |
---|
| 427 | |
---|
[7321] | 428 | class ApplicantDisplayFormPage(SIRPDisplayFormPage): |
---|
[5273] | 429 | grok.context(IApplicant) |
---|
| 430 | grok.name('index') |
---|
[7113] | 431 | grok.require('waeup.viewApplication') |
---|
[7200] | 432 | grok.template('applicantdisplaypage') |
---|
[6320] | 433 | form_fields = grok.AutoFields(IApplicant).omit( |
---|
[7347] | 434 | 'locked', 'course_admitted', 'password') |
---|
[6054] | 435 | form_fields['date_of_birth'].custom_widget = FriendlyDateDisplayWidget('le') |
---|
[5273] | 436 | label = 'Applicant' |
---|
[5843] | 437 | pnav = 3 |
---|
[5273] | 438 | |
---|
[7063] | 439 | def update(self): |
---|
| 440 | self.passport_url = self.url(self.context, 'passport.jpg') |
---|
[7240] | 441 | # Mark application as started if applicant logs in for the first time |
---|
[7272] | 442 | usertype = getattr(self.request.principal, 'user_type', None) |
---|
| 443 | if usertype == 'applicant' and \ |
---|
| 444 | IWorkflowState(self.context).getState() == INITIALIZED: |
---|
[7240] | 445 | IWorkflowInfo(self.context).fireTransition('start') |
---|
[7063] | 446 | return |
---|
| 447 | |
---|
[6196] | 448 | @property |
---|
[7240] | 449 | def hasPassword(self): |
---|
| 450 | if self.context.password: |
---|
| 451 | return 'set' |
---|
| 452 | return 'unset' |
---|
| 453 | |
---|
| 454 | @property |
---|
[6196] | 455 | def title(self): |
---|
[7240] | 456 | return 'Application Record %s' % self.context.application_number |
---|
[6196] | 457 | |
---|
| 458 | @property |
---|
| 459 | def label(self): |
---|
| 460 | container_title = self.context.__parent__.title |
---|
[7240] | 461 | return '%s Application Record %s' % ( |
---|
| 462 | container_title, self.context.application_number) |
---|
[6196] | 463 | |
---|
[7347] | 464 | def getCourseAdmitted(self): |
---|
| 465 | """Return link, title and code in html format to the certificate |
---|
| 466 | admitted. |
---|
| 467 | """ |
---|
| 468 | course_admitted = self.context.course_admitted |
---|
[7351] | 469 | if getattr(course_admitted, '__parent__',None): |
---|
[7347] | 470 | url = self.url(course_admitted) |
---|
| 471 | title = course_admitted.title |
---|
| 472 | code = course_admitted.code |
---|
| 473 | return '<a href="%s">%s - %s</a>' %(url,code,title) |
---|
| 474 | return '' |
---|
[6254] | 475 | |
---|
[7259] | 476 | class ApplicantBaseDisplayFormPage(ApplicantDisplayFormPage): |
---|
| 477 | grok.context(IApplicant) |
---|
| 478 | grok.name('base') |
---|
| 479 | form_fields = grok.AutoFields(IApplicant).select( |
---|
| 480 | 'applicant_id', 'firstname', 'lastname','email', 'course1') |
---|
| 481 | |
---|
[7341] | 482 | class CreateStudentPage(grok.View): |
---|
| 483 | """Create a student object from applicatnt data |
---|
| 484 | and copy applicant object. |
---|
| 485 | """ |
---|
| 486 | grok.context(IApplicant) |
---|
| 487 | grok.name('createstudent') |
---|
| 488 | grok.require('waeup.manageStudent') |
---|
| 489 | |
---|
| 490 | def update(self): |
---|
| 491 | msg = self.context.createStudent()[1] |
---|
| 492 | self.flash(msg) |
---|
| 493 | self.redirect(self.url(self.context)) |
---|
| 494 | return |
---|
| 495 | |
---|
| 496 | def render(self): |
---|
| 497 | return |
---|
| 498 | |
---|
[7250] | 499 | class AcceptanceFeePaymentAddPage(grok.View): |
---|
| 500 | """ Page to add an online payment ticket |
---|
| 501 | """ |
---|
| 502 | grok.context(IApplicant) |
---|
| 503 | grok.name('addafp') |
---|
| 504 | grok.require('waeup.payApplicant') |
---|
| 505 | |
---|
| 506 | def update(self): |
---|
| 507 | p_category = 'acceptance' |
---|
| 508 | d = {} |
---|
| 509 | session = str(self.context.__parent__.year) |
---|
| 510 | try: |
---|
| 511 | academic_session = grok.getSite()['configuration'][session] |
---|
| 512 | except KeyError: |
---|
| 513 | self.flash('Session configuration object is not available.') |
---|
| 514 | return |
---|
| 515 | timestamp = "%d" % int(time()*1000) |
---|
| 516 | for key in self.context.keys(): |
---|
| 517 | ticket = self.context[key] |
---|
| 518 | if ticket.p_state == 'paid': |
---|
| 519 | self.flash( |
---|
| 520 | 'This type of payment has already been made.') |
---|
| 521 | self.redirect(self.url(self.context)) |
---|
| 522 | return |
---|
| 523 | payment = createObject(u'waeup.ApplicantOnlinePayment') |
---|
| 524 | payment.p_id = "p%s" % timestamp |
---|
| 525 | payment.p_item = self.context.__parent__.title |
---|
| 526 | payment.p_year = self.context.__parent__.year |
---|
| 527 | payment.p_category = p_category |
---|
| 528 | payment.amount_auth = academic_session.acceptance_fee |
---|
| 529 | payment.surcharge_1 = academic_session.surcharge_1 |
---|
| 530 | payment.surcharge_2 = academic_session.surcharge_2 |
---|
| 531 | payment.surcharge_3 = academic_session.surcharge_3 |
---|
| 532 | self.context[payment.p_id] = payment |
---|
| 533 | self.flash('Payment ticket created.') |
---|
| 534 | return |
---|
| 535 | |
---|
| 536 | def render(self): |
---|
| 537 | usertype = getattr(self.request.principal, 'user_type', None) |
---|
| 538 | if usertype == 'applicant': |
---|
| 539 | self.redirect(self.url(self.context, '@@edit')) |
---|
| 540 | return |
---|
| 541 | self.redirect(self.url(self.context, '@@manage')) |
---|
| 542 | return |
---|
| 543 | |
---|
| 544 | |
---|
[7321] | 545 | class OnlinePaymentDisplayFormPage(SIRPDisplayFormPage): |
---|
[7250] | 546 | """ Page to view an online payment ticket |
---|
| 547 | """ |
---|
| 548 | grok.context(IApplicantOnlinePayment) |
---|
| 549 | grok.name('index') |
---|
| 550 | grok.require('waeup.viewApplication') |
---|
| 551 | form_fields = grok.AutoFields(IApplicantOnlinePayment) |
---|
[7363] | 552 | form_fields[ |
---|
| 553 | 'creation_date'].custom_widget = FriendlyDatetimeDisplayWidget('le') |
---|
| 554 | form_fields[ |
---|
| 555 | 'payment_date'].custom_widget = FriendlyDatetimeDisplayWidget('le') |
---|
[7250] | 556 | pnav = 3 |
---|
| 557 | |
---|
| 558 | @property |
---|
| 559 | def title(self): |
---|
| 560 | return 'Online Payment Ticket %s' % self.context.p_id |
---|
| 561 | |
---|
| 562 | @property |
---|
| 563 | def label(self): |
---|
| 564 | return '%s: Online Payment Ticket %s' % ( |
---|
| 565 | self.context.__parent__.fullname,self.context.p_id) |
---|
| 566 | |
---|
| 567 | class PaymentReceiptActionButton(ManageActionButton): |
---|
| 568 | grok.order(1) |
---|
| 569 | grok.context(IApplicantOnlinePayment) |
---|
| 570 | grok.view(OnlinePaymentDisplayFormPage) |
---|
| 571 | grok.require('waeup.viewApplication') |
---|
| 572 | icon = 'actionicon_pdf.png' |
---|
| 573 | text = 'Download payment receipt' |
---|
| 574 | target = 'payment_receipt.pdf' |
---|
| 575 | |
---|
| 576 | @property |
---|
| 577 | def target_url(self): |
---|
| 578 | if self.context.p_state != 'paid': |
---|
| 579 | return '' |
---|
| 580 | return self.view.url(self.view.context, self.target) |
---|
| 581 | |
---|
| 582 | class RequestCallbackActionButton(ManageActionButton): |
---|
| 583 | grok.order(2) |
---|
| 584 | grok.context(IApplicantOnlinePayment) |
---|
| 585 | grok.view(OnlinePaymentDisplayFormPage) |
---|
| 586 | grok.require('waeup.payApplicant') |
---|
| 587 | icon = 'actionicon_call.png' |
---|
| 588 | text = 'Request callback' |
---|
| 589 | target = 'callback' |
---|
| 590 | |
---|
| 591 | @property |
---|
| 592 | def target_url(self): |
---|
| 593 | if self.context.p_state != 'unpaid': |
---|
| 594 | return '' |
---|
| 595 | return self.view.url(self.view.context, self.target) |
---|
| 596 | |
---|
| 597 | class OnlinePaymentCallbackPage(grok.View): |
---|
| 598 | """ Callback view |
---|
| 599 | """ |
---|
| 600 | grok.context(IApplicantOnlinePayment) |
---|
| 601 | grok.name('callback') |
---|
| 602 | grok.require('waeup.payApplicant') |
---|
| 603 | |
---|
| 604 | # This update method simulates a valid callback und must be |
---|
| 605 | # specified in the customization package. The parameters must be taken |
---|
| 606 | # from the incoming request. |
---|
| 607 | def update(self): |
---|
[7322] | 608 | self.wf_info = IWorkflowInfo(self.context.__parent__) |
---|
| 609 | try: |
---|
| 610 | self.wf_info.fireTransition('pay') |
---|
| 611 | except InvalidTransitionError: |
---|
| 612 | self.flash('Error: %s' % sys.exc_info()[1]) |
---|
[7250] | 613 | return |
---|
| 614 | self.context.r_amount_approved = self.context.amount_auth |
---|
| 615 | self.context.r_card_num = u'0000' |
---|
| 616 | self.context.r_code = u'00' |
---|
| 617 | self.context.p_state = 'paid' |
---|
| 618 | self.context.payment_date = datetime.now() |
---|
| 619 | ob_class = self.__implemented__.__name__.replace('waeup.sirp.','') |
---|
| 620 | self.context.__parent__.loggerInfo( |
---|
| 621 | ob_class, 'valid callback: %s' % self.context.p_id) |
---|
| 622 | self.flash('Valid callback received.') |
---|
| 623 | return |
---|
| 624 | |
---|
| 625 | def render(self): |
---|
| 626 | self.redirect(self.url(self.context, '@@index')) |
---|
| 627 | return |
---|
| 628 | |
---|
| 629 | class ExportPDFPaymentSlipPage(grok.View): |
---|
| 630 | """Deliver a PDF slip of the context. |
---|
| 631 | """ |
---|
| 632 | grok.context(IApplicantOnlinePayment) |
---|
| 633 | grok.name('payment_receipt.pdf') |
---|
| 634 | grok.require('waeup.viewApplication') |
---|
| 635 | form_fields = grok.AutoFields(IApplicantOnlinePayment) |
---|
| 636 | form_fields['creation_date'].custom_widget = FriendlyDateDisplayWidget('le') |
---|
| 637 | form_fields['payment_date'].custom_widget = FriendlyDateDisplayWidget('le') |
---|
| 638 | prefix = 'form' |
---|
[7318] | 639 | title = 'Payment Data' |
---|
[7250] | 640 | |
---|
| 641 | @property |
---|
| 642 | def label(self): |
---|
| 643 | return 'Online Payment Receipt %s' % self.context.p_id |
---|
| 644 | |
---|
| 645 | def render(self): |
---|
| 646 | if self.context.p_state != 'paid': |
---|
| 647 | self.flash('Ticket not yet paid.') |
---|
| 648 | self.redirect(self.url(self.context)) |
---|
| 649 | return |
---|
[7259] | 650 | applicantview = ApplicantBaseDisplayFormPage(self.context.__parent__, |
---|
[7250] | 651 | self.request) |
---|
| 652 | students_utils = getUtility(IStudentsUtils) |
---|
[7318] | 653 | return students_utils.renderPDF(self,'payment_receipt.pdf', |
---|
[7250] | 654 | self.context.__parent__, applicantview) |
---|
| 655 | |
---|
[6358] | 656 | class PDFActionButton(ManageActionButton): |
---|
| 657 | grok.context(IApplicant) |
---|
[7136] | 658 | grok.require('waeup.viewApplication') |
---|
[6358] | 659 | icon = 'actionicon_pdf.png' |
---|
[6367] | 660 | text = 'Download application slip' |
---|
[6358] | 661 | target = 'application_slip.pdf' |
---|
| 662 | |
---|
| 663 | class ExportPDFPage(grok.View): |
---|
| 664 | """Deliver a PDF slip of the context. |
---|
| 665 | """ |
---|
| 666 | grok.context(IApplicant) |
---|
| 667 | grok.name('application_slip.pdf') |
---|
[7136] | 668 | grok.require('waeup.viewApplication') |
---|
[6358] | 669 | form_fields = grok.AutoFields(IApplicant).omit( |
---|
[7347] | 670 | 'locked', 'course_admitted') |
---|
[6358] | 671 | form_fields['date_of_birth'].custom_widget = FriendlyDateDisplayWidget('le') |
---|
| 672 | prefix = 'form' |
---|
| 673 | |
---|
[6363] | 674 | @property |
---|
| 675 | def label(self): |
---|
| 676 | container_title = self.context.__parent__.title |
---|
[7240] | 677 | return '%s Application Record %s' % ( |
---|
| 678 | container_title, self.context.application_number) |
---|
[6363] | 679 | |
---|
[7347] | 680 | def getCourseAdmitted(self): |
---|
| 681 | """Return title and code in html format to the certificate |
---|
| 682 | admitted. |
---|
| 683 | """ |
---|
| 684 | course_admitted = self.context.course_admitted |
---|
[7351] | 685 | #if ICertificate.providedBy(course_admitted): |
---|
| 686 | if getattr(course_admitted, '__parent__',None): |
---|
[7347] | 687 | title = course_admitted.title |
---|
| 688 | code = course_admitted.code |
---|
| 689 | return '%s - %s' %(code,title) |
---|
| 690 | return '' |
---|
[6358] | 691 | |
---|
| 692 | def setUpWidgets(self, ignore_request=False): |
---|
| 693 | self.adapters = {} |
---|
| 694 | self.widgets = setUpEditWidgets( |
---|
| 695 | self.form_fields, self.prefix, self.context, self.request, |
---|
| 696 | adapters=self.adapters, for_display=True, |
---|
| 697 | ignore_request=ignore_request |
---|
| 698 | ) |
---|
| 699 | |
---|
| 700 | def render(self): |
---|
[7276] | 701 | # To recall the table coordinate system: |
---|
| 702 | # (0,0),(-1,-1) = whole table |
---|
| 703 | # (0,0),(0,-1) = first column |
---|
| 704 | # (-1,0),(-1,-1) = last column |
---|
| 705 | # (0,0),(-1,0) = first row |
---|
| 706 | # (0,-1),(-1,-1) = last row |
---|
| 707 | |
---|
[6364] | 708 | SLIP_STYLE = TableStyle( |
---|
| 709 | [('VALIGN',(0,0),(-1,-1),'TOP')] |
---|
| 710 | ) |
---|
[6358] | 711 | |
---|
| 712 | pdf = canvas.Canvas('application_slip.pdf',pagesize=A4) |
---|
[6364] | 713 | pdf.setTitle(self.label) |
---|
| 714 | pdf.setSubject('Application') |
---|
| 715 | pdf.setAuthor('%s (%s)' % (self.request.principal.title, |
---|
| 716 | self.request.principal.id)) |
---|
[7321] | 717 | pdf.setCreator('SIRP SIRP') |
---|
[6358] | 718 | width, height = A4 |
---|
| 719 | style = getSampleStyleSheet() |
---|
[6365] | 720 | pdf.line(1*cm,height-(1.8*cm),width-(1*cm),height-(1.8*cm)) |
---|
[6363] | 721 | |
---|
[6358] | 722 | story = [] |
---|
[6365] | 723 | frame_header = Frame(1*cm,1*cm,width-(1.7*cm),height-(1.7*cm)) |
---|
[6363] | 724 | header_title = getattr(grok.getSite(), 'name', u'Sample University') |
---|
| 725 | story.append(Paragraph(header_title, style["Heading1"])) |
---|
| 726 | frame_header.addFromList(story,pdf) |
---|
| 727 | |
---|
| 728 | story = [] |
---|
[6365] | 729 | frame_body = Frame(1*cm,1*cm,width-(2*cm),height-(3.5*cm)) |
---|
[6364] | 730 | story.append(Paragraph(self.label, style["Heading2"])) |
---|
| 731 | story.append(Spacer(1, 18)) |
---|
| 732 | for msg in self.context.history.messages: |
---|
| 733 | f_msg = '<font face="Courier" size=10>%s</font>' % msg |
---|
| 734 | story.append(Paragraph(f_msg, style["Normal"])) |
---|
[6363] | 735 | story.append(Spacer(1, 24)) |
---|
[7276] | 736 | # Setup table data |
---|
| 737 | data = [] |
---|
| 738 | # Insert passport photograph |
---|
[7063] | 739 | img = getUtility(IExtFileStore).getFileByContext(self.context) |
---|
| 740 | if img is None: |
---|
[7089] | 741 | img = open(DEFAULT_PASSPORT_IMAGE_PATH, 'rb') |
---|
[7063] | 742 | doc_img = Image(img.name, width=4*cm, height=3*cm, kind='bound') |
---|
[7276] | 743 | data.append([doc_img]) |
---|
| 744 | data.append([Spacer(1, 18)]) |
---|
| 745 | # Render widget fields |
---|
[6358] | 746 | self.setUpWidgets() |
---|
| 747 | for widget in self.widgets: |
---|
[6462] | 748 | f_label = '<font size=12>%s</font>:' % widget.label.strip() |
---|
[6363] | 749 | f_label = Paragraph(f_label, style["Normal"]) |
---|
[7063] | 750 | f_text = '<font size=12>%s</font>' % widget() |
---|
| 751 | f_text = Paragraph(f_text, style["Normal"]) |
---|
| 752 | data.append([f_label,f_text]) |
---|
[7347] | 753 | course_admitted = self.getCourseAdmitted() |
---|
| 754 | f_label = '<font size=12>Admitted Course of Study:</font>' |
---|
| 755 | f_text = '<font size=12>%s</font>' % course_admitted |
---|
[6363] | 756 | f_label = Paragraph(f_label, style["Normal"]) |
---|
| 757 | f_text = Paragraph(f_text, style["Normal"]) |
---|
| 758 | data.append([f_label,f_text]) |
---|
[7341] | 759 | |
---|
[7347] | 760 | course_admitted = self.context.course_admitted |
---|
[7351] | 761 | if getattr(course_admitted, '__parent__',None): |
---|
[7347] | 762 | f_label = '<font size=12>Department:</font>' |
---|
| 763 | f_text = '<font size=12>%s</font>' % ( |
---|
| 764 | course_admitted.__parent__.__parent__.longtitle()) |
---|
| 765 | f_label = Paragraph(f_label, style["Normal"]) |
---|
| 766 | f_text = Paragraph(f_text, style["Normal"]) |
---|
| 767 | data.append([f_label,f_text]) |
---|
[7341] | 768 | |
---|
[7347] | 769 | f_label = '<font size=12>Faculty:</font>' |
---|
| 770 | f_text = '<font size=12>%s</font>' % ( |
---|
| 771 | course_admitted.__parent__.__parent__.__parent__.longtitle()) |
---|
| 772 | f_label = Paragraph(f_label, style["Normal"]) |
---|
| 773 | f_text = Paragraph(f_text, style["Normal"]) |
---|
| 774 | data.append([f_label,f_text]) |
---|
| 775 | |
---|
[7276] | 776 | # Create table |
---|
[6364] | 777 | table = Table(data,style=SLIP_STYLE) |
---|
[6363] | 778 | story.append(table) |
---|
| 779 | frame_body.addFromList(story,pdf) |
---|
| 780 | |
---|
[6364] | 781 | story = [] |
---|
[6365] | 782 | frame_footer = Frame(1*cm,0,width-(2*cm),1*cm) |
---|
[6364] | 783 | timestamp = datetime.now().strftime("%d/%m/%Y %H:%M:%S") |
---|
| 784 | f_text = '<font size=10>%s</font>' % timestamp |
---|
| 785 | story.append(Paragraph(f_text, style["Normal"])) |
---|
| 786 | frame_footer.addFromList(story,pdf) |
---|
| 787 | |
---|
[6358] | 788 | self.response.setHeader( |
---|
| 789 | 'Content-Type', 'application/pdf') |
---|
| 790 | return pdf.getpdfdata() |
---|
| 791 | |
---|
[6383] | 792 | class ApplicantManageActionButton(ManageActionButton): |
---|
[6198] | 793 | grok.context(IApplicant) |
---|
[7200] | 794 | grok.view(ApplicantDisplayFormPage) |
---|
[7136] | 795 | grok.require('waeup.manageApplication') |
---|
[6383] | 796 | text = 'Manage application record' |
---|
[7200] | 797 | target = 'manage' |
---|
[6198] | 798 | |
---|
[7240] | 799 | class ApplicantEditActionButton(ManageActionButton): |
---|
| 800 | grok.context(IApplicant) |
---|
| 801 | grok.view(ApplicantDisplayFormPage) |
---|
| 802 | grok.require('waeup.handleApplication') |
---|
| 803 | text = 'Edit application record' |
---|
| 804 | target ='edit' |
---|
[7081] | 805 | |
---|
[7240] | 806 | @property |
---|
| 807 | def target_url(self): |
---|
| 808 | """Get a URL to the target... |
---|
| 809 | """ |
---|
| 810 | if self.context.locked: |
---|
| 811 | return |
---|
| 812 | return self.view.url(self.view.context, self.target) |
---|
| 813 | |
---|
[7081] | 814 | def handle_img_upload(upload, context, view): |
---|
[7063] | 815 | """Handle upload of applicant image. |
---|
[7081] | 816 | |
---|
| 817 | Returns `True` in case of success or `False`. |
---|
| 818 | |
---|
| 819 | Please note that file pointer passed in (`upload`) most probably |
---|
| 820 | points to end of file when leaving this function. |
---|
[7063] | 821 | """ |
---|
[7081] | 822 | size = file_size(upload) |
---|
| 823 | if size > MAX_UPLOAD_SIZE: |
---|
| 824 | view.flash('Uploaded image is too big!') |
---|
| 825 | return False |
---|
[7247] | 826 | dummy, ext = os.path.splitext(upload.filename) |
---|
| 827 | ext.lower() |
---|
| 828 | if ext != '.jpg': |
---|
| 829 | view.flash('jpg file extension expected.') |
---|
| 830 | return False |
---|
[7081] | 831 | upload.seek(0) # file pointer moved when determining size |
---|
[7063] | 832 | store = getUtility(IExtFileStore) |
---|
| 833 | file_id = IFileStoreNameChooser(context).chooseName() |
---|
| 834 | store.createFile(file_id, upload) |
---|
[7081] | 835 | return True |
---|
[7063] | 836 | |
---|
[7321] | 837 | class ApplicantManageFormPage(SIRPEditFormPage): |
---|
[6196] | 838 | """A full edit view for applicant data. |
---|
| 839 | """ |
---|
| 840 | grok.context(IApplicant) |
---|
[7200] | 841 | grok.name('manage') |
---|
[7136] | 842 | grok.require('waeup.manageApplication') |
---|
[6476] | 843 | form_fields = grok.AutoFields(IApplicant) |
---|
[6196] | 844 | form_fields['date_of_birth'].custom_widget = FriendlyDateWidget('le-year') |
---|
[7351] | 845 | form_fields['student_id'].for_display = True |
---|
[7200] | 846 | grok.template('applicanteditpage') |
---|
[6322] | 847 | manage_applications = True |
---|
[6196] | 848 | pnav = 3 |
---|
[7250] | 849 | display_actions = [['Save', 'Final Submit'], |
---|
| 850 | ['Add online payment ticket','Remove selected tickets']] |
---|
[6196] | 851 | |
---|
| 852 | def update(self): |
---|
| 853 | datepicker.need() # Enable jQuery datepicker in date fields. |
---|
[7330] | 854 | warning.need() |
---|
[7200] | 855 | super(ApplicantManageFormPage, self).update() |
---|
[6353] | 856 | self.wf_info = IWorkflowInfo(self.context) |
---|
[7081] | 857 | self.max_upload_size = string_from_bytes(MAX_UPLOAD_SIZE) |
---|
[7084] | 858 | self.passport_changed = None |
---|
[6598] | 859 | upload = self.request.form.get('form.passport', None) |
---|
| 860 | if upload: |
---|
| 861 | # We got a fresh upload |
---|
[7084] | 862 | self.passport_changed = handle_img_upload( |
---|
| 863 | upload, self.context, self) |
---|
[6196] | 864 | return |
---|
| 865 | |
---|
| 866 | @property |
---|
| 867 | def title(self): |
---|
[7240] | 868 | return 'Application Record %s' % self.context.application_number |
---|
[6196] | 869 | |
---|
| 870 | @property |
---|
| 871 | def label(self): |
---|
| 872 | container_title = self.context.__parent__.title |
---|
[7240] | 873 | return '%s Application Form %s' % ( |
---|
| 874 | container_title, self.context.application_number) |
---|
[6196] | 875 | |
---|
[6303] | 876 | def getTransitions(self): |
---|
[6351] | 877 | """Return a list of dicts of allowed transition ids and titles. |
---|
[6353] | 878 | |
---|
| 879 | Each list entry provides keys ``name`` and ``title`` for |
---|
| 880 | internal name and (human readable) title of a single |
---|
| 881 | transition. |
---|
[6349] | 882 | """ |
---|
[6353] | 883 | allowed_transitions = self.wf_info.getManualTransitions() |
---|
[6355] | 884 | return [dict(name='', title='No transition')] +[ |
---|
| 885 | dict(name=x, title=y) for x, y in allowed_transitions] |
---|
[6303] | 886 | |
---|
[6196] | 887 | @grok.action('Save') |
---|
| 888 | def save(self, **data): |
---|
[7240] | 889 | form = self.request.form |
---|
| 890 | password = form.get('password', None) |
---|
| 891 | password_ctl = form.get('control_password', None) |
---|
| 892 | if password: |
---|
| 893 | validator = getUtility(IPasswordValidator) |
---|
| 894 | errors = validator.validate_password(password, password_ctl) |
---|
| 895 | if errors: |
---|
| 896 | self.flash( ' '.join(errors)) |
---|
| 897 | return |
---|
[7084] | 898 | if self.passport_changed is False: # False is not None! |
---|
| 899 | return # error during image upload. Ignore other values |
---|
[6475] | 900 | changed_fields = self.applyData(self.context, **data) |
---|
[7199] | 901 | # Turn list of lists into single list |
---|
| 902 | if changed_fields: |
---|
| 903 | changed_fields = reduce(lambda x,y: x+y, changed_fields.values()) |
---|
[7240] | 904 | else: |
---|
| 905 | changed_fields = [] |
---|
| 906 | if self.passport_changed: |
---|
| 907 | changed_fields.append('passport') |
---|
| 908 | if password: |
---|
| 909 | # Now we know that the form has no errors and can set password ... |
---|
| 910 | IUserAccount(self.context).setPassword(password) |
---|
| 911 | changed_fields.append('password') |
---|
[7199] | 912 | fields_string = ' + '.join(changed_fields) |
---|
[7085] | 913 | trans_id = form.get('transition', None) |
---|
| 914 | if trans_id: |
---|
| 915 | self.wf_info.fireTransition(trans_id) |
---|
[6196] | 916 | self.flash('Form has been saved.') |
---|
[6475] | 917 | ob_class = self.__implemented__.__name__.replace('waeup.sirp.','') |
---|
[6644] | 918 | if fields_string: |
---|
| 919 | self.context.loggerInfo(ob_class, 'saved: % s' % fields_string) |
---|
[6196] | 920 | return |
---|
| 921 | |
---|
[7250] | 922 | def unremovable(self, ticket): |
---|
[7330] | 923 | return False |
---|
[7250] | 924 | |
---|
| 925 | # This method is also used by the ApplicantEditFormPage |
---|
| 926 | def delPaymentTickets(self, **data): |
---|
| 927 | form = self.request.form |
---|
| 928 | if form.has_key('val_id'): |
---|
| 929 | child_id = form['val_id'] |
---|
| 930 | else: |
---|
| 931 | self.flash('No payment selected.') |
---|
| 932 | self.redirect(self.url(self.context)) |
---|
| 933 | return |
---|
| 934 | if not isinstance(child_id, list): |
---|
| 935 | child_id = [child_id] |
---|
| 936 | deleted = [] |
---|
| 937 | for id in child_id: |
---|
| 938 | # Applicants are not allowed to remove used payment tickets |
---|
| 939 | if not self.unremovable(self.context[id]): |
---|
| 940 | try: |
---|
| 941 | del self.context[id] |
---|
| 942 | deleted.append(id) |
---|
| 943 | except: |
---|
| 944 | self.flash('Could not delete %s: %s: %s' % ( |
---|
| 945 | id, sys.exc_info()[0], sys.exc_info()[1])) |
---|
| 946 | if len(deleted): |
---|
| 947 | self.flash('Successfully removed: %s' % ', '.join(deleted)) |
---|
| 948 | ob_class = self.__implemented__.__name__.replace('waeup.sirp.','') |
---|
[7363] | 949 | self.context.loggerInfo( |
---|
| 950 | ob_class, 'removed: % s' % ', '.join(deleted)) |
---|
[7250] | 951 | return |
---|
| 952 | |
---|
[7252] | 953 | # We explicitely want the forms to be validated before payment tickets |
---|
| 954 | # can be created. If no validation is requested, use |
---|
| 955 | # 'validator=NullValidator' in the grok.action directive |
---|
[7250] | 956 | @grok.action('Add online payment ticket') |
---|
| 957 | def addPaymentTicket(self, **data): |
---|
| 958 | self.redirect(self.url(self.context, '@@addafp')) |
---|
[7252] | 959 | return |
---|
[7250] | 960 | |
---|
[7330] | 961 | @jsaction('Remove selected tickets') |
---|
[7250] | 962 | def removePaymentTickets(self, **data): |
---|
| 963 | self.delPaymentTickets(**data) |
---|
| 964 | self.redirect(self.url(self.context) + '/@@manage') |
---|
| 965 | return |
---|
| 966 | |
---|
[7200] | 967 | class ApplicantEditFormPage(ApplicantManageFormPage): |
---|
[5982] | 968 | """An applicant-centered edit view for applicant data. |
---|
| 969 | """ |
---|
[6196] | 970 | grok.context(IApplicantEdit) |
---|
[5273] | 971 | grok.name('edit') |
---|
[6198] | 972 | grok.require('waeup.handleApplication') |
---|
[6459] | 973 | form_fields = grok.AutoFields(IApplicantEdit).omit( |
---|
[6476] | 974 | 'locked', 'course_admitted', 'student_id', |
---|
[7270] | 975 | 'screening_score', 'applicant_id', 'reg_number' |
---|
[6459] | 976 | ) |
---|
[6054] | 977 | form_fields['date_of_birth'].custom_widget = FriendlyDateWidget('le-year') |
---|
[7363] | 978 | #form_fields['phone'].custom_widget = PhoneWidget |
---|
[7200] | 979 | grok.template('applicanteditpage') |
---|
[6322] | 980 | manage_applications = False |
---|
[6465] | 981 | title = u'Your Application Form' |
---|
[5484] | 982 | |
---|
[7250] | 983 | @property |
---|
| 984 | def display_actions(self): |
---|
| 985 | state = IWorkflowState(self.context).getState() |
---|
| 986 | if state == INITIALIZED: |
---|
| 987 | actions = [[],[]] |
---|
| 988 | elif state == STARTED: |
---|
| 989 | actions = [['Save'], |
---|
| 990 | ['Add online payment ticket','Remove selected tickets']] |
---|
| 991 | elif state == PAID: |
---|
| 992 | actions = [['Save', 'Final Submit'], |
---|
| 993 | ['Remove selected tickets']] |
---|
[7351] | 994 | else: |
---|
[7250] | 995 | actions = [[],[]] |
---|
| 996 | return actions |
---|
| 997 | |
---|
[7330] | 998 | def unremovable(self, ticket): |
---|
| 999 | state = IWorkflowState(self.context).getState() |
---|
| 1000 | return ticket.r_code or state in (INITIALIZED, SUBMITTED) |
---|
| 1001 | |
---|
[7145] | 1002 | def emit_lock_message(self): |
---|
[6105] | 1003 | self.flash('The requested form is locked (read-only).') |
---|
[5941] | 1004 | self.redirect(self.url(self.context)) |
---|
| 1005 | return |
---|
[6078] | 1006 | |
---|
[5686] | 1007 | def update(self): |
---|
[5941] | 1008 | if self.context.locked: |
---|
[7145] | 1009 | self.emit_lock_message() |
---|
[5941] | 1010 | return |
---|
[7200] | 1011 | super(ApplicantEditFormPage, self).update() |
---|
[5686] | 1012 | return |
---|
[5952] | 1013 | |
---|
[6196] | 1014 | def dataNotComplete(self): |
---|
[7252] | 1015 | store = getUtility(IExtFileStore) |
---|
| 1016 | if not store.getFileByContext(self.context, attr=u'passport.jpg'): |
---|
| 1017 | return 'No passport picture uploaded.' |
---|
[6322] | 1018 | if not self.request.form.get('confirm_passport', False): |
---|
[7252] | 1019 | return 'Passport picture confirmation box not ticked.' |
---|
[6196] | 1020 | return False |
---|
[5952] | 1021 | |
---|
[7252] | 1022 | # We explicitely want the forms to be validated before payment tickets |
---|
| 1023 | # can be created. If no validation is requested, use |
---|
| 1024 | # 'validator=NullValidator' in the grok.action directive |
---|
[7250] | 1025 | @grok.action('Add online payment ticket') |
---|
| 1026 | def addPaymentTicket(self, **data): |
---|
| 1027 | self.redirect(self.url(self.context, '@@addafp')) |
---|
[7252] | 1028 | return |
---|
[7250] | 1029 | |
---|
[7330] | 1030 | @jsaction('Remove selected tickets') |
---|
[7250] | 1031 | def removePaymentTickets(self, **data): |
---|
| 1032 | self.delPaymentTickets(**data) |
---|
| 1033 | self.redirect(self.url(self.context) + '/@@edit') |
---|
| 1034 | return |
---|
| 1035 | |
---|
[5273] | 1036 | @grok.action('Save') |
---|
| 1037 | def save(self, **data): |
---|
[7084] | 1038 | if self.passport_changed is False: # False is not None! |
---|
| 1039 | return # error during image upload. Ignore other values |
---|
[5273] | 1040 | self.applyData(self.context, **data) |
---|
[6196] | 1041 | self.flash('Form has been saved.') |
---|
[5273] | 1042 | return |
---|
| 1043 | |
---|
[5484] | 1044 | @grok.action('Final Submit') |
---|
| 1045 | def finalsubmit(self, **data): |
---|
[7084] | 1046 | if self.passport_changed is False: # False is not None! |
---|
| 1047 | return # error during image upload. Ignore other values |
---|
[6196] | 1048 | if self.dataNotComplete(): |
---|
| 1049 | self.flash(self.dataNotComplete()) |
---|
[5941] | 1050 | return |
---|
[7252] | 1051 | self.applyData(self.context, **data) |
---|
[6303] | 1052 | state = IWorkflowState(self.context).getState() |
---|
[6322] | 1053 | # This shouldn't happen, but the application officer |
---|
| 1054 | # might have forgotten to lock the form after changing the state |
---|
[7250] | 1055 | if state != PAID: |
---|
[6322] | 1056 | self.flash('This form cannot be submitted. Wrong state!') |
---|
[6303] | 1057 | return |
---|
| 1058 | IWorkflowInfo(self.context).fireTransition('submit') |
---|
[6476] | 1059 | self.context.application_date = datetime.now() |
---|
[5941] | 1060 | self.context.locked = True |
---|
[6196] | 1061 | self.flash('Form has been submitted.') |
---|
| 1062 | self.redirect(self.url(self.context)) |
---|
[5273] | 1063 | return |
---|
[5941] | 1064 | |
---|
[6367] | 1065 | class ApplicantViewActionButton(ManageActionButton): |
---|
| 1066 | grok.context(IApplicant) |
---|
[7200] | 1067 | grok.view(ApplicantManageFormPage) |
---|
[7240] | 1068 | grok.require('waeup.viewApplication') |
---|
[6383] | 1069 | icon = 'actionicon_view.png' |
---|
[6367] | 1070 | text = 'View application record' |
---|
[6598] | 1071 | target = 'index' |
---|
[7063] | 1072 | |
---|
| 1073 | class PassportImage(grok.View): |
---|
| 1074 | """Renders the passport image for applicants. |
---|
| 1075 | """ |
---|
| 1076 | grok.name('passport.jpg') |
---|
| 1077 | grok.context(IApplicant) |
---|
[7113] | 1078 | grok.require('waeup.viewApplication') |
---|
[7063] | 1079 | |
---|
| 1080 | def render(self): |
---|
| 1081 | # A filename chooser turns a context into a filename suitable |
---|
| 1082 | # for file storage. |
---|
| 1083 | image = getUtility(IExtFileStore).getFileByContext(self.context) |
---|
| 1084 | self.response.setHeader( |
---|
| 1085 | 'Content-Type', 'image/jpeg') |
---|
| 1086 | if image is None: |
---|
| 1087 | # show placeholder image |
---|
[7089] | 1088 | return open(DEFAULT_PASSPORT_IMAGE_PATH, 'rb').read() |
---|
[7063] | 1089 | return image |
---|
[7363] | 1090 | |
---|
| 1091 | class ApplicantRegistrationSuccessful(SIRPPage): |
---|
| 1092 | """Info page when applicant registration was successful. |
---|
| 1093 | """ |
---|
| 1094 | grok.context(IApplicantsContainer) |
---|
| 1095 | grok.name('register_success') |
---|
| 1096 | grok.require('waeup.Public') |
---|
| 1097 | grok.template('applicantregister_succ') |
---|
| 1098 | label = 'Applicant Registration Successful' |
---|
| 1099 | title = label |
---|
| 1100 | |
---|
| 1101 | def update(self, app_id=None): |
---|
| 1102 | if app_id is None: |
---|
| 1103 | self.redirect(self.url(self.context, 'register')) |
---|
| 1104 | self.app_id = app_id |
---|
| 1105 | self.login_url = self.url(grok.getSite(), 'login') |
---|
| 1106 | |
---|
| 1107 | class ApplicantRegistrationPage(SIRPAddFormPage): |
---|
| 1108 | """Captcha'd registration page for applicants. |
---|
| 1109 | """ |
---|
| 1110 | grok.context(IApplicantsContainer) |
---|
| 1111 | grok.name('register') |
---|
| 1112 | grok.require('waeup.Public') |
---|
| 1113 | grok.template('applicantregister') |
---|
| 1114 | label = 'Applicant Registration' |
---|
| 1115 | title = label |
---|
| 1116 | form_fields = grok.AutoFields(IApplicantEdit).select( |
---|
| 1117 | 'firstname', 'middlename', 'lastname', 'email', 'phone') |
---|
| 1118 | form_fields['phone'].custom_widget = PhoneWidget |
---|
| 1119 | |
---|
| 1120 | def update(self): |
---|
| 1121 | # handle captcha |
---|
| 1122 | self.captcha = getUtility(ICaptchaManager).getCaptcha() |
---|
| 1123 | self.captcha_result = self.captcha.verify(self.request) |
---|
| 1124 | self.captcha_code = self.captcha.display(self.captcha_result.error_code) |
---|
| 1125 | return |
---|
| 1126 | |
---|
| 1127 | @grok.action('Register') |
---|
| 1128 | def register(self, **data): |
---|
| 1129 | if not self.captcha_result.is_valid: |
---|
| 1130 | # captcha will display error messages automatically. |
---|
| 1131 | # No need to flash something. |
---|
| 1132 | return |
---|
| 1133 | |
---|
| 1134 | # handle password field manually |
---|
| 1135 | form = self.request.form |
---|
| 1136 | password = form.get('password', None) |
---|
| 1137 | password_ctl = form.get('control_password', None) |
---|
| 1138 | if password: |
---|
| 1139 | validator = getUtility(IPasswordValidator) |
---|
| 1140 | errors = validator.validate_password(password, password_ctl) |
---|
| 1141 | if errors: |
---|
| 1142 | self.flash( ' '.join(errors)) |
---|
| 1143 | return |
---|
| 1144 | |
---|
| 1145 | # add applicant and redirect to success page |
---|
| 1146 | applicant = createObject('waeup.Applicant') |
---|
| 1147 | self.applyData(applicant, **data) |
---|
| 1148 | self.context.addApplicant(applicant) |
---|
| 1149 | IUserAccount(applicant).setPassword(password) |
---|
| 1150 | app_id = applicant.applicant_id |
---|
| 1151 | self.redirect(self.url(self.context, 'register_success', |
---|
| 1152 | data=dict(app_id=app_id))) |
---|
| 1153 | return |
---|