[5273] | 1 | ## $Id: browser.py 13133 2015-07-02 10:06:52Z henrik $ |
---|
[6078] | 2 | ## |
---|
[7192] | 3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
[5273] | 4 | ## This program is free software; you can redistribute it and/or modify |
---|
| 5 | ## it under the terms of the GNU General Public License as published by |
---|
| 6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | ## (at your option) any later version. |
---|
[6078] | 8 | ## |
---|
[5273] | 9 | ## This program is distributed in the hope that it will be useful, |
---|
| 10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | ## GNU General Public License for more details. |
---|
[6078] | 13 | ## |
---|
[5273] | 14 | ## You should have received a copy of the GNU General Public License |
---|
| 15 | ## along with this program; if not, write to the Free Software |
---|
| 16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 17 | ## |
---|
[5824] | 18 | """UI components for basic applicants and related components. |
---|
[5273] | 19 | """ |
---|
[7063] | 20 | import os |
---|
[6082] | 21 | import sys |
---|
[5273] | 22 | import grok |
---|
[7370] | 23 | from datetime import datetime, date |
---|
[8042] | 24 | from zope.event import notify |
---|
[7392] | 25 | from zope.component import getUtility, createObject, getAdapter |
---|
[8033] | 26 | from zope.catalog.interfaces import ICatalog |
---|
[7714] | 27 | from zope.i18n import translate |
---|
[7322] | 28 | from hurry.workflow.interfaces import ( |
---|
| 29 | IWorkflowInfo, IWorkflowState, InvalidTransitionError) |
---|
[7811] | 30 | from waeup.kofa.applicants.interfaces import ( |
---|
[7363] | 31 | IApplicant, IApplicantEdit, IApplicantsRoot, |
---|
[7683] | 32 | IApplicantsContainer, IApplicantsContainerAdd, |
---|
[8033] | 33 | MAX_UPLOAD_SIZE, IApplicantOnlinePayment, IApplicantsUtils, |
---|
[10845] | 34 | IApplicantRegisterUpdate, ISpecialApplicant |
---|
[7363] | 35 | ) |
---|
[12247] | 36 | from waeup.kofa.utils.helpers import html2dict |
---|
[10655] | 37 | from waeup.kofa.applicants.container import ( |
---|
| 38 | ApplicantsContainer, VirtualApplicantsExportJobContainer) |
---|
[8404] | 39 | from waeup.kofa.applicants.applicant import search |
---|
[8636] | 40 | from waeup.kofa.applicants.workflow import ( |
---|
| 41 | INITIALIZED, STARTED, PAID, SUBMITTED, ADMITTED) |
---|
[7811] | 42 | from waeup.kofa.browser import ( |
---|
[9217] | 43 | # KofaPage, KofaEditFormPage, KofaAddFormPage, KofaDisplayFormPage, |
---|
[7363] | 44 | DEFAULT_PASSPORT_IMAGE_PATH) |
---|
[9217] | 45 | from waeup.kofa.browser.layout import ( |
---|
| 46 | KofaPage, KofaEditFormPage, KofaAddFormPage, KofaDisplayFormPage) |
---|
[7811] | 47 | from waeup.kofa.browser.interfaces import ICaptchaManager |
---|
| 48 | from waeup.kofa.browser.breadcrumbs import Breadcrumb |
---|
| 49 | from waeup.kofa.browser.layout import ( |
---|
[11437] | 50 | NullValidator, jsaction, action, UtilityView) |
---|
[10655] | 51 | from waeup.kofa.browser.pages import ( |
---|
| 52 | add_local_role, del_local_roles, doll_up, ExportCSVView) |
---|
[7811] | 53 | from waeup.kofa.interfaces import ( |
---|
[7819] | 54 | IKofaObject, ILocalRolesAssignable, IExtFileStore, IPDF, |
---|
| 55 | IFileStoreNameChooser, IPasswordValidator, IUserAccount, IKofaUtils) |
---|
[7811] | 56 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
| 57 | from waeup.kofa.permissions import get_users_with_local_roles |
---|
| 58 | from waeup.kofa.students.interfaces import IStudentsUtils |
---|
[8186] | 59 | from waeup.kofa.utils.helpers import string_from_bytes, file_size, now |
---|
[8170] | 60 | from waeup.kofa.widgets.datewidget import ( |
---|
[10831] | 61 | FriendlyDateDisplayWidget, |
---|
[8170] | 62 | FriendlyDatetimeDisplayWidget) |
---|
[5320] | 63 | |
---|
[7819] | 64 | grok.context(IKofaObject) # Make IKofaObject the default context |
---|
[5273] | 65 | |
---|
[11437] | 66 | WARNING = _('You can not edit your application records after final submission.' |
---|
| 67 | ' You really want to submit?') |
---|
[8550] | 68 | |
---|
[8388] | 69 | class ApplicantsRootPage(KofaDisplayFormPage): |
---|
[5822] | 70 | grok.context(IApplicantsRoot) |
---|
| 71 | grok.name('index') |
---|
[6153] | 72 | grok.require('waeup.Public') |
---|
[8388] | 73 | form_fields = grok.AutoFields(IApplicantsRoot) |
---|
[13076] | 74 | label = _('Applicants Section') |
---|
[5843] | 75 | pnav = 3 |
---|
[6012] | 76 | |
---|
| 77 | def update(self): |
---|
[6067] | 78 | super(ApplicantsRootPage, self).update() |
---|
[6012] | 79 | return |
---|
| 80 | |
---|
[8388] | 81 | @property |
---|
| 82 | def introduction(self): |
---|
| 83 | # Here we know that the cookie has been set |
---|
| 84 | lang = self.request.cookies.get('kofa.language') |
---|
| 85 | html = self.context.description_dict.get(lang,'') |
---|
| 86 | if html == '': |
---|
| 87 | portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
| 88 | html = self.context.description_dict.get(portal_language,'') |
---|
| 89 | return html |
---|
| 90 | |
---|
[10097] | 91 | @property |
---|
| 92 | def containers(self): |
---|
[10098] | 93 | if self.layout.isAuthenticated(): |
---|
| 94 | return self.context.values() |
---|
[10097] | 95 | return [value for value in self.context.values() if not value.hidden] |
---|
| 96 | |
---|
[8404] | 97 | class ApplicantsSearchPage(KofaPage): |
---|
| 98 | grok.context(IApplicantsRoot) |
---|
| 99 | grok.name('search') |
---|
| 100 | grok.require('waeup.viewApplication') |
---|
[10644] | 101 | label = _('Find applicants') |
---|
[10645] | 102 | search_button = _('Find applicant') |
---|
[8404] | 103 | pnav = 3 |
---|
| 104 | |
---|
| 105 | def update(self, *args, **kw): |
---|
| 106 | form = self.request.form |
---|
| 107 | self.results = [] |
---|
| 108 | if 'searchterm' in form and form['searchterm']: |
---|
| 109 | self.searchterm = form['searchterm'] |
---|
| 110 | self.searchtype = form['searchtype'] |
---|
| 111 | elif 'old_searchterm' in form: |
---|
| 112 | self.searchterm = form['old_searchterm'] |
---|
| 113 | self.searchtype = form['old_searchtype'] |
---|
| 114 | else: |
---|
| 115 | if 'search' in form: |
---|
[11254] | 116 | self.flash(_('Empty search string'), type='warning') |
---|
[8404] | 117 | return |
---|
| 118 | self.results = search(query=self.searchterm, |
---|
| 119 | searchtype=self.searchtype, view=self) |
---|
| 120 | if not self.results: |
---|
[11254] | 121 | self.flash(_('No applicant found.'), type='warning') |
---|
[8404] | 122 | return |
---|
| 123 | |
---|
[7819] | 124 | class ApplicantsRootManageFormPage(KofaEditFormPage): |
---|
[5828] | 125 | grok.context(IApplicantsRoot) |
---|
| 126 | grok.name('manage') |
---|
[6107] | 127 | grok.template('applicantsrootmanagepage') |
---|
[8388] | 128 | form_fields = grok.AutoFields(IApplicantsRoot) |
---|
[13076] | 129 | label = _('Manage applicants section') |
---|
[5843] | 130 | pnav = 3 |
---|
[7136] | 131 | grok.require('waeup.manageApplication') |
---|
[8388] | 132 | taboneactions = [_('Save')] |
---|
| 133 | tabtwoactions = [_('Add applicants container'), _('Remove selected')] |
---|
| 134 | tabthreeactions1 = [_('Remove selected local roles')] |
---|
| 135 | tabthreeactions2 = [_('Add local role')] |
---|
[7710] | 136 | subunits = _('Applicants Containers') |
---|
[6078] | 137 | |
---|
[6184] | 138 | def getLocalRoles(self): |
---|
| 139 | roles = ILocalRolesAssignable(self.context) |
---|
| 140 | return roles() |
---|
| 141 | |
---|
| 142 | def getUsers(self): |
---|
| 143 | """Get a list of all users. |
---|
| 144 | """ |
---|
| 145 | for key, val in grok.getSite()['users'].items(): |
---|
| 146 | url = self.url(val) |
---|
| 147 | yield(dict(url=url, name=key, val=val)) |
---|
| 148 | |
---|
| 149 | def getUsersWithLocalRoles(self): |
---|
| 150 | return get_users_with_local_roles(self.context) |
---|
| 151 | |
---|
[7710] | 152 | @jsaction(_('Remove selected')) |
---|
[6069] | 153 | def delApplicantsContainers(self, **data): |
---|
| 154 | form = self.request.form |
---|
[9701] | 155 | if 'val_id' in form: |
---|
[8388] | 156 | child_id = form['val_id'] |
---|
| 157 | else: |
---|
[11254] | 158 | self.flash(_('No container selected!'), type='warning') |
---|
| 159 | self.redirect(self.url(self.context, '@@manage')+'#tab2') |
---|
[8388] | 160 | return |
---|
[6069] | 161 | if not isinstance(child_id, list): |
---|
| 162 | child_id = [child_id] |
---|
| 163 | deleted = [] |
---|
| 164 | for id in child_id: |
---|
| 165 | try: |
---|
| 166 | del self.context[id] |
---|
| 167 | deleted.append(id) |
---|
| 168 | except: |
---|
[7710] | 169 | self.flash(_('Could not delete:') + ' %s: %s: %s' % ( |
---|
[11254] | 170 | id, sys.exc_info()[0], sys.exc_info()[1]), type='danger') |
---|
[6069] | 171 | if len(deleted): |
---|
[7738] | 172 | self.flash(_('Successfully removed: ${a}', |
---|
| 173 | mapping = {'a':', '.join(deleted)})) |
---|
[12892] | 174 | ob_class = self.__implemented__.__name__.replace('waeup.kofa.','') |
---|
| 175 | self.context.logger.info( |
---|
| 176 | '%s - removed: %s' % (ob_class, ', '.join(deleted))) |
---|
[11254] | 177 | self.redirect(self.url(self.context, '@@manage')+'#tab2') |
---|
[6078] | 178 | return |
---|
[5828] | 179 | |
---|
[7710] | 180 | @action(_('Add applicants container'), validator=NullValidator) |
---|
[6069] | 181 | def addApplicantsContainer(self, **data): |
---|
| 182 | self.redirect(self.url(self.context, '@@add')) |
---|
[6078] | 183 | return |
---|
| 184 | |
---|
[7710] | 185 | @action(_('Add local role'), validator=NullValidator) |
---|
[6184] | 186 | def addLocalRole(self, **data): |
---|
[7484] | 187 | return add_local_role(self,3, **data) |
---|
[6184] | 188 | |
---|
[7710] | 189 | @action(_('Remove selected local roles')) |
---|
[6184] | 190 | def delLocalRoles(self, **data): |
---|
[7484] | 191 | return del_local_roles(self,3,**data) |
---|
[6184] | 192 | |
---|
[8388] | 193 | @action(_('Save'), style='primary') |
---|
| 194 | def save(self, **data): |
---|
| 195 | self.applyData(self.context, **data) |
---|
[12247] | 196 | description = getattr(self.context, 'description', None) |
---|
| 197 | portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
| 198 | self.context.description_dict = html2dict(description, portal_language) |
---|
[8390] | 199 | self.flash(_('Form has been saved.')) |
---|
[8388] | 200 | return |
---|
| 201 | |
---|
[7819] | 202 | class ApplicantsContainerAddFormPage(KofaAddFormPage): |
---|
[5822] | 203 | grok.context(IApplicantsRoot) |
---|
[7136] | 204 | grok.require('waeup.manageApplication') |
---|
[5822] | 205 | grok.name('add') |
---|
[6107] | 206 | grok.template('applicantscontaineraddpage') |
---|
[7710] | 207 | label = _('Add applicants container') |
---|
[5843] | 208 | pnav = 3 |
---|
[6078] | 209 | |
---|
[6103] | 210 | form_fields = grok.AutoFields( |
---|
[7903] | 211 | IApplicantsContainerAdd).omit('code').omit('title') |
---|
[6078] | 212 | |
---|
[7710] | 213 | @action(_('Add applicants container')) |
---|
[6069] | 214 | def addApplicantsContainer(self, **data): |
---|
[6103] | 215 | year = data['year'] |
---|
| 216 | code = u'%s%s' % (data['prefix'], year) |
---|
[9529] | 217 | apptypes_dict = getUtility(IApplicantsUtils).APP_TYPES_DICT |
---|
| 218 | title = apptypes_dict[data['prefix']][0] |
---|
[7685] | 219 | title = u'%s %s/%s' % (title, year, year + 1) |
---|
[6087] | 220 | if code in self.context.keys(): |
---|
[6105] | 221 | self.flash( |
---|
[11254] | 222 | _('An applicants container for the same application ' |
---|
| 223 | 'type and entrance year exists already in the database.'), |
---|
| 224 | type='warning') |
---|
[5822] | 225 | return |
---|
| 226 | # Add new applicants container... |
---|
[8009] | 227 | container = createObject(u'waeup.ApplicantsContainer') |
---|
[6069] | 228 | self.applyData(container, **data) |
---|
[6087] | 229 | container.code = code |
---|
| 230 | container.title = title |
---|
| 231 | self.context[code] = container |
---|
[7710] | 232 | self.flash(_('Added:') + ' "%s".' % code) |
---|
[12892] | 233 | ob_class = self.__implemented__.__name__.replace('waeup.kofa.','') |
---|
| 234 | self.context.logger.info('%s - added: %s' % (ob_class, code)) |
---|
[7484] | 235 | self.redirect(self.url(self.context, u'@@manage')) |
---|
[5822] | 236 | return |
---|
[6078] | 237 | |
---|
[7710] | 238 | @action(_('Cancel'), validator=NullValidator) |
---|
[6069] | 239 | def cancel(self, **data): |
---|
[7484] | 240 | self.redirect(self.url(self.context, '@@manage')) |
---|
[6078] | 241 | |
---|
[5845] | 242 | class ApplicantsRootBreadcrumb(Breadcrumb): |
---|
| 243 | """A breadcrumb for applicantsroot. |
---|
| 244 | """ |
---|
| 245 | grok.context(IApplicantsRoot) |
---|
[7710] | 246 | title = _(u'Applicants') |
---|
[6078] | 247 | |
---|
[5845] | 248 | class ApplicantsContainerBreadcrumb(Breadcrumb): |
---|
| 249 | """A breadcrumb for applicantscontainers. |
---|
| 250 | """ |
---|
| 251 | grok.context(IApplicantsContainer) |
---|
[6319] | 252 | |
---|
[10655] | 253 | |
---|
| 254 | class ApplicantsExportsBreadcrumb(Breadcrumb): |
---|
| 255 | """A breadcrumb for exports. |
---|
| 256 | """ |
---|
| 257 | grok.context(VirtualApplicantsExportJobContainer) |
---|
| 258 | title = _(u'Applicant Data Exports') |
---|
| 259 | target = None |
---|
| 260 | |
---|
[6153] | 261 | class ApplicantBreadcrumb(Breadcrumb): |
---|
| 262 | """A breadcrumb for applicants. |
---|
| 263 | """ |
---|
| 264 | grok.context(IApplicant) |
---|
[6319] | 265 | |
---|
[6153] | 266 | @property |
---|
| 267 | def title(self): |
---|
| 268 | """Get a title for a context. |
---|
| 269 | """ |
---|
[7240] | 270 | return self.context.application_number |
---|
[5828] | 271 | |
---|
[7250] | 272 | class OnlinePaymentBreadcrumb(Breadcrumb): |
---|
| 273 | """A breadcrumb for payments. |
---|
| 274 | """ |
---|
| 275 | grok.context(IApplicantOnlinePayment) |
---|
| 276 | |
---|
| 277 | @property |
---|
| 278 | def title(self): |
---|
| 279 | return self.context.p_id |
---|
| 280 | |
---|
[8563] | 281 | class ApplicantsStatisticsPage(KofaDisplayFormPage): |
---|
| 282 | """Some statistics about applicants in a container. |
---|
| 283 | """ |
---|
| 284 | grok.context(IApplicantsContainer) |
---|
| 285 | grok.name('statistics') |
---|
[8565] | 286 | grok.require('waeup.viewApplicationStatistics') |
---|
[8563] | 287 | grok.template('applicantcontainerstatistics') |
---|
| 288 | |
---|
| 289 | @property |
---|
| 290 | def label(self): |
---|
| 291 | return "%s" % self.context.title |
---|
| 292 | |
---|
[7819] | 293 | class ApplicantsContainerPage(KofaDisplayFormPage): |
---|
[5830] | 294 | """The standard view for regular applicant containers. |
---|
| 295 | """ |
---|
| 296 | grok.context(IApplicantsContainer) |
---|
| 297 | grok.name('index') |
---|
[6153] | 298 | grok.require('waeup.Public') |
---|
[6029] | 299 | grok.template('applicantscontainerpage') |
---|
[5850] | 300 | pnav = 3 |
---|
[6053] | 301 | |
---|
[9078] | 302 | @property |
---|
| 303 | def form_fields(self): |
---|
[12247] | 304 | form_fields = grok.AutoFields(IApplicantsContainer).omit( |
---|
| 305 | 'title', 'description') |
---|
[9078] | 306 | form_fields[ |
---|
| 307 | 'startdate'].custom_widget = FriendlyDatetimeDisplayWidget('le') |
---|
| 308 | form_fields[ |
---|
| 309 | 'enddate'].custom_widget = FriendlyDatetimeDisplayWidget('le') |
---|
| 310 | if self.request.principal.id == 'zope.anybody': |
---|
| 311 | form_fields = form_fields.omit( |
---|
[10101] | 312 | 'code', 'prefix', 'year', 'mode', 'hidden', |
---|
[11870] | 313 | 'strict_deadline', 'application_category', |
---|
| 314 | 'application_slip_notice') |
---|
[9078] | 315 | return form_fields |
---|
[6053] | 316 | |
---|
[5837] | 317 | @property |
---|
[7708] | 318 | def introduction(self): |
---|
[7833] | 319 | # Here we know that the cookie has been set |
---|
| 320 | lang = self.request.cookies.get('kofa.language') |
---|
[7708] | 321 | html = self.context.description_dict.get(lang,'') |
---|
[8388] | 322 | if html == '': |
---|
[7833] | 323 | portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
[7708] | 324 | html = self.context.description_dict.get(portal_language,'') |
---|
[8388] | 325 | return html |
---|
[7708] | 326 | |
---|
| 327 | @property |
---|
[7467] | 328 | def label(self): |
---|
[7493] | 329 | return "%s" % self.context.title |
---|
[5837] | 330 | |
---|
[7819] | 331 | class ApplicantsContainerManageFormPage(KofaEditFormPage): |
---|
[5837] | 332 | grok.context(IApplicantsContainer) |
---|
[5850] | 333 | grok.name('manage') |
---|
[6107] | 334 | grok.template('applicantscontainermanagepage') |
---|
[10625] | 335 | form_fields = grok.AutoFields(IApplicantsContainer) |
---|
[7710] | 336 | taboneactions = [_('Save'),_('Cancel')] |
---|
[8684] | 337 | tabtwoactions = [_('Remove selected'),_('Cancel'), |
---|
[8314] | 338 | _('Create students from selected')] |
---|
[7710] | 339 | tabthreeactions1 = [_('Remove selected local roles')] |
---|
| 340 | tabthreeactions2 = [_('Add local role')] |
---|
[5844] | 341 | # Use friendlier date widget... |
---|
[7136] | 342 | grok.require('waeup.manageApplication') |
---|
[5850] | 343 | |
---|
| 344 | @property |
---|
| 345 | def label(self): |
---|
[7710] | 346 | return _('Manage applicants container') |
---|
[5850] | 347 | |
---|
[5845] | 348 | pnav = 3 |
---|
[5837] | 349 | |
---|
[8547] | 350 | @property |
---|
| 351 | def showApplicants(self): |
---|
| 352 | if len(self.context) < 5000: |
---|
| 353 | return True |
---|
| 354 | return False |
---|
| 355 | |
---|
[6184] | 356 | def getLocalRoles(self): |
---|
| 357 | roles = ILocalRolesAssignable(self.context) |
---|
| 358 | return roles() |
---|
| 359 | |
---|
| 360 | def getUsers(self): |
---|
| 361 | """Get a list of all users. |
---|
| 362 | """ |
---|
| 363 | for key, val in grok.getSite()['users'].items(): |
---|
| 364 | url = self.url(val) |
---|
| 365 | yield(dict(url=url, name=key, val=val)) |
---|
| 366 | |
---|
| 367 | def getUsersWithLocalRoles(self): |
---|
| 368 | return get_users_with_local_roles(self.context) |
---|
| 369 | |
---|
[7714] | 370 | @action(_('Save'), style='primary') |
---|
[7489] | 371 | def save(self, **data): |
---|
[9531] | 372 | changed_fields = self.applyData(self.context, **data) |
---|
| 373 | if changed_fields: |
---|
| 374 | changed_fields = reduce(lambda x,y: x+y, changed_fields.values()) |
---|
| 375 | else: |
---|
| 376 | changed_fields = [] |
---|
[12247] | 377 | description = getattr(self.context, 'description', None) |
---|
| 378 | portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
| 379 | self.context.description_dict = html2dict(description, portal_language) |
---|
[7710] | 380 | self.flash(_('Form has been saved.')) |
---|
[9531] | 381 | fields_string = ' + '.join(changed_fields) |
---|
[12892] | 382 | self.context.writeLogMessage(self, 'saved: %s' % fields_string) |
---|
[5837] | 383 | return |
---|
[6078] | 384 | |
---|
[7710] | 385 | @jsaction(_('Remove selected')) |
---|
[6105] | 386 | def delApplicant(self, **data): |
---|
[6189] | 387 | form = self.request.form |
---|
[9701] | 388 | if 'val_id' in form: |
---|
[6189] | 389 | child_id = form['val_id'] |
---|
| 390 | else: |
---|
[11254] | 391 | self.flash(_('No applicant selected!'), type='warning') |
---|
| 392 | self.redirect(self.url(self.context, '@@manage')+'#tab2') |
---|
[6189] | 393 | return |
---|
| 394 | if not isinstance(child_id, list): |
---|
| 395 | child_id = [child_id] |
---|
| 396 | deleted = [] |
---|
| 397 | for id in child_id: |
---|
| 398 | try: |
---|
| 399 | del self.context[id] |
---|
| 400 | deleted.append(id) |
---|
| 401 | except: |
---|
[7710] | 402 | self.flash(_('Could not delete:') + ' %s: %s: %s' % ( |
---|
[11254] | 403 | id, sys.exc_info()[0], sys.exc_info()[1]), type='danger') |
---|
[6189] | 404 | if len(deleted): |
---|
[7741] | 405 | self.flash(_('Successfully removed: ${a}', |
---|
[7738] | 406 | mapping = {'a':', '.join(deleted)})) |
---|
[11254] | 407 | self.redirect(self.url(self.context, u'@@manage')+'#tab2') |
---|
[6189] | 408 | return |
---|
[6105] | 409 | |
---|
[8314] | 410 | @action(_('Create students from selected')) |
---|
| 411 | def createStudents(self, **data): |
---|
| 412 | form = self.request.form |
---|
[9701] | 413 | if 'val_id' in form: |
---|
[8314] | 414 | child_id = form['val_id'] |
---|
| 415 | else: |
---|
[11254] | 416 | self.flash(_('No applicant selected!'), type='warning') |
---|
| 417 | self.redirect(self.url(self.context, '@@manage')+'#tab2') |
---|
[8314] | 418 | return |
---|
| 419 | if not isinstance(child_id, list): |
---|
| 420 | child_id = [child_id] |
---|
| 421 | created = [] |
---|
| 422 | for id in child_id: |
---|
| 423 | success, msg = self.context[id].createStudent(view=self) |
---|
| 424 | if success: |
---|
| 425 | created.append(id) |
---|
| 426 | if len(created): |
---|
| 427 | self.flash(_('${a} students successfully created.', |
---|
| 428 | mapping = {'a': len(created)})) |
---|
| 429 | else: |
---|
[11254] | 430 | self.flash(_('No student could be created.'), type='warning') |
---|
| 431 | self.redirect(self.url(self.context, u'@@manage')+'#tab2') |
---|
[8314] | 432 | return |
---|
| 433 | |
---|
[7710] | 434 | @action(_('Cancel'), validator=NullValidator) |
---|
[5837] | 435 | def cancel(self, **data): |
---|
| 436 | self.redirect(self.url(self.context)) |
---|
| 437 | return |
---|
[5886] | 438 | |
---|
[7710] | 439 | @action(_('Add local role'), validator=NullValidator) |
---|
[6184] | 440 | def addLocalRole(self, **data): |
---|
| 441 | return add_local_role(self,3, **data) |
---|
[6105] | 442 | |
---|
[7710] | 443 | @action(_('Remove selected local roles')) |
---|
[6184] | 444 | def delLocalRoles(self, **data): |
---|
| 445 | return del_local_roles(self,3,**data) |
---|
| 446 | |
---|
[7819] | 447 | class ApplicantAddFormPage(KofaAddFormPage): |
---|
[6622] | 448 | """Add-form to add an applicant. |
---|
[6327] | 449 | """ |
---|
| 450 | grok.context(IApplicantsContainer) |
---|
[7136] | 451 | grok.require('waeup.manageApplication') |
---|
[6327] | 452 | grok.name('addapplicant') |
---|
[7240] | 453 | #grok.template('applicantaddpage') |
---|
| 454 | form_fields = grok.AutoFields(IApplicant).select( |
---|
[7356] | 455 | 'firstname', 'middlename', 'lastname', |
---|
[7240] | 456 | 'email', 'phone') |
---|
[7714] | 457 | label = _('Add applicant') |
---|
[6327] | 458 | pnav = 3 |
---|
| 459 | |
---|
[7714] | 460 | @action(_('Create application record')) |
---|
[6327] | 461 | def addApplicant(self, **data): |
---|
[8008] | 462 | applicant = createObject(u'waeup.Applicant') |
---|
[7240] | 463 | self.applyData(applicant, **data) |
---|
| 464 | self.context.addApplicant(applicant) |
---|
[13073] | 465 | self.flash(_('Application record created.')) |
---|
[7363] | 466 | self.redirect( |
---|
| 467 | self.url(self.context[applicant.application_number], 'index')) |
---|
[6327] | 468 | return |
---|
| 469 | |
---|
[7819] | 470 | class ApplicantDisplayFormPage(KofaDisplayFormPage): |
---|
[8014] | 471 | """A display view for applicant data. |
---|
| 472 | """ |
---|
[5273] | 473 | grok.context(IApplicant) |
---|
| 474 | grok.name('index') |
---|
[7113] | 475 | grok.require('waeup.viewApplication') |
---|
[7200] | 476 | grok.template('applicantdisplaypage') |
---|
[7714] | 477 | label = _('Applicant') |
---|
[5843] | 478 | pnav = 3 |
---|
[8922] | 479 | hide_hint = False |
---|
[5273] | 480 | |
---|
[8046] | 481 | @property |
---|
[10831] | 482 | def form_fields(self): |
---|
| 483 | if self.context.special: |
---|
[11599] | 484 | form_fields = grok.AutoFields(ISpecialApplicant).omit('locked') |
---|
[10831] | 485 | else: |
---|
| 486 | form_fields = grok.AutoFields(IApplicant).omit( |
---|
[10845] | 487 | 'locked', 'course_admitted', 'password', 'suspended') |
---|
[10831] | 488 | return form_fields |
---|
| 489 | |
---|
| 490 | @property |
---|
[10534] | 491 | def target(self): |
---|
| 492 | return getattr(self.context.__parent__, 'prefix', None) |
---|
| 493 | |
---|
| 494 | @property |
---|
[8046] | 495 | def separators(self): |
---|
| 496 | return getUtility(IApplicantsUtils).SEPARATORS_DICT |
---|
| 497 | |
---|
[7063] | 498 | def update(self): |
---|
| 499 | self.passport_url = self.url(self.context, 'passport.jpg') |
---|
[7240] | 500 | # Mark application as started if applicant logs in for the first time |
---|
[7272] | 501 | usertype = getattr(self.request.principal, 'user_type', None) |
---|
| 502 | if usertype == 'applicant' and \ |
---|
| 503 | IWorkflowState(self.context).getState() == INITIALIZED: |
---|
[7240] | 504 | IWorkflowInfo(self.context).fireTransition('start') |
---|
[10895] | 505 | if usertype == 'applicant' and self.context.state == 'created': |
---|
[10908] | 506 | session = '%s/%s' % (self.context.__parent__.year, |
---|
| 507 | self.context.__parent__.year+1) |
---|
| 508 | title = getattr(grok.getSite()['configuration'], 'name', u'Sample University') |
---|
[10895] | 509 | msg = _( |
---|
| 510 | '\n <strong>Congratulations!</strong>' + |
---|
[10933] | 511 | ' You have been offered provisional admission into the' + |
---|
[10908] | 512 | ' ${c} Academic Session of ${d}.' |
---|
| 513 | ' Your student record has been created for you.' + |
---|
| 514 | ' Please, logout again and proceed to the' + |
---|
| 515 | ' login page of the portal.' |
---|
[10895] | 516 | ' Then enter your new student credentials:' + |
---|
| 517 | ' user name= ${a}, password = ${b}.' + |
---|
| 518 | ' Change your password when you have logged in.', |
---|
| 519 | mapping = { |
---|
| 520 | 'a':self.context.student_id, |
---|
[10908] | 521 | 'b':self.context.application_number, |
---|
| 522 | 'c':session, |
---|
| 523 | 'd':title} |
---|
[10895] | 524 | ) |
---|
| 525 | self.flash(msg) |
---|
[7063] | 526 | return |
---|
| 527 | |
---|
[6196] | 528 | @property |
---|
[7240] | 529 | def hasPassword(self): |
---|
| 530 | if self.context.password: |
---|
[7714] | 531 | return _('set') |
---|
| 532 | return _('unset') |
---|
[7240] | 533 | |
---|
| 534 | @property |
---|
[6196] | 535 | def label(self): |
---|
| 536 | container_title = self.context.__parent__.title |
---|
[8096] | 537 | return _('${a} <br /> Application Record ${b}', mapping = { |
---|
[7714] | 538 | 'a':container_title, 'b':self.context.application_number}) |
---|
[6196] | 539 | |
---|
[7347] | 540 | def getCourseAdmitted(self): |
---|
| 541 | """Return link, title and code in html format to the certificate |
---|
| 542 | admitted. |
---|
| 543 | """ |
---|
| 544 | course_admitted = self.context.course_admitted |
---|
[7351] | 545 | if getattr(course_admitted, '__parent__',None): |
---|
[7347] | 546 | url = self.url(course_admitted) |
---|
| 547 | title = course_admitted.title |
---|
| 548 | code = course_admitted.code |
---|
| 549 | return '<a href="%s">%s - %s</a>' %(url,code,title) |
---|
| 550 | return '' |
---|
[6254] | 551 | |
---|
[7259] | 552 | class ApplicantBaseDisplayFormPage(ApplicantDisplayFormPage): |
---|
| 553 | grok.context(IApplicant) |
---|
| 554 | grok.name('base') |
---|
| 555 | form_fields = grok.AutoFields(IApplicant).select( |
---|
[9141] | 556 | 'applicant_id','email', 'course1') |
---|
[7259] | 557 | |
---|
[7459] | 558 | class CreateStudentPage(UtilityView, grok.View): |
---|
[8636] | 559 | """Create a student object from applicant data. |
---|
[7341] | 560 | """ |
---|
| 561 | grok.context(IApplicant) |
---|
| 562 | grok.name('createstudent') |
---|
| 563 | grok.require('waeup.manageStudent') |
---|
| 564 | |
---|
| 565 | def update(self): |
---|
[8314] | 566 | msg = self.context.createStudent(view=self)[1] |
---|
[11254] | 567 | self.flash(msg, type='warning') |
---|
[7341] | 568 | self.redirect(self.url(self.context)) |
---|
| 569 | return |
---|
| 570 | |
---|
| 571 | def render(self): |
---|
| 572 | return |
---|
| 573 | |
---|
[8636] | 574 | class CreateAllStudentsPage(UtilityView, grok.View): |
---|
| 575 | """Create all student objects from applicant data |
---|
[11874] | 576 | in the root container or in a specific applicants container only. |
---|
[11826] | 577 | Only PortalManagers can do this. |
---|
[8636] | 578 | """ |
---|
[9900] | 579 | #grok.context(IApplicantsContainer) |
---|
[8636] | 580 | grok.name('createallstudents') |
---|
| 581 | grok.require('waeup.managePortal') |
---|
| 582 | |
---|
| 583 | def update(self): |
---|
| 584 | cat = getUtility(ICatalog, name='applicants_catalog') |
---|
| 585 | results = list(cat.searchResults(state=(ADMITTED, ADMITTED))) |
---|
| 586 | created = [] |
---|
[9900] | 587 | container_only = False |
---|
| 588 | applicants_root = grok.getSite()['applicants'] |
---|
| 589 | if isinstance(self.context, ApplicantsContainer): |
---|
| 590 | container_only = True |
---|
[8636] | 591 | for result in results: |
---|
[9900] | 592 | if container_only and result.__parent__ is not self.context: |
---|
[8636] | 593 | continue |
---|
| 594 | success, msg = result.createStudent(view=self) |
---|
| 595 | if success: |
---|
| 596 | created.append(result.applicant_id) |
---|
| 597 | else: |
---|
[12893] | 598 | ob_class = self.__implemented__.__name__.replace( |
---|
| 599 | 'waeup.kofa.','') |
---|
[9900] | 600 | applicants_root.logger.info( |
---|
[8742] | 601 | '%s - %s - %s' % (ob_class, result.applicant_id, msg)) |
---|
[8636] | 602 | if len(created): |
---|
| 603 | self.flash(_('${a} students successfully created.', |
---|
| 604 | mapping = {'a': len(created)})) |
---|
| 605 | else: |
---|
[11254] | 606 | self.flash(_('No student could be created.'), type='warning') |
---|
[9900] | 607 | self.redirect(self.url(self.context)) |
---|
[8636] | 608 | return |
---|
| 609 | |
---|
| 610 | def render(self): |
---|
| 611 | return |
---|
| 612 | |
---|
[8260] | 613 | class ApplicationFeePaymentAddPage(UtilityView, grok.View): |
---|
[7250] | 614 | """ Page to add an online payment ticket |
---|
| 615 | """ |
---|
| 616 | grok.context(IApplicant) |
---|
| 617 | grok.name('addafp') |
---|
| 618 | grok.require('waeup.payApplicant') |
---|
[8243] | 619 | factory = u'waeup.ApplicantOnlinePayment' |
---|
[7250] | 620 | |
---|
[11726] | 621 | @property |
---|
| 622 | def custom_requirements(self): |
---|
| 623 | return '' |
---|
| 624 | |
---|
[7250] | 625 | def update(self): |
---|
[11726] | 626 | # Additional requirements in custom packages. |
---|
| 627 | if self.custom_requirements: |
---|
| 628 | self.flash( |
---|
| 629 | self.custom_requirements, |
---|
| 630 | type='danger') |
---|
| 631 | self.redirect(self.url(self.context)) |
---|
[11727] | 632 | return |
---|
[11575] | 633 | if not self.context.special: |
---|
| 634 | for key in self.context.keys(): |
---|
| 635 | ticket = self.context[key] |
---|
| 636 | if ticket.p_state == 'paid': |
---|
| 637 | self.flash( |
---|
| 638 | _('This type of payment has already been made.'), |
---|
| 639 | type='warning') |
---|
| 640 | self.redirect(self.url(self.context)) |
---|
| 641 | return |
---|
[8524] | 642 | applicants_utils = getUtility(IApplicantsUtils) |
---|
| 643 | container = self.context.__parent__ |
---|
[8243] | 644 | payment = createObject(self.factory) |
---|
[11254] | 645 | failure = applicants_utils.setPaymentDetails( |
---|
[10831] | 646 | container, payment, self.context) |
---|
[11254] | 647 | if failure is not None: |
---|
[13123] | 648 | self.flash(failure, type='danger') |
---|
[8524] | 649 | self.redirect(self.url(self.context)) |
---|
| 650 | return |
---|
[7250] | 651 | self.context[payment.p_id] = payment |
---|
[12893] | 652 | self.context.writeLogMessage(self, 'added: %s' % payment.p_id) |
---|
[7714] | 653 | self.flash(_('Payment ticket created.')) |
---|
[8280] | 654 | self.redirect(self.url(payment)) |
---|
[7250] | 655 | return |
---|
| 656 | |
---|
| 657 | def render(self): |
---|
| 658 | return |
---|
| 659 | |
---|
| 660 | |
---|
[7819] | 661 | class OnlinePaymentDisplayFormPage(KofaDisplayFormPage): |
---|
[7250] | 662 | """ Page to view an online payment ticket |
---|
| 663 | """ |
---|
| 664 | grok.context(IApplicantOnlinePayment) |
---|
| 665 | grok.name('index') |
---|
| 666 | grok.require('waeup.viewApplication') |
---|
[9984] | 667 | form_fields = grok.AutoFields(IApplicantOnlinePayment).omit('p_item') |
---|
[8170] | 668 | form_fields[ |
---|
| 669 | 'creation_date'].custom_widget = FriendlyDatetimeDisplayWidget('le') |
---|
| 670 | form_fields[ |
---|
| 671 | 'payment_date'].custom_widget = FriendlyDatetimeDisplayWidget('le') |
---|
[7250] | 672 | pnav = 3 |
---|
| 673 | |
---|
| 674 | @property |
---|
| 675 | def label(self): |
---|
[7714] | 676 | return _('${a}: Online Payment Ticket ${b}', mapping = { |
---|
[8170] | 677 | 'a':self.context.__parent__.display_fullname, |
---|
| 678 | 'b':self.context.p_id}) |
---|
[7250] | 679 | |
---|
[8420] | 680 | class OnlinePaymentApprovePage(UtilityView, grok.View): |
---|
| 681 | """ Approval view |
---|
[7250] | 682 | """ |
---|
| 683 | grok.context(IApplicantOnlinePayment) |
---|
[8420] | 684 | grok.name('approve') |
---|
| 685 | grok.require('waeup.managePortal') |
---|
[7250] | 686 | |
---|
| 687 | def update(self): |
---|
[11580] | 688 | flashtype, msg, log = self.context.approveApplicantPayment() |
---|
[8428] | 689 | if log is not None: |
---|
[9771] | 690 | applicant = self.context.__parent__ |
---|
| 691 | # Add log message to applicants.log |
---|
| 692 | applicant.writeLogMessage(self, log) |
---|
| 693 | # Add log message to payments.log |
---|
| 694 | self.context.logger.info( |
---|
[9795] | 695 | '%s,%s,%s,%s,%s,,,,,,' % ( |
---|
[9771] | 696 | applicant.applicant_id, |
---|
| 697 | self.context.p_id, self.context.p_category, |
---|
| 698 | self.context.amount_auth, self.context.r_code)) |
---|
[11580] | 699 | self.flash(msg, type=flashtype) |
---|
[7250] | 700 | return |
---|
| 701 | |
---|
| 702 | def render(self): |
---|
| 703 | self.redirect(self.url(self.context, '@@index')) |
---|
| 704 | return |
---|
| 705 | |
---|
[7459] | 706 | class ExportPDFPaymentSlipPage(UtilityView, grok.View): |
---|
[7250] | 707 | """Deliver a PDF slip of the context. |
---|
| 708 | """ |
---|
| 709 | grok.context(IApplicantOnlinePayment) |
---|
[8262] | 710 | grok.name('payment_slip.pdf') |
---|
[7250] | 711 | grok.require('waeup.viewApplication') |
---|
[9984] | 712 | form_fields = grok.AutoFields(IApplicantOnlinePayment).omit('p_item') |
---|
[8173] | 713 | form_fields['creation_date'].custom_widget = FriendlyDatetimeDisplayWidget('le') |
---|
| 714 | form_fields['payment_date'].custom_widget = FriendlyDatetimeDisplayWidget('le') |
---|
[7250] | 715 | prefix = 'form' |
---|
[8258] | 716 | note = None |
---|
[7250] | 717 | |
---|
| 718 | @property |
---|
[7714] | 719 | def title(self): |
---|
[7819] | 720 | portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
[7811] | 721 | return translate(_('Payment Data'), 'waeup.kofa', |
---|
[7714] | 722 | target_language=portal_language) |
---|
| 723 | |
---|
| 724 | @property |
---|
[7250] | 725 | def label(self): |
---|
[7819] | 726 | portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
[8262] | 727 | return translate(_('Online Payment Slip'), |
---|
[7811] | 728 | 'waeup.kofa', target_language=portal_language) \ |
---|
[7714] | 729 | + ' %s' % self.context.p_id |
---|
[7250] | 730 | |
---|
[11754] | 731 | @property |
---|
| 732 | def payment_slip_download_warning(self): |
---|
| 733 | if self.context.__parent__.state != SUBMITTED: |
---|
| 734 | return _('Please submit the application form before ' |
---|
| 735 | 'trying to download payment slips.') |
---|
| 736 | return '' |
---|
| 737 | |
---|
[7250] | 738 | def render(self): |
---|
[11754] | 739 | if self.payment_slip_download_warning: |
---|
| 740 | self.flash(self.payment_slip_download_warning, type='danger') |
---|
[11599] | 741 | self.redirect(self.url(self.context)) |
---|
| 742 | return |
---|
[7259] | 743 | applicantview = ApplicantBaseDisplayFormPage(self.context.__parent__, |
---|
[7250] | 744 | self.request) |
---|
| 745 | students_utils = getUtility(IStudentsUtils) |
---|
[8262] | 746 | return students_utils.renderPDF(self,'payment_slip.pdf', |
---|
[8258] | 747 | self.context.__parent__, applicantview, note=self.note) |
---|
[7250] | 748 | |
---|
[10571] | 749 | class ExportPDFPageApplicationSlip(UtilityView, grok.View): |
---|
[6358] | 750 | """Deliver a PDF slip of the context. |
---|
| 751 | """ |
---|
| 752 | grok.context(IApplicant) |
---|
| 753 | grok.name('application_slip.pdf') |
---|
[7136] | 754 | grok.require('waeup.viewApplication') |
---|
[6358] | 755 | prefix = 'form' |
---|
| 756 | |
---|
[8666] | 757 | def update(self): |
---|
[9051] | 758 | if self.context.state in ('initialized', 'started', 'paid'): |
---|
[8666] | 759 | self.flash( |
---|
[11254] | 760 | _('Please pay and submit before trying to download ' |
---|
| 761 | 'the application slip.'), type='warning') |
---|
[8666] | 762 | return self.redirect(self.url(self.context)) |
---|
| 763 | return |
---|
| 764 | |
---|
[6358] | 765 | def render(self): |
---|
[12395] | 766 | try: |
---|
| 767 | pdfstream = getAdapter(self.context, IPDF, name='application_slip')( |
---|
| 768 | view=self) |
---|
| 769 | except IOError: |
---|
| 770 | self.flash( |
---|
| 771 | _('Your image file is corrupted. ' |
---|
| 772 | 'Please replace.'), type='danger') |
---|
| 773 | return self.redirect(self.url(self.context)) |
---|
[6358] | 774 | self.response.setHeader( |
---|
| 775 | 'Content-Type', 'application/pdf') |
---|
[7392] | 776 | return pdfstream |
---|
[6358] | 777 | |
---|
[7081] | 778 | def handle_img_upload(upload, context, view): |
---|
[7063] | 779 | """Handle upload of applicant image. |
---|
[7081] | 780 | |
---|
| 781 | Returns `True` in case of success or `False`. |
---|
| 782 | |
---|
| 783 | Please note that file pointer passed in (`upload`) most probably |
---|
| 784 | points to end of file when leaving this function. |
---|
[7063] | 785 | """ |
---|
[7081] | 786 | size = file_size(upload) |
---|
| 787 | if size > MAX_UPLOAD_SIZE: |
---|
[11254] | 788 | view.flash(_('Uploaded image is too big!'), type='danger') |
---|
[7081] | 789 | return False |
---|
[7247] | 790 | dummy, ext = os.path.splitext(upload.filename) |
---|
| 791 | ext.lower() |
---|
| 792 | if ext != '.jpg': |
---|
[11254] | 793 | view.flash(_('jpg file extension expected.'), type='danger') |
---|
[7247] | 794 | return False |
---|
[7081] | 795 | upload.seek(0) # file pointer moved when determining size |
---|
[7063] | 796 | store = getUtility(IExtFileStore) |
---|
| 797 | file_id = IFileStoreNameChooser(context).chooseName() |
---|
| 798 | store.createFile(file_id, upload) |
---|
[7081] | 799 | return True |
---|
[7063] | 800 | |
---|
[7819] | 801 | class ApplicantManageFormPage(KofaEditFormPage): |
---|
[6196] | 802 | """A full edit view for applicant data. |
---|
| 803 | """ |
---|
| 804 | grok.context(IApplicant) |
---|
[7200] | 805 | grok.name('manage') |
---|
[7136] | 806 | grok.require('waeup.manageApplication') |
---|
[7200] | 807 | grok.template('applicanteditpage') |
---|
[6322] | 808 | manage_applications = True |
---|
[6196] | 809 | pnav = 3 |
---|
[12664] | 810 | display_actions = [[_('Save'), _('Finally Submit')], |
---|
[7714] | 811 | [_('Add online payment ticket'),_('Remove selected tickets')]] |
---|
[6196] | 812 | |
---|
[8046] | 813 | @property |
---|
[10831] | 814 | def form_fields(self): |
---|
| 815 | if self.context.special: |
---|
[10845] | 816 | form_fields = grok.AutoFields(ISpecialApplicant) |
---|
| 817 | form_fields['applicant_id'].for_display = True |
---|
[10831] | 818 | else: |
---|
| 819 | form_fields = grok.AutoFields(IApplicant) |
---|
| 820 | form_fields['student_id'].for_display = True |
---|
| 821 | form_fields['applicant_id'].for_display = True |
---|
| 822 | return form_fields |
---|
| 823 | |
---|
| 824 | @property |
---|
[10534] | 825 | def target(self): |
---|
| 826 | return getattr(self.context.__parent__, 'prefix', None) |
---|
| 827 | |
---|
| 828 | @property |
---|
[8046] | 829 | def separators(self): |
---|
| 830 | return getUtility(IApplicantsUtils).SEPARATORS_DICT |
---|
| 831 | |
---|
[11733] | 832 | @property |
---|
| 833 | def custom_upload_requirements(self): |
---|
| 834 | return '' |
---|
| 835 | |
---|
[6196] | 836 | def update(self): |
---|
[7200] | 837 | super(ApplicantManageFormPage, self).update() |
---|
[6353] | 838 | self.wf_info = IWorkflowInfo(self.context) |
---|
[7081] | 839 | self.max_upload_size = string_from_bytes(MAX_UPLOAD_SIZE) |
---|
[10090] | 840 | self.upload_success = None |
---|
[6598] | 841 | upload = self.request.form.get('form.passport', None) |
---|
| 842 | if upload: |
---|
[11733] | 843 | if self.custom_upload_requirements: |
---|
| 844 | self.flash( |
---|
| 845 | self.custom_upload_requirements, |
---|
| 846 | type='danger') |
---|
| 847 | self.redirect(self.url(self.context)) |
---|
| 848 | return |
---|
[10090] | 849 | # We got a fresh upload, upload_success is |
---|
| 850 | # either True or False |
---|
| 851 | self.upload_success = handle_img_upload( |
---|
[7084] | 852 | upload, self.context, self) |
---|
[10090] | 853 | if self.upload_success: |
---|
[10095] | 854 | self.context.writeLogMessage(self, 'saved: passport') |
---|
[6196] | 855 | return |
---|
| 856 | |
---|
| 857 | @property |
---|
| 858 | def label(self): |
---|
| 859 | container_title = self.context.__parent__.title |
---|
[8096] | 860 | return _('${a} <br /> Application Form ${b}', mapping = { |
---|
[7714] | 861 | 'a':container_title, 'b':self.context.application_number}) |
---|
[6196] | 862 | |
---|
[6303] | 863 | def getTransitions(self): |
---|
[6351] | 864 | """Return a list of dicts of allowed transition ids and titles. |
---|
[6353] | 865 | |
---|
| 866 | Each list entry provides keys ``name`` and ``title`` for |
---|
| 867 | internal name and (human readable) title of a single |
---|
| 868 | transition. |
---|
[6349] | 869 | """ |
---|
[8434] | 870 | allowed_transitions = [t for t in self.wf_info.getManualTransitions() |
---|
[11482] | 871 | if not t[0] in ('pay', 'create')] |
---|
[7687] | 872 | return [dict(name='', title=_('No transition'))] +[ |
---|
[6355] | 873 | dict(name=x, title=y) for x, y in allowed_transitions] |
---|
[6303] | 874 | |
---|
[7714] | 875 | @action(_('Save'), style='primary') |
---|
[6196] | 876 | def save(self, **data): |
---|
[7240] | 877 | form = self.request.form |
---|
| 878 | password = form.get('password', None) |
---|
| 879 | password_ctl = form.get('control_password', None) |
---|
| 880 | if password: |
---|
| 881 | validator = getUtility(IPasswordValidator) |
---|
| 882 | errors = validator.validate_password(password, password_ctl) |
---|
| 883 | if errors: |
---|
[11254] | 884 | self.flash( ' '.join(errors), type='danger') |
---|
[7240] | 885 | return |
---|
[10090] | 886 | if self.upload_success is False: # False is not None! |
---|
| 887 | # Error during image upload. Ignore other values. |
---|
| 888 | return |
---|
[6475] | 889 | changed_fields = self.applyData(self.context, **data) |
---|
[7199] | 890 | # Turn list of lists into single list |
---|
| 891 | if changed_fields: |
---|
| 892 | changed_fields = reduce(lambda x,y: x+y, changed_fields.values()) |
---|
[7240] | 893 | else: |
---|
| 894 | changed_fields = [] |
---|
| 895 | if password: |
---|
| 896 | # Now we know that the form has no errors and can set password ... |
---|
| 897 | IUserAccount(self.context).setPassword(password) |
---|
| 898 | changed_fields.append('password') |
---|
[7199] | 899 | fields_string = ' + '.join(changed_fields) |
---|
[7085] | 900 | trans_id = form.get('transition', None) |
---|
| 901 | if trans_id: |
---|
| 902 | self.wf_info.fireTransition(trans_id) |
---|
[7714] | 903 | self.flash(_('Form has been saved.')) |
---|
[6644] | 904 | if fields_string: |
---|
[12892] | 905 | self.context.writeLogMessage(self, 'saved: %s' % fields_string) |
---|
[6196] | 906 | return |
---|
| 907 | |
---|
[7250] | 908 | def unremovable(self, ticket): |
---|
[7330] | 909 | return False |
---|
[7250] | 910 | |
---|
| 911 | # This method is also used by the ApplicantEditFormPage |
---|
| 912 | def delPaymentTickets(self, **data): |
---|
| 913 | form = self.request.form |
---|
[9701] | 914 | if 'val_id' in form: |
---|
[7250] | 915 | child_id = form['val_id'] |
---|
| 916 | else: |
---|
[11254] | 917 | self.flash(_('No payment selected.'), type='warning') |
---|
[7250] | 918 | self.redirect(self.url(self.context)) |
---|
| 919 | return |
---|
| 920 | if not isinstance(child_id, list): |
---|
| 921 | child_id = [child_id] |
---|
| 922 | deleted = [] |
---|
| 923 | for id in child_id: |
---|
| 924 | # Applicants are not allowed to remove used payment tickets |
---|
| 925 | if not self.unremovable(self.context[id]): |
---|
| 926 | try: |
---|
| 927 | del self.context[id] |
---|
| 928 | deleted.append(id) |
---|
| 929 | except: |
---|
[7714] | 930 | self.flash(_('Could not delete:') + ' %s: %s: %s' % ( |
---|
[11254] | 931 | id, sys.exc_info()[0], sys.exc_info()[1]), type='danger') |
---|
[7250] | 932 | if len(deleted): |
---|
[7741] | 933 | self.flash(_('Successfully removed: ${a}', |
---|
[7738] | 934 | mapping = {'a':', '.join(deleted)})) |
---|
[8742] | 935 | self.context.writeLogMessage( |
---|
| 936 | self, 'removed: % s' % ', '.join(deleted)) |
---|
[7250] | 937 | return |
---|
| 938 | |
---|
[7252] | 939 | # We explicitely want the forms to be validated before payment tickets |
---|
| 940 | # can be created. If no validation is requested, use |
---|
[7459] | 941 | # 'validator=NullValidator' in the action directive |
---|
[11578] | 942 | @action(_('Add online payment ticket'), style='primary') |
---|
[7250] | 943 | def addPaymentTicket(self, **data): |
---|
| 944 | self.redirect(self.url(self.context, '@@addafp')) |
---|
[7252] | 945 | return |
---|
[7250] | 946 | |
---|
[7714] | 947 | @jsaction(_('Remove selected tickets')) |
---|
[7250] | 948 | def removePaymentTickets(self, **data): |
---|
| 949 | self.delPaymentTickets(**data) |
---|
| 950 | self.redirect(self.url(self.context) + '/@@manage') |
---|
| 951 | return |
---|
| 952 | |
---|
[10094] | 953 | # Not used in base package |
---|
| 954 | def file_exists(self, attr): |
---|
| 955 | file = getUtility(IExtFileStore).getFileByContext( |
---|
| 956 | self.context, attr=attr) |
---|
| 957 | if file: |
---|
| 958 | return True |
---|
| 959 | else: |
---|
| 960 | return False |
---|
| 961 | |
---|
[7200] | 962 | class ApplicantEditFormPage(ApplicantManageFormPage): |
---|
[5982] | 963 | """An applicant-centered edit view for applicant data. |
---|
| 964 | """ |
---|
[6196] | 965 | grok.context(IApplicantEdit) |
---|
[5273] | 966 | grok.name('edit') |
---|
[6198] | 967 | grok.require('waeup.handleApplication') |
---|
[7200] | 968 | grok.template('applicanteditpage') |
---|
[6322] | 969 | manage_applications = False |
---|
[10358] | 970 | submit_state = PAID |
---|
[5484] | 971 | |
---|
[7250] | 972 | @property |
---|
[10831] | 973 | def form_fields(self): |
---|
| 974 | if self.context.special: |
---|
[11657] | 975 | form_fields = grok.AutoFields(ISpecialApplicant).omit( |
---|
| 976 | 'locked', 'suspended') |
---|
[10845] | 977 | form_fields['applicant_id'].for_display = True |
---|
[10831] | 978 | else: |
---|
| 979 | form_fields = grok.AutoFields(IApplicantEdit).omit( |
---|
| 980 | 'locked', 'course_admitted', 'student_id', |
---|
[10845] | 981 | 'suspended' |
---|
[10831] | 982 | ) |
---|
| 983 | form_fields['applicant_id'].for_display = True |
---|
| 984 | form_fields['reg_number'].for_display = True |
---|
| 985 | return form_fields |
---|
| 986 | |
---|
| 987 | @property |
---|
[7250] | 988 | def display_actions(self): |
---|
[8286] | 989 | state = IWorkflowState(self.context).getState() |
---|
[13100] | 990 | # If the form is unlocked, applicants are allowed to save the form |
---|
| 991 | # and remove unused tickets. |
---|
| 992 | actions = [[_('Save')], [_('Remove selected tickets')]] |
---|
| 993 | # Only in state started they can also add tickets. |
---|
[10358] | 994 | if state == STARTED: |
---|
[7714] | 995 | actions = [[_('Save')], |
---|
| 996 | [_('Add online payment ticket'),_('Remove selected tickets')]] |
---|
[13100] | 997 | # In state paid, they can submit the data and further add tickets |
---|
| 998 | # if the application is special. |
---|
[11599] | 999 | elif self.context.special and state == PAID: |
---|
[12664] | 1000 | actions = [[_('Save'), _('Finally Submit')], |
---|
[11599] | 1001 | [_('Add online payment ticket'),_('Remove selected tickets')]] |
---|
[8286] | 1002 | elif state == PAID: |
---|
[12664] | 1003 | actions = [[_('Save'), _('Finally Submit')], |
---|
[7714] | 1004 | [_('Remove selected tickets')]] |
---|
[7250] | 1005 | return actions |
---|
| 1006 | |
---|
[7330] | 1007 | def unremovable(self, ticket): |
---|
[13100] | 1008 | return ticket.r_code |
---|
[7330] | 1009 | |
---|
[7145] | 1010 | def emit_lock_message(self): |
---|
[11254] | 1011 | self.flash(_('The requested form is locked (read-only).'), |
---|
| 1012 | type='warning') |
---|
[5941] | 1013 | self.redirect(self.url(self.context)) |
---|
| 1014 | return |
---|
[6078] | 1015 | |
---|
[5686] | 1016 | def update(self): |
---|
[8665] | 1017 | if self.context.locked or ( |
---|
| 1018 | self.context.__parent__.expired and |
---|
| 1019 | self.context.__parent__.strict_deadline): |
---|
[7145] | 1020 | self.emit_lock_message() |
---|
[5941] | 1021 | return |
---|
[7200] | 1022 | super(ApplicantEditFormPage, self).update() |
---|
[5686] | 1023 | return |
---|
[5952] | 1024 | |
---|
[6196] | 1025 | def dataNotComplete(self): |
---|
[7252] | 1026 | store = getUtility(IExtFileStore) |
---|
| 1027 | if not store.getFileByContext(self.context, attr=u'passport.jpg'): |
---|
[7714] | 1028 | return _('No passport picture uploaded.') |
---|
[6322] | 1029 | if not self.request.form.get('confirm_passport', False): |
---|
[7714] | 1030 | return _('Passport picture confirmation box not ticked.') |
---|
[6196] | 1031 | return False |
---|
[5952] | 1032 | |
---|
[7252] | 1033 | # We explicitely want the forms to be validated before payment tickets |
---|
| 1034 | # can be created. If no validation is requested, use |
---|
[7459] | 1035 | # 'validator=NullValidator' in the action directive |
---|
[11578] | 1036 | @action(_('Add online payment ticket'), style='primary') |
---|
[7250] | 1037 | def addPaymentTicket(self, **data): |
---|
| 1038 | self.redirect(self.url(self.context, '@@addafp')) |
---|
[7252] | 1039 | return |
---|
[7250] | 1040 | |
---|
[7714] | 1041 | @jsaction(_('Remove selected tickets')) |
---|
[7250] | 1042 | def removePaymentTickets(self, **data): |
---|
| 1043 | self.delPaymentTickets(**data) |
---|
| 1044 | self.redirect(self.url(self.context) + '/@@edit') |
---|
| 1045 | return |
---|
| 1046 | |
---|
[7996] | 1047 | @action(_('Save'), style='primary') |
---|
[5273] | 1048 | def save(self, **data): |
---|
[10090] | 1049 | if self.upload_success is False: # False is not None! |
---|
| 1050 | # Error during image upload. Ignore other values. |
---|
| 1051 | return |
---|
[10219] | 1052 | if data.get('course1', 1) == data.get('course2', 2): |
---|
[11254] | 1053 | self.flash(_('1st and 2nd choice must be different.'), |
---|
| 1054 | type='warning') |
---|
[10210] | 1055 | return |
---|
[5273] | 1056 | self.applyData(self.context, **data) |
---|
[10210] | 1057 | self.flash(_('Form has been saved.')) |
---|
[5273] | 1058 | return |
---|
| 1059 | |
---|
[12664] | 1060 | @action(_('Finally Submit'), warning=WARNING) |
---|
[5484] | 1061 | def finalsubmit(self, **data): |
---|
[10090] | 1062 | if self.upload_success is False: # False is not None! |
---|
[7084] | 1063 | return # error during image upload. Ignore other values |
---|
[6196] | 1064 | if self.dataNotComplete(): |
---|
[11254] | 1065 | self.flash(self.dataNotComplete(), type='danger') |
---|
[5941] | 1066 | return |
---|
[7252] | 1067 | self.applyData(self.context, **data) |
---|
[8286] | 1068 | state = IWorkflowState(self.context).getState() |
---|
[6322] | 1069 | # This shouldn't happen, but the application officer |
---|
| 1070 | # might have forgotten to lock the form after changing the state |
---|
[10358] | 1071 | if state != self.submit_state: |
---|
[11254] | 1072 | self.flash(_('The form cannot be submitted. Wrong state!'), |
---|
| 1073 | type='danger') |
---|
[6303] | 1074 | return |
---|
| 1075 | IWorkflowInfo(self.context).fireTransition('submit') |
---|
[8589] | 1076 | # application_date is used in export files for sorting. |
---|
| 1077 | # We can thus store utc. |
---|
[8194] | 1078 | self.context.application_date = datetime.utcnow() |
---|
[7714] | 1079 | self.flash(_('Form has been submitted.')) |
---|
[6196] | 1080 | self.redirect(self.url(self.context)) |
---|
[5273] | 1081 | return |
---|
[5941] | 1082 | |
---|
[7063] | 1083 | class PassportImage(grok.View): |
---|
| 1084 | """Renders the passport image for applicants. |
---|
| 1085 | """ |
---|
| 1086 | grok.name('passport.jpg') |
---|
| 1087 | grok.context(IApplicant) |
---|
[7113] | 1088 | grok.require('waeup.viewApplication') |
---|
[7063] | 1089 | |
---|
| 1090 | def render(self): |
---|
| 1091 | # A filename chooser turns a context into a filename suitable |
---|
| 1092 | # for file storage. |
---|
| 1093 | image = getUtility(IExtFileStore).getFileByContext(self.context) |
---|
| 1094 | self.response.setHeader( |
---|
| 1095 | 'Content-Type', 'image/jpeg') |
---|
| 1096 | if image is None: |
---|
| 1097 | # show placeholder image |
---|
[7089] | 1098 | return open(DEFAULT_PASSPORT_IMAGE_PATH, 'rb').read() |
---|
[7063] | 1099 | return image |
---|
[7363] | 1100 | |
---|
[7819] | 1101 | class ApplicantRegistrationPage(KofaAddFormPage): |
---|
[7363] | 1102 | """Captcha'd registration page for applicants. |
---|
| 1103 | """ |
---|
| 1104 | grok.context(IApplicantsContainer) |
---|
| 1105 | grok.name('register') |
---|
[7373] | 1106 | grok.require('waeup.Anonymous') |
---|
[7363] | 1107 | grok.template('applicantregister') |
---|
| 1108 | |
---|
[7368] | 1109 | @property |
---|
[8033] | 1110 | def form_fields(self): |
---|
| 1111 | form_fields = None |
---|
[8128] | 1112 | if self.context.mode == 'update': |
---|
| 1113 | form_fields = grok.AutoFields(IApplicantRegisterUpdate).select( |
---|
[11738] | 1114 | 'lastname','reg_number','email') |
---|
[8128] | 1115 | else: #if self.context.mode == 'create': |
---|
[8033] | 1116 | form_fields = grok.AutoFields(IApplicantEdit).select( |
---|
| 1117 | 'firstname', 'middlename', 'lastname', 'email', 'phone') |
---|
| 1118 | return form_fields |
---|
| 1119 | |
---|
| 1120 | @property |
---|
[7368] | 1121 | def label(self): |
---|
[8078] | 1122 | return _('Apply for ${a}', |
---|
[7714] | 1123 | mapping = {'a':self.context.title}) |
---|
[7368] | 1124 | |
---|
[7363] | 1125 | def update(self): |
---|
[8665] | 1126 | if self.context.expired: |
---|
[11254] | 1127 | self.flash(_('Outside application period.'), type='warning') |
---|
[7368] | 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 | |
---|
[8629] | 1136 | def _redirect(self, email, password, applicant_id): |
---|
| 1137 | # Forward only email to landing page in base package. |
---|
| 1138 | self.redirect(self.url(self.context, 'registration_complete', |
---|
| 1139 | data = dict(email=email))) |
---|
| 1140 | return |
---|
| 1141 | |
---|
[9178] | 1142 | @action(_('Send login credentials to email address'), style='primary') |
---|
[7363] | 1143 | def register(self, **data): |
---|
| 1144 | if not self.captcha_result.is_valid: |
---|
[8037] | 1145 | # Captcha will display error messages automatically. |
---|
[7363] | 1146 | # No need to flash something. |
---|
| 1147 | return |
---|
[8033] | 1148 | if self.context.mode == 'create': |
---|
| 1149 | # Add applicant |
---|
| 1150 | applicant = createObject(u'waeup.Applicant') |
---|
| 1151 | self.applyData(applicant, **data) |
---|
| 1152 | self.context.addApplicant(applicant) |
---|
[8042] | 1153 | applicant.reg_number = applicant.applicant_id |
---|
| 1154 | notify(grok.ObjectModifiedEvent(applicant)) |
---|
[8033] | 1155 | elif self.context.mode == 'update': |
---|
| 1156 | # Update applicant |
---|
[8037] | 1157 | reg_number = data.get('reg_number','') |
---|
[11738] | 1158 | lastname = data.get('lastname','') |
---|
[8033] | 1159 | cat = getUtility(ICatalog, name='applicants_catalog') |
---|
| 1160 | results = list( |
---|
| 1161 | cat.searchResults(reg_number=(reg_number, reg_number))) |
---|
| 1162 | if results: |
---|
| 1163 | applicant = results[0] |
---|
[11738] | 1164 | if getattr(applicant,'lastname',None) is None: |
---|
[11254] | 1165 | self.flash(_('An error occurred.'), type='danger') |
---|
[8037] | 1166 | return |
---|
[11738] | 1167 | elif applicant.lastname.lower() != lastname.lower(): |
---|
[8042] | 1168 | # Don't tell the truth here. Anonymous must not |
---|
[11738] | 1169 | # know that a record was found and only the lastname |
---|
[8042] | 1170 | # verification failed. |
---|
[13099] | 1171 | self.flash( |
---|
| 1172 | _('No application record found.'), type='warning') |
---|
[8037] | 1173 | return |
---|
[8627] | 1174 | elif applicant.password is not None and \ |
---|
| 1175 | applicant.state != INITIALIZED: |
---|
| 1176 | self.flash(_('Your password has already been set and used. ' |
---|
[11254] | 1177 | 'Please proceed to the login page.'), |
---|
| 1178 | type='warning') |
---|
[8042] | 1179 | return |
---|
| 1180 | # Store email address but nothing else. |
---|
[8033] | 1181 | applicant.email = data['email'] |
---|
[8042] | 1182 | notify(grok.ObjectModifiedEvent(applicant)) |
---|
[8033] | 1183 | else: |
---|
[8042] | 1184 | # No record found, this is the truth. |
---|
[11254] | 1185 | self.flash(_('No application record found.'), type='warning') |
---|
[8033] | 1186 | return |
---|
| 1187 | else: |
---|
[8042] | 1188 | # Does not happen but anyway ... |
---|
[8033] | 1189 | return |
---|
[7819] | 1190 | kofa_utils = getUtility(IKofaUtils) |
---|
[7811] | 1191 | password = kofa_utils.genPassword() |
---|
[7380] | 1192 | IUserAccount(applicant).setPassword(password) |
---|
[7365] | 1193 | # Send email with credentials |
---|
[7399] | 1194 | login_url = self.url(grok.getSite(), 'login') |
---|
[8853] | 1195 | url_info = u'Login: %s' % login_url |
---|
[7714] | 1196 | msg = _('You have successfully been registered for the') |
---|
[7811] | 1197 | if kofa_utils.sendCredentials(IUserAccount(applicant), |
---|
[8853] | 1198 | password, url_info, msg): |
---|
[8629] | 1199 | email_sent = applicant.email |
---|
[7380] | 1200 | else: |
---|
[8629] | 1201 | email_sent = None |
---|
| 1202 | self._redirect(email=email_sent, password=password, |
---|
| 1203 | applicant_id=applicant.applicant_id) |
---|
[7380] | 1204 | return |
---|
| 1205 | |
---|
[7819] | 1206 | class ApplicantRegistrationEmailSent(KofaPage): |
---|
[7380] | 1207 | """Landing page after successful registration. |
---|
[8629] | 1208 | |
---|
[7380] | 1209 | """ |
---|
| 1210 | grok.name('registration_complete') |
---|
| 1211 | grok.require('waeup.Public') |
---|
| 1212 | grok.template('applicantregemailsent') |
---|
[7714] | 1213 | label = _('Your registration was successful.') |
---|
[7380] | 1214 | |
---|
[8629] | 1215 | def update(self, email=None, applicant_id=None, password=None): |
---|
[7380] | 1216 | self.email = email |
---|
[8629] | 1217 | self.password = password |
---|
| 1218 | self.applicant_id = applicant_id |
---|
[7380] | 1219 | return |
---|
[10655] | 1220 | |
---|
| 1221 | class ExportJobContainerOverview(KofaPage): |
---|
| 1222 | """Page that lists active applicant data export jobs and provides links |
---|
| 1223 | to discard or download CSV files. |
---|
| 1224 | |
---|
| 1225 | """ |
---|
| 1226 | grok.context(VirtualApplicantsExportJobContainer) |
---|
| 1227 | grok.require('waeup.manageApplication') |
---|
| 1228 | grok.name('index.html') |
---|
| 1229 | grok.template('exportjobsindex') |
---|
[11254] | 1230 | label = _('Data Exports') |
---|
[10655] | 1231 | pnav = 3 |
---|
| 1232 | |
---|
| 1233 | def update(self, CREATE=None, DISCARD=None, job_id=None): |
---|
| 1234 | if CREATE: |
---|
| 1235 | self.redirect(self.url('@@start_export')) |
---|
| 1236 | return |
---|
| 1237 | if DISCARD and job_id: |
---|
| 1238 | entry = self.context.entry_from_job_id(job_id) |
---|
| 1239 | self.context.delete_export_entry(entry) |
---|
| 1240 | ob_class = self.__implemented__.__name__.replace('waeup.kofa.','') |
---|
| 1241 | self.context.logger.info( |
---|
| 1242 | '%s - discarded: job_id=%s' % (ob_class, job_id)) |
---|
| 1243 | self.flash(_('Discarded export') + ' %s' % job_id) |
---|
| 1244 | self.entries = doll_up(self, user=self.request.principal.id) |
---|
| 1245 | return |
---|
| 1246 | |
---|
| 1247 | class ExportJobContainerJobStart(KofaPage): |
---|
| 1248 | """Page that starts an applicants export job. |
---|
| 1249 | |
---|
| 1250 | """ |
---|
| 1251 | grok.context(VirtualApplicantsExportJobContainer) |
---|
| 1252 | grok.require('waeup.manageApplication') |
---|
| 1253 | grok.name('start_export') |
---|
| 1254 | |
---|
| 1255 | def update(self): |
---|
| 1256 | exporter = 'applicants' |
---|
| 1257 | container_code = self.context.__parent__.code |
---|
| 1258 | job_id = self.context.start_export_job(exporter, |
---|
| 1259 | self.request.principal.id, |
---|
| 1260 | container=container_code) |
---|
| 1261 | |
---|
| 1262 | ob_class = self.__implemented__.__name__.replace('waeup.kofa.','') |
---|
| 1263 | self.context.logger.info( |
---|
| 1264 | '%s - exported: %s (%s), job_id=%s' |
---|
| 1265 | % (ob_class, exporter, container_code, job_id)) |
---|
| 1266 | self.flash(_('Export started.')) |
---|
| 1267 | self.redirect(self.url(self.context)) |
---|
| 1268 | return |
---|
| 1269 | |
---|
| 1270 | def render(self): |
---|
| 1271 | return |
---|
| 1272 | |
---|
| 1273 | class ExportJobContainerDownload(ExportCSVView): |
---|
| 1274 | """Page that downloads a students export csv file. |
---|
| 1275 | |
---|
| 1276 | """ |
---|
| 1277 | grok.context(VirtualApplicantsExportJobContainer) |
---|
[11253] | 1278 | grok.require('waeup.manageApplication') |
---|