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

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

Introduce low_jamb_score property attribute. course2 is only editable if low_jamb_score is True.

Browser tests will follow.

  • Property svn:keywords set to Id
File size: 11.8 KB
Line 
1## $Id: browser.py 10206 2013-05-22 19:26:05Z 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    PUTME_OMIT_EDIT_FIELDS,
41    PUTME_OMIT_DISPLAY_FIELDS,
42    PUTME_OMIT_PDF_FIELDS,
43    PUTME_OMIT_MANAGE_FIELDS,
44    PUTME_OMIT_EDIT_FIELDS,
45    )
46from waeup.futminna.interfaces import MessageFactory as _
47from kofacustom.nigeria.applicants.browser import (
48    NigeriaApplicantDisplayFormPage,
49    NigeriaApplicantManageFormPage,
50    NigeriaApplicantEditFormPage,
51    NigeriaPDFApplicationSlip)
52
53MAX_FILE_UPLOAD_SIZE = 1024 * 500
54
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
78class CustomApplicantDisplayFormPage(NigeriaApplicantDisplayFormPage):
79    """A display view for applicant data.
80    """
81
82    grok.template('applicantdisplaypage')
83
84    @property
85    def file_links(self):
86        html = ''
87        pdf = getUtility(IExtFileStore).getFileByContext(
88            self.context, attr='extraform.pdf')
89        if pdf:
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:
94            html += '<a href="refereeform.pdf">Referee\'s Form</a>'
95        return html
96
97    @property
98    def form_fields(self):
99        target = getattr(self.context.__parent__, 'prefix', None)
100        if target is not None and target.startswith('pg'):
101            form_fields = grok.AutoFields(ICustomPGApplicant)
102            for field in PG_OMIT_DISPLAY_FIELDS:
103                form_fields = form_fields.omit(field)
104        else:
105            form_fields = grok.AutoFields(ICustomUGApplicant)
106            for field in UG_OMIT_DISPLAY_FIELDS:
107                form_fields = form_fields.omit(field)
108        if form_fields.get('perm_address', None):
109            form_fields['perm_address'].custom_widget = BytesDisplayWidget
110        form_fields['notice'].custom_widget = BytesDisplayWidget
111        if not getattr(self.context, 'student_id'):
112            form_fields = form_fields.omit('student_id')
113        if not getattr(self.context, 'screening_score'):
114            form_fields = form_fields.omit('screening_score')
115        if not getattr(self.context, 'screening_venue'):
116            form_fields = form_fields.omit('screening_venue')
117        if not getattr(self.context, 'screening_date'):
118            form_fields = form_fields.omit('screening_date')
119        if not self.context.low_jamb_score:
120            form_fields = form_fields.omit('course2')
121        return form_fields
122
123    def update(self):
124        super(CustomApplicantDisplayFormPage, self).update()
125        self.extraform_url = self.url(self.context, 'extraform.pdf')
126        self.referreeform_url = self.url(self.context, 'refereeform.pdf')
127        return
128
129class CustomPDFApplicationSlip(NigeriaPDFApplicationSlip):
130
131    @property
132    def form_fields(self):
133        target = getattr(self.context.__parent__, 'prefix', None)
134        if target is not None and target.startswith('pg'):
135            form_fields = grok.AutoFields(ICustomPGApplicant)
136            for field in PG_OMIT_PDF_FIELDS:
137                form_fields = form_fields.omit(field)
138        elif target is not None and target.startswith('putme'):
139            form_fields = grok.AutoFields(ICustomUGApplicant)
140            if self._reduced_slip():
141                for field in PUTME_OMIT_RESULT_SLIP_FIELDS:
142                    form_fields = form_fields.omit(field)
143        else:
144            form_fields = grok.AutoFields(ICustomUGApplicant)
145            for field in UG_OMIT_PDF_FIELDS:
146                form_fields = form_fields.omit(field)
147        if not getattr(self.context, 'student_id'):
148            form_fields = form_fields.omit('student_id')
149        if not getattr(self.context, 'screening_score'):
150            form_fields = form_fields.omit('screening_score')
151        if not getattr(self.context, 'screening_venue'):
152            form_fields = form_fields.omit('screening_venue')
153        if not getattr(self.context, 'screening_date'):
154            form_fields = form_fields.omit('screening_date')
155        if not self.context.low_jamb_score:
156            form_fields = form_fields.omit('course2')
157        return form_fields
158
159class CustomApplicantManageFormPage(NigeriaApplicantManageFormPage):
160    """A full edit view for applicant data.
161    """
162    grok.template('applicanteditpage')
163
164    @property
165    def form_fields(self):
166        target = getattr(self.context.__parent__, 'prefix', None)
167        if target is not None and target.startswith('pg'):
168            form_fields = grok.AutoFields(ICustomPGApplicant)
169            for field in PG_OMIT_MANAGE_FIELDS:
170                form_fields = form_fields.omit(field)
171        elif target is not None and target.startswith('putme'):
172            form_fields = grok.AutoFields(ICustomUGApplicant)
173            for field in PUTME_OMIT_MANAGE_FIELDS:
174                form_fields = form_fields.omit(field)
175        else:
176            form_fields = grok.AutoFields(ICustomUGApplicant)
177            for field in UG_OMIT_MANAGE_FIELDS:
178                form_fields = form_fields.omit(field)
179        form_fields['student_id'].for_display = True
180        form_fields['applicant_id'].for_display = True
181        return form_fields
182
183    def update(self):
184        super(CustomApplicantManageFormPage, self).update()
185        upload_extraform = self.request.form.get('form.extraform', None)
186        if upload_extraform:
187            # We got a fresh extraform upload
188            success = handle_file_upload(
189                upload_extraform, self.context, self, attr='extraform.pdf')
190            if success:
191                self.context.writeLogMessage(self, 'saved: extraform')
192            else:
193                self.upload_success = False
194        upload_refereeform = self.request.form.get('form.refereeform', None)
195        if upload_refereeform:
196            # We got a fresh refereeform upload
197            success = handle_file_upload(
198                upload_refereeform, self.context, self, attr='refereeform.pdf')
199            if success:
200                self.context.writeLogMessage(self, 'saved: refereeform')
201            else:
202                self.upload_success = False
203        self.max_file_upload_size = string_from_bytes(MAX_FILE_UPLOAD_SIZE)
204        return
205
206class CustomApplicantEditFormPage(NigeriaApplicantEditFormPage):
207    """An applicant-centered edit view for applicant data.
208    """
209    grok.template('applicanteditpage')
210
211    @property
212    def form_fields(self):
213        target = getattr(self.context.__parent__, 'prefix', None)
214        if target is not None and target.startswith('pg'):
215            form_fields = grok.AutoFields(ICustomPGApplicantEdit)
216            for field in PG_OMIT_EDIT_FIELDS:
217                form_fields = form_fields.omit(field)
218        elif target is not None and target.startswith('putme'):
219            form_fields = grok.AutoFields(ICustomUGApplicantEdit)
220            for field in PUTME_OMIT_EDIT_FIELDS:
221                form_fields = form_fields.omit(field)
222        else:
223            form_fields = grok.AutoFields(ICustomUGApplicantEdit)
224            for field in UG_OMIT_EDIT_FIELDS:
225                form_fields = form_fields.omit(field)
226        form_fields['applicant_id'].for_display = True
227        form_fields['reg_number'].for_display = True
228        if not self.context.low_jamb_score:
229            form_fields = form_fields.omit('course2')
230        return form_fields
231
232    def dataNotComplete(self):
233        store = getUtility(IExtFileStore)
234        if not store.getFileByContext(self.context, attr=u'passport.jpg'):
235            return _('No passport picture uploaded.')
236        if not store.getFileByContext(self.context, attr=u'extraform.pdf'):
237            return _('No extra information form pdf file uploaded.')
238        if not store.getFileByContext(self.context, attr=u'refereeform.pdf'):
239            return _('No referee form pdf file uploaded.')
240        if not self.request.form.get('confirm_passport', False):
241            return _('Passport picture confirmation box not ticked.')
242        return False
243
244    def update(self):
245        if self.context.locked or (
246            self.context.__parent__.expired and
247            self.context.__parent__.strict_deadline):
248            self.emit_lock_message()
249            return
250        super(CustomApplicantEditFormPage, self).update()
251        upload_extraform = self.request.form.get('form.extraform', None)
252        if upload_extraform:
253            # We got a fresh extraform upload
254            success = handle_file_upload(
255                upload_extraform, self.context, self, attr='extraform.pdf')
256            if not success:
257                self.upload_success = False
258        upload_refereeform = self.request.form.get('form.refereeform', None)
259        if upload_refereeform:
260            # We got a fresh refereeform upload
261            success = handle_file_upload(
262                upload_refereeform, self.context, self, attr='refereeform.pdf')
263            if not success:
264                self.upload_success = False
265        self.max_file_upload_size = string_from_bytes(MAX_FILE_UPLOAD_SIZE)
266        return
267
268class ExtraForm(grok.View):
269    """Renders the pdf form extension for applicants.
270    """
271    grok.name('extraform.pdf')
272    grok.context(ICustomApplicant)
273    grok.require('waeup.viewApplication')
274
275    def render(self):
276        pdf = getUtility(IExtFileStore).getFileByContext(
277            self.context, attr='extraform.pdf')
278        self.response.setHeader('Content-Type', 'application/pdf')
279        return pdf
280
281class RefereeForm(grok.View):
282    """Renders the pdf referee's form for applicants.
283    """
284    grok.name('refereeform.pdf')
285    grok.context(ICustomApplicant)
286    grok.require('waeup.viewApplication')
287
288    def render(self):
289        pdf = getUtility(IExtFileStore).getFileByContext(
290            self.context, attr='refereeform.pdf')
291        self.response.setHeader('Content-Type', 'application/pdf')
292        return pdf
Note: See TracBrowser for help on using the repository browser.