Changeset 7106


Ignore:
Timestamp:
13 Nov 2011, 20:13:07 (13 years ago)
Author:
Henrik Bettermann
Message:

Second trial:

Check file name extension of original filename and compare with expected extension taken from the download_name attribute of the respective file upload viewlet. The download_name attribute is also handed over as attr parameter in chooseName and in getFileByContext.

chooseName separates attr into the base filename and the extension.

download_name = u'nice_image.abc'
File storage path: students/A/A123456/nice_image_A123456.abc

StudentFileNameChooser? itself does not require any special file extension. The extension is given by the upload and display viewlets.

(Tests will follow)

Location:
main/waeup.sirp/trunk/src/waeup/sirp/students
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/students/browser_templates/filedisplay.pt

    r7098 r7106  
    77  <td class="field">
    88    <span class="widget">
    9       <a tal:attributes="href viewlet/img_src"
    10          tal:content="viewlet/img_src"
     9      <a tal:attributes="href viewlet/download_name"
     10         tal:content="viewlet/download_name"
    1111         target="image">
    1212        LINK
  • main/waeup.sirp/trunk/src/waeup/sirp/students/browser_templates/fileupload.pt

    r7098 r7106  
    77  <td class="field">
    88    <span class="widget">
    9       <a tal:attributes="href viewlet/img_src"
    10          tal:content="viewlet/img_src"
     9      <a tal:attributes="href viewlet/download_name"
     10         tal:content="viewlet/download_name"
    1111         target="image">
    1212        LINK
  • main/waeup.sirp/trunk/src/waeup/sirp/students/browser_templates/imagedisplay.pt

    r7098 r7106  
    77  <td class="field">
    88    <span class="widget">
    9       <img tal:attributes="src viewlet/img_src" /><br />
     9      <img tal:attributes="src viewlet/download_name" /><br />
    1010    </span>
    1111  </td>
  • main/waeup.sirp/trunk/src/waeup/sirp/students/browser_templates/imageupload.pt

    r7098 r7106  
    77  <td class="field">
    88    <span class="widget">
    9       <img tal:attributes="src viewlet/img_src" /><br />
     9      <img tal:attributes="src viewlet/download_name" /><br />
    1010      <input type="file" tal:attributes="name viewlet/input_name"/>
    1111      <br />
  • main/waeup.sirp/trunk/src/waeup/sirp/students/student.py

    r7105 r7106  
    177177        return name == self.chooseName()
    178178
    179     def chooseName(self, name=None, attr=None):
     179    def chooseName(self, attr, name=None):
    180180        """Get a valid file id for student context.
    181181
     
    183183
    184184        For a student with student id ``'A123456'`` and
    185         with attr ``'nice_image'`` stored in
     185        with attr ``'nice_image.jpeg'`` stored in
    186186        the students container this chooser would create:
    187187
    188           ``'__file-student__students/A/A123456/nice_image_A123456.jpg'``
     188          ``'__file-student__students/A/A123456/nice_image_A123456.jpeg'``
    189189
    190190        meaning that the nice image of this applicant would be
    191191        stored in the site-wide file storage in path:
    192192
    193           ``students/A/A123456/nice_image_A123456.jpg``
    194 
    195         """
     193          ``students/A/A123456/nice_image_A123456.jpeg``
     194
     195        """
     196        basename, ext = os.path.splitext(attr)
    196197        stud_id = self.context.student_id
    197         marked_filename = '__%s__%s/%s/%s_%s.jpg' % (
    198             STUDENT_FILE_STORE_NAME, stud_id[0], stud_id, attr, stud_id)
     198        marked_filename = '__%s__%s/%s/%s_%s%s' % (
     199            STUDENT_FILE_STORE_NAME, stud_id[0], stud_id, basename, stud_id, ext)
    199200        return marked_filename
    200201
  • main/waeup.sirp/trunk/src/waeup/sirp/students/tests/test_browser.py

    r7101 r7106  
    393393        self.browser.getControl("Save").click() # submit form
    394394        self.assertTrue(
    395             'Uploaded image is too big' in self.browser.contents)
     395            'Uploaded file is too big' in self.browser.contents)
    396396
    397397    def test_manage_course_lists(self):
  • main/waeup.sirp/trunk/src/waeup/sirp/students/viewlets.py

    r7105 r7106  
     1import os
    12import grok
    23from zope.component import getUtility
     
    198199        return self.view.application_url() + rel_link
    199200
    200 def handle_img_upload(upload, context, view, max_size, attr=None):
    201     """Handle upload of applicant image.
     201def handle_file_upload(upload, context, view, max_size, download_name=None):
     202    """Handle upload of student file.
    202203
    203204    Returns `True` in case of success or `False`.
     
    206207    points to end of file when leaving this function.
    207208    """
     209    # Check some file requirements first
     210    if upload.filename.count('.') == 0:
     211        view.flash('File name has no extension.')
     212        return False
     213    if upload.filename.count('.') > 1:
     214        view.flash('File name contains more than one dot.')
     215        return False
     216    basename, expected_ext = os.path.splitext(download_name)
     217    dummy, ext = os.path.splitext(upload.filename)
     218    ext.lower()
     219    if ext != expected_ext:
     220        view.flash('%s extension expected.' % expected_ext)
     221        return False
    208222    size = file_size(upload)
    209223    if size > max_size:
    210         view.flash('Uploaded image is too big!')
     224        view.flash('Uploaded file is too big.')
    211225        return False
    212226    upload.seek(0) # file pointer moved when determining size
    213227    store = getUtility(IExtFileStore)
    214     file_id = IFileStoreNameChooser(context).chooseName(attr=attr)
     228    file_id = IFileStoreNameChooser(context).chooseName(attr=download_name)
    215229    store.createFile(file_id, upload)
    216230    return True
     
    231245    grok.order(1)
    232246    grok.require('waeup.viewStudent')
    233     label = u'Some Text:'
    234     img_src = u'filename.jpg'
    235     attr = None
     247    label = u'File:'
     248    download_name = u'filename.jpg'
    236249
    237250class FileUpload(FileDisplay):
     
    252265        if upload:
    253266            # We got a fresh upload
    254             file_changed = handle_img_upload(
    255                 upload, self.context, self.view, self.mus, self.attr)
     267            file_changed = handle_file_upload(
     268                upload, self.context, self.view, self.mus, self.download_name)
    256269            if file_changed is False:  # False is not None!
    257                 self.view.redirect(self.view.url(self.context))
    258                 return # error during image upload. Ignore other values
     270                self.view.redirect(
     271                    self.view.url(self.context, self.view.__name__))
     272                return # error during file upload. Ignore other values
    259273            else:
    260                 self.view.files_changed += self.img_src
     274                self.view.files_changed += self.download_name
    261275        return
    262276
     
    266280    grok.order(1)
    267281    label = u'Birth Certificate:'
    268     img_src = u'birth_certificate.jpg'
    269     attr = u'birth_certificate'
     282    download_name = u'birth_certificate.jpg'
    270283
    271284class BirthCertificateUpload(FileUpload):
     
    273286    """
    274287    grok.order(1)
    275     label = u'Birth Certificate:'
     288    label = u'Birth Certificate (jpeg image):'
    276289    mus = 1024 * 150
    277     img_src = u'birth_certificate.jpg'
     290    download_name = u'birth_certificate.jpg'
    278291    input_name = u'form.birth_certificate'
    279     attr = u'birth_certificate'
    280292
    281293class Image(grok.View):
    282     """Renders image for students.
    283     """
     294    """Renders jpeg images for students.
     295    """
     296    grok.baseclass()
    284297    grok.name('none.jpg')
    285298    grok.view(StudentClearanceManageFormPage)
    286299    grok.require('waeup.viewStudent')
    287     grok.baseclass()
    288     attr = None
     300    download_name = u'none.jpg'
    289301
    290302    def render(self):
     
    292304        # for file storage.
    293305        image = getUtility(IExtFileStore).getFileByContext(
    294             self.context, attr=self.attr)
     306            self.context, attr=self.download_name)
     307        # We expect that image is a jpeg pictures
    295308        self.response.setHeader(
    296309            'Content-Type', 'image/jpeg')
     
    301314
    302315class BirthCertificateImage(Image):
    303     """Renders birth certificate.
     316    """Renders birth certificate jpeg image.
    304317    """
    305318    grok.name('birth_certificate.jpg')
    306     attr = u'birth_certificate'
     319    download_name = u'birth_certificate.jpg'
Note: See TracChangeset for help on using the changeset viewer.