Changeset 7081 for main


Ignore:
Timestamp:
11 Nov 2011, 10:04:00 (13 years ago)
Author:
uli
Message:

Tune handle_img_upload: we check file size now and emit a flash
message when there is a problem with it. More tests could happen here,
for instance for file type, etc.

File:
1 edited

Legend:

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

    r7068 r7081  
    5858from waeup.sirp.permissions import get_users_with_local_roles
    5959from waeup.sirp.university.interfaces import ICertificate
     60from waeup.sirp.utils.helpers import string_from_bytes, file_size
    6061from waeup.sirp.widgets.datewidget import (
    6162    FriendlyDateWidget, FriendlyDateDisplayWidget)
     
    6768    IApplicant, IApplicantPrincipal,IApplicantEdit, IApplicantsRoot,
    6869    IApplicantsContainer, IApplicantsContainerAdd, application_types_vocab,
    69     IMAGE_PATH,
     70    IMAGE_PATH, MAX_UPLOAD_SIZE,
    7071    )
    7172from waeup.sirp.applicants.workflow import INITIALIZED, STARTED
     
    695696    target = 'edit_full'
    696697
    697 def handle_img_upload(upload, context):
     698
     699def handle_img_upload(upload, context, view):
    698700    """Handle upload of applicant image.
    699     """
     701
     702    Returns `True` in case of success or `False`.
     703
     704    Please note that file pointer passed in (`upload`) most probably
     705    points to end of file when leaving this function.
     706    """
     707    size = file_size(upload)
     708    if size > MAX_UPLOAD_SIZE:
     709        view.flash('Uploaded image is too big!')
     710        return False
     711    upload.seek(0) # file pointer moved when determining size
    700712    store = getUtility(IExtFileStore)
    701713    file_id = IFileStoreNameChooser(context).chooseName()
    702714    store.createFile(file_id, upload)
    703     upload.seek(0) # XXX: really neccessary?
    704     return
     715    return True
    705716
    706717class EditApplicantFull(WAeUPEditFormPage):
     
    720731        super(EditApplicantFull, self).update()
    721732        self.wf_info = IWorkflowInfo(self.context)
     733        self.max_upload_size = string_from_bytes(MAX_UPLOAD_SIZE)
    722734        upload = self.request.form.get('form.passport', None)
    723735        if upload:
    724736            # We got a fresh upload
    725             handle_img_upload(upload, self.context)
    726             self.passport_changed = True
     737            passport_changed = handle_img_upload(upload, self.context, self)
    727738        return
    728739
     
    789800            return
    790801        datepicker.need() # Enable jQuery datepicker in date fields.
    791         upload = self.request.form.get('form.passport', None)
    792         if upload:
    793             # We got a fresh upload
    794             handle_img_upload(upload, self.context)
    795             self.passport_changed = True
    796802        super(EditApplicantStudent, self).update()
    797803        return
Note: See TracChangeset for help on using the changeset viewer.