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

Last change on this file since 17386 was 17245, checked in by Henrik Bettermann, 23 months ago

Save p_option.

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