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

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

Enable pdf file upload of Offline Form Extension.

  • Property svn:keywords set to Id
File size: 9.0 KB
Line 
1## $Id: browser.py 10096 2013-04-23 09:45:04Z 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='formextension.pdf')
85        if pdf:
86            html += '<a href="formextension.pdf">Offline Form Extension</a>'
87        return html
88
89    @property
90    def form_fields(self):
91        target = getattr(self.context.__parent__, 'prefix', None)
92        if target is not None and target.startswith('pg'):
93            form_fields = grok.AutoFields(ICustomPGApplicant)
94            for field in PG_OMIT_DISPLAY_FIELDS:
95                form_fields = form_fields.omit(field)
96        else:
97            form_fields = grok.AutoFields(ICustomUGApplicant)
98            for field in UG_OMIT_DISPLAY_FIELDS:
99                form_fields = form_fields.omit(field)
100        if form_fields.get('perm_address', None):
101            form_fields['perm_address'].custom_widget = BytesDisplayWidget
102        form_fields['notice'].custom_widget = BytesDisplayWidget
103        if not getattr(self.context, 'student_id'):
104            form_fields = form_fields.omit('student_id')
105        if not getattr(self.context, 'screening_score'):
106            form_fields = form_fields.omit('screening_score')
107        if not getattr(self.context, 'screening_venue'):
108            form_fields = form_fields.omit('screening_venue')
109        if not getattr(self.context, 'screening_date'):
110            form_fields = form_fields.omit('screening_date')
111        return form_fields
112
113    def update(self):
114        super(CustomApplicantDisplayFormPage, self).update()
115        self.formextension_url = self.url(self.context, 'formextension.pdf')
116        return
117
118class CustomPDFApplicationSlip(NigeriaPDFApplicationSlip):
119
120    @property
121    def form_fields(self):
122        target = getattr(self.context.__parent__, 'prefix', None)
123        if target is not None and target.startswith('pg'):
124            form_fields = grok.AutoFields(ICustomPGApplicant)
125            for field in PG_OMIT_PDF_FIELDS:
126                form_fields = form_fields.omit(field)
127        else:
128            form_fields = grok.AutoFields(ICustomUGApplicant)
129            for field in UG_OMIT_PDF_FIELDS:
130                form_fields = form_fields.omit(field)
131        if not getattr(self.context, 'student_id'):
132            form_fields = form_fields.omit('student_id')
133        if not getattr(self.context, 'screening_score'):
134            form_fields = form_fields.omit('screening_score')
135        if not getattr(self.context, 'screening_venue'):
136            form_fields = form_fields.omit('screening_venue')
137        if not getattr(self.context, 'screening_date'):
138            form_fields = form_fields.omit('screening_date')
139        return form_fields
140
141class CustomApplicantManageFormPage(NigeriaApplicantManageFormPage):
142    """A full edit view for applicant data.
143    """
144    grok.template('applicanteditpage')
145
146    @property
147    def form_fields(self):
148        target = getattr(self.context.__parent__, 'prefix', None)
149        if target is not None and target.startswith('pg'):
150            form_fields = grok.AutoFields(ICustomPGApplicant)
151            for field in PG_OMIT_MANAGE_FIELDS:
152                form_fields = form_fields.omit(field)
153        else:
154            form_fields = grok.AutoFields(ICustomUGApplicant)
155            for field in UG_OMIT_MANAGE_FIELDS:
156                form_fields = form_fields.omit(field)
157        form_fields['student_id'].for_display = True
158        form_fields['applicant_id'].for_display = True
159        return form_fields
160
161    def update(self):
162        super(CustomApplicantManageFormPage, self).update()
163        upload_formextension = self.request.form.get('form.formextension', None)
164        if upload_formextension:
165            # We got a fresh formextension upload
166            self.upload_success = handle_file_upload(
167                upload_formextension, self.context, self, attr='formextension.pdf')
168            if self.upload_success:
169                self.context.writeLogMessage(self, 'saved: formextension')
170        self.max_file_upload_size = string_from_bytes(MAX_FILE_UPLOAD_SIZE)
171        return
172
173class CustomApplicantEditFormPage(NigeriaApplicantEditFormPage):
174    """An applicant-centered edit view for applicant data.
175    """
176    grok.template('applicanteditpage')
177
178    @property
179    def form_fields(self):
180        target = getattr(self.context.__parent__, 'prefix', None)
181        if target is not None and target.startswith('pg'):
182            form_fields = grok.AutoFields(ICustomPGApplicantEdit)
183            for field in PG_OMIT_EDIT_FIELDS:
184                form_fields = form_fields.omit(field)
185        else:
186            form_fields = grok.AutoFields(ICustomUGApplicantEdit)
187            for field in UG_OMIT_EDIT_FIELDS:
188                form_fields = form_fields.omit(field)
189        form_fields['applicant_id'].for_display = True
190        form_fields['reg_number'].for_display = True
191        return form_fields
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'formextension.pdf'):
198            return _('No form extension pdf file uploaded.')
199        if not self.request.form.get('confirm_passport', False):
200            return _('Passport picture confirmation box not ticked.')
201        return False
202
203    def update(self):
204        if self.context.locked or (
205            self.context.__parent__.expired and
206            self.context.__parent__.strict_deadline):
207            self.emit_lock_message()
208            return
209        super(CustomApplicantEditFormPage, self).update()
210        upload_formextension = self.request.form.get('form.formextension', None)
211        if upload_formextension:
212            # We got a fresh formextension upload
213            self.upload_success = handle_file_upload(
214                upload_formextension, self.context, self, attr='formextension.pdf')
215        self.max_file_upload_size = string_from_bytes(MAX_FILE_UPLOAD_SIZE)
216        return
217
218class FormExtension(grok.View):
219    """Renders the pdf form extension for applicants.
220    """
221    grok.name('formextension.pdf')
222    grok.context(ICustomApplicant)
223    grok.require('waeup.viewApplication')
224
225    def render(self):
226        pdf = getUtility(IExtFileStore).getFileByContext(
227            self.context, attr='formextension.pdf')
228        self.response.setHeader('Content-Type', 'application/pdf')
229        return pdf
Note: See TracBrowser for help on using the repository browser.