source: main/waeup.imostate/src/waeup/imostate/applicants/browser.py @ 10366

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

Customize workflow. Tests will follow.

  • Property svn:keywords set to Id
File size: 10.8 KB
Line 
1## $Id: browser.py 10359 2013-06-23 06:06:24Z henrik $
2##
3## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
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.
8##
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.
13##
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##
18"""UI components for basic applicants and related components.
19"""
20import grok
21import os
22from zope.component import getUtility
23from waeup.kofa.interfaces import (
24    IExtFileStore, IFileStoreNameChooser)
25from zope.formlib.textwidgets import BytesDisplayWidget
26from hurry.workflow.interfaces import IWorkflowState
27from waeup.kofa.utils.helpers import string_from_bytes, file_size
28from waeup.imostate.applicants.interfaces import (
29    ICustomApplicant,
30    ICustomUGApplicant,
31    ICustomUGApplicantEdit,
32    )
33from waeup.imostate.interfaces import MessageFactory as _
34from kofacustom.nigeria.applicants.browser import (
35    NigeriaApplicantDisplayFormPage,
36    NigeriaApplicantManageFormPage,
37    NigeriaApplicantEditFormPage,
38    NigeriaPDFApplicationSlip)
39
40from waeup.imostate.applicants.workflow import STARTED
41
42MAX_FILE_UPLOAD_SIZE = 1024 * 500
43
44def handle_file_upload(upload, context, view, attr=None):
45    """Handle upload of applicant files.
46
47    Returns `True` in case of success or `False`.
48
49    Please note that file pointer passed in (`upload`) most probably
50    points to end of file when leaving this function.
51    """
52    size = file_size(upload)
53    if size > MAX_FILE_UPLOAD_SIZE:
54        view.flash(_('Uploaded file is too big!'))
55        return False
56    dummy, ext = os.path.splitext(upload.filename)
57    ext.lower()
58    if ext != '.pdf':
59        view.flash(_('pdf file extension expected.'))
60        return False
61    upload.seek(0) # file pointer moved when determining size
62    store = getUtility(IExtFileStore)
63    file_id = IFileStoreNameChooser(context).chooseName(attr=attr)
64    store.createFile(file_id, upload)
65    return True
66
67class CustomApplicantDisplayFormPage(NigeriaApplicantDisplayFormPage):
68    """A display view for applicant data.
69    """
70
71    grok.template('applicantdisplaypage')
72
73    @property
74    def file_links(self):
75        html = ''
76        pdf = getUtility(IExtFileStore).getFileByContext(
77            self.context, attr='extraform.pdf')
78        if pdf:
79            html += '<a href="extraform.pdf">Extra Applicant Information Form</a>, '
80        pdf = getUtility(IExtFileStore).getFileByContext(
81            self.context, attr='refereeform.pdf')
82        if pdf:
83            html += '<a href="refereeform.pdf">Referee\'s Form</a>, '
84        pdf = getUtility(IExtFileStore).getFileByContext(
85            self.context, attr='credentials.pdf')
86        if pdf:
87            html += '<a href="credentials.pdf">Credentials</a>'
88        return html
89
90    @property
91    def form_fields(self):
92        form_fields = grok.AutoFields(ICustomUGApplicant)
93        if form_fields.get('perm_address', None):
94            form_fields['perm_address'].custom_widget = BytesDisplayWidget
95        form_fields['notice'].custom_widget = BytesDisplayWidget
96        if not getattr(self.context, 'student_id'):
97            form_fields = form_fields.omit('student_id')
98        if not getattr(self.context, 'screening_score'):
99            form_fields = form_fields.omit('screening_score')
100        if not getattr(self.context, 'screening_venue'):
101            form_fields = form_fields.omit('screening_venue')
102        if not getattr(self.context, 'screening_date'):
103            form_fields = form_fields.omit('screening_date')
104        return form_fields
105
106    def update(self):
107        super(CustomApplicantDisplayFormPage, self).update()
108        self.extraform_url = self.url(self.context, 'extraform.pdf')
109        self.referreeform_url = self.url(self.context, 'refereeform.pdf')
110        self.credentials_url = self.url(self.context, 'credentials.pdf')
111        return
112
113class CustomPDFApplicationSlip(NigeriaPDFApplicationSlip):
114
115    @property
116    def form_fields(self):
117        form_fields = grok.AutoFields(ICustomUGApplicant)
118        if not getattr(self.context, 'student_id'):
119            form_fields = form_fields.omit('student_id')
120        if not getattr(self.context, 'screening_score'):
121            form_fields = form_fields.omit('screening_score')
122        if not getattr(self.context, 'screening_venue'):
123            form_fields = form_fields.omit('screening_venue')
124        if not getattr(self.context, 'screening_date'):
125            form_fields = form_fields.omit('screening_date')
126        return form_fields
127
128class CustomApplicantManageFormPage(NigeriaApplicantManageFormPage):
129    """A full edit view for applicant data.
130    """
131    grok.template('applicanteditpage')
132
133    @property
134    def form_fields(self):
135        form_fields = grok.AutoFields(ICustomUGApplicant)
136        form_fields['student_id'].for_display = True
137        form_fields['applicant_id'].for_display = True
138        return form_fields
139
140    def update(self):
141        super(CustomApplicantManageFormPage, self).update()
142        upload_extraform = self.request.form.get('form.extraform', None)
143        if upload_extraform:
144            # We got a fresh extraform upload
145            success = handle_file_upload(
146                upload_extraform, self.context, self, attr='extraform.pdf')
147            if success:
148                self.context.writeLogMessage(self, 'saved: extraform')
149            else:
150                self.upload_success = False
151        upload_refereeform = self.request.form.get('form.refereeform', None)
152        if upload_refereeform:
153            # We got a fresh refereeform upload
154            success = handle_file_upload(
155                upload_refereeform, self.context, self, attr='refereeform.pdf')
156            if success:
157                self.context.writeLogMessage(self, 'saved: refereeform')
158            else:
159                self.upload_success = False
160        upload_credentials = self.request.form.get('form.credentials', None)
161        if upload_credentials:
162            # We got a fresh credentials upload
163            success = handle_file_upload(
164                upload_credentials, self.context, self, attr='credentials.pdf')
165            if success:
166                self.context.writeLogMessage(self, 'saved: credentials')
167            else:
168                self.upload_success = False
169        self.max_file_upload_size = string_from_bytes(MAX_FILE_UPLOAD_SIZE)
170        return
171
172class CustomApplicantEditFormPage(NigeriaApplicantEditFormPage):
173    """An applicant-centered edit view for applicant data.
174    """
175    grok.template('applicanteditpage')
176    submit_state = STARTED
177
178    @property
179    def form_fields(self):
180        form_fields = grok.AutoFields(ICustomUGApplicantEdit)
181        form_fields['applicant_id'].for_display = True
182        form_fields['reg_number'].for_display = True
183        return form_fields
184
185    @property
186    def display_actions(self):
187        state = IWorkflowState(self.context).getState()
188        actions = [[],[]]
189        if state == STARTED:
190            actions = [[_('Save'), _('Final Submit')], []]
191        return actions
192
193    def dataNotComplete(self):
194        store = getUtility(IExtFileStore)
195        if not store.getFileByContext(self.context, attr=u'passport.jpg'):
196            return _('No passport picture uploaded.')
197        if not store.getFileByContext(self.context, attr=u'extraform.pdf'):
198            return _('No extra information form pdf file uploaded.')
199        if not store.getFileByContext(self.context, attr=u'refereeform.pdf'):
200            return _('No referee form pdf file uploaded.')
201        if self.target is not None and self.target.startswith('pg') \
202            and not store.getFileByContext(self.context, attr=u'credentials.pdf'):
203            return _('No credentials pdf file uploaded.')
204        if not self.request.form.get('confirm_passport', False):
205            return _('Passport picture confirmation box not ticked.')
206        return False
207
208    def update(self):
209        if self.context.locked or (
210            self.context.__parent__.expired and
211            self.context.__parent__.strict_deadline):
212            self.emit_lock_message()
213            return
214        super(CustomApplicantEditFormPage, self).update()
215        upload_extraform = self.request.form.get('form.extraform', None)
216        if upload_extraform:
217            # We got a fresh extraform upload
218            success = handle_file_upload(
219                upload_extraform, self.context, self, attr='extraform.pdf')
220            if not success:
221                self.upload_success = False
222        upload_refereeform = self.request.form.get('form.refereeform', None)
223        if upload_refereeform:
224            # We got a fresh refereeform upload
225            success = handle_file_upload(
226                upload_refereeform, self.context, self, attr='refereeform.pdf')
227            if not success:
228                self.upload_success = False
229        upload_credentials = self.request.form.get('form.credentials', None)
230        if upload_credentials:
231            # We got a fresh credentials upload
232            success = handle_file_upload(
233                upload_credentials, self.context, self, attr='credentials.pdf')
234            if not success:
235                self.upload_success = False
236        self.max_file_upload_size = string_from_bytes(MAX_FILE_UPLOAD_SIZE)
237        return
238
239class ExtraForm(grok.View):
240    """Renders the pdf form extension for applicants.
241    """
242    grok.name('extraform.pdf')
243    grok.context(ICustomApplicant)
244    grok.require('waeup.viewApplication')
245
246    def render(self):
247        pdf = getUtility(IExtFileStore).getFileByContext(
248            self.context, attr='extraform.pdf')
249        self.response.setHeader('Content-Type', 'application/pdf')
250        return pdf
251
252class RefereeForm(grok.View):
253    """Renders the pdf referee's form for applicants.
254    """
255    grok.name('refereeform.pdf')
256    grok.context(ICustomApplicant)
257    grok.require('waeup.viewApplication')
258
259    def render(self):
260        pdf = getUtility(IExtFileStore).getFileByContext(
261            self.context, attr='refereeform.pdf')
262        self.response.setHeader('Content-Type', 'application/pdf')
263        return pdf
264
265class Credentials(grok.View):
266    """Renders the pdf credential's form for applicants.
267    """
268    grok.name('credentials.pdf')
269    grok.context(ICustomApplicant)
270    grok.require('waeup.viewApplication')
271
272    def render(self):
273        pdf = getUtility(IExtFileStore).getFileByContext(
274            self.context, attr='credentials.pdf')
275        self.response.setHeader('Content-Type', 'application/pdf')
276        return pdf
Note: See TracBrowser for help on using the repository browser.