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

Last change on this file since 8142 was 8128, checked in by uli, 13 years ago

Use one of the modes as fallback when displaying the registration view
for applicants. This way the view still works with older data.fs.

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