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

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

Credential upload is only for pg students. This requires a pg applicants container in tests.

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