source: main/waeup.futminna/trunk/src/waeup/futminna/applicants/browser.py @ 10222

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

Add credential form upload facilities.

  • Property svn:keywords set to Id
File size: 13.4 KB
RevLine 
[10088]1## $Id: browser.py 10220 2013-05-24 06:51:01Z 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
[10096]21import os
22from zope.component import getUtility
23from waeup.kofa.interfaces import (
24    IExtFileStore, IFileStoreNameChooser)
[10088]25from zope.formlib.textwidgets import BytesDisplayWidget
[10096]26from waeup.kofa.utils.helpers import string_from_bytes, file_size
[10088]27from waeup.futminna.applicants.interfaces import (
[10096]28    ICustomApplicant,
[10088]29    ICustomUGApplicant,
30    ICustomPGApplicant,
31    ICustomPGApplicantEdit,
32    ICustomUGApplicantEdit,
33    UG_OMIT_DISPLAY_FIELDS,
34    UG_OMIT_PDF_FIELDS,
35    UG_OMIT_MANAGE_FIELDS,
36    UG_OMIT_EDIT_FIELDS,
37    PG_OMIT_DISPLAY_FIELDS,
38    PG_OMIT_PDF_FIELDS,
39    PG_OMIT_MANAGE_FIELDS,
[10206]40    PUTME_OMIT_EDIT_FIELDS,
41    PUTME_OMIT_DISPLAY_FIELDS,
42    PUTME_OMIT_PDF_FIELDS,
43    PUTME_OMIT_MANAGE_FIELDS,
44    PUTME_OMIT_EDIT_FIELDS,
[10088]45    )
[10096]46from waeup.futminna.interfaces import MessageFactory as _
[10088]47from kofacustom.nigeria.applicants.browser import (
48    NigeriaApplicantDisplayFormPage,
49    NigeriaApplicantManageFormPage,
50    NigeriaApplicantEditFormPage,
51    NigeriaPDFApplicationSlip)
52
[10096]53MAX_FILE_UPLOAD_SIZE = 1024 * 500
[10088]54
[10096]55def handle_file_upload(upload, context, view, attr=None):
56    """Handle upload of applicant files.
57
58    Returns `True` in case of success or `False`.
59
60    Please note that file pointer passed in (`upload`) most probably
61    points to end of file when leaving this function.
62    """
63    size = file_size(upload)
64    if size > MAX_FILE_UPLOAD_SIZE:
65        view.flash(_('Uploaded file is too big!'))
66        return False
67    dummy, ext = os.path.splitext(upload.filename)
68    ext.lower()
69    if ext != '.pdf':
70        view.flash(_('pdf file extension expected.'))
71        return False
72    upload.seek(0) # file pointer moved when determining size
73    store = getUtility(IExtFileStore)
74    file_id = IFileStoreNameChooser(context).chooseName(attr=attr)
75    store.createFile(file_id, upload)
76    return True
77
[10088]78class CustomApplicantDisplayFormPage(NigeriaApplicantDisplayFormPage):
79    """A display view for applicant data.
80    """
81
82    grok.template('applicantdisplaypage')
83
84    @property
[10096]85    def file_links(self):
86        html = ''
87        pdf = getUtility(IExtFileStore).getFileByContext(
[10105]88            self.context, attr='extraform.pdf')
[10096]89        if pdf:
[10105]90            html += '<a href="extraform.pdf">Extra Applicant Information Form</a>, '
91        pdf = getUtility(IExtFileStore).getFileByContext(
92            self.context, attr='refereeform.pdf')
93        if pdf:
[10220]94            html += '<a href="refereeform.pdf">Referee\'s Form</a>, '
95        pdf = getUtility(IExtFileStore).getFileByContext(
96            self.context, attr='credentialform.pdf')
97        if pdf:
98            html += '<a href="credentialform.pdf">Credential Form</a>'
[10096]99        return html
100
101    @property
[10088]102    def form_fields(self):
103        target = getattr(self.context.__parent__, 'prefix', None)
104        if target is not None and target.startswith('pg'):
105            form_fields = grok.AutoFields(ICustomPGApplicant)
106            for field in PG_OMIT_DISPLAY_FIELDS:
107                form_fields = form_fields.omit(field)
108        else:
109            form_fields = grok.AutoFields(ICustomUGApplicant)
110            for field in UG_OMIT_DISPLAY_FIELDS:
111                form_fields = form_fields.omit(field)
[10096]112        if form_fields.get('perm_address', None):
113            form_fields['perm_address'].custom_widget = BytesDisplayWidget
[10088]114        form_fields['notice'].custom_widget = BytesDisplayWidget
115        if not getattr(self.context, 'student_id'):
116            form_fields = form_fields.omit('student_id')
117        if not getattr(self.context, 'screening_score'):
118            form_fields = form_fields.omit('screening_score')
119        if not getattr(self.context, 'screening_venue'):
120            form_fields = form_fields.omit('screening_venue')
121        if not getattr(self.context, 'screening_date'):
122            form_fields = form_fields.omit('screening_date')
[10206]123        if not self.context.low_jamb_score:
124            form_fields = form_fields.omit('course2')
[10088]125        return form_fields
126
[10096]127    def update(self):
128        super(CustomApplicantDisplayFormPage, self).update()
[10105]129        self.extraform_url = self.url(self.context, 'extraform.pdf')
130        self.referreeform_url = self.url(self.context, 'refereeform.pdf')
[10220]131        self.credentialform_url = self.url(self.context, 'credentialform.pdf')
[10096]132        return
133
[10088]134class CustomPDFApplicationSlip(NigeriaPDFApplicationSlip):
135
136    @property
137    def form_fields(self):
138        target = getattr(self.context.__parent__, 'prefix', None)
139        if target is not None and target.startswith('pg'):
140            form_fields = grok.AutoFields(ICustomPGApplicant)
141            for field in PG_OMIT_PDF_FIELDS:
142                form_fields = form_fields.omit(field)
[10206]143        elif target is not None and target.startswith('putme'):
144            form_fields = grok.AutoFields(ICustomUGApplicant)
145            if self._reduced_slip():
146                for field in PUTME_OMIT_RESULT_SLIP_FIELDS:
147                    form_fields = form_fields.omit(field)
[10088]148        else:
149            form_fields = grok.AutoFields(ICustomUGApplicant)
150            for field in UG_OMIT_PDF_FIELDS:
151                form_fields = form_fields.omit(field)
152        if not getattr(self.context, 'student_id'):
153            form_fields = form_fields.omit('student_id')
154        if not getattr(self.context, 'screening_score'):
155            form_fields = form_fields.omit('screening_score')
156        if not getattr(self.context, 'screening_venue'):
157            form_fields = form_fields.omit('screening_venue')
158        if not getattr(self.context, 'screening_date'):
159            form_fields = form_fields.omit('screening_date')
[10206]160        if not self.context.low_jamb_score:
161            form_fields = form_fields.omit('course2')
[10088]162        return form_fields
163
164class CustomApplicantManageFormPage(NigeriaApplicantManageFormPage):
165    """A full edit view for applicant data.
166    """
[10096]167    grok.template('applicanteditpage')
168
[10088]169    @property
170    def form_fields(self):
171        target = getattr(self.context.__parent__, 'prefix', None)
172        if target is not None and target.startswith('pg'):
173            form_fields = grok.AutoFields(ICustomPGApplicant)
174            for field in PG_OMIT_MANAGE_FIELDS:
175                form_fields = form_fields.omit(field)
[10206]176        elif target is not None and target.startswith('putme'):
177            form_fields = grok.AutoFields(ICustomUGApplicant)
178            for field in PUTME_OMIT_MANAGE_FIELDS:
179                form_fields = form_fields.omit(field)
[10088]180        else:
181            form_fields = grok.AutoFields(ICustomUGApplicant)
182            for field in UG_OMIT_MANAGE_FIELDS:
183                form_fields = form_fields.omit(field)
184        form_fields['student_id'].for_display = True
185        form_fields['applicant_id'].for_display = True
186        return form_fields
187
[10096]188    def update(self):
189        super(CustomApplicantManageFormPage, self).update()
[10105]190        upload_extraform = self.request.form.get('form.extraform', None)
191        if upload_extraform:
192            # We got a fresh extraform upload
193            success = handle_file_upload(
194                upload_extraform, self.context, self, attr='extraform.pdf')
195            if success:
196                self.context.writeLogMessage(self, 'saved: extraform')
197            else:
198                self.upload_success = False
199        upload_refereeform = self.request.form.get('form.refereeform', None)
200        if upload_refereeform:
201            # We got a fresh refereeform upload
202            success = handle_file_upload(
203                upload_refereeform, self.context, self, attr='refereeform.pdf')
204            if success:
205                self.context.writeLogMessage(self, 'saved: refereeform')
206            else:
207                self.upload_success = False
[10220]208        upload_credentialform = self.request.form.get('form.credentialform', None)
209        if upload_credentialform:
210            # We got a fresh credentialform upload
211            success = handle_file_upload(
212                upload_credentialform, self.context, self, attr='credentialform.pdf')
213            if success:
214                self.context.writeLogMessage(self, 'saved: credentialform')
215            else:
216                self.upload_success = False
[10096]217        self.max_file_upload_size = string_from_bytes(MAX_FILE_UPLOAD_SIZE)
218        return
219
[10088]220class CustomApplicantEditFormPage(NigeriaApplicantEditFormPage):
221    """An applicant-centered edit view for applicant data.
222    """
[10096]223    grok.template('applicanteditpage')
[10088]224
225    @property
226    def form_fields(self):
227        target = getattr(self.context.__parent__, 'prefix', None)
228        if target is not None and target.startswith('pg'):
229            form_fields = grok.AutoFields(ICustomPGApplicantEdit)
230            for field in PG_OMIT_EDIT_FIELDS:
231                form_fields = form_fields.omit(field)
[10206]232        elif target is not None and target.startswith('putme'):
233            form_fields = grok.AutoFields(ICustomUGApplicantEdit)
234            for field in PUTME_OMIT_EDIT_FIELDS:
235                form_fields = form_fields.omit(field)
[10088]236        else:
237            form_fields = grok.AutoFields(ICustomUGApplicantEdit)
238            for field in UG_OMIT_EDIT_FIELDS:
239                form_fields = form_fields.omit(field)
240        form_fields['applicant_id'].for_display = True
241        form_fields['reg_number'].for_display = True
[10206]242        if not self.context.low_jamb_score:
243            form_fields = form_fields.omit('course2')
[10088]244        return form_fields
[10096]245
246    def dataNotComplete(self):
247        store = getUtility(IExtFileStore)
248        if not store.getFileByContext(self.context, attr=u'passport.jpg'):
249            return _('No passport picture uploaded.')
[10105]250        if not store.getFileByContext(self.context, attr=u'extraform.pdf'):
251            return _('No extra information form pdf file uploaded.')
252        if not store.getFileByContext(self.context, attr=u'refereeform.pdf'):
253            return _('No referee form pdf file uploaded.')
[10220]254        if not store.getFileByContext(self.context, attr=u'credentialform.pdf'):
255            return _('No credential form pdf file uploaded.')
[10096]256        if not self.request.form.get('confirm_passport', False):
257            return _('Passport picture confirmation box not ticked.')
258        return False
259
260    def update(self):
261        if self.context.locked or (
262            self.context.__parent__.expired and
263            self.context.__parent__.strict_deadline):
264            self.emit_lock_message()
265            return
266        super(CustomApplicantEditFormPage, self).update()
[10105]267        upload_extraform = self.request.form.get('form.extraform', None)
268        if upload_extraform:
269            # We got a fresh extraform upload
270            success = handle_file_upload(
271                upload_extraform, self.context, self, attr='extraform.pdf')
272            if not success:
273                self.upload_success = False
274        upload_refereeform = self.request.form.get('form.refereeform', None)
275        if upload_refereeform:
276            # We got a fresh refereeform upload
277            success = handle_file_upload(
278                upload_refereeform, self.context, self, attr='refereeform.pdf')
279            if not success:
280                self.upload_success = False
[10220]281        upload_credentialform = self.request.form.get('form.credentialform', None)
282        if upload_credentialform:
283            # We got a fresh credentialform upload
284            success = handle_file_upload(
285                upload_credentialform, self.context, self, attr='credentialform.pdf')
286            if not success:
287                self.upload_success = False
[10096]288        self.max_file_upload_size = string_from_bytes(MAX_FILE_UPLOAD_SIZE)
289        return
290
[10105]291class ExtraForm(grok.View):
[10096]292    """Renders the pdf form extension for applicants.
293    """
[10105]294    grok.name('extraform.pdf')
[10096]295    grok.context(ICustomApplicant)
296    grok.require('waeup.viewApplication')
297
298    def render(self):
299        pdf = getUtility(IExtFileStore).getFileByContext(
[10105]300            self.context, attr='extraform.pdf')
[10096]301        self.response.setHeader('Content-Type', 'application/pdf')
[10105]302        return pdf
303
304class RefereeForm(grok.View):
305    """Renders the pdf referee's form for applicants.
306    """
307    grok.name('refereeform.pdf')
308    grok.context(ICustomApplicant)
309    grok.require('waeup.viewApplication')
310
311    def render(self):
312        pdf = getUtility(IExtFileStore).getFileByContext(
313            self.context, attr='refereeform.pdf')
314        self.response.setHeader('Content-Type', 'application/pdf')
[10220]315        return pdf
316
317class CredentialForm(grok.View):
318    """Renders the pdf credential's form for applicants.
319    """
320    grok.name('credentialform.pdf')
321    grok.context(ICustomApplicant)
322    grok.require('waeup.viewApplication')
323
324    def render(self):
325        pdf = getUtility(IExtFileStore).getFileByContext(
326            self.context, attr='credentialform.pdf')
327        self.response.setHeader('Content-Type', 'application/pdf')
[10096]328        return pdf
Note: See TracBrowser for help on using the repository browser.