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

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

Implement search page for applicants. Add fullname to applicants_catalog.
Plugins must be updated and /reindex?ctlg=applicants must be performed.

Tests will follow.

Rename ApplicantCatalog? to ApplicantsCatalog?. This does not affect persistent data.

Rename StudentIndexes? to StudentsCatalog?.

Add more localization.

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