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

Last change on this file since 8201 was 8200, checked in by uli, 13 years ago
  • Use datetime for applicants containers and
  • Register new datetime widget as default for entering datetimes.

Fixed all tests to work with the new stuff.

As this is just a quick shot I put all changes into one commit
to ease any later rollback.

  • Property svn:keywords set to Id
File size: 34.9 KB
RevLine 
[5273]1## $Id: browser.py 8200 2012-04-17 23:35:24Z uli $
[6078]2##
[7192]3## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
[5273]4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
[6078]8##
[5273]9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
[6078]13##
[5273]14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
[5824]18"""UI components for basic applicants and related components.
[5273]19"""
[7063]20import os
[8200]21import pytz
[6082]22import sys
[5273]23import grok
[7250]24from time import time
[7370]25from datetime import datetime, date
[8042]26from zope.event import notify
[7392]27from zope.component import getUtility, createObject, getAdapter
[8033]28from zope.catalog.interfaces import ICatalog
[7714]29from zope.i18n import translate
[7322]30from hurry.workflow.interfaces import (
31    IWorkflowInfo, IWorkflowState, InvalidTransitionError)
[7811]32from waeup.kofa.applicants.interfaces import (
[7363]33    IApplicant, IApplicantEdit, IApplicantsRoot,
[7683]34    IApplicantsContainer, IApplicantsContainerAdd,
[8033]35    MAX_UPLOAD_SIZE, IApplicantOnlinePayment, IApplicantsUtils,
[8037]36    IApplicantRegisterUpdate
[7363]37    )
[7811]38from waeup.kofa.applicants.workflow import INITIALIZED, STARTED, PAID, SUBMITTED
39from waeup.kofa.browser import (
[7819]40    KofaPage, KofaEditFormPage, KofaAddFormPage, KofaDisplayFormPage,
[7363]41    DEFAULT_PASSPORT_IMAGE_PATH)
[7811]42from waeup.kofa.browser.interfaces import ICaptchaManager
43from waeup.kofa.browser.breadcrumbs import Breadcrumb
44from waeup.kofa.browser.layout import (
[7459]45    NullValidator, jsaction, action, UtilityView)
[7811]46from waeup.kofa.browser.pages import add_local_role, del_local_roles
47from waeup.kofa.browser.resources import datepicker, tabs, datatable, warning
48from waeup.kofa.interfaces import (
[7819]49    IKofaObject, ILocalRolesAssignable, IExtFileStore, IPDF,
50    IFileStoreNameChooser, IPasswordValidator, IUserAccount, IKofaUtils)
[7811]51from waeup.kofa.interfaces import MessageFactory as _
52from waeup.kofa.permissions import get_users_with_local_roles
53from waeup.kofa.students.interfaces import IStudentsUtils
[8186]54from waeup.kofa.utils.helpers import string_from_bytes, file_size, now
[8170]55from waeup.kofa.widgets.datewidget import (
56    FriendlyDateDisplayWidget, FriendlyDateDisplayWidget,
57    FriendlyDatetimeDisplayWidget)
[7811]58from waeup.kofa.widgets.restwidget import ReSTDisplayWidget
[5320]59
[7819]60grok.context(IKofaObject) # Make IKofaObject the default context
[5273]61
[7819]62class ApplicantsRootPage(KofaPage):
[5822]63    grok.context(IApplicantsRoot)
64    grok.name('index')
[6153]65    grok.require('waeup.Public')
[7710]66    label = _('Application Section')
[5843]67    pnav = 3
[6012]68
69    def update(self):
[6067]70        super(ApplicantsRootPage, self).update()
[7493]71        #datatable.need()
[6012]72        return
73
[7819]74class ApplicantsRootManageFormPage(KofaEditFormPage):
[5828]75    grok.context(IApplicantsRoot)
76    grok.name('manage')
[6107]77    grok.template('applicantsrootmanagepage')
[7710]78    label = _('Manage application section')
[5843]79    pnav = 3
[7136]80    grok.require('waeup.manageApplication')
[7710]81    taboneactions = [_('Add applicants container'), _('Remove selected'),
82        _('Cancel')]
83    tabtwoactions1 = [_('Remove selected local roles')]
84    tabtwoactions2 = [_('Add local role')]
85    subunits = _('Applicants Containers')
[6078]86
[6069]87    def update(self):
88        tabs.need()
[6108]89        datatable.need()
[7330]90        warning.need()
[6069]91        return super(ApplicantsRootManageFormPage, self).update()
[5828]92
[6184]93    def getLocalRoles(self):
94        roles = ILocalRolesAssignable(self.context)
95        return roles()
96
97    def getUsers(self):
98        """Get a list of all users.
99        """
100        for key, val in grok.getSite()['users'].items():
101            url = self.url(val)
102            yield(dict(url=url, name=key, val=val))
103
104    def getUsersWithLocalRoles(self):
105        return get_users_with_local_roles(self.context)
106
[7710]107    @jsaction(_('Remove selected'))
[6069]108    def delApplicantsContainers(self, **data):
109        form = self.request.form
110        child_id = form['val_id']
111        if not isinstance(child_id, list):
112            child_id = [child_id]
113        deleted = []
114        for id in child_id:
115            try:
116                del self.context[id]
117                deleted.append(id)
118            except:
[7710]119                self.flash(_('Could not delete:') + ' %s: %s: %s' % (
[6069]120                        id, sys.exc_info()[0], sys.exc_info()[1]))
121        if len(deleted):
[7738]122            self.flash(_('Successfully removed: ${a}',
123                mapping = {'a':', '.join(deleted)}))
[7484]124        self.redirect(self.url(self.context, '@@manage'))
[6078]125        return
[5828]126
[7710]127    @action(_('Add applicants container'), validator=NullValidator)
[6069]128    def addApplicantsContainer(self, **data):
129        self.redirect(self.url(self.context, '@@add'))
[6078]130        return
131
[7710]132    @action(_('Cancel'), validator=NullValidator)
[6069]133    def cancel(self, **data):
134        self.redirect(self.url(self.context))
[6078]135        return
136
[7710]137    @action(_('Add local role'), validator=NullValidator)
[6184]138    def addLocalRole(self, **data):
[7484]139        return add_local_role(self,3, **data)
[6184]140
[7710]141    @action(_('Remove selected local roles'))
[6184]142    def delLocalRoles(self, **data):
[7484]143        return del_local_roles(self,3,**data)
[6184]144
[7819]145class ApplicantsContainerAddFormPage(KofaAddFormPage):
[5822]146    grok.context(IApplicantsRoot)
[7136]147    grok.require('waeup.manageApplication')
[5822]148    grok.name('add')
[6107]149    grok.template('applicantscontaineraddpage')
[7710]150    label = _('Add applicants container')
[5843]151    pnav = 3
[6078]152
[6103]153    form_fields = grok.AutoFields(
[7903]154        IApplicantsContainerAdd).omit('code').omit('title')
[6078]155
[6083]156    def update(self):
157        datepicker.need() # Enable jQuery datepicker in date fields.
158        return super(ApplicantsContainerAddFormPage, self).update()
159
[7710]160    @action(_('Add applicants container'))
[6069]161    def addApplicantsContainer(self, **data):
[6103]162        year = data['year']
163        code = u'%s%s' % (data['prefix'], year)
[7844]164        appcats_dict = getUtility(IApplicantsUtils).APP_TYPES_DICT
[7685]165        title = appcats_dict[data['prefix']][0]
166        title = u'%s %s/%s' % (title, year, year + 1)
[6087]167        if code in self.context.keys():
[6105]168            self.flash(
[7710]169                _('An applicants container for the same application type and entrance year exists already in the database.'))
[5822]170            return
171        # Add new applicants container...
[8009]172        container = createObject(u'waeup.ApplicantsContainer')
[6069]173        self.applyData(container, **data)
[6087]174        container.code = code
175        container.title = title
176        self.context[code] = container
[7710]177        self.flash(_('Added:') + ' "%s".' % code)
[7484]178        self.redirect(self.url(self.context, u'@@manage'))
[5822]179        return
[6078]180
[7710]181    @action(_('Cancel'), validator=NullValidator)
[6069]182    def cancel(self, **data):
[7484]183        self.redirect(self.url(self.context, '@@manage'))
[6078]184
[5845]185class ApplicantsRootBreadcrumb(Breadcrumb):
186    """A breadcrumb for applicantsroot.
187    """
188    grok.context(IApplicantsRoot)
[7710]189    title = _(u'Applicants')
[6078]190
[5845]191class ApplicantsContainerBreadcrumb(Breadcrumb):
192    """A breadcrumb for applicantscontainers.
193    """
194    grok.context(IApplicantsContainer)
[6319]195
[6153]196class ApplicantBreadcrumb(Breadcrumb):
197    """A breadcrumb for applicants.
198    """
199    grok.context(IApplicant)
[6319]200
[6153]201    @property
202    def title(self):
203        """Get a title for a context.
204        """
[7240]205        return self.context.application_number
[5828]206
[7250]207class OnlinePaymentBreadcrumb(Breadcrumb):
208    """A breadcrumb for payments.
209    """
210    grok.context(IApplicantOnlinePayment)
211
212    @property
213    def title(self):
214        return self.context.p_id
215
[7819]216class ApplicantsContainerPage(KofaDisplayFormPage):
[5830]217    """The standard view for regular applicant containers.
218    """
219    grok.context(IApplicantsContainer)
220    grok.name('index')
[6153]221    grok.require('waeup.Public')
[6029]222    grok.template('applicantscontainerpage')
[5850]223    pnav = 3
[6053]224
[8128]225    form_fields = grok.AutoFields(IApplicantsContainer).omit('title')
[6084]226    form_fields['description'].custom_widget = ReSTDisplayWidget
[6053]227
[5837]228    @property
[7708]229    def introduction(self):
[7833]230        # Here we know that the cookie has been set
231        lang = self.request.cookies.get('kofa.language')
[7708]232        html = self.context.description_dict.get(lang,'')
233        if html =='':
[7833]234            portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
[7708]235            html = self.context.description_dict.get(portal_language,'')
236        if html =='':
237            return ''
238        else:
239            return html
240
241    @property
[7467]242    def label(self):
[7493]243        return "%s" % self.context.title
[5837]244
[7819]245class ApplicantsContainerManageFormPage(KofaEditFormPage):
[5837]246    grok.context(IApplicantsContainer)
[5850]247    grok.name('manage')
[6107]248    grok.template('applicantscontainermanagepage')
[7903]249    form_fields = grok.AutoFields(IApplicantsContainer).omit('title')
[7710]250    taboneactions = [_('Save'),_('Cancel')]
251    tabtwoactions = [_('Add applicant'), _('Remove selected'),_('Cancel')]
252    tabthreeactions1 = [_('Remove selected local roles')]
253    tabthreeactions2 = [_('Add local role')]
[5844]254    # Use friendlier date widget...
[7136]255    grok.require('waeup.manageApplication')
[5850]256
257    @property
258    def label(self):
[7710]259        return _('Manage applicants container')
[5850]260
[5845]261    pnav = 3
[5837]262
263    def update(self):
[5850]264        datepicker.need() # Enable jQuery datepicker in date fields.
[5982]265        tabs.need()
[7484]266        self.tab1 = self.tab2 = self.tab3 = ''
267        qs = self.request.get('QUERY_STRING', '')
268        if not qs:
269            qs = 'tab1'
270        setattr(self, qs, 'active')
[7330]271        warning.need()
[6015]272        datatable.need()  # Enable jQurey datatables for contents listing
[6107]273        return super(ApplicantsContainerManageFormPage, self).update()
[5837]274
[6184]275    def getLocalRoles(self):
276        roles = ILocalRolesAssignable(self.context)
277        return roles()
278
279    def getUsers(self):
280        """Get a list of all users.
281        """
282        for key, val in grok.getSite()['users'].items():
283            url = self.url(val)
284            yield(dict(url=url, name=key, val=val))
285
286    def getUsersWithLocalRoles(self):
287        return get_users_with_local_roles(self.context)
288
[7708]289    def _description(self):
290        view = ApplicantsContainerPage(
291            self.context,self.request)
292        view.setUpWidgets()
293        return view.widgets['description']()
294
[7714]295    @action(_('Save'), style='primary')
[7489]296    def save(self, **data):
[5837]297        self.applyData(self.context, **data)
[7708]298        self.context.description_dict = self._description()
[7710]299        self.flash(_('Form has been saved.'))
[5837]300        return
[6078]301
[7710]302    @jsaction(_('Remove selected'))
[6105]303    def delApplicant(self, **data):
[6189]304        form = self.request.form
305        if form.has_key('val_id'):
306            child_id = form['val_id']
307        else:
[7710]308            self.flash(_('No applicant selected!'))
[7484]309            self.redirect(self.url(self.context, '@@manage')+'?tab2')
[6189]310            return
311        if not isinstance(child_id, list):
312            child_id = [child_id]
313        deleted = []
314        for id in child_id:
315            try:
316                del self.context[id]
317                deleted.append(id)
318            except:
[7710]319                self.flash(_('Could not delete:') + ' %s: %s: %s' % (
[6189]320                        id, sys.exc_info()[0], sys.exc_info()[1]))
321        if len(deleted):
[7741]322            self.flash(_('Successfully removed: ${a}',
[7738]323                mapping = {'a':', '.join(deleted)}))
[7484]324        self.redirect(self.url(self.context, u'@@manage')+'?tab2')
[6189]325        return
[6105]326
[7710]327    @action(_('Add applicant'), validator=NullValidator)
[6105]328    def addApplicant(self, **data):
[6327]329        self.redirect(self.url(self.context, 'addapplicant'))
330        return
[6105]331
[7710]332    @action(_('Cancel'), validator=NullValidator)
[5837]333    def cancel(self, **data):
334        self.redirect(self.url(self.context))
335        return
[5886]336
[7710]337    @action(_('Add local role'), validator=NullValidator)
[6184]338    def addLocalRole(self, **data):
339        return add_local_role(self,3, **data)
[6105]340
[7710]341    @action(_('Remove selected local roles'))
[6184]342    def delLocalRoles(self, **data):
343        return del_local_roles(self,3,**data)
344
[7819]345class ApplicantAddFormPage(KofaAddFormPage):
[6622]346    """Add-form to add an applicant.
[6327]347    """
348    grok.context(IApplicantsContainer)
[7136]349    grok.require('waeup.manageApplication')
[6327]350    grok.name('addapplicant')
[7240]351    #grok.template('applicantaddpage')
352    form_fields = grok.AutoFields(IApplicant).select(
[7356]353        'firstname', 'middlename', 'lastname',
[7240]354        'email', 'phone')
[7714]355    label = _('Add applicant')
[6327]356    pnav = 3
357
[7714]358    @action(_('Create application record'))
[6327]359    def addApplicant(self, **data):
[8008]360        applicant = createObject(u'waeup.Applicant')
[7240]361        self.applyData(applicant, **data)
362        self.context.addApplicant(applicant)
[7714]363        self.flash(_('Applicant record created.'))
[7363]364        self.redirect(
365            self.url(self.context[applicant.application_number], 'index'))
[6327]366        return
367
[7819]368class ApplicantDisplayFormPage(KofaDisplayFormPage):
[8014]369    """A display view for applicant data.
370    """
[5273]371    grok.context(IApplicant)
372    grok.name('index')
[7113]373    grok.require('waeup.viewApplication')
[7200]374    grok.template('applicantdisplaypage')
[6320]375    form_fields = grok.AutoFields(IApplicant).omit(
[7347]376        'locked', 'course_admitted', 'password')
[7714]377    label = _('Applicant')
[5843]378    pnav = 3
[5273]379
[8046]380    @property
381    def separators(self):
382        return getUtility(IApplicantsUtils).SEPARATORS_DICT
383
[7063]384    def update(self):
385        self.passport_url = self.url(self.context, 'passport.jpg')
[7240]386        # Mark application as started if applicant logs in for the first time
[7272]387        usertype = getattr(self.request.principal, 'user_type', None)
388        if usertype == 'applicant' and \
389            IWorkflowState(self.context).getState() == INITIALIZED:
[7240]390            IWorkflowInfo(self.context).fireTransition('start')
[7063]391        return
392
[6196]393    @property
[7240]394    def hasPassword(self):
395        if self.context.password:
[7714]396            return _('set')
397        return _('unset')
[7240]398
399    @property
[6196]400    def label(self):
401        container_title = self.context.__parent__.title
[8096]402        return _('${a} <br /> Application Record ${b}', mapping = {
[7714]403            'a':container_title, 'b':self.context.application_number})
[6196]404
[7347]405    def getCourseAdmitted(self):
406        """Return link, title and code in html format to the certificate
407           admitted.
408        """
409        course_admitted = self.context.course_admitted
[7351]410        if getattr(course_admitted, '__parent__',None):
[7347]411            url = self.url(course_admitted)
412            title = course_admitted.title
413            code = course_admitted.code
414            return '<a href="%s">%s - %s</a>' %(url,code,title)
415        return ''
[6254]416
[7259]417class ApplicantBaseDisplayFormPage(ApplicantDisplayFormPage):
418    grok.context(IApplicant)
419    grok.name('base')
420    form_fields = grok.AutoFields(IApplicant).select(
421        'applicant_id', 'firstname', 'lastname','email', 'course1')
422
[7459]423class CreateStudentPage(UtilityView, grok.View):
[7341]424    """Create a student object from applicatnt data
425    and copy applicant object.
426    """
427    grok.context(IApplicant)
428    grok.name('createstudent')
429    grok.require('waeup.manageStudent')
430
431    def update(self):
432        msg = self.context.createStudent()[1]
433        self.flash(msg)
434        self.redirect(self.url(self.context))
435        return
436
437    def render(self):
438        return
439
[7459]440class AcceptanceFeePaymentAddPage(UtilityView, grok.View):
[7250]441    """ Page to add an online payment ticket
442    """
443    grok.context(IApplicant)
444    grok.name('addafp')
445    grok.require('waeup.payApplicant')
446
447    def update(self):
448        p_category = 'acceptance'
449        session = str(self.context.__parent__.year)
450        try:
451            academic_session = grok.getSite()['configuration'][session]
452        except KeyError:
[7714]453            self.flash(_('Session configuration object is not available.'))
[7250]454            return
455        timestamp = "%d" % int(time()*1000)
456        for key in self.context.keys():
457            ticket = self.context[key]
458            if ticket.p_state == 'paid':
459                  self.flash(
[7714]460                      _('This type of payment has already been made.'))
[7250]461                  self.redirect(self.url(self.context))
462                  return
463        payment = createObject(u'waeup.ApplicantOnlinePayment')
464        payment.p_id = "p%s" % timestamp
465        payment.p_item = self.context.__parent__.title
466        payment.p_year = self.context.__parent__.year
467        payment.p_category = p_category
468        payment.amount_auth = academic_session.acceptance_fee
469        payment.surcharge_1 = academic_session.surcharge_1
470        payment.surcharge_2 = academic_session.surcharge_2
471        payment.surcharge_3 = academic_session.surcharge_3
472        self.context[payment.p_id] = payment
[7714]473        self.flash(_('Payment ticket created.'))
[7250]474        return
475
476    def render(self):
477        usertype = getattr(self.request.principal, 'user_type', None)
478        if usertype == 'applicant':
479            self.redirect(self.url(self.context, '@@edit'))
480            return
481        self.redirect(self.url(self.context, '@@manage'))
482        return
483
484
[7819]485class OnlinePaymentDisplayFormPage(KofaDisplayFormPage):
[7250]486    """ Page to view an online payment ticket
487    """
488    grok.context(IApplicantOnlinePayment)
489    grok.name('index')
490    grok.require('waeup.viewApplication')
491    form_fields = grok.AutoFields(IApplicantOnlinePayment)
[8170]492    form_fields[
493        'creation_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
494    form_fields[
495        'payment_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
[7250]496    pnav = 3
497
498    @property
499    def label(self):
[7714]500        return _('${a}: Online Payment Ticket ${b}', mapping = {
[8170]501            'a':self.context.__parent__.display_fullname,
502            'b':self.context.p_id})
[7250]503
[7459]504class OnlinePaymentCallbackPage(UtilityView, grok.View):
[7250]505    """ Callback view
506    """
507    grok.context(IApplicantOnlinePayment)
[7997]508    grok.name('simulate_callback')
[7250]509    grok.require('waeup.payApplicant')
510
511    # This update method simulates a valid callback und must be
[7997]512    # neutralized in the customization package.
[7250]513    def update(self):
[7322]514        self.wf_info = IWorkflowInfo(self.context.__parent__)
515        try:
516            self.wf_info.fireTransition('pay')
517        except InvalidTransitionError:
518            self.flash('Error: %s' % sys.exc_info()[1])
[7250]519            return
520        self.context.r_amount_approved = self.context.amount_auth
521        self.context.r_card_num = u'0000'
522        self.context.r_code = u'00'
523        self.context.p_state = 'paid'
[8194]524        self.context.payment_date = datetime.utcnow()
[7811]525        ob_class = self.__implemented__.__name__.replace('waeup.kofa.','')
[7250]526        self.context.__parent__.loggerInfo(
527            ob_class, 'valid callback: %s' % self.context.p_id)
[7714]528        self.flash(_('Valid callback received.'))
[7250]529        return
530
531    def render(self):
532        self.redirect(self.url(self.context, '@@index'))
533        return
534
[7459]535class ExportPDFPaymentSlipPage(UtilityView, grok.View):
[7250]536    """Deliver a PDF slip of the context.
537    """
538    grok.context(IApplicantOnlinePayment)
539    grok.name('payment_receipt.pdf')
540    grok.require('waeup.viewApplication')
541    form_fields = grok.AutoFields(IApplicantOnlinePayment)
[8173]542    form_fields['creation_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
543    form_fields['payment_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
[7250]544    prefix = 'form'
545
546    @property
[7714]547    def title(self):
[7819]548        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
[7811]549        return translate(_('Payment Data'), 'waeup.kofa',
[7714]550            target_language=portal_language)
551
552    @property
[7250]553    def label(self):
[7819]554        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
[7714]555        return translate(_('Online Payment Receipt'),
[7811]556            'waeup.kofa', target_language=portal_language) \
[7714]557            + ' %s' % self.context.p_id
[7250]558
559    def render(self):
560        if self.context.p_state != 'paid':
[7714]561            self.flash(_('Ticket not yet paid.'))
[7250]562            self.redirect(self.url(self.context))
563            return
[7259]564        applicantview = ApplicantBaseDisplayFormPage(self.context.__parent__,
[7250]565            self.request)
566        students_utils = getUtility(IStudentsUtils)
[7318]567        return students_utils.renderPDF(self,'payment_receipt.pdf',
[7250]568            self.context.__parent__, applicantview)
569
[7459]570class ExportPDFPage(UtilityView, grok.View):
[6358]571    """Deliver a PDF slip of the context.
572    """
573    grok.context(IApplicant)
574    grok.name('application_slip.pdf')
[7136]575    grok.require('waeup.viewApplication')
[6358]576    prefix = 'form'
577
578    def render(self):
[7392]579        pdfstream = getAdapter(self.context, IPDF, name='application_slip')(
580            view=self)
[6358]581        self.response.setHeader(
582            'Content-Type', 'application/pdf')
[7392]583        return pdfstream
[6358]584
[7081]585def handle_img_upload(upload, context, view):
[7063]586    """Handle upload of applicant image.
[7081]587
588    Returns `True` in case of success or `False`.
589
590    Please note that file pointer passed in (`upload`) most probably
591    points to end of file when leaving this function.
[7063]592    """
[7081]593    size = file_size(upload)
594    if size > MAX_UPLOAD_SIZE:
[7714]595        view.flash(_('Uploaded image is too big!'))
[7081]596        return False
[7247]597    dummy, ext = os.path.splitext(upload.filename)
598    ext.lower()
599    if ext != '.jpg':
[7714]600        view.flash(_('jpg file extension expected.'))
[7247]601        return False
[7081]602    upload.seek(0) # file pointer moved when determining size
[7063]603    store = getUtility(IExtFileStore)
604    file_id = IFileStoreNameChooser(context).chooseName()
605    store.createFile(file_id, upload)
[7081]606    return True
[7063]607
[7819]608class ApplicantManageFormPage(KofaEditFormPage):
[6196]609    """A full edit view for applicant data.
610    """
611    grok.context(IApplicant)
[7200]612    grok.name('manage')
[7136]613    grok.require('waeup.manageApplication')
[6476]614    form_fields = grok.AutoFields(IApplicant)
[7351]615    form_fields['student_id'].for_display = True
[7378]616    form_fields['applicant_id'].for_display = True
[7200]617    grok.template('applicanteditpage')
[6322]618    manage_applications = True
[6196]619    pnav = 3
[7714]620    display_actions = [[_('Save'), _('Final Submit')],
621        [_('Add online payment ticket'),_('Remove selected tickets')]]
[6196]622
[8046]623    @property
624    def separators(self):
625        return getUtility(IApplicantsUtils).SEPARATORS_DICT
626
[6196]627    def update(self):
628        datepicker.need() # Enable jQuery datepicker in date fields.
[7330]629        warning.need()
[7200]630        super(ApplicantManageFormPage, self).update()
[6353]631        self.wf_info = IWorkflowInfo(self.context)
[7081]632        self.max_upload_size = string_from_bytes(MAX_UPLOAD_SIZE)
[7084]633        self.passport_changed = None
[6598]634        upload = self.request.form.get('form.passport', None)
635        if upload:
636            # We got a fresh upload
[7084]637            self.passport_changed = handle_img_upload(
638                upload, self.context, self)
[6196]639        return
640
641    @property
642    def label(self):
643        container_title = self.context.__parent__.title
[8096]644        return _('${a} <br /> Application Form ${b}', mapping = {
[7714]645            'a':container_title, 'b':self.context.application_number})
[6196]646
[6303]647    def getTransitions(self):
[6351]648        """Return a list of dicts of allowed transition ids and titles.
[6353]649
650        Each list entry provides keys ``name`` and ``title`` for
651        internal name and (human readable) title of a single
652        transition.
[6349]653        """
[6353]654        allowed_transitions = self.wf_info.getManualTransitions()
[7687]655        return [dict(name='', title=_('No transition'))] +[
[6355]656            dict(name=x, title=y) for x, y in allowed_transitions]
[6303]657
[7714]658    @action(_('Save'), style='primary')
[6196]659    def save(self, **data):
[7240]660        form = self.request.form
661        password = form.get('password', None)
662        password_ctl = form.get('control_password', None)
663        if password:
664            validator = getUtility(IPasswordValidator)
665            errors = validator.validate_password(password, password_ctl)
666            if errors:
667                self.flash( ' '.join(errors))
668                return
[7084]669        if self.passport_changed is False:  # False is not None!
670            return # error during image upload. Ignore other values
[6475]671        changed_fields = self.applyData(self.context, **data)
[7199]672        # Turn list of lists into single list
673        if changed_fields:
674            changed_fields = reduce(lambda x,y: x+y, changed_fields.values())
[7240]675        else:
676            changed_fields = []
677        if self.passport_changed:
678            changed_fields.append('passport')
679        if password:
680            # Now we know that the form has no errors and can set password ...
681            IUserAccount(self.context).setPassword(password)
682            changed_fields.append('password')
[7199]683        fields_string = ' + '.join(changed_fields)
[7085]684        trans_id = form.get('transition', None)
685        if trans_id:
686            self.wf_info.fireTransition(trans_id)
[7714]687        self.flash(_('Form has been saved.'))
[7811]688        ob_class = self.__implemented__.__name__.replace('waeup.kofa.','')
[6644]689        if fields_string:
690            self.context.loggerInfo(ob_class, 'saved: % s' % fields_string)
[6196]691        return
692
[7250]693    def unremovable(self, ticket):
[7330]694        return False
[7250]695
696    # This method is also used by the ApplicantEditFormPage
697    def delPaymentTickets(self, **data):
698        form = self.request.form
699        if form.has_key('val_id'):
700            child_id = form['val_id']
701        else:
[7714]702            self.flash(_('No payment selected.'))
[7250]703            self.redirect(self.url(self.context))
704            return
705        if not isinstance(child_id, list):
706            child_id = [child_id]
707        deleted = []
708        for id in child_id:
709            # Applicants are not allowed to remove used payment tickets
710            if not self.unremovable(self.context[id]):
711                try:
712                    del self.context[id]
713                    deleted.append(id)
714                except:
[7714]715                    self.flash(_('Could not delete:') + ' %s: %s: %s' % (
[7250]716                            id, sys.exc_info()[0], sys.exc_info()[1]))
717        if len(deleted):
[7741]718            self.flash(_('Successfully removed: ${a}',
[7738]719                mapping = {'a':', '.join(deleted)}))
[7811]720            ob_class = self.__implemented__.__name__.replace('waeup.kofa.','')
[7363]721            self.context.loggerInfo(
722                ob_class, 'removed: % s' % ', '.join(deleted))
[7250]723        return
724
[7252]725    # We explicitely want the forms to be validated before payment tickets
726    # can be created. If no validation is requested, use
[7459]727    # 'validator=NullValidator' in the action directive
[7714]728    @action(_('Add online payment ticket'))
[7250]729    def addPaymentTicket(self, **data):
730        self.redirect(self.url(self.context, '@@addafp'))
[7252]731        return
[7250]732
[7714]733    @jsaction(_('Remove selected tickets'))
[7250]734    def removePaymentTickets(self, **data):
735        self.delPaymentTickets(**data)
736        self.redirect(self.url(self.context) + '/@@manage')
737        return
738
[7200]739class ApplicantEditFormPage(ApplicantManageFormPage):
[5982]740    """An applicant-centered edit view for applicant data.
741    """
[6196]742    grok.context(IApplicantEdit)
[5273]743    grok.name('edit')
[6198]744    grok.require('waeup.handleApplication')
[6459]745    form_fields = grok.AutoFields(IApplicantEdit).omit(
[6476]746        'locked', 'course_admitted', 'student_id',
[8039]747        'screening_score',
[6459]748        )
[7459]749    form_fields['applicant_id'].for_display = True
[8039]750    form_fields['reg_number'].for_display = True
[7200]751    grok.template('applicanteditpage')
[6322]752    manage_applications = False
[5484]753
[7250]754    @property
755    def display_actions(self):
756        state = IWorkflowState(self.context).getState()
757        if state == INITIALIZED:
758            actions = [[],[]]
759        elif state == STARTED:
[7714]760            actions = [[_('Save')],
761                [_('Add online payment ticket'),_('Remove selected tickets')]]
[7250]762        elif state == PAID:
[7714]763            actions = [[_('Save'), _('Final Submit')],
764                [_('Remove selected tickets')]]
[7351]765        else:
[7250]766            actions = [[],[]]
767        return actions
768
[7330]769    def unremovable(self, ticket):
770        state = IWorkflowState(self.context).getState()
771        return ticket.r_code or state in (INITIALIZED, SUBMITTED)
772
[7145]773    def emit_lock_message(self):
[7714]774        self.flash(_('The requested form is locked (read-only).'))
[5941]775        self.redirect(self.url(self.context))
776        return
[6078]777
[5686]778    def update(self):
[5941]779        if self.context.locked:
[7145]780            self.emit_lock_message()
[5941]781            return
[7200]782        super(ApplicantEditFormPage, self).update()
[5686]783        return
[5952]784
[6196]785    def dataNotComplete(self):
[7252]786        store = getUtility(IExtFileStore)
787        if not store.getFileByContext(self.context, attr=u'passport.jpg'):
[7714]788            return _('No passport picture uploaded.')
[6322]789        if not self.request.form.get('confirm_passport', False):
[7714]790            return _('Passport picture confirmation box not ticked.')
[6196]791        return False
[5952]792
[7252]793    # We explicitely want the forms to be validated before payment tickets
794    # can be created. If no validation is requested, use
[7459]795    # 'validator=NullValidator' in the action directive
[7714]796    @action(_('Add online payment ticket'))
[7250]797    def addPaymentTicket(self, **data):
798        self.redirect(self.url(self.context, '@@addafp'))
[7252]799        return
[7250]800
[7714]801    @jsaction(_('Remove selected tickets'))
[7250]802    def removePaymentTickets(self, **data):
803        self.delPaymentTickets(**data)
804        self.redirect(self.url(self.context) + '/@@edit')
805        return
806
[7996]807    @action(_('Save'), style='primary')
[5273]808    def save(self, **data):
[7084]809        if self.passport_changed is False:  # False is not None!
810            return # error during image upload. Ignore other values
[5273]811        self.applyData(self.context, **data)
[6196]812        self.flash('Form has been saved.')
[5273]813        return
814
[7714]815    @action(_('Final Submit'))
[5484]816    def finalsubmit(self, **data):
[7084]817        if self.passport_changed is False:  # False is not None!
818            return # error during image upload. Ignore other values
[6196]819        if self.dataNotComplete():
820            self.flash(self.dataNotComplete())
[5941]821            return
[7252]822        self.applyData(self.context, **data)
[6303]823        state = IWorkflowState(self.context).getState()
[6322]824        # This shouldn't happen, but the application officer
825        # might have forgotten to lock the form after changing the state
[7250]826        if state != PAID:
[7714]827            self.flash(_('This form cannot be submitted. Wrong state!'))
[6303]828            return
829        IWorkflowInfo(self.context).fireTransition('submit')
[8194]830        self.context.application_date = datetime.utcnow()
[5941]831        self.context.locked = True
[7714]832        self.flash(_('Form has been submitted.'))
[6196]833        self.redirect(self.url(self.context))
[5273]834        return
[5941]835
[7063]836class PassportImage(grok.View):
837    """Renders the passport image for applicants.
838    """
839    grok.name('passport.jpg')
840    grok.context(IApplicant)
[7113]841    grok.require('waeup.viewApplication')
[7063]842
843    def render(self):
844        # A filename chooser turns a context into a filename suitable
845        # for file storage.
846        image = getUtility(IExtFileStore).getFileByContext(self.context)
847        self.response.setHeader(
848            'Content-Type', 'image/jpeg')
849        if image is None:
850            # show placeholder image
[7089]851            return open(DEFAULT_PASSPORT_IMAGE_PATH, 'rb').read()
[7063]852        return image
[7363]853
[7819]854class ApplicantRegistrationPage(KofaAddFormPage):
[7363]855    """Captcha'd registration page for applicants.
856    """
857    grok.context(IApplicantsContainer)
858    grok.name('register')
[7373]859    grok.require('waeup.Anonymous')
[7363]860    grok.template('applicantregister')
861
[7368]862    @property
[8033]863    def form_fields(self):
864        form_fields = None
[8128]865        if self.context.mode == 'update':
866            form_fields = grok.AutoFields(IApplicantRegisterUpdate).select(
867                'firstname','reg_number','email')
868        else: #if self.context.mode == 'create':
[8033]869            form_fields = grok.AutoFields(IApplicantEdit).select(
870                'firstname', 'middlename', 'lastname', 'email', 'phone')
871        return form_fields
872
873    @property
[7368]874    def label(self):
[8078]875        return _('Apply for ${a}',
[7714]876            mapping = {'a':self.context.title})
[7368]877
[7363]878    def update(self):
[7368]879        # Check if application has started ...
[8200]880        if not self.context.startdate or (
881            self.context.startdate > datetime.now(pytz.utc)):
[7714]882            self.flash(_('Application has not yet started.'))
[7368]883            self.redirect(self.url(self.context))
884            return
885        # ... or ended
[8200]886        if not self.context.enddate or (
887            self.context.enddate < datetime.now(pytz.utc)):
[7714]888            self.flash(_('Application has ended.'))
[7368]889            self.redirect(self.url(self.context))
890            return
891        # Handle captcha
[7363]892        self.captcha = getUtility(ICaptchaManager).getCaptcha()
893        self.captcha_result = self.captcha.verify(self.request)
894        self.captcha_code = self.captcha.display(self.captcha_result.error_code)
895        return
896
[7714]897    @action(_('Get login credentials'), style='primary')
[7363]898    def register(self, **data):
899        if not self.captcha_result.is_valid:
[8037]900            # Captcha will display error messages automatically.
[7363]901            # No need to flash something.
902            return
[8033]903        if self.context.mode == 'create':
904            # Add applicant
905            applicant = createObject(u'waeup.Applicant')
906            self.applyData(applicant, **data)
907            self.context.addApplicant(applicant)
[8042]908            applicant.reg_number = applicant.applicant_id
909            notify(grok.ObjectModifiedEvent(applicant))
[8033]910        elif self.context.mode == 'update':
911            # Update applicant
[8037]912            reg_number = data.get('reg_number','')
913            firstname = data.get('firstname','')
[8033]914            cat = getUtility(ICatalog, name='applicants_catalog')
915            results = list(
916                cat.searchResults(reg_number=(reg_number, reg_number)))
917            if results:
918                applicant = results[0]
[8042]919                if getattr(applicant,'firstname',None) is None:
[8037]920                    self.flash(_('An error occurred.'))
921                    return
922                elif applicant.firstname.lower() != firstname.lower():
[8042]923                    # Don't tell the truth here. Anonymous must not
924                    # know that a record was found and only the firstname
925                    # verification failed.
[8037]926                    self.flash(_('No application record found.'))
927                    return
[8042]928                elif applicant.password is not None:
929                    self.flash(_('Your password has already been set. '
930                                 'Please proceed to the login page.'))
931                    return
932                # Store email address but nothing else.
[8033]933                applicant.email = data['email']
[8042]934                notify(grok.ObjectModifiedEvent(applicant))
[8033]935            else:
[8042]936                # No record found, this is the truth.
[8033]937                self.flash(_('No application record found.'))
938                return
939        else:
[8042]940            # Does not happen but anyway ...
[8033]941            return
[7819]942        kofa_utils = getUtility(IKofaUtils)
[7811]943        password = kofa_utils.genPassword()
[7380]944        IUserAccount(applicant).setPassword(password)
[7365]945        # Send email with credentials
[7399]946        login_url = self.url(grok.getSite(), 'login')
[7714]947        msg = _('You have successfully been registered for the')
[7811]948        if kofa_utils.sendCredentials(IUserAccount(applicant),
[7407]949            password, login_url, msg):
[7380]950            self.redirect(self.url(self.context, 'registration_complete',
951                                   data = dict(email=applicant.email)))
952            return
953        else:
[7714]954            self.flash(_('Email could not been sent. Please retry later.'))
[7380]955        return
956
[7819]957class ApplicantRegistrationEmailSent(KofaPage):
[7380]958    """Landing page after successful registration.
959    """
960    grok.name('registration_complete')
961    grok.require('waeup.Public')
962    grok.template('applicantregemailsent')
[7714]963    label = _('Your registration was successful.')
[7380]964
965    def update(self, email=None):
966        self.email = email
967        return
Note: See TracBrowser for help on using the repository browser.