source: main/waeup.sirp/trunk/src/waeup/sirp/applicants/browser.py @ 7475

Last change on this file since 7475 was 7467, checked in by Henrik Bettermann, 13 years ago

Page titles are deprecated. We only have labels.

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