source: main/waeup.kofa/trunk/src/waeup/kofa/applicants/browser.py @ 15871

Last change on this file since 15871 was 15833, checked in by Henrik Bettermann, 5 years ago

Make max passport picture size customizable.

  • Property svn:keywords set to Id
File size: 62.4 KB
RevLine 
[5273]1## $Id: browser.py 15833 2019-11-21 06:43:08Z 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]20import os
[6082]21import sys
[5273]22import grok
[13950]23import transaction
[15584]24from cgi import escape
[14014]25from urllib import urlencode
[7370]26from datetime import datetime, date
[14934]27from time import time, sleep
[8042]28from zope.event import notify
[13152]29from zope.component import getUtility, queryUtility, createObject, getAdapter
[8033]30from zope.catalog.interfaces import ICatalog
[7714]31from zope.i18n import translate
[14949]32from zope.security import checkPermission
[7322]33from hurry.workflow.interfaces import (
34    IWorkflowInfo, IWorkflowState, InvalidTransitionError)
[14256]35from reportlab.platypus.doctemplate import LayoutError
[14014]36from waeup.kofa.mandates.mandate import RefereeReportMandate
[7811]37from waeup.kofa.applicants.interfaces import (
[7363]38    IApplicant, IApplicantEdit, IApplicantsRoot,
[7683]39    IApplicantsContainer, IApplicantsContainerAdd,
[15833]40    IApplicantOnlinePayment, IApplicantsUtils,
[13976]41    IApplicantRegisterUpdate, ISpecialApplicant,
42    IApplicantRefereeReport
[7363]43    )
[12247]44from waeup.kofa.utils.helpers import html2dict
[10655]45from waeup.kofa.applicants.container import (
46    ApplicantsContainer, VirtualApplicantsExportJobContainer)
[8404]47from waeup.kofa.applicants.applicant import search
[8636]48from waeup.kofa.applicants.workflow import (
[13254]49    INITIALIZED, STARTED, PAID, SUBMITTED, ADMITTED, NOT_ADMITTED, CREATED)
[7811]50from waeup.kofa.browser import (
[9217]51#    KofaPage, KofaEditFormPage, KofaAddFormPage, KofaDisplayFormPage,
[7363]52    DEFAULT_PASSPORT_IMAGE_PATH)
[9217]53from waeup.kofa.browser.layout import (
54    KofaPage, KofaEditFormPage, KofaAddFormPage, KofaDisplayFormPage)
[7811]55from waeup.kofa.browser.interfaces import ICaptchaManager
56from waeup.kofa.browser.breadcrumbs import Breadcrumb
57from waeup.kofa.browser.layout import (
[11437]58    NullValidator, jsaction, action, UtilityView)
[10655]59from waeup.kofa.browser.pages import (
60    add_local_role, del_local_roles, doll_up, ExportCSVView)
[7811]61from waeup.kofa.interfaces import (
[13177]62    IKofaObject, ILocalRolesAssignable, IExtFileStore, IPDF, DOCLINK,
[7819]63    IFileStoreNameChooser, IPasswordValidator, IUserAccount, IKofaUtils)
[7811]64from waeup.kofa.interfaces import MessageFactory as _
65from waeup.kofa.permissions import get_users_with_local_roles
66from waeup.kofa.students.interfaces import IStudentsUtils
[8186]67from waeup.kofa.utils.helpers import string_from_bytes, file_size, now
[8170]68from waeup.kofa.widgets.datewidget import (
[10831]69    FriendlyDateDisplayWidget,
[8170]70    FriendlyDatetimeDisplayWidget)
[5320]71
[7819]72grok.context(IKofaObject) # Make IKofaObject the default context
[5273]73
[14025]74WARNING = _('You can not edit your application records after final submission.'
75            ' You really want to submit?')
76
[8388]77class ApplicantsRootPage(KofaDisplayFormPage):
[5822]78    grok.context(IApplicantsRoot)
79    grok.name('index')
[6153]80    grok.require('waeup.Public')
[8388]81    form_fields = grok.AutoFields(IApplicantsRoot)
[13076]82    label = _('Applicants Section')
[5843]83    pnav = 3
[6012]84
85    def update(self):
[6067]86        super(ApplicantsRootPage, self).update()
[6012]87        return
88
[8388]89    @property
90    def introduction(self):
91        # Here we know that the cookie has been set
92        lang = self.request.cookies.get('kofa.language')
93        html = self.context.description_dict.get(lang,'')
94        if html == '':
95            portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
96            html = self.context.description_dict.get(portal_language,'')
97        return html
98
[10097]99    @property
100    def containers(self):
[10098]101        if self.layout.isAuthenticated():
[13249]102            return self.context.values()
[13217]103        values = sorted([container for container in self.context.values()
[13249]104                         if not container.hidden and container.enddate],
[13222]105                        key=lambda value: value.enddate, reverse=True)
[13217]106        return values
[10097]107
[8404]108class ApplicantsSearchPage(KofaPage):
109    grok.context(IApplicantsRoot)
110    grok.name('search')
111    grok.require('waeup.viewApplication')
[10644]112    label = _('Find applicants')
[10645]113    search_button = _('Find applicant')
[8404]114    pnav = 3
115
116    def update(self, *args, **kw):
117        form = self.request.form
118        self.results = []
119        if 'searchterm' in form and form['searchterm']:
120            self.searchterm = form['searchterm']
121            self.searchtype = form['searchtype']
122        elif 'old_searchterm' in form:
123            self.searchterm = form['old_searchterm']
124            self.searchtype = form['old_searchtype']
125        else:
126            if 'search' in form:
[11254]127                self.flash(_('Empty search string'), type='warning')
[8404]128            return
129        self.results = search(query=self.searchterm,
130            searchtype=self.searchtype, view=self)
131        if not self.results:
[11254]132            self.flash(_('No applicant found.'), type='warning')
[8404]133        return
134
[7819]135class ApplicantsRootManageFormPage(KofaEditFormPage):
[5828]136    grok.context(IApplicantsRoot)
137    grok.name('manage')
[6107]138    grok.template('applicantsrootmanagepage')
[8388]139    form_fields = grok.AutoFields(IApplicantsRoot)
[13076]140    label = _('Manage applicants section')
[5843]141    pnav = 3
[7136]142    grok.require('waeup.manageApplication')
[8388]143    taboneactions = [_('Save')]
144    tabtwoactions = [_('Add applicants container'), _('Remove selected')]
145    tabthreeactions1 = [_('Remove selected local roles')]
146    tabthreeactions2 = [_('Add local role')]
[7710]147    subunits = _('Applicants Containers')
[13177]148    doclink = DOCLINK + '/applicants.html'
[6078]149
[6184]150    def getLocalRoles(self):
151        roles = ILocalRolesAssignable(self.context)
152        return roles()
153
154    def getUsers(self):
155        """Get a list of all users.
156        """
157        for key, val in grok.getSite()['users'].items():
158            url = self.url(val)
159            yield(dict(url=url, name=key, val=val))
160
161    def getUsersWithLocalRoles(self):
162        return get_users_with_local_roles(self.context)
163
[7710]164    @jsaction(_('Remove selected'))
[6069]165    def delApplicantsContainers(self, **data):
166        form = self.request.form
[9701]167        if 'val_id' in form:
[8388]168            child_id = form['val_id']
169        else:
[11254]170            self.flash(_('No container selected!'), type='warning')
171            self.redirect(self.url(self.context, '@@manage')+'#tab2')
[8388]172            return
[6069]173        if not isinstance(child_id, list):
174            child_id = [child_id]
175        deleted = []
176        for id in child_id:
177            try:
178                del self.context[id]
179                deleted.append(id)
180            except:
[7710]181                self.flash(_('Could not delete:') + ' %s: %s: %s' % (
[11254]182                    id, sys.exc_info()[0], sys.exc_info()[1]), type='danger')
[6069]183        if len(deleted):
[7738]184            self.flash(_('Successfully removed: ${a}',
185                mapping = {'a':', '.join(deleted)}))
[12892]186        ob_class = self.__implemented__.__name__.replace('waeup.kofa.','')
187        self.context.logger.info(
188            '%s - removed: %s' % (ob_class, ', '.join(deleted)))
[11254]189        self.redirect(self.url(self.context, '@@manage')+'#tab2')
[6078]190        return
[5828]191
[7710]192    @action(_('Add applicants container'), validator=NullValidator)
[6069]193    def addApplicantsContainer(self, **data):
194        self.redirect(self.url(self.context, '@@add'))
[6078]195        return
196
[7710]197    @action(_('Add local role'), validator=NullValidator)
[6184]198    def addLocalRole(self, **data):
[7484]199        return add_local_role(self,3, **data)
[6184]200
[7710]201    @action(_('Remove selected local roles'))
[6184]202    def delLocalRoles(self, **data):
[7484]203        return del_local_roles(self,3,**data)
[6184]204
[8388]205    @action(_('Save'), style='primary')
206    def save(self, **data):
207        self.applyData(self.context, **data)
[12247]208        description = getattr(self.context, 'description', None)
209        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
210        self.context.description_dict = html2dict(description, portal_language)
[8390]211        self.flash(_('Form has been saved.'))
[8388]212        return
213
[7819]214class ApplicantsContainerAddFormPage(KofaAddFormPage):
[5822]215    grok.context(IApplicantsRoot)
[7136]216    grok.require('waeup.manageApplication')
[5822]217    grok.name('add')
[6107]218    grok.template('applicantscontaineraddpage')
[7710]219    label = _('Add applicants container')
[5843]220    pnav = 3
[6078]221
[6103]222    form_fields = grok.AutoFields(
[7903]223        IApplicantsContainerAdd).omit('code').omit('title')
[6078]224
[7710]225    @action(_('Add applicants container'))
[6069]226    def addApplicantsContainer(self, **data):
[6103]227        year = data['year']
[15548]228        if not data['container_number']:
229            code = u'%s%s' % (data['prefix'], year)
230        else:
231            code = u'%s%s' % (data['prefix'], data['container_number'])
[9529]232        apptypes_dict = getUtility(IApplicantsUtils).APP_TYPES_DICT
233        title = apptypes_dict[data['prefix']][0]
[7685]234        title = u'%s %s/%s' % (title, year, year + 1)
[6087]235        if code in self.context.keys():
[6105]236            self.flash(
[11254]237              _('An applicants container for the same application '
238                'type and entrance year exists already in the database.'),
239                type='warning')
[5822]240            return
241        # Add new applicants container...
[8009]242        container = createObject(u'waeup.ApplicantsContainer')
[6069]243        self.applyData(container, **data)
[6087]244        container.code = code
245        container.title = title
246        self.context[code] = container
[7710]247        self.flash(_('Added:') + ' "%s".' % code)
[12892]248        ob_class = self.__implemented__.__name__.replace('waeup.kofa.','')
249        self.context.logger.info('%s - added: %s' % (ob_class, code))
[7484]250        self.redirect(self.url(self.context, u'@@manage'))
[5822]251        return
[6078]252
[7710]253    @action(_('Cancel'), validator=NullValidator)
[6069]254    def cancel(self, **data):
[7484]255        self.redirect(self.url(self.context, '@@manage'))
[6078]256
[5845]257class ApplicantsRootBreadcrumb(Breadcrumb):
258    """A breadcrumb for applicantsroot.
259    """
260    grok.context(IApplicantsRoot)
[7710]261    title = _(u'Applicants')
[6078]262
[5845]263class ApplicantsContainerBreadcrumb(Breadcrumb):
264    """A breadcrumb for applicantscontainers.
265    """
266    grok.context(IApplicantsContainer)
[6319]267
[10655]268
269class ApplicantsExportsBreadcrumb(Breadcrumb):
270    """A breadcrumb for exports.
271    """
272    grok.context(VirtualApplicantsExportJobContainer)
273    title = _(u'Applicant Data Exports')
274    target = None
275
[6153]276class ApplicantBreadcrumb(Breadcrumb):
277    """A breadcrumb for applicants.
278    """
279    grok.context(IApplicant)
[6319]280
[6153]281    @property
282    def title(self):
283        """Get a title for a context.
284        """
[7240]285        return self.context.application_number
[5828]286
[7250]287class OnlinePaymentBreadcrumb(Breadcrumb):
288    """A breadcrumb for payments.
289    """
290    grok.context(IApplicantOnlinePayment)
291
292    @property
293    def title(self):
294        return self.context.p_id
295
[13976]296class RefereeReportBreadcrumb(Breadcrumb):
297    """A breadcrumb for referee reports.
298    """
299    grok.context(IApplicantRefereeReport)
300
301    @property
302    def title(self):
303        return self.context.r_id
304
[8563]305class ApplicantsStatisticsPage(KofaDisplayFormPage):
306    """Some statistics about applicants in a container.
307    """
308    grok.context(IApplicantsContainer)
309    grok.name('statistics')
[8565]310    grok.require('waeup.viewApplicationStatistics')
[8563]311    grok.template('applicantcontainerstatistics')
312
313    @property
314    def label(self):
315        return "%s" % self.context.title
316
[7819]317class ApplicantsContainerPage(KofaDisplayFormPage):
[5830]318    """The standard view for regular applicant containers.
319    """
320    grok.context(IApplicantsContainer)
321    grok.name('index')
[6153]322    grok.require('waeup.Public')
[6029]323    grok.template('applicantscontainerpage')
[5850]324    pnav = 3
[6053]325
[9078]326    @property
327    def form_fields(self):
[12247]328        form_fields = grok.AutoFields(IApplicantsContainer).omit(
329            'title', 'description')
[9078]330        form_fields[
331            'startdate'].custom_widget = FriendlyDatetimeDisplayWidget('le')
332        form_fields[
333            'enddate'].custom_widget = FriendlyDatetimeDisplayWidget('le')
334        if self.request.principal.id == 'zope.anybody':
335            form_fields = form_fields.omit(
[10101]336                'code', 'prefix', 'year', 'mode', 'hidden',
[11870]337                'strict_deadline', 'application_category',
[15819]338                'application_slip_notice', 'with_picture')
[9078]339        return form_fields
[6053]340
[5837]341    @property
[7708]342    def introduction(self):
[7833]343        # Here we know that the cookie has been set
344        lang = self.request.cookies.get('kofa.language')
[7708]345        html = self.context.description_dict.get(lang,'')
[8388]346        if html == '':
[7833]347            portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
[7708]348            html = self.context.description_dict.get(portal_language,'')
[8388]349        return html
[7708]350
351    @property
[7467]352    def label(self):
[7493]353        return "%s" % self.context.title
[5837]354
[7819]355class ApplicantsContainerManageFormPage(KofaEditFormPage):
[5837]356    grok.context(IApplicantsContainer)
[5850]357    grok.name('manage')
[6107]358    grok.template('applicantscontainermanagepage')
[10625]359    form_fields = grok.AutoFields(IApplicantsContainer)
[7710]360    taboneactions = [_('Save'),_('Cancel')]
[8684]361    tabtwoactions = [_('Remove selected'),_('Cancel'),
[8314]362        _('Create students from selected')]
[7710]363    tabthreeactions1 = [_('Remove selected local roles')]
364    tabthreeactions2 = [_('Add local role')]
[5844]365    # Use friendlier date widget...
[7136]366    grok.require('waeup.manageApplication')
[13177]367    doclink = DOCLINK + '/applicants.html'
[5850]368
369    @property
370    def label(self):
[7710]371        return _('Manage applicants container')
[5850]372
[5845]373    pnav = 3
[5837]374
[8547]375    @property
376    def showApplicants(self):
[13217]377        if self.context.counts[1] < 1000:
[8547]378            return True
379        return False
380
[6184]381    def getLocalRoles(self):
382        roles = ILocalRolesAssignable(self.context)
383        return roles()
384
385    def getUsers(self):
386        """Get a list of all users.
387        """
388        for key, val in grok.getSite()['users'].items():
389            url = self.url(val)
390            yield(dict(url=url, name=key, val=val))
391
392    def getUsersWithLocalRoles(self):
393        return get_users_with_local_roles(self.context)
394
[7714]395    @action(_('Save'), style='primary')
[7489]396    def save(self, **data):
[9531]397        changed_fields = self.applyData(self.context, **data)
398        if changed_fields:
399            changed_fields = reduce(lambda x,y: x+y, changed_fields.values())
400        else:
401            changed_fields = []
[12247]402        description = getattr(self.context, 'description', None)
403        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
404        self.context.description_dict = html2dict(description, portal_language)
[7710]405        self.flash(_('Form has been saved.'))
[9531]406        fields_string = ' + '.join(changed_fields)
[12892]407        self.context.writeLogMessage(self, 'saved: %s' % fields_string)
[5837]408        return
[6078]409
[7710]410    @jsaction(_('Remove selected'))
[6105]411    def delApplicant(self, **data):
[6189]412        form = self.request.form
[9701]413        if 'val_id' in form:
[6189]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')
[6189]418            return
419        if not isinstance(child_id, list):
420            child_id = [child_id]
421        deleted = []
422        for id in child_id:
423            try:
424                del self.context[id]
425                deleted.append(id)
426            except:
[7710]427                self.flash(_('Could not delete:') + ' %s: %s: %s' % (
[11254]428                    id, sys.exc_info()[0], sys.exc_info()[1]), type='danger')
[6189]429        if len(deleted):
[7741]430            self.flash(_('Successfully removed: ${a}',
[7738]431                mapping = {'a':', '.join(deleted)}))
[11254]432        self.redirect(self.url(self.context, u'@@manage')+'#tab2')
[6189]433        return
[6105]434
[8314]435    @action(_('Create students from selected'))
436    def createStudents(self, **data):
[14949]437        if not checkPermission('waeup.createStudents', self.context):
438            self.flash(
439                _('You don\'t have permission to create student records.'),
440                type='warning')
441            self.redirect(self.url(self.context, '@@manage')+'#tab2')
442            return
[8314]443        form = self.request.form
[9701]444        if 'val_id' in form:
[8314]445            child_id = form['val_id']
446        else:
[11254]447            self.flash(_('No applicant selected!'), type='warning')
448            self.redirect(self.url(self.context, '@@manage')+'#tab2')
[8314]449            return
450        if not isinstance(child_id, list):
451            child_id = [child_id]
452        created = []
[14682]453        if len(child_id) > 10 and self.request.principal.id != 'admin':
454            self.flash(_('A maximum of 10 applicants can be selected!'),
455                       type='warning')
456            self.redirect(self.url(self.context, '@@manage')+'#tab2')
457            return
[8314]458        for id in child_id:
459            success, msg = self.context[id].createStudent(view=self)
460            if success:
461                created.append(id)
462        if len(created):
463            self.flash(_('${a} students successfully created.',
464                mapping = {'a': len(created)}))
465        else:
[11254]466            self.flash(_('No student could be created.'), type='warning')
467        self.redirect(self.url(self.context, u'@@manage')+'#tab2')
[8314]468        return
469
[7710]470    @action(_('Cancel'), validator=NullValidator)
[5837]471    def cancel(self, **data):
472        self.redirect(self.url(self.context))
473        return
[5886]474
[7710]475    @action(_('Add local role'), validator=NullValidator)
[6184]476    def addLocalRole(self, **data):
477        return add_local_role(self,3, **data)
[6105]478
[7710]479    @action(_('Remove selected local roles'))
[6184]480    def delLocalRoles(self, **data):
481        return del_local_roles(self,3,**data)
482
[7819]483class ApplicantAddFormPage(KofaAddFormPage):
[6622]484    """Add-form to add an applicant.
[6327]485    """
486    grok.context(IApplicantsContainer)
[7136]487    grok.require('waeup.manageApplication')
[6327]488    grok.name('addapplicant')
[7240]489    #grok.template('applicantaddpage')
490    form_fields = grok.AutoFields(IApplicant).select(
[7356]491        'firstname', 'middlename', 'lastname',
[7240]492        'email', 'phone')
[7714]493    label = _('Add applicant')
[6327]494    pnav = 3
[13177]495    doclink = DOCLINK + '/applicants.html'
[6327]496
[7714]497    @action(_('Create application record'))
[6327]498    def addApplicant(self, **data):
[8008]499        applicant = createObject(u'waeup.Applicant')
[7240]500        self.applyData(applicant, **data)
501        self.context.addApplicant(applicant)
[13073]502        self.flash(_('Application record created.'))
[7363]503        self.redirect(
504            self.url(self.context[applicant.application_number], 'index'))
[6327]505        return
506
[13217]507class ApplicantsContainerPrefillFormPage(KofaAddFormPage):
[13218]508    """Form to pre-fill applicants containers.
[13217]509    """
510    grok.context(IApplicantsContainer)
511    grok.require('waeup.manageApplication')
512    grok.name('prefill')
[13218]513    grok.template('prefillcontainer')
[13217]514    label = _('Pre-fill container')
515    pnav = 3
[13232]516    doclink = DOCLINK + '/applicants/browser.html#preparation-and-maintenance-of-applicants-containers'
[13217]517
518    def update(self):
519        if self.context.mode == 'update':
520            self.flash(_('Container must be in create mode to be pre-filled.'),
521                type='danger')
522            self.redirect(self.url(self.context))
523            return
524        super(ApplicantsContainerPrefillFormPage, self).update()
525        return
526
527    @action(_('Pre-fill now'), style='primary')
[13218]528    def addApplicants(self):
[13217]529        form = self.request.form
530        if 'number' in form and form['number']:
531            number = int(form['number'])
532        for i in range(number):
533            applicant = createObject(u'waeup.Applicant')
534            self.context.addApplicant(applicant)
535        self.flash(_('%s application records created.' % number))
536        self.context.writeLogMessage(self, '%s applicants created' % (number))
537        self.redirect(self.url(self.context, 'index'))
538        return
539
540    @action(_('Cancel'), validator=NullValidator)
541    def cancel(self, **data):
542        self.redirect(self.url(self.context))
543        return
544
[13218]545class ApplicantsContainerPurgeFormPage(KofaEditFormPage):
[14109]546    """Form to purge applicants containers.
[13218]547    """
548    grok.context(IApplicantsContainer)
549    grok.require('waeup.manageApplication')
550    grok.name('purge')
551    grok.template('purgecontainer')
552    label = _('Purge container')
553    pnav = 3
[13232]554    doclink = DOCLINK + '/applicants/browser.html#preparation-and-maintenance-of-applicants-containers'
[13218]555
[13232]556    @action(_('Remove initialized records'),
557              tooltip=_('Don\'t use if application is in progress!'),
558              warning=_('Are you really sure?'),
559              style='primary')
[13218]560    def purgeInitialized(self):
561        form = self.request.form
562        purged = 0
563        keys = [key for key in self.context.keys()]
564        for key in keys:
565            if self.context[key].state == 'initialized':
566                del self.context[key]
567                purged += 1
568        self.flash(_('%s application records purged.' % purged))
569        self.context.writeLogMessage(self, '%s applicants purged' % (purged))
570        self.redirect(self.url(self.context, 'index'))
571        return
572
573    @action(_('Cancel'), validator=NullValidator)
574    def cancel(self, **data):
575        self.redirect(self.url(self.context))
576        return
577
[7819]578class ApplicantDisplayFormPage(KofaDisplayFormPage):
[8014]579    """A display view for applicant data.
580    """
[5273]581    grok.context(IApplicant)
582    grok.name('index')
[7113]583    grok.require('waeup.viewApplication')
[7200]584    grok.template('applicantdisplaypage')
[7714]585    label = _('Applicant')
[5843]586    pnav = 3
[8922]587    hide_hint = False
[5273]588
[8046]589    @property
[13886]590    def display_payments(self):
[15122]591        if self.context.payments:
592            return True
[13886]593        if self.context.special:
594            return True
595        return getattr(self.context.__parent__, 'application_fee', None)
596
597    @property
[10831]598    def form_fields(self):
599        if self.context.special:
[11599]600            form_fields = grok.AutoFields(ISpecialApplicant).omit('locked')
[10831]601        else:
602            form_fields = grok.AutoFields(IApplicant).omit(
[10845]603                'locked', 'course_admitted', 'password', 'suspended')
[10831]604        return form_fields
605
606    @property
[10534]607    def target(self):
608        return getattr(self.context.__parent__, 'prefix', None)
609
610    @property
[8046]611    def separators(self):
612        return getUtility(IApplicantsUtils).SEPARATORS_DICT
613
[7063]614    def update(self):
615        self.passport_url = self.url(self.context, 'passport.jpg')
[7240]616        # Mark application as started if applicant logs in for the first time
[7272]617        usertype = getattr(self.request.principal, 'user_type', None)
618        if usertype == 'applicant' and \
619            IWorkflowState(self.context).getState() == INITIALIZED:
[7240]620            IWorkflowInfo(self.context).fireTransition('start')
[10895]621        if usertype == 'applicant' and self.context.state == 'created':
[10908]622            session = '%s/%s' % (self.context.__parent__.year,
623                                 self.context.__parent__.year+1)
624            title = getattr(grok.getSite()['configuration'], 'name', u'Sample University')
[10895]625            msg = _(
626                '\n <strong>Congratulations!</strong>' +
[10933]627                ' You have been offered provisional admission into the' +
[10908]628                ' ${c} Academic Session of ${d}.'
629                ' Your student record has been created for you.' +
630                ' Please, logout again and proceed to the' +
631                ' login page of the portal.'
[10895]632                ' Then enter your new student credentials:' +
633                ' user name= ${a}, password = ${b}.' +
634                ' Change your password when you have logged in.',
635                mapping = {
636                    'a':self.context.student_id,
[10908]637                    'b':self.context.application_number,
638                    'c':session,
639                    'd':title}
[10895]640                )
641            self.flash(msg)
[7063]642        return
643
[6196]644    @property
[7240]645    def hasPassword(self):
646        if self.context.password:
[7714]647            return _('set')
648        return _('unset')
[7240]649
650    @property
[6196]651    def label(self):
652        container_title = self.context.__parent__.title
[8096]653        return _('${a} <br /> Application Record ${b}', mapping = {
[7714]654            'a':container_title, 'b':self.context.application_number})
[6196]655
[7347]656    def getCourseAdmitted(self):
657        """Return link, title and code in html format to the certificate
658           admitted.
659        """
660        course_admitted = self.context.course_admitted
[7351]661        if getattr(course_admitted, '__parent__',None):
[7347]662            url = self.url(course_admitted)
663            title = course_admitted.title
664            code = course_admitted.code
665            return '<a href="%s">%s - %s</a>' %(url,code,title)
666        return ''
[6254]667
[7259]668class ApplicantBaseDisplayFormPage(ApplicantDisplayFormPage):
669    grok.context(IApplicant)
670    grok.name('base')
[15581]671    @property
672    def form_fields(self):
673        form_fields = grok.AutoFields(IApplicant).select(
674            'applicant_id', 'reg_number', 'email', 'course1')
675        if self.context.__parent__.prefix in ('special',):
676            form_fields['reg_number'].field.title = u'Identification Number'
677            return form_fields
678        return form_fields
[7259]679
[7459]680class CreateStudentPage(UtilityView, grok.View):
[8636]681    """Create a student object from applicant data.
[7341]682    """
683    grok.context(IApplicant)
684    grok.name('createstudent')
[14948]685    grok.require('waeup.createStudents')
[7341]686
687    def update(self):
[14346]688        success, msg = self.context.createStudent(view=self)
689        if success:
690            self.flash(msg)
691        else:
692            self.flash(msg, type='warning')
[7341]693        self.redirect(self.url(self.context))
694        return
695
696    def render(self):
697        return
698
[14951]699class CreateAllStudentsPage(KofaPage):
[8636]700    """Create all student objects from applicant data
[14934]701    in the root container or in a specific applicants container only.
[11826]702    Only PortalManagers can do this.
[8636]703    """
[9900]704    #grok.context(IApplicantsContainer)
[8636]705    grok.name('createallstudents')
[14948]706    grok.require('waeup.createStudents')
[14951]707    label = _('Student Record Creation Report')
[8636]708
709    def update(self):
[14934]710        grok.getSite()['configuration'].maintmode_enabled_by = u'admin'
711        transaction.commit()
712        # Wait 10 seconds for all transactions to be finished.
713        # Do not wait in tests.
714        if not self.request.principal.id == 'zope.mgr':
715            sleep(10)
[8636]716        cat = getUtility(ICatalog, name='applicants_catalog')
717        results = list(cat.searchResults(state=(ADMITTED, ADMITTED)))
718        created = []
[14949]719        failed = []
[9900]720        container_only = False
721        applicants_root = grok.getSite()['applicants']
722        if isinstance(self.context, ApplicantsContainer):
723            container_only = True
[8636]724        for result in results:
[9900]725            if container_only and result.__parent__ is not self.context:
[8636]726                continue
727            success, msg = result.createStudent(view=self)
728            if success:
729                created.append(result.applicant_id)
730            else:
[14951]731                failed.append(
732                    (result.applicant_id, self.url(result, 'manage'), msg))
[12893]733                ob_class = self.__implemented__.__name__.replace(
734                    'waeup.kofa.','')
[14934]735        grok.getSite()['configuration'].maintmode_enabled_by = None
[14951]736        self.successful = ', '.join(created)
737        self.failed = failed
[8636]738        return
739
740
[8260]741class ApplicationFeePaymentAddPage(UtilityView, grok.View):
[7250]742    """ Page to add an online payment ticket
743    """
744    grok.context(IApplicant)
745    grok.name('addafp')
746    grok.require('waeup.payApplicant')
[8243]747    factory = u'waeup.ApplicantOnlinePayment'
[7250]748
[11726]749    @property
750    def custom_requirements(self):
751        return ''
752
[7250]753    def update(self):
[11726]754        # Additional requirements in custom packages.
755        if self.custom_requirements:
756            self.flash(
757                self.custom_requirements,
758                type='danger')
759            self.redirect(self.url(self.context))
[11727]760            return
[11575]761        if not self.context.special:
762            for key in self.context.keys():
763                ticket = self.context[key]
764                if ticket.p_state == 'paid':
765                      self.flash(
766                          _('This type of payment has already been made.'),
767                          type='warning')
768                      self.redirect(self.url(self.context))
769                      return
[8524]770        applicants_utils = getUtility(IApplicantsUtils)
771        container = self.context.__parent__
[8243]772        payment = createObject(self.factory)
[11254]773        failure = applicants_utils.setPaymentDetails(
[10831]774            container, payment, self.context)
[11254]775        if failure is not None:
[13123]776            self.flash(failure, type='danger')
[8524]777            self.redirect(self.url(self.context))
778            return
[7250]779        self.context[payment.p_id] = payment
[12893]780        self.context.writeLogMessage(self, 'added: %s' % payment.p_id)
[7714]781        self.flash(_('Payment ticket created.'))
[8280]782        self.redirect(self.url(payment))
[7250]783        return
784
785    def render(self):
786        return
787
788
[7819]789class OnlinePaymentDisplayFormPage(KofaDisplayFormPage):
[7250]790    """ Page to view an online payment ticket
791    """
792    grok.context(IApplicantOnlinePayment)
793    grok.name('index')
794    grok.require('waeup.viewApplication')
[9984]795    form_fields = grok.AutoFields(IApplicantOnlinePayment).omit('p_item')
[8170]796    form_fields[
797        'creation_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
798    form_fields[
799        'payment_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
[7250]800    pnav = 3
801
802    @property
803    def label(self):
[7714]804        return _('${a}: Online Payment Ticket ${b}', mapping = {
[8170]805            'a':self.context.__parent__.display_fullname,
806            'b':self.context.p_id})
[7250]807
[8420]808class OnlinePaymentApprovePage(UtilityView, grok.View):
809    """ Approval view
[7250]810    """
811    grok.context(IApplicantOnlinePayment)
[8420]812    grok.name('approve')
813    grok.require('waeup.managePortal')
[7250]814
815    def update(self):
[11580]816        flashtype, msg, log = self.context.approveApplicantPayment()
[8428]817        if log is not None:
[9771]818            applicant = self.context.__parent__
819            # Add log message to applicants.log
820            applicant.writeLogMessage(self, log)
821            # Add log message to payments.log
822            self.context.logger.info(
[9795]823                '%s,%s,%s,%s,%s,,,,,,' % (
[9771]824                applicant.applicant_id,
825                self.context.p_id, self.context.p_category,
826                self.context.amount_auth, self.context.r_code))
[11580]827        self.flash(msg, type=flashtype)
[7250]828        return
829
830    def render(self):
831        self.redirect(self.url(self.context, '@@index'))
832        return
833
[7459]834class ExportPDFPaymentSlipPage(UtilityView, grok.View):
[7250]835    """Deliver a PDF slip of the context.
836    """
837    grok.context(IApplicantOnlinePayment)
[8262]838    grok.name('payment_slip.pdf')
[7250]839    grok.require('waeup.viewApplication')
[9984]840    form_fields = grok.AutoFields(IApplicantOnlinePayment).omit('p_item')
[8173]841    form_fields['creation_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
842    form_fields['payment_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
[7250]843    prefix = 'form'
[8258]844    note = None
[7250]845
846    @property
[7714]847    def title(self):
[7819]848        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
[7811]849        return translate(_('Payment Data'), 'waeup.kofa',
[7714]850            target_language=portal_language)
851
852    @property
[7250]853    def label(self):
[7819]854        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
[8262]855        return translate(_('Online Payment Slip'),
[7811]856            'waeup.kofa', target_language=portal_language) \
[7714]857            + ' %s' % self.context.p_id
[7250]858
[11754]859    @property
860    def payment_slip_download_warning(self):
[14567]861        if self.context.__parent__.state not in (
862            SUBMITTED, ADMITTED, NOT_ADMITTED, CREATED):
[11754]863            return _('Please submit the application form before '
864                     'trying to download payment slips.')
865        return ''
866
[7250]867    def render(self):
[11754]868        if self.payment_slip_download_warning:
869            self.flash(self.payment_slip_download_warning, type='danger')
[11599]870            self.redirect(self.url(self.context))
871            return
[7259]872        applicantview = ApplicantBaseDisplayFormPage(self.context.__parent__,
[7250]873            self.request)
874        students_utils = getUtility(IStudentsUtils)
[8262]875        return students_utils.renderPDF(self,'payment_slip.pdf',
[8258]876            self.context.__parent__, applicantview, note=self.note)
[7250]877
[10571]878class ExportPDFPageApplicationSlip(UtilityView, grok.View):
[6358]879    """Deliver a PDF slip of the context.
880    """
881    grok.context(IApplicant)
882    grok.name('application_slip.pdf')
[7136]883    grok.require('waeup.viewApplication')
[6358]884    prefix = 'form'
885
[8666]886    def update(self):
[9051]887        if self.context.state in ('initialized', 'started', 'paid'):
[8666]888            self.flash(
[11254]889                _('Please pay and submit before trying to download '
890                  'the application slip.'), type='warning')
[8666]891            return self.redirect(self.url(self.context))
892        return
893
[6358]894    def render(self):
[12395]895        try:
896            pdfstream = getAdapter(self.context, IPDF, name='application_slip')(
897                view=self)
898        except IOError:
899            self.flash(
900                _('Your image file is corrupted. '
901                  'Please replace.'), type='danger')
902            return self.redirect(self.url(self.context))
[14256]903        except LayoutError, err:
[15583]904            self.flash(
[14256]905                'PDF file could not be created. Reportlab error message: %s'
906                % escape(err.message),
907                type="danger")
908            return self.redirect(self.url(self.context))
[6358]909        self.response.setHeader(
910            'Content-Type', 'application/pdf')
[7392]911        return pdfstream
[6358]912
[7081]913def handle_img_upload(upload, context, view):
[7063]914    """Handle upload of applicant image.
[7081]915
916    Returns `True` in case of success or `False`.
917
918    Please note that file pointer passed in (`upload`) most probably
919    points to end of file when leaving this function.
[7063]920    """
[15833]921    max_upload_size = getUtility(IKofaUtils).MAX_PASSPORT_SIZE
[7081]922    size = file_size(upload)
[15833]923    if size > max_upload_size:
[11254]924        view.flash(_('Uploaded image is too big!'), type='danger')
[7081]925        return False
[7247]926    dummy, ext = os.path.splitext(upload.filename)
927    ext.lower()
928    if ext != '.jpg':
[11254]929        view.flash(_('jpg file extension expected.'), type='danger')
[7247]930        return False
[7081]931    upload.seek(0) # file pointer moved when determining size
[7063]932    store = getUtility(IExtFileStore)
933    file_id = IFileStoreNameChooser(context).chooseName()
[14001]934    try:
935        store.createFile(file_id, upload)
936    except IOError:
937        view.flash(_('Image file cannot be changed.'), type='danger')
938        return False
[7081]939    return True
[7063]940
[7819]941class ApplicantManageFormPage(KofaEditFormPage):
[6196]942    """A full edit view for applicant data.
943    """
944    grok.context(IApplicant)
[7200]945    grok.name('manage')
[7136]946    grok.require('waeup.manageApplication')
[7200]947    grok.template('applicanteditpage')
[6322]948    manage_applications = True
[6196]949    pnav = 3
[12664]950    display_actions = [[_('Save'), _('Finally Submit')],
[7714]951        [_('Add online payment ticket'),_('Remove selected tickets')]]
[6196]952
[8046]953    @property
[13886]954    def display_payments(self):
[15122]955        if self.context.payments:
956            return True
[13886]957        if self.context.special:
958            return True
959        return getattr(self.context.__parent__, 'application_fee', None)
960
961    @property
[13976]962    def display_refereereports(self):
963        if self.context.refereereports:
964            return True
965        return False
966
967    @property
[10831]968    def form_fields(self):
969        if self.context.special:
[10845]970            form_fields = grok.AutoFields(ISpecialApplicant)
971            form_fields['applicant_id'].for_display = True
[10831]972        else:
973            form_fields = grok.AutoFields(IApplicant)
974            form_fields['student_id'].for_display = True
975            form_fields['applicant_id'].for_display = True
976        return form_fields
977
978    @property
[10534]979    def target(self):
980        return getattr(self.context.__parent__, 'prefix', None)
981
982    @property
[8046]983    def separators(self):
984        return getUtility(IApplicantsUtils).SEPARATORS_DICT
985
[11733]986    @property
987    def custom_upload_requirements(self):
988        return ''
989
[6196]990    def update(self):
[7200]991        super(ApplicantManageFormPage, self).update()
[15833]992        max_upload_size = getUtility(IKofaUtils).MAX_PASSPORT_SIZE
[6353]993        self.wf_info = IWorkflowInfo(self.context)
[15833]994        self.max_upload_size = string_from_bytes(max_upload_size)
[10090]995        self.upload_success = None
[6598]996        upload = self.request.form.get('form.passport', None)
997        if upload:
[11733]998            if self.custom_upload_requirements:
999                self.flash(
1000                    self.custom_upload_requirements,
1001                    type='danger')
1002                self.redirect(self.url(self.context))
1003                return
[10090]1004            # We got a fresh upload, upload_success is
1005            # either True or False
1006            self.upload_success = handle_img_upload(
[7084]1007                upload, self.context, self)
[10090]1008            if self.upload_success:
[10095]1009                self.context.writeLogMessage(self, 'saved: passport')
[6196]1010        return
1011
1012    @property
1013    def label(self):
1014        container_title = self.context.__parent__.title
[8096]1015        return _('${a} <br /> Application Form ${b}', mapping = {
[7714]1016            'a':container_title, 'b':self.context.application_number})
[6196]1017
[6303]1018    def getTransitions(self):
[6351]1019        """Return a list of dicts of allowed transition ids and titles.
[6353]1020
1021        Each list entry provides keys ``name`` and ``title`` for
1022        internal name and (human readable) title of a single
1023        transition.
[6349]1024        """
[8434]1025        allowed_transitions = [t for t in self.wf_info.getManualTransitions()
[11482]1026            if not t[0] in ('pay', 'create')]
[7687]1027        return [dict(name='', title=_('No transition'))] +[
[6355]1028            dict(name=x, title=y) for x, y in allowed_transitions]
[6303]1029
[7714]1030    @action(_('Save'), style='primary')
[6196]1031    def save(self, **data):
[7240]1032        form = self.request.form
1033        password = form.get('password', None)
1034        password_ctl = form.get('control_password', None)
1035        if password:
1036            validator = getUtility(IPasswordValidator)
1037            errors = validator.validate_password(password, password_ctl)
1038            if errors:
[11254]1039                self.flash( ' '.join(errors), type='danger')
[7240]1040                return
[10090]1041        if self.upload_success is False:  # False is not None!
1042            # Error during image upload. Ignore other values.
1043            return
[6475]1044        changed_fields = self.applyData(self.context, **data)
[7199]1045        # Turn list of lists into single list
1046        if changed_fields:
1047            changed_fields = reduce(lambda x,y: x+y, changed_fields.values())
[7240]1048        else:
1049            changed_fields = []
1050        if password:
1051            # Now we know that the form has no errors and can set password ...
1052            IUserAccount(self.context).setPassword(password)
1053            changed_fields.append('password')
[7199]1054        fields_string = ' + '.join(changed_fields)
[7085]1055        trans_id = form.get('transition', None)
1056        if trans_id:
1057            self.wf_info.fireTransition(trans_id)
[7714]1058        self.flash(_('Form has been saved.'))
[6644]1059        if fields_string:
[12892]1060            self.context.writeLogMessage(self, 'saved: %s' % fields_string)
[6196]1061        return
1062
[7250]1063    def unremovable(self, ticket):
[7330]1064        return False
[7250]1065
1066    # This method is also used by the ApplicantEditFormPage
1067    def delPaymentTickets(self, **data):
1068        form = self.request.form
[9701]1069        if 'val_id' in form:
[7250]1070            child_id = form['val_id']
1071        else:
[11254]1072            self.flash(_('No payment selected.'), type='warning')
[7250]1073            self.redirect(self.url(self.context))
1074            return
1075        if not isinstance(child_id, list):
1076            child_id = [child_id]
1077        deleted = []
1078        for id in child_id:
1079            # Applicants are not allowed to remove used payment tickets
1080            if not self.unremovable(self.context[id]):
1081                try:
1082                    del self.context[id]
1083                    deleted.append(id)
1084                except:
[7714]1085                    self.flash(_('Could not delete:') + ' %s: %s: %s' % (
[11254]1086                      id, sys.exc_info()[0], sys.exc_info()[1]), type='danger')
[7250]1087        if len(deleted):
[7741]1088            self.flash(_('Successfully removed: ${a}',
[7738]1089                mapping = {'a':', '.join(deleted)}))
[8742]1090            self.context.writeLogMessage(
1091                self, 'removed: % s' % ', '.join(deleted))
[7250]1092        return
1093
[7252]1094    # We explicitely want the forms to be validated before payment tickets
1095    # can be created. If no validation is requested, use
[7459]1096    # 'validator=NullValidator' in the action directive
[11578]1097    @action(_('Add online payment ticket'), style='primary')
[7250]1098    def addPaymentTicket(self, **data):
1099        self.redirect(self.url(self.context, '@@addafp'))
[7252]1100        return
[7250]1101
[7714]1102    @jsaction(_('Remove selected tickets'))
[7250]1103    def removePaymentTickets(self, **data):
1104        self.delPaymentTickets(**data)
1105        self.redirect(self.url(self.context) + '/@@manage')
1106        return
1107
[10094]1108    # Not used in base package
1109    def file_exists(self, attr):
1110        file = getUtility(IExtFileStore).getFileByContext(
1111            self.context, attr=attr)
1112        if file:
1113            return True
1114        else:
1115            return False
1116
[7200]1117class ApplicantEditFormPage(ApplicantManageFormPage):
[5982]1118    """An applicant-centered edit view for applicant data.
1119    """
[6196]1120    grok.context(IApplicantEdit)
[5273]1121    grok.name('edit')
[6198]1122    grok.require('waeup.handleApplication')
[7200]1123    grok.template('applicanteditpage')
[6322]1124    manage_applications = False
[10358]1125    submit_state = PAID
[5484]1126
[7250]1127    @property
[13976]1128    def display_refereereports(self):
1129        return False
1130
1131    @property
[10831]1132    def form_fields(self):
1133        if self.context.special:
[11657]1134            form_fields = grok.AutoFields(ISpecialApplicant).omit(
1135                'locked', 'suspended')
[10845]1136            form_fields['applicant_id'].for_display = True
[10831]1137        else:
1138            form_fields = grok.AutoFields(IApplicantEdit).omit(
1139                'locked', 'course_admitted', 'student_id',
[10845]1140                'suspended'
[10831]1141                )
1142            form_fields['applicant_id'].for_display = True
1143            form_fields['reg_number'].for_display = True
1144        return form_fields
1145
1146    @property
[7250]1147    def display_actions(self):
[8286]1148        state = IWorkflowState(self.context).getState()
[13100]1149        # If the form is unlocked, applicants are allowed to save the form
1150        # and remove unused tickets.
1151        actions = [[_('Save')], [_('Remove selected tickets')]]
1152        # Only in state started they can also add tickets.
[10358]1153        if state == STARTED:
[7714]1154            actions = [[_('Save')],
1155                [_('Add online payment ticket'),_('Remove selected tickets')]]
[13100]1156        # In state paid, they can submit the data and further add tickets
1157        # if the application is special.
[11599]1158        elif self.context.special and state == PAID:
[12664]1159            actions = [[_('Save'), _('Finally Submit')],
[11599]1160                [_('Add online payment ticket'),_('Remove selected tickets')]]
[8286]1161        elif state == PAID:
[12664]1162            actions = [[_('Save'), _('Finally Submit')],
[7714]1163                [_('Remove selected tickets')]]
[7250]1164        return actions
1165
[7330]1166    def unremovable(self, ticket):
[13100]1167        return ticket.r_code
[7330]1168
[7145]1169    def emit_lock_message(self):
[11254]1170        self.flash(_('The requested form is locked (read-only).'),
1171                   type='warning')
[5941]1172        self.redirect(self.url(self.context))
1173        return
[6078]1174
[5686]1175    def update(self):
[8665]1176        if self.context.locked or (
1177            self.context.__parent__.expired and
1178            self.context.__parent__.strict_deadline):
[7145]1179            self.emit_lock_message()
[5941]1180            return
[7200]1181        super(ApplicantEditFormPage, self).update()
[5686]1182        return
[5952]1183
[15634]1184    def dataNotComplete(self, data):
[15502]1185        if self.context.__parent__.with_picture:
1186            store = getUtility(IExtFileStore)
1187            if not store.getFileByContext(self.context, attr=u'passport.jpg'):
1188                return _('No passport picture uploaded.')
1189            if not self.request.form.get('confirm_passport', False):
1190                return _('Passport picture confirmation box not ticked.')
[6196]1191        return False
[5952]1192
[7252]1193    # We explicitely want the forms to be validated before payment tickets
1194    # can be created. If no validation is requested, use
[7459]1195    # 'validator=NullValidator' in the action directive
[11578]1196    @action(_('Add online payment ticket'), style='primary')
[7250]1197    def addPaymentTicket(self, **data):
1198        self.redirect(self.url(self.context, '@@addafp'))
[7252]1199        return
[7250]1200
[7714]1201    @jsaction(_('Remove selected tickets'))
[7250]1202    def removePaymentTickets(self, **data):
1203        self.delPaymentTickets(**data)
1204        self.redirect(self.url(self.context) + '/@@edit')
1205        return
1206
[7996]1207    @action(_('Save'), style='primary')
[5273]1208    def save(self, **data):
[10090]1209        if self.upload_success is False:  # False is not None!
1210            # Error during image upload. Ignore other values.
1211            return
[5273]1212        self.applyData(self.context, **data)
[10210]1213        self.flash(_('Form has been saved.'))
[5273]1214        return
1215
[14014]1216    def informReferees(self):
1217        site = grok.getSite()
1218        kofa_utils = getUtility(IKofaUtils)
1219        ob_class = self.__implemented__.__name__.replace('waeup.kofa.','')
1220        failed = ''
[14016]1221        emails_sent = 0
[14014]1222        for referee in self.context.referees:
1223            if referee.email_sent:
1224                continue
1225            mandate = RefereeReportMandate()
1226            mandate.params['name'] = referee.name
1227            mandate.params['email'] = referee.email
1228            mandate.params[
1229                'redirect_path'] = '/applicants/%s/%s/addrefereereport' % (
1230                    self.context.__parent__.code,
1231                    self.context.application_number)
1232            site['mandates'].addMandate(mandate)
1233            # Send invitation email
1234            args = {'mandate_id':mandate.mandate_id}
1235            mandate_url = self.url(site) + '/mandate?%s' % urlencode(args)
1236            url_info = u'Report link: %s' % mandate_url
1237            success = kofa_utils.inviteReferee(referee, self.context, url_info)
1238            if success:
[14016]1239                emails_sent += 1
[14014]1240                self.context.writeLogMessage(
1241                    self, 'email sent: %s' % referee.email)
1242                referee.email_sent = True
1243            else:
1244                failed += '%s ' % referee.email
[14016]1245        return failed, emails_sent
[14014]1246
[14025]1247    @action(_('Finally Submit'), warning=WARNING)
[5484]1248    def finalsubmit(self, **data):
[10090]1249        if self.upload_success is False:  # False is not None!
[7084]1250            return # error during image upload. Ignore other values
[15636]1251        dnt = self.dataNotComplete(data)
1252        if dnt:
1253            self.flash(dnt, type='danger')
[5941]1254            return
[7252]1255        self.applyData(self.context, **data)
[8286]1256        state = IWorkflowState(self.context).getState()
[6322]1257        # This shouldn't happen, but the application officer
1258        # might have forgotten to lock the form after changing the state
[10358]1259        if state != self.submit_state:
[11254]1260            self.flash(_('The form cannot be submitted. Wrong state!'),
1261                       type='danger')
[6303]1262            return
[14014]1263        msg = _('Form has been submitted.')
1264        # Create mandates and send emails to referees
1265        if getattr(self.context, 'referees', None):
[14016]1266            failed, emails_sent = self.informReferees()
[14014]1267            if failed:
1268                self.flash(
1269                    _('Some invitation emails could not be sent:') + failed,
1270                    type='danger')
1271                return
1272            msg = _('Form has been successfully submitted and '
[14016]1273                    '${a} invitation emails were sent.',
1274                    mapping = {'a':  emails_sent})
[6303]1275        IWorkflowInfo(self.context).fireTransition('submit')
[8589]1276        # application_date is used in export files for sorting.
1277        # We can thus store utc.
[8194]1278        self.context.application_date = datetime.utcnow()
[14014]1279        self.flash(msg)
[6196]1280        self.redirect(self.url(self.context))
[5273]1281        return
[5941]1282
[7063]1283class PassportImage(grok.View):
1284    """Renders the passport image for applicants.
1285    """
1286    grok.name('passport.jpg')
1287    grok.context(IApplicant)
[7113]1288    grok.require('waeup.viewApplication')
[7063]1289
1290    def render(self):
1291        # A filename chooser turns a context into a filename suitable
1292        # for file storage.
1293        image = getUtility(IExtFileStore).getFileByContext(self.context)
1294        self.response.setHeader(
1295            'Content-Type', 'image/jpeg')
1296        if image is None:
1297            # show placeholder image
[7089]1298            return open(DEFAULT_PASSPORT_IMAGE_PATH, 'rb').read()
[7063]1299        return image
[7363]1300
[7819]1301class ApplicantRegistrationPage(KofaAddFormPage):
[7363]1302    """Captcha'd registration page for applicants.
1303    """
1304    grok.context(IApplicantsContainer)
1305    grok.name('register')
[7373]1306    grok.require('waeup.Anonymous')
[7363]1307    grok.template('applicantregister')
1308
[7368]1309    @property
[8033]1310    def form_fields(self):
1311        form_fields = None
[8128]1312        if self.context.mode == 'update':
1313            form_fields = grok.AutoFields(IApplicantRegisterUpdate).select(
[11738]1314                'lastname','reg_number','email')
[8128]1315        else: #if self.context.mode == 'create':
[8033]1316            form_fields = grok.AutoFields(IApplicantEdit).select(
1317                'firstname', 'middlename', 'lastname', 'email', 'phone')
1318        return form_fields
1319
1320    @property
[7368]1321    def label(self):
[8078]1322        return _('Apply for ${a}',
[7714]1323            mapping = {'a':self.context.title})
[7368]1324
[7363]1325    def update(self):
[8665]1326        if self.context.expired:
[11254]1327            self.flash(_('Outside application period.'), type='warning')
[7368]1328            self.redirect(self.url(self.context))
1329            return
[13394]1330        blocker = grok.getSite()['configuration'].maintmode_enabled_by
1331        if blocker:
1332            self.flash(_('The portal is in maintenance mode '
1333                        'and registration temporarily disabled.'),
1334                       type='warning')
1335            self.redirect(self.url(self.context))
1336            return
[7368]1337        # Handle captcha
[7363]1338        self.captcha = getUtility(ICaptchaManager).getCaptcha()
1339        self.captcha_result = self.captcha.verify(self.request)
1340        self.captcha_code = self.captcha.display(self.captcha_result.error_code)
1341        return
1342
[8629]1343    def _redirect(self, email, password, applicant_id):
1344        # Forward only email to landing page in base package.
1345        self.redirect(self.url(self.context, 'registration_complete',
1346            data = dict(email=email)))
1347        return
1348
[14578]1349    @property
1350    def _postfix(self):
1351        """In customized packages we can add a container dependent string if
1352        applicants have been imported into several containers.
1353        """
1354        return ''
1355
[9178]1356    @action(_('Send login credentials to email address'), style='primary')
[7363]1357    def register(self, **data):
1358        if not self.captcha_result.is_valid:
[8037]1359            # Captcha will display error messages automatically.
[7363]1360            # No need to flash something.
1361            return
[8033]1362        if self.context.mode == 'create':
[13217]1363            # Check if there are unused records in this container which
1364            # can be taken
1365            applicant = self.context.first_unused
1366            if applicant is None:
[13215]1367                # Add applicant
1368                applicant = createObject(u'waeup.Applicant')
1369                self.context.addApplicant(applicant)
[14110]1370            else:
1371                applicants_root = grok.getSite()['applicants']
1372                ob_class = self.__implemented__.__name__.replace(
1373                    'waeup.kofa.','')
1374                applicants_root.logger.info('%s - used: %s' % (
1375                    ob_class, applicant.applicant_id))
[8033]1376            self.applyData(applicant, **data)
[15578]1377            # applicant.reg_number = applicant.applicant_id
[8042]1378            notify(grok.ObjectModifiedEvent(applicant))
[8033]1379        elif self.context.mode == 'update':
1380            # Update applicant
[8037]1381            reg_number = data.get('reg_number','')
[11738]1382            lastname = data.get('lastname','')
[8033]1383            cat = getUtility(ICatalog, name='applicants_catalog')
[14578]1384            searchstr = reg_number + self._postfix
[8033]1385            results = list(
[14578]1386                cat.searchResults(reg_number=(searchstr, searchstr)))
[8033]1387            if results:
1388                applicant = results[0]
[11738]1389                if getattr(applicant,'lastname',None) is None:
[11254]1390                    self.flash(_('An error occurred.'), type='danger')
[8037]1391                    return
[11738]1392                elif applicant.lastname.lower() != lastname.lower():
[8042]1393                    # Don't tell the truth here. Anonymous must not
[11738]1394                    # know that a record was found and only the lastname
[8042]1395                    # verification failed.
[13099]1396                    self.flash(
1397                        _('No application record found.'), type='warning')
[8037]1398                    return
[8627]1399                elif applicant.password is not None and \
1400                    applicant.state != INITIALIZED:
1401                    self.flash(_('Your password has already been set and used. '
[11254]1402                                 'Please proceed to the login page.'),
1403                               type='warning')
[8042]1404                    return
1405                # Store email address but nothing else.
[8033]1406                applicant.email = data['email']
[8042]1407                notify(grok.ObjectModifiedEvent(applicant))
[8033]1408            else:
[8042]1409                # No record found, this is the truth.
[11254]1410                self.flash(_('No application record found.'), type='warning')
[8033]1411                return
1412        else:
[8042]1413            # Does not happen but anyway ...
[8033]1414            return
[7819]1415        kofa_utils = getUtility(IKofaUtils)
[7811]1416        password = kofa_utils.genPassword()
[7380]1417        IUserAccount(applicant).setPassword(password)
[7365]1418        # Send email with credentials
[7399]1419        login_url = self.url(grok.getSite(), 'login')
[8853]1420        url_info = u'Login: %s' % login_url
[7714]1421        msg = _('You have successfully been registered for the')
[7811]1422        if kofa_utils.sendCredentials(IUserAccount(applicant),
[8853]1423            password, url_info, msg):
[8629]1424            email_sent = applicant.email
[7380]1425        else:
[8629]1426            email_sent = None
1427        self._redirect(email=email_sent, password=password,
1428            applicant_id=applicant.applicant_id)
[7380]1429        return
1430
[7819]1431class ApplicantRegistrationEmailSent(KofaPage):
[7380]1432    """Landing page after successful registration.
[8629]1433
[7380]1434    """
1435    grok.name('registration_complete')
1436    grok.require('waeup.Public')
1437    grok.template('applicantregemailsent')
[7714]1438    label = _('Your registration was successful.')
[7380]1439
[8629]1440    def update(self, email=None, applicant_id=None, password=None):
[7380]1441        self.email = email
[8629]1442        self.password = password
1443        self.applicant_id = applicant_id
[7380]1444        return
[10655]1445
[13254]1446class ApplicantCheckStatusPage(KofaPage):
1447    """Captcha'd status checking page for applicants.
1448    """
1449    grok.context(IApplicantsRoot)
1450    grok.name('checkstatus')
1451    grok.require('waeup.Anonymous')
1452    grok.template('applicantcheckstatus')
1453    buttonname = _('Submit')
1454
1455    def label(self):
1456        if self.result:
[13429]1457            return _('Admission status of ${a}',
[13254]1458                     mapping = {'a':self.applicant.applicant_id})
[13428]1459        return _('Check your admission status')
[13254]1460
1461    def update(self, SUBMIT=None):
1462        form = self.request.form
1463        self.result = False
1464        # Handle captcha
1465        self.captcha = getUtility(ICaptchaManager).getCaptcha()
1466        self.captcha_result = self.captcha.verify(self.request)
1467        self.captcha_code = self.captcha.display(self.captcha_result.error_code)
1468        if SUBMIT:
1469            if not self.captcha_result.is_valid:
1470                # Captcha will display error messages automatically.
1471                # No need to flash something.
1472                return
[13363]1473            unique_id = form.get('unique_id', None)
[13254]1474            lastname = form.get('lastname', None)
[13363]1475            if not unique_id or not lastname:
[13254]1476                self.flash(
1477                    _('Required input missing.'), type='warning')
1478                return
1479            cat = getUtility(ICatalog, name='applicants_catalog')
1480            results = list(
[13363]1481                cat.searchResults(applicant_id=(unique_id, unique_id)))
1482            if not results:
1483                results = list(
1484                    cat.searchResults(reg_number=(unique_id, unique_id)))
[13254]1485            if results:
1486                applicant = results[0]
[13282]1487                if applicant.lastname.lower().strip() != lastname.lower():
[13254]1488                    # Don't tell the truth here. Anonymous must not
1489                    # know that a record was found and only the lastname
1490                    # verification failed.
1491                    self.flash(
1492                        _('No application record found.'), type='warning')
1493                    return
1494            else:
1495                self.flash(_('No application record found.'), type='warning')
1496                return
1497            self.applicant = applicant
1498            self.entry_session = "%s/%s" % (
1499                applicant.__parent__.year,
1500                applicant.__parent__.year+1)
1501            course_admitted = getattr(applicant, 'course_admitted', None)
1502            self.course_admitted = False
1503            if course_admitted is not None:
[14394]1504                try:
1505                    self.course_admitted = True
1506                    self.longtitle = course_admitted.longtitle
1507                    self.department = course_admitted.__parent__.__parent__.longtitle
1508                    self.faculty = course_admitted.__parent__.__parent__.__parent__.longtitle
1509                except AttributeError:
1510                    self.flash(_('Application record invalid.'), type='warning')
1511                    return
[13254]1512            self.result = True
1513            self.admitted = False
1514            self.not_admitted = False
1515            self.submitted = False
1516            self.not_submitted = False
[13356]1517            self.created = False
[13254]1518            if applicant.state in (ADMITTED, CREATED):
1519                self.admitted = True
[13356]1520            if applicant.state in (CREATED):
1521                self.created = True
[13365]1522                self.student_id = applicant.student_id
1523                self.password = applicant.application_number
[13254]1524            if applicant.state in (NOT_ADMITTED,):
1525                self.not_admitted = True
1526            if applicant.state in (SUBMITTED,):
1527                self.submitted = True
1528            if applicant.state in (INITIALIZED, STARTED, PAID):
1529                self.not_submitted = True
1530        return
1531
[10655]1532class ExportJobContainerOverview(KofaPage):
1533    """Page that lists active applicant data export jobs and provides links
1534    to discard or download CSV files.
1535
1536    """
1537    grok.context(VirtualApplicantsExportJobContainer)
1538    grok.require('waeup.manageApplication')
1539    grok.name('index.html')
1540    grok.template('exportjobsindex')
[11254]1541    label = _('Data Exports')
[10655]1542    pnav = 3
1543
1544    def update(self, CREATE=None, DISCARD=None, job_id=None):
1545        if CREATE:
1546            self.redirect(self.url('@@start_export'))
1547            return
1548        if DISCARD and job_id:
1549            entry = self.context.entry_from_job_id(job_id)
1550            self.context.delete_export_entry(entry)
1551            ob_class = self.__implemented__.__name__.replace('waeup.kofa.','')
1552            self.context.logger.info(
1553                '%s - discarded: job_id=%s' % (ob_class, job_id))
1554            self.flash(_('Discarded export') + ' %s' % job_id)
1555        self.entries = doll_up(self, user=self.request.principal.id)
1556        return
1557
[13950]1558class ExportJobContainerJobStart(UtilityView, grok.View):
1559    """View that starts two export jobs, one for applicants and a second
1560    one for applicant payments.
[10655]1561    """
1562    grok.context(VirtualApplicantsExportJobContainer)
1563    grok.require('waeup.manageApplication')
1564    grok.name('start_export')
1565
1566    def update(self):
[13152]1567        utils = queryUtility(IKofaUtils)
1568        if not utils.expensive_actions_allowed():
1569            self.flash(_(
1570                "Currently, exporters cannot be started due to high "
1571                "system load. Please try again later."), type='danger')
1572            self.entries = doll_up(self, user=None)
1573            return
[13950]1574
1575        ob_class = self.__implemented__.__name__.replace('waeup.kofa.','')
1576        container_code = self.context.__parent__.code
1577        # Start first exporter
[10655]1578        exporter = 'applicants'
1579        job_id = self.context.start_export_job(exporter,
1580                                      self.request.principal.id,
1581                                      container=container_code)
1582        self.context.logger.info(
1583            '%s - exported: %s (%s), job_id=%s'
1584            % (ob_class, exporter, container_code, job_id))
[13950]1585        # Commit transaction so that job is stored in the ZODB
1586        transaction.commit()
1587        # Start second exporter
1588        exporter = 'applicantpayments'
1589        job_id = self.context.start_export_job(exporter,
1590                                      self.request.principal.id,
1591                                      container=container_code)
1592        self.context.logger.info(
1593            '%s - exported: %s (%s), job_id=%s'
1594            % (ob_class, exporter, container_code, job_id))
1595
1596        self.flash(_('Exports started.'))
[10655]1597        self.redirect(self.url(self.context))
1598        return
1599
1600    def render(self):
1601        return
1602
1603class ExportJobContainerDownload(ExportCSVView):
1604    """Page that downloads a students export csv file.
1605
1606    """
1607    grok.context(VirtualApplicantsExportJobContainer)
[11253]1608    grok.require('waeup.manageApplication')
[13976]1609
1610class RefereeReportDisplayFormPage(KofaDisplayFormPage):
1611    """A display view for referee reports.
1612    """
1613    grok.context(IApplicantRefereeReport)
1614    grok.name('index')
1615    grok.require('waeup.manageApplication')
1616    label = _('Referee Report')
1617    pnav = 3
1618
1619class RefereeReportAddFormPage(KofaAddFormPage):
1620    """Add-form to add an referee report. This form
[13992]1621    is protected by a mandate.
[13976]1622    """
1623    grok.context(IApplicant)
[13991]1624    grok.require('waeup.Public')
[13976]1625    grok.name('addrefereereport')
1626    form_fields = grok.AutoFields(
1627        IApplicantRefereeReport).omit('creation_date')
[13992]1628    grok.template('refereereportpage')
[13976]1629    label = _('Add referee report')
1630    pnav = 3
1631    #doclink = DOCLINK + '/refereereports.html'
1632
1633    def update(self):
[13991]1634        blocker = grok.getSite()['configuration'].maintmode_enabled_by
1635        if blocker:
1636            self.flash(_('The portal is in maintenance mode. '
1637                        'Referee report forms are temporarily disabled.'),
1638                       type='warning')
1639            self.redirect(self.application_url())
1640            return
1641        # Check mandate
1642        form = self.request.form
[13992]1643        self.mandate_id = form.get('mandate_id', None)
1644        self.mandates = grok.getSite()['mandates']
1645        mandate = self.mandates.get(self.mandate_id, None)
[13991]1646        if mandate is None and not self.request.form.get('form.actions.submit'):
1647            self.flash(_('No mandate.'), type='warning')
1648            self.redirect(self.application_url())
1649            return
1650        if mandate:
1651            # Prefill form with mandate params
1652            self.form_fields.get(
1653                'name').field.default = mandate.params['name']
1654            self.form_fields.get(
1655                'email').field.default = mandate.params['email']
[13976]1656        super(RefereeReportAddFormPage, self).update()
1657        return
1658
1659    @action(_('Submit'),
1660              warning=_('Are you really sure? '
1661                        'Reports can neither be modified or added '
1662                        'after submission.'),
1663              style='primary')
1664    def addRefereeReport(self, **data):
1665        report = createObject(u'waeup.ApplicantRefereeReport')
1666        timestamp = ("%d" % int(time()*10000))[1:]
1667        report.r_id = "r%s" % timestamp
1668        self.applyData(report, **data)
1669        self.context[report.r_id] = report
[13991]1670        self.flash(_('Referee report has been saved. Thank you!'))
[13976]1671        self.context.writeLogMessage(self, 'added: %s' % report.r_id)
[13992]1672        # Delete mandate
1673        del self.mandates[self.mandate_id]
[13991]1674        self.redirect(self.application_url())
[13976]1675        return
Note: See TracBrowser for help on using the repository browser.