Changeset 9032


Ignore:
Timestamp:
21 Jul 2012, 08:16:03 (12 years ago)
Author:
Henrik Bettermann
Message:

Implement SkeletonDownloadView? for downloading csv skeleton files.

Compose export filenames of portal acronym and the exporter name.

Location:
main/waeup.kofa/trunk/src/waeup/kofa
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/browser/pages.py

    r9025 r9032  
    914914        return open(fullpath, 'rb').read()
    915915
     916class SkeletonDownloadView(UtilityView, grok.View):
     917    grok.context(IDataCenter)
     918    grok.name('skeleton')
     919    grok.require('waeup.manageDataCenter')
     920
     921    def update(self, processorname=None):
     922        self.processorname = self.request.form['name']
     923        self.filename = ('%s_000.csv' %
     924            self.processorname.replace('processor','import'))
     925        return
     926
     927    def render(self):
     928        #ob_class = self.__implemented__.__name__.replace('waeup.kofa.','')
     929        #self.context.logger.info(
     930        #    '%s - skeleton downloaded: %s' % (ob_class, self.filename))
     931        self.response.setHeader(
     932            'Content-Type', 'text/csv; charset=UTF-8')
     933        self.response.setHeader(
     934            'Content-Disposition:', 'attachment; filename="%s' % self.filename)
     935        processor = getUtility(IBatchProcessor, name=self.processorname)
     936        csv_data = processor.get_csv_skeleton()
     937        return csv_data
     938
    916939class DatacenterImportStep1(KofaPage):
    917940    """Manual import step 1: choose file
     
    13751398        self.response.setHeader(
    13761399            'Content-Type', 'text/csv; charset=UTF-8')
     1400        acronym = grok.getSite()['configuration'].acronym.replace(' ','')
     1401        filename = "%s%s.csv" % (acronym, exporter.title.title().replace(' ',''))
     1402        self.response.setHeader(
     1403            'Content-Disposition:', 'attachment; filename="%s' % filename)
    13771404        return csv_data
    13781405
  • main/waeup.kofa/trunk/src/waeup/kofa/browser/templates/datacenteruploadpage.pt

    r9027 r9032  
    1919      <th i18n:translate="">Processor</th>
    2020      <th i18n:translate="">Available Fields</th>
    21       <th i18n:translate="">CSV Skeleton File (not yet)</th>
     21      <th i18n:translate="">CSV Skeleton File</th>
    2222    </tr>
    2323  </thead>
     
    3131    </td>
    3232    <td>
    33       <span tal:replace="importer/name"></span>.csv
     33      <a tal:attributes="href python: 'skeleton?name=' + importer['name']"
     34         tal:content="python: importer['name'].replace('processor', 'import') + '_000.csv'">
     35         FILENAME</a>
    3436    </td>
    3537  </tr>
  • main/waeup.kofa/trunk/src/waeup/kofa/browser/tests/test_browser.py

    r9024 r9032  
    160160        self.assertEqual(self.browser.headers['Content-Type'],
    161161                         'text/csv; charset=UTF-8')
     162        self.assertTrue ('WAeUP.KofaFaculties.csv' in
     163            self.browser.headers['content-disposition'])
    162164        self.assertEqual(self.browser.contents,
    163165            'code,title,title_prefix,users_with_local_roles\r\n'
     
    169171                        'exported: faculties' in logcontent)
    170172        return
     173
     174    def test_skeleton_download(self):
     175        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
     176        self.browser.open(self.datacenter_path)
     177        self.browser.getLink("Upload data").click()
     178        self.assertTrue(
     179            '<a href="skeleton?name=facultyprocessor">facultyimport_000.csv</a>'
     180            in self.browser.contents)
     181        self.browser.getLink('facultyimport_000.csv').click()
     182        self.assertEqual(self.browser.headers['Status'], '200 Ok')
     183        self.assertEqual(self.browser.headers['Content-Type'],
     184                         'text/csv; charset=UTF-8')
     185        self.assertEqual(self.browser.contents,
     186            'code,local_roles,title,title_prefix\r\n')
     187        return
  • main/waeup.kofa/trunk/src/waeup/kofa/utils/batching.py

    r8905 r9032  
    394394                os.path.abspath(finished_path), failed_path)
    395395
     396    def get_csv_skeleton(self):
     397        """Export CSV file only with a header of available fields.
     398
     399        A raw string with CSV data should be returned.
     400        """
     401        outfile = StringIO()
     402        writer = csv.DictWriter(outfile, self.available_fields)
     403        writer.writerow(dict(zip(self.available_fields, self.available_fields))) # header
     404        outfile.seek(0)
     405        return outfile.read()
     406
    396407class ExporterBase(object):
    397408    """A base for exporters.
Note: See TracChangeset for help on using the changeset viewer.