Ignore:
Timestamp:
24 May 2013, 06:51:01 (12 years ago)
Author:
Henrik Bettermann
Message:

Add credential form upload facilities.

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

Legend:

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

    r10206 r10220  
    3434    file_store.deleteFileByContext(applicant, attr='extraform.pdf')
    3535    file_store.deleteFileByContext(applicant, attr='refereeform.pdf')
     36    file_store.deleteFileByContext(applicant, attr='credentialform.pdf')
    3637    return
    3738
  • main/waeup.futminna/trunk/src/waeup/futminna/applicants/browser.py

    r10206 r10220  
    9292            self.context, attr='refereeform.pdf')
    9393        if pdf:
    94             html += '<a href="refereeform.pdf">Referee\'s Form</a>'
     94            html += '<a href="refereeform.pdf">Referee\'s Form</a>, '
     95        pdf = getUtility(IExtFileStore).getFileByContext(
     96            self.context, attr='credentialform.pdf')
     97        if pdf:
     98            html += '<a href="credentialform.pdf">Credential Form</a>'
    9599        return html
    96100
     
    125129        self.extraform_url = self.url(self.context, 'extraform.pdf')
    126130        self.referreeform_url = self.url(self.context, 'refereeform.pdf')
     131        self.credentialform_url = self.url(self.context, 'credentialform.pdf')
    127132        return
    128133
     
    201206            else:
    202207                self.upload_success = False
     208        upload_credentialform = self.request.form.get('form.credentialform', None)
     209        if upload_credentialform:
     210            # We got a fresh credentialform upload
     211            success = handle_file_upload(
     212                upload_credentialform, self.context, self, attr='credentialform.pdf')
     213            if success:
     214                self.context.writeLogMessage(self, 'saved: credentialform')
     215            else:
     216                self.upload_success = False
    203217        self.max_file_upload_size = string_from_bytes(MAX_FILE_UPLOAD_SIZE)
    204218        return
     
    238252        if not store.getFileByContext(self.context, attr=u'refereeform.pdf'):
    239253            return _('No referee form pdf file uploaded.')
     254        if not store.getFileByContext(self.context, attr=u'credentialform.pdf'):
     255            return _('No credential form pdf file uploaded.')
    240256        if not self.request.form.get('confirm_passport', False):
    241257            return _('Passport picture confirmation box not ticked.')
     
    263279            if not success:
    264280                self.upload_success = False
     281        upload_credentialform = self.request.form.get('form.credentialform', None)
     282        if upload_credentialform:
     283            # We got a fresh credentialform upload
     284            success = handle_file_upload(
     285                upload_credentialform, self.context, self, attr='credentialform.pdf')
     286            if not success:
     287                self.upload_success = False
    265288        self.max_file_upload_size = string_from_bytes(MAX_FILE_UPLOAD_SIZE)
    266289        return
     
    291314        self.response.setHeader('Content-Type', 'application/pdf')
    292315        return pdf
     316
     317class CredentialForm(grok.View):
     318    """Renders the pdf credential's form for applicants.
     319    """
     320    grok.name('credentialform.pdf')
     321    grok.context(ICustomApplicant)
     322    grok.require('waeup.viewApplication')
     323
     324    def render(self):
     325        pdf = getUtility(IExtFileStore).getFileByContext(
     326            self.context, attr='credentialform.pdf')
     327        self.response.setHeader('Content-Type', 'application/pdf')
     328        return pdf
  • main/waeup.futminna/trunk/src/waeup/futminna/applicants/browser_templates/applicanteditpage.pt

    r10105 r10220  
    7979          </p>
    8080          <input type="file" name="form.refereeform" />
     81          <br />
     82          <span i18n:translate="">
     83            Max. file size:
     84          </span>
     85          <span tal:replace="view/max_file_upload_size">10 KB</span>
     86      </tr>
     87      <tr>
     88        <td class="fieldname" i18n:translate="">
     89          Credential Form:
     90        </td>
     91        <td>
     92          <p tal:condition="python: view.file_exists('credentialform.pdf')">
     93            <a href="credentialform.pdf"
     94               i18n:translate="">
     95              Download pdf file
     96            </a>
     97          </p>
     98          <input type="file" name="form.credentialform" />
    8199          <br />
    82100          <span i18n:translate="">
  • main/waeup.futminna/trunk/src/waeup/futminna/applicants/tests/test_browser.py

    r10216 r10220  
    249249        self.assertTrue(fd is None)
    250250
     251    def test_upload_credentialform_by_student(self):
     252        self.login()
     253        self.browser.open(self.edit_path)
     254        # Create a pseudo file and select it to be uploaded in form
     255        pdf_content = 'A' * 1024 * 600  # A string of 600 KB size
     256        pseudo_pdf = StringIO(pdf_content)
     257        ctrl = self.browser.getControl(name='form.credentialform')
     258        file_ctrl = ctrl.mech_control
     259        file_ctrl.add_file(pseudo_pdf, filename='myform.pdf')
     260        self.browser.getControl("Save").click() # submit form
     261        # We get a warning message
     262        self.assertTrue(
     263            'Uploaded file is too big' in self.browser.contents)
     264        # Create a pseudo file with acceptable size
     265        pdf_content = 'A' * 1024 * 300  # A string of 300 KB size
     266        pseudo_pdf = StringIO(pdf_content)
     267        ctrl = self.browser.getControl(name='form.credentialform')
     268        file_ctrl = ctrl.mech_control
     269        file_ctrl.add_file(pseudo_pdf, filename='myform.pdf')
     270        self.browser.getControl("Save").click() # submit form
     271        # Even though the form could not be saved ...
     272        self.assertTrue('Required input is missing' in self.browser.contents)
     273        # ... the file has been successfully uploaded
     274        pdf_url = self.edit_path.replace('edit', 'credentialform.pdf')
     275        self.browser.open(pdf_url)
     276        self.assertEqual(
     277            self.browser.headers['content-type'], 'application/pdf')
     278        self.assertEqual(len(self.browser.contents), 307200)
     279        # There is really a file stored for the applicant
     280        storage = getUtility(IExtFileStore)
     281        file_id = IFileStoreNameChooser(self.applicant).chooseName(
     282            attr='credentialform.pdf')
     283        # The stored file can be fetched
     284        fd = storage.getFile(file_id)
     285        file_len = len(fd.read())
     286        self.assertEqual(file_len, 307200)
     287        # A file link is displayed on the edit view ...
     288        self.browser.open(self.edit_path)
     289        self.assertTrue('<a href="credentialform.pdf">' in self.browser.contents)
     290        # ... and on the dislay view
     291        self.browser.open(self.view_path)
     292        self.assertTrue('<a href="credentialform.pdf">Credential Form</a>'
     293            in self.browser.contents)
     294
     295    def test_upload_credentialform_by_manager(self):
     296        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
     297        self.browser.open(self.manage_path)
     298        # Create a pseudo file with acceptable size
     299        pdf_content = 'A' * 1024 * 300  # A string of 300 KB size
     300        pseudo_pdf = StringIO(pdf_content)
     301        ctrl = self.browser.getControl(name='form.credentialform')
     302        file_ctrl = ctrl.mech_control
     303        file_ctrl.add_file(pseudo_pdf, filename='myform.pdf')
     304        self.browser.getControl("Save").click() # submit form
     305        # Even though the form could not be saved ...
     306        self.assertTrue('Required input is missing' in self.browser.contents)
     307        # ... the file has been successfully uploaded
     308        pdf_url = self.manage_path.replace('manage', 'credentialform.pdf')
     309        self.browser.open(pdf_url)
     310        self.assertEqual(
     311            self.browser.headers['content-type'], 'application/pdf')
     312        self.assertEqual(len(self.browser.contents), 307200)
     313        # There is rally a file stored for the applicant
     314        storage = getUtility(IExtFileStore)
     315        file_id = IFileStoreNameChooser(self.applicant).chooseName(
     316            attr='credentialform.pdf')
     317        # The stored file can be fetched
     318        fd = storage.getFile(file_id)
     319        file_len = len(fd.read())
     320        self.assertEqual(file_len, 307200)
     321        # A file link is displayed on the edit view ...
     322        self.browser.open(self.manage_path)
     323        self.assertTrue('<a href="credentialform.pdf">' in self.browser.contents)
     324        # ... and on the dislay view
     325        self.browser.open(self.view_path)
     326        self.assertTrue('<a href="credentialform.pdf">Credential Form</a>'
     327            in self.browser.contents)
     328        # Adding file is properly logged
     329        logfile = os.path.join(
     330            self.app['datacenter'].storage, 'logs', 'applicants.log')
     331        logcontent = open(logfile).read()
     332        self.assertTrue(
     333            'zope.mgr - waeup.futminna.applicants.browser.CustomApplicantManageFormPage'
     334            ' - %s - saved: credentialform'
     335            % (self.applicant.applicant_id)
     336            in logcontent)
     337        # When an applicant is removed, also the pdf files are gone.
     338        del self.app['applicants']['app2011'][self.applicant.application_number]
     339        fd = storage.getFile(file_id)
     340        self.assertTrue(fd is None)
     341
    251342    def test_certificate_source(self):
    252343        # We add a second 'alternative' certificate
Note: See TracChangeset for help on using the changeset viewer.