Changeset 10096


Ignore:
Timestamp:
23 Apr 2013, 09:45:04 (12 years ago)
Author:
Henrik Bettermann
Message:

Enable pdf file upload of Offline Form Extension.

Location:
main/waeup.futminna/trunk/src/waeup/futminna/applicants
Files:
3 added
3 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.futminna/trunk/src/waeup/futminna/applicants/browser.py

    r10088 r10096  
    1919"""
    2020import grok
     21import os
     22from zope.component import getUtility
     23from waeup.kofa.interfaces import (
     24    IExtFileStore, IFileStoreNameChooser)
    2125from zope.formlib.textwidgets import BytesDisplayWidget
     26from waeup.kofa.utils.helpers import string_from_bytes, file_size
    2227from waeup.futminna.applicants.interfaces import (
     28    ICustomApplicant,
    2329    ICustomUGApplicant,
    2430    ICustomPGApplicant,
     
    3440    PG_OMIT_EDIT_FIELDS,
    3541    )
     42from waeup.futminna.interfaces import MessageFactory as _
    3643from kofacustom.nigeria.applicants.browser import (
    3744    NigeriaApplicantDisplayFormPage,
     
    4047    NigeriaPDFApplicationSlip)
    4148
     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
    4273
    4374class CustomApplicantDisplayFormPage(NigeriaApplicantDisplayFormPage):
     
    4879
    4980    @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
    5090    def form_fields(self):
    5191        target = getattr(self.context.__parent__, 'prefix', None)
     
    5898            for field in UG_OMIT_DISPLAY_FIELDS:
    5999                form_fields = form_fields.omit(field)
    60         form_fields['perm_address'].custom_widget = BytesDisplayWidget
     100        if form_fields.get('perm_address', None):
     101            form_fields['perm_address'].custom_widget = BytesDisplayWidget
    61102        form_fields['notice'].custom_widget = BytesDisplayWidget
    62103        if not getattr(self.context, 'student_id'):
     
    70111        return form_fields
    71112
     113    def update(self):
     114        super(CustomApplicantDisplayFormPage, self).update()
     115        self.formextension_url = self.url(self.context, 'formextension.pdf')
     116        return
     117
    72118class CustomPDFApplicationSlip(NigeriaPDFApplicationSlip):
    73119
     
    96142    """A full edit view for applicant data.
    97143    """
    98    
     144    grok.template('applicanteditpage')
     145
    99146    @property
    100147    def form_fields(self):
     
    112159        return form_fields
    113160
     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
    114173class CustomApplicantEditFormPage(NigeriaApplicantEditFormPage):
    115174    """An applicant-centered edit view for applicant data.
    116175    """
     176    grok.template('applicanteditpage')
    117177
    118178    @property
     
    130190        form_fields['reg_number'].for_display = True
    131191        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
  • main/waeup.futminna/trunk/src/waeup/futminna/applicants/browser_templates/applicantdisplaypage.pt

    r10088 r10096  
    3636      </td>
    3737    <tr>
     38    <tr>
     39      <td class="fieldname" i18n:translate="">
     40        Files:
     41      </td>
     42      <td>
     43        <span tal:replace="structure view/file_links" />
     44      </td>
     45    </tr>
    3846  </tbody>
    3947</table>
  • main/waeup.futminna/trunk/src/waeup/futminna/applicants/interfaces.py

    r10088 r10096  
    136136        """
    137137
    138 class ICustomUGApplicantEdit(INigeriaUGApplicantEdit):
     138class ICustomUGApplicantEdit(ICustomUGApplicant):
    139139    """An undergraduate applicant interface for edit forms.
    140140
     
    148148    """
    149149
    150 class ICustomPGApplicantEdit(INigeriaPGApplicantEdit):
     150class ICustomPGApplicantEdit(ICustomPGApplicant):
    151151    """A postgraduate applicant interface for editing.
    152152
     
    160160    """
    161161
     162
    162163class ICustomApplicantOnlinePayment(INigeriaApplicantOnlinePayment):
    163164    """An applicant payment via payment gateways.
    164165
    165     """
    166 
    167 class IPUTMEApplicantEdit(IPUTMEApplicantEdit):
    168     """An undergraduate applicant interface for editing.
    169 
    170     Here we can repeat the fields from base data and set the
    171     `required` and `readonly` attributes to True to further restrict
    172     the data access. Or we can allow only certain certificates to be
    173     selected by choosing the appropriate source.
    174 
    175     We cannot omit fields here. This has to be done in the
    176     respective form page.
    177166    """
    178167
Note: See TracChangeset for help on using the changeset viewer.