Changeset 7108


Ignore:
Timestamp:
14 Nov 2011, 10:01:17 (13 years ago)
Author:
Henrik Bettermann
Message:

Reorganize file upload. Each viewlet gets an upload and a delete button and writes its own log message. Thus there is one line per file deletion or file upload in the log file. The save action does no longer upload the files.

It works perfectly in the UI but I have still some problems with the browser test.

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

Legend:

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

    r7107 r7108  
    7070        changed_fields = reduce(lambda x,y: x+y, changed_fields.values())
    7171    fields_string = ' + '.join(changed_fields)
    72     view.context._p_changed = True
     72    #view.context._p_changed = True
    7373    view.flash('Form has been saved.')
    7474    if fields_string:
    75         write_log_message(view, 'saved: % s' % fields_string)
     75        write_log_message(view, 'saved: %s' % fields_string)
    7676    return
    7777
     
    470470    title = 'Clearance Data'
    471471    pnav = 4
    472     files_changed = []
    473     files_deleted = []
    474472
    475473    form_fields['date_of_birth'].custom_widget = FriendlyDateWidget('le-year')
     
    479477        return super(StudentClearanceManageFormPage, self).update()
    480478
    481     @grok.action('Save')
     479    @grok.action('Save (no upload)')
    482480    def save(self, **data):
    483481        msave(self, **data)
  • main/waeup.sirp/trunk/src/waeup/sirp/students/browser_templates/fileupload.pt

    r7107 r7108  
    88    <span class="widget">
    99      <tal:file tal:condition="viewlet/file_exists">
    10         <input type="submit" id="delete"
    11                name="delete" value="Delete file" class="button" />
    1210        <a tal:attributes="href viewlet/download_name"
    1311           tal:content="viewlet/download_name"
     
    1513          LINK
    1614        </a>
     15        <input type="submit" id="delete"
     16               name="delete" value="Delete" class="button" />
    1717        <br /><br />
    1818      </tal:file>
    19       Upload new file:<br />
    2019      <input type="file" tal:attributes="name viewlet/input_name"/>
     20      <input type="submit" id="upload"
     21             name="upload" value="Upload" class="button" />
    2122      <br />
    2223      <span i18n:translate="">
  • main/waeup.sirp/trunk/src/waeup/sirp/students/tests/test_browser.py

    r7106 r7108  
    365365        # as birth certificate
    366366        pseudo_image = StringIO('I pretend to be a graphics file')
    367         ctrl = self.browser.getControl(name='form.birth_certificate')
     367        ctrl = self.browser.getControl(name='birth_certificate')
    368368        file_ctrl = ctrl.mech_control
    369369        file_ctrl.add_file(pseudo_image, filename='myphoto.jpg')
    370         self.browser.getControl("Save").click() # submit form
    371         # Form has errors ...
    372         self.assertTrue(
    373             'Required input is missing' in self.browser.contents)
    374         # ... but file has been uploaded and a correct <img> link included
    375         self.assertTrue(
     370        # The Save action does not upload files
     371        self.browser.getControl("Save (no upload)").click() # submit form
     372        self.assertFalse(
    376373            '<a target="image" href="birth_certificate.jpg">' in self.browser.contents)
     374        # ... but the Upload submit button
     375        import pdb; pdb.set_trace()
     376        ctrl = self.browser.getControl(name='birth_certificate')
     377        file_ctrl = ctrl.mech_control
     378        file_ctrl.add_file(pseudo_image, filename='myphoto.jpg')
     379        self.browser.getControl("Upload").click() # submit form
    377380        # There is a correct <img> link included
    378381        self.assertTrue(
     
    388391        photo_content = 'A' * 1024 * 151  # A string of 21 KB size
    389392        pseudo_image = StringIO(photo_content)
    390         ctrl = self.browser.getControl(name='form.birth_certificate')
     393        ctrl = self.browser.getControl(name='birth_certificate')
    391394        file_ctrl = ctrl.mech_control
    392395        file_ctrl.add_file(pseudo_image, filename='myphoto.jpg')
  • main/waeup.sirp/trunk/src/waeup/sirp/students/viewlets.py

    r7107 r7108  
    88from waeup.sirp.browser import DEFAULT_IMAGE_PATH
    99from waeup.sirp.students.browser import (
    10     StudentClearanceDisplayFormPage, StudentClearanceManageFormPage)
     10    StudentClearanceDisplayFormPage, StudentClearanceManageFormPage,
     11    write_log_message)
    1112from waeup.sirp.students.interfaces import IStudentClearance
    1213
     
    205206    store = getUtility(IExtFileStore)
    206207    store.deleteFileByContext(context, attr=download_name)
     208    write_log_message(view, 'deleted: %s' % download_name)
    207209    view.flash('File %s deleted.' % download_name)
    208210    return
     
    227229    ext.lower()
    228230    if ext != expected_ext:
    229         view.flash('%s extension expected.' % expected_ext)
     231        view.flash('%s file extension expected.' % expected_ext)
    230232        return False
    231233    size = file_size(upload)
     
    237239    file_id = IFileStoreNameChooser(context).chooseName(attr=download_name)
    238240    store.createFile(file_id, upload)
     241    write_log_message(view, 'uploaded: %s (%s)' % (download_name,upload.filename))
     242    view.flash('File %s uploaded.' % download_name)
    239243    return True
    240244
     
    276280    grok.require('waeup.manageStudents')
    277281    mus = 1024 * 150
    278     input_name = u'form.filename'
     282    input_name = u'filename'
    279283
    280284    def update(self):
    281285        self.max_upload_size = string_from_bytes(self.mus)
    282         delete = self.request.form.get('delete', None)
    283         if delete:
    284             self.view.files_deleted += self.download_name
     286        delete_button = self.request.form.get('delete', None)
     287        if delete_button:
    285288            handle_file_delete(
    286289                context=self.context, view=self.view,
     
    289292                self.view.url(self.context, self.view.__name__))
    290293            return
    291         upload = self.request.form.get(self.input_name, None)
    292         if upload:
    293             # We got a fresh upload
    294             file_changed = handle_file_upload(
    295                 upload, self.context, self.view, self.mus, self.download_name)
    296             if file_changed is False:  # False is not None!
    297                 self.view.redirect(
    298                     self.view.url(self.context, self.view.__name__))
    299                 return # error during file upload. Ignore other values
    300             else:
    301                 self.view.files_changed += self.download_name
     294        upload_button = self.request.form.get('upload', None)
     295        if upload_button:
     296            upload = self.request.form.get(self.input_name, None)
     297            if upload:
     298                # We got a fresh upload
     299                file_changed = handle_file_upload(
     300                    upload, self.context, self.view, self.mus, self.download_name)
     301                if file_changed is False:  # False is not None!
     302                    self.view.redirect(
     303                        self.view.url(self.context, self.view.__name__))
     304                    return # error during file upload. Ignore other values
    302305        return
    303306
     
    313316    """
    314317    grok.order(1)
    315     label = u'Birth Certificate (jpeg image):'
     318    label = u'Birth Certificate (jpg only):'
    316319    mus = 1024 * 150
    317320    download_name = u'birth_certificate.jpg'
    318     input_name = u'form.birth_certificate'
     321    input_name = u'birth_certificate'
    319322
    320323class Image(grok.View):
Note: See TracChangeset for help on using the changeset viewer.