Ignore:
Timestamp:
9 Dec 2019, 21:39:51 (5 years ago)
Author:
Henrik Bettermann
Message:

Add file upload facilities for statement of result upload.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/kofacustom.iuokada/trunk/src/kofacustom/iuokada/applicants/browser.py

    r15809 r15878  
    1919"""
    2020import grok
     21import os
     22from zope.component import getUtility
    2123from zope.formlib.textwidgets import BytesDisplayWidget
     24from waeup.kofa.interfaces import (
     25    IExtFileStore, IFileStoreNameChooser)
     26from waeup.kofa.utils.helpers import string_from_bytes, file_size, now
    2227from waeup.kofa.applicants.browser import (
    2328    ApplicantRegistrationPage, ApplicantsContainerPage)
     
    4348    )
    4449from kofacustom.iuokada.applicants.interfaces import (
    45     ICustomPGApplicant, ICustomUGApplicant,
     50    ICustomPGApplicant, ICustomUGApplicant, ICustomApplicant,
    4651    ICustomPGApplicantEdit, ICustomUGApplicantEdit,
    4752    ICustomApplicantOnlinePayment
    4853    )
    4954from kofacustom.iuokada.interfaces import MessageFactory as _
     55
     56MAX_FILE_UPLOAD_SIZE = 1024 * 500
     57
     58def handle_file_upload(upload, context, view, attr=None):
     59    """Handle upload of applicant files.
     60
     61    Returns `True` in case of success or `False`.
     62
     63    Please note that file pointer passed in (`upload`) most probably
     64    points to end of file when leaving this function.
     65    """
     66    size = file_size(upload)
     67    if size > MAX_FILE_UPLOAD_SIZE:
     68        view.flash(_('Uploaded file is too big!'))
     69        return False
     70    dummy, ext = os.path.splitext(upload.filename)
     71    ext.lower()
     72    if ext != '.pdf':
     73        view.flash(_('pdf file extension expected.'))
     74        return False
     75    upload.seek(0) # file pointer moved when determining size
     76    store = getUtility(IExtFileStore)
     77    file_id = IFileStoreNameChooser(context).chooseName(attr=attr)
     78    store.createFile(file_id, upload)
     79    return True
    5080
    5181class CustomApplicantsContainerPage(ApplicantsContainerPage):
     
    73103
    74104    @property
     105    def file_links(self):
     106        html = ''
     107        pdf = getUtility(IExtFileStore).getFileByContext(
     108            self.context, attr='stateresult.pdf')
     109        if pdf:
     110            html += '<a href="%s">Statement of Result</a>' % self.url(
     111                self.context, 'stateresult.pdf')
     112        return html
     113
     114    @property
    75115    def form_fields(self):
    76116        if self.target is not None and self.target.startswith('pg'):
     
    112152        return form_fields
    113153
     154    def update(self):
     155        super(CustomApplicantManageFormPage, self).update()
     156        upload_stateresult = self.request.form.get('form.stateresult', None)
     157        if upload_stateresult:
     158            # We got a fresh stateresult upload
     159            success = handle_file_upload(
     160                upload_stateresult, self.context, self, attr='stateresult.pdf')
     161            if success:
     162                self.context.writeLogMessage(self, 'saved: stateresult')
     163            else:
     164                self.upload_success = False
     165        self.max_file_upload_size = string_from_bytes(MAX_FILE_UPLOAD_SIZE)
     166        return
     167
    114168class CustomApplicantEditFormPage(NigeriaApplicantEditFormPage):
    115169    """An applicant-centered edit view for applicant data.
    116170    """
    117171
     172    def dataNotComplete(self, data):
     173        store = getUtility(IExtFileStore)
     174        if self.context.__parent__.with_picture:
     175            store = getUtility(IExtFileStore)
     176            if not store.getFileByContext(self.context, attr=u'passport.jpg'):
     177                return _('No passport picture uploaded.')
     178            if not self.request.form.get('confirm_passport', False):
     179                return _('Passport picture confirmation box not ticked.')
     180        if self.context.subtype == 'transfer' and \
     181            not store.getFileByContext(self.context, attr=u'stateresult.pdf'):
     182            return _('No statement of result pdf file uploaded.')
     183        return False
     184
    118185    @property
    119186    def form_fields(self):
     
    129196        form_fields['reg_number'].for_display = True
    130197        return form_fields
     198
     199    def update(self):
     200        if self.context.locked or (
     201            self.context.__parent__.expired and
     202            self.context.__parent__.strict_deadline):
     203            self.emit_lock_message()
     204            return
     205        super(CustomApplicantEditFormPage, self).update()
     206        upload_stateresult = self.request.form.get('form.stateresult', None)
     207        if upload_stateresult:
     208            # We got a fresh stateresult upload
     209            success = handle_file_upload(
     210                upload_stateresult, self.context, self, attr='stateresult.pdf')
     211            if not success:
     212                self.upload_success = False
     213        self.max_file_upload_size = string_from_bytes(MAX_FILE_UPLOAD_SIZE)
     214        return
    131215
    132216class CustomPDFApplicationSlip(NigeriaPDFApplicationSlip):
     
    152236        return form_fields
    153237
     238class StateResult(grok.View):
     239    """Renders the pdf form extension for applicants.
     240    """
     241    grok.name('stateresult.pdf')
     242    grok.context(ICustomApplicant)
     243    grok.require('waeup.viewApplication')
     244
     245    def render(self):
     246        pdf = getUtility(IExtFileStore).getFileByContext(
     247            self.context, attr='stateresult.pdf')
     248        self.response.setHeader('Content-Type', 'application/pdf')
     249        return pdf
Note: See TracChangeset for help on using the changeset viewer.