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

Last change on this file since 10203 was 10105, checked in by Henrik Bettermann, 12 years ago

Make provision for uploading the referee's form. Rename form extension.

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