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

Last change on this file since 10650 was 10645, checked in by Henrik Bettermann, 11 years ago

Rename buttons.

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