source: main/waeup.imostate/trunk/src/waeup/imostate/applicants/browser.py @ 10371

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

Select correct fields on forms.

  • Property svn:keywords set to Id
File size: 11.3 KB
Line 
1## $Id: browser.py 10367 2013-06-23 20:01:26Z 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 hurry.workflow.interfaces import IWorkflowState
27from waeup.kofa.utils.helpers import string_from_bytes, file_size
28from waeup.imostate.applicants.interfaces import (
29    ICustomApplicant,
30    ICustomUGApplicant,
31    ICustomUGApplicantEdit,
32    )
33from waeup.imostate.interfaces import MessageFactory as _
34from kofacustom.nigeria.applicants.browser import (
35    NigeriaApplicantDisplayFormPage,
36    NigeriaApplicantManageFormPage,
37    NigeriaApplicantEditFormPage,
38    NigeriaPDFApplicationSlip,
39    UG_OMIT_DISPLAY_FIELDS,
40    UG_OMIT_PDF_FIELDS,
41    UG_OMIT_MANAGE_FIELDS,
42    UG_OMIT_EDIT_FIELDS,
43    )
44
45from waeup.imostate.applicants.workflow import STARTED
46
47MAX_FILE_UPLOAD_SIZE = 1024 * 500
48
49def handle_file_upload(upload, context, view, attr=None):
50    """Handle upload of applicant files.
51
52    Returns `True` in case of success or `False`.
53
54    Please note that file pointer passed in (`upload`) most probably
55    points to end of file when leaving this function.
56    """
57    size = file_size(upload)
58    if size > MAX_FILE_UPLOAD_SIZE:
59        view.flash(_('Uploaded file is too big!'))
60        return False
61    dummy, ext = os.path.splitext(upload.filename)
62    ext.lower()
63    if ext != '.pdf':
64        view.flash(_('pdf file extension expected.'))
65        return False
66    upload.seek(0) # file pointer moved when determining size
67    store = getUtility(IExtFileStore)
68    file_id = IFileStoreNameChooser(context).chooseName(attr=attr)
69    store.createFile(file_id, upload)
70    return True
71
72class CustomApplicantDisplayFormPage(NigeriaApplicantDisplayFormPage):
73    """A display view for applicant data.
74    """
75
76    grok.template('applicantdisplaypage')
77
78    @property
79    def file_links(self):
80        html = ''
81        pdf = getUtility(IExtFileStore).getFileByContext(
82            self.context, attr='extraform.pdf')
83        if pdf:
84            html += '<a href="extraform.pdf">Extra Applicant Information Form</a>, '
85        pdf = getUtility(IExtFileStore).getFileByContext(
86            self.context, attr='refereeform.pdf')
87        if pdf:
88            html += '<a href="refereeform.pdf">Referee\'s Form</a>, '
89        pdf = getUtility(IExtFileStore).getFileByContext(
90            self.context, attr='credentials.pdf')
91        if pdf:
92            html += '<a href="credentials.pdf">Credentials</a>'
93        return html
94
95    @property
96    def form_fields(self):
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.extraform_url = self.url(self.context, 'extraform.pdf')
116        self.referreeform_url = self.url(self.context, 'refereeform.pdf')
117        self.credentials_url = self.url(self.context, 'credentials.pdf')
118        return
119
120class CustomPDFApplicationSlip(NigeriaPDFApplicationSlip):
121
122    @property
123    def form_fields(self):
124        form_fields = grok.AutoFields(ICustomUGApplicant)
125        for field in UG_OMIT_PDF_FIELDS:
126            form_fields = form_fields.omit(field)
127        if not getattr(self.context, 'student_id'):
128            form_fields = form_fields.omit('student_id')
129        if not getattr(self.context, 'screening_score'):
130            form_fields = form_fields.omit('screening_score')
131        if not getattr(self.context, 'screening_venue'):
132            form_fields = form_fields.omit('screening_venue')
133        if not getattr(self.context, 'screening_date'):
134            form_fields = form_fields.omit('screening_date')
135        return form_fields
136
137class CustomApplicantManageFormPage(NigeriaApplicantManageFormPage):
138    """A full edit view for applicant data.
139    """
140    grok.template('applicanteditpage')
141
142    @property
143    def form_fields(self):
144        form_fields = grok.AutoFields(ICustomUGApplicant)
145        for field in UG_OMIT_MANAGE_FIELDS:
146            form_fields = form_fields.omit(field)
147        form_fields['student_id'].for_display = True
148        form_fields['applicant_id'].for_display = True
149        return form_fields
150
151    def update(self):
152        super(CustomApplicantManageFormPage, self).update()
153        upload_extraform = self.request.form.get('form.extraform', None)
154        if upload_extraform:
155            # We got a fresh extraform upload
156            success = handle_file_upload(
157                upload_extraform, self.context, self, attr='extraform.pdf')
158            if success:
159                self.context.writeLogMessage(self, 'saved: extraform')
160            else:
161                self.upload_success = False
162        upload_refereeform = self.request.form.get('form.refereeform', None)
163        if upload_refereeform:
164            # We got a fresh refereeform upload
165            success = handle_file_upload(
166                upload_refereeform, self.context, self, attr='refereeform.pdf')
167            if success:
168                self.context.writeLogMessage(self, 'saved: refereeform')
169            else:
170                self.upload_success = False
171        upload_credentials = self.request.form.get('form.credentials', None)
172        if upload_credentials:
173            # We got a fresh credentials upload
174            success = handle_file_upload(
175                upload_credentials, self.context, self, attr='credentials.pdf')
176            if success:
177                self.context.writeLogMessage(self, 'saved: credentials')
178            else:
179                self.upload_success = False
180        self.max_file_upload_size = string_from_bytes(MAX_FILE_UPLOAD_SIZE)
181        return
182
183class CustomApplicantEditFormPage(NigeriaApplicantEditFormPage):
184    """An applicant-centered edit view for applicant data.
185    """
186    grok.template('applicanteditpage')
187    submit_state = STARTED
188
189    @property
190    def form_fields(self):
191        form_fields = grok.AutoFields(ICustomUGApplicantEdit)
192        for field in UG_OMIT_EDIT_FIELDS:
193            form_fields = form_fields.omit(field)
194        form_fields['applicant_id'].for_display = True
195        form_fields['reg_number'].for_display = True
196        return form_fields
197
198    @property
199    def display_actions(self):
200        state = IWorkflowState(self.context).getState()
201        actions = [[],[]]
202        if state == STARTED:
203            actions = [[_('Save'), _('Final Submit')], []]
204        return actions
205
206    def dataNotComplete(self):
207        store = getUtility(IExtFileStore)
208        if not store.getFileByContext(self.context, attr=u'passport.jpg'):
209            return _('No passport picture uploaded.')
210        if not store.getFileByContext(self.context, attr=u'extraform.pdf'):
211            return _('No extra information form pdf file uploaded.')
212        if not store.getFileByContext(self.context, attr=u'refereeform.pdf'):
213            return _('No referee form pdf file uploaded.')
214        if self.target is not None and self.target.startswith('pg') \
215            and not store.getFileByContext(self.context, attr=u'credentials.pdf'):
216            return _('No credentials 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        upload_credentials = self.request.form.get('form.credentials', None)
243        if upload_credentials:
244            # We got a fresh credentials upload
245            success = handle_file_upload(
246                upload_credentials, self.context, self, attr='credentials.pdf')
247            if not success:
248                self.upload_success = False
249        self.max_file_upload_size = string_from_bytes(MAX_FILE_UPLOAD_SIZE)
250        return
251
252class ExtraForm(grok.View):
253    """Renders the pdf form extension for applicants.
254    """
255    grok.name('extraform.pdf')
256    grok.context(ICustomApplicant)
257    grok.require('waeup.viewApplication')
258
259    def render(self):
260        pdf = getUtility(IExtFileStore).getFileByContext(
261            self.context, attr='extraform.pdf')
262        self.response.setHeader('Content-Type', 'application/pdf')
263        return pdf
264
265class RefereeForm(grok.View):
266    """Renders the pdf referee's form for applicants.
267    """
268    grok.name('refereeform.pdf')
269    grok.context(ICustomApplicant)
270    grok.require('waeup.viewApplication')
271
272    def render(self):
273        pdf = getUtility(IExtFileStore).getFileByContext(
274            self.context, attr='refereeform.pdf')
275        self.response.setHeader('Content-Type', 'application/pdf')
276        return pdf
277
278class Credentials(grok.View):
279    """Renders the pdf credential's form for applicants.
280    """
281    grok.name('credentials.pdf')
282    grok.context(ICustomApplicant)
283    grok.require('waeup.viewApplication')
284
285    def render(self):
286        pdf = getUtility(IExtFileStore).getFileByContext(
287            self.context, attr='credentials.pdf')
288        self.response.setHeader('Content-Type', 'application/pdf')
289        return pdf
Note: See TracBrowser for help on using the repository browser.