Ignore:
Timestamp:
6 May 2012, 08:28:32 (13 years ago)
Author:
Henrik Bettermann
Message:

We expect many uploaded files and have to improve file management.

  • Provide download view for uploaded csv files.
  • Let managers delete uploaded files.
  • Restrict upload to csv files.
  • Pending files must not be re-uploaded. They can either be edited in the files system (e.g. through ftp) or removed.

ToDo?:

  • Add browser tests
  • Add upload permission permission (People, who have the permission to manage files in the datacenter, are not automatically allowed to import data)
Location:
main/waeup.kofa/trunk/src/waeup/kofa/browser
Files:
2 edited

Legend:

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

    r8361 r8366  
    749749#
    750750
    751 class DatacenterPage(KofaPage):
     751class DatacenterPage(KofaEditFormPage):
    752752    grok.context(IDataCenter)
    753753    grok.name('index')
    754     grok.require('waeup.manageUniversity')
     754    grok.require('waeup.manageDataCenter')
    755755    label = _(u'Data Center')
    756756    pnav = 0
     757
     758    def update(self):
     759        datatable.need()
     760        warning.need()
     761        return super(DatacenterPage, self).update()
     762
     763    @jsaction(_('Remove selected'))
     764    def delFiles(self, **data):
     765        form = self.request.form
     766        logger = self.context.logger
     767        if form.has_key('val_id'):
     768            child_id = form['val_id']
     769        else:
     770            self.flash(_('No item selected.'))
     771            return
     772        if not isinstance(child_id, list):
     773            child_id = [child_id]
     774        deleted = []
     775        for id in child_id:
     776            fullpath = os.path.join(self.context.storage, id)
     777            try:
     778                os.remove(fullpath)
     779                deleted.append(id)
     780            except OSError:
     781                self.flash(_('OSError: The file could not be deleted.'))
     782                return
     783        if len(deleted):
     784            self.flash(_('Successfully deleted: ${a}',
     785                mapping = {'a': ', '.join(deleted)}))
     786            logger.info('%s: Deleted files %s' % (
     787                self.request.principal.id, ', '.join(deleted)))
     788        return
    757789
    758790class DatacenterUploadPage(KofaPage):
    759791    grok.context(IDataCenter)
    760792    grok.name('upload')
    761     grok.require('waeup.manageUniversity')
     793    grok.require('waeup.manageDataCenter')
    762794    label = _(u'Upload file')
    763795    pnav = 0
     
    773805        try:
    774806            filename = uploadfile.filename
     807            if 'pending' in filename:
     808                self.flash(_("You can't re-upload pending data files."))
     809                return
     810            if not filename.endswith('.csv'):
     811                self.flash(_("Only csv files are allowed."))
     812                return
    775813            target = os.path.join(self.context.storage,
    776814                                  self.getNormalizedFileName(filename))
    777815            open(target, 'wb').write(uploadfile.read())
    778816            logger = self.context.logger
    779             logger.info('%s: Uploaded file "%s"' % (
     817            logger.info('%s: Uploaded file %s' % (
    780818                self.request.principal.id, target))
    781819        except IOError:
     
    801839        return '%s_%s%s' % (base, filtered_username, ext.lower())
    802840
     841class FileDownloadView(UtilityView, grok.View):
     842    grok.context(IDataCenter)
     843    grok.name('download')
     844    grok.require('waeup.manageDataCenter')
     845
     846    def update(self, filename=None):
     847        self.filename = self.request.form['filename']
     848        return
     849
     850    def render(self):
     851        self.response.setHeader(
     852            'Content-Type', 'text/csv; charset=UTF-8')
     853        self.response.setHeader(
     854            'Content-Disposition:', 'attachment; filename="%s.csv' %
     855            self.filename.replace('.csv',''))
     856        fullpath = os.path.join(self.context.storage, self.filename)
     857        return open(fullpath, 'rb').read()
     858
    803859class DatacenterImportStep1(KofaPage):
    804860    """Manual import step 1: choose file
     
    807863    grok.name('import1')
    808864    grok.template('datacenterimport1page')
    809     grok.require('waeup.manageUniversity')
     865    grok.require('waeup.manageDataCenter')
    810866    label = _(u'Process CSV file')
    811867    pnav = 0
     
    837893    grok.name('import2')
    838894    grok.template('datacenterimport2page')
    839     grok.require('waeup.manageUniversity')
     895    grok.require('waeup.manageDataCenter')
    840896    label = _(u'Process CSV file')
    841897    pnav = 0
     
    9571013    grok.name('import3')
    9581014    grok.template('datacenterimport3page')
    959     grok.require('waeup.manageUniversity')
     1015    grok.require('waeup.manageDataCenter')
    9601016    label = _(u'Process CSV file')
    9611017    pnav = 0
     
    10651121    grok.name('import4')
    10661122    grok.template('datacenterimport4page')
    1067     grok.require('waeup.manageUniversity')
     1123    grok.require('waeup.manageDataCenter')
    10681124    label = _(u'Process CSV file')
    10691125    pnav = 0
     
    11831239    grok.name('manage')
    11841240    grok.template('datacentermanagepage')
    1185     grok.require('waeup.manageUniversity')
     1241    grok.require('waeup.manageDataCenter')
    11861242    label = _('Edit data center settings')
    11871243    pnav = 0
  • main/waeup.kofa/trunk/src/waeup/kofa/browser/templates/datacenterpage.pt

    r7811 r8366  
    99</p>
    1010
    11 <table i18n:domain="waeup.kofa">
    12   <thead>
    13     <tr>
    14       <th i18n:translate="">File</th>
    15       <th i18n:translate="">Size</th>
    16       <th i18n:translate="">Upload Date</th>
    17     </tr>
    18   </thead>
    19   <tbody>
    20     <tr tal:repeat="file context/getFiles">
    21       <td tal:content="file/name">FILENAME</td>
    22       <td tal:content="file/size">12 k</td>
    23       <td tal:content="file/uploaddate">Mar 12, 2008</td>
    24     </tr>
    25   </tbody>
    26 </table>
     11<form action="." tal:attributes="action request/URL" method="post"
     12      i18n:domain="waeup.kofa" enctype="multipart/form-data">
     13    <table i18n:domain="waeup.kofa" class="display dataTableManage">
     14      <thead>
     15        <tr>
     16          <th></th>
     17          <th i18n:translate="">File</th>
     18          <th i18n:translate="">Size</th>
     19          <th i18n:translate="">Upload Date</th>
     20        </tr>
     21      </thead>
     22      <tbody>
     23        <tr tal:repeat="file context/getFiles" class="gradeC">
     24          <td>
     25            <input type="checkbox" name="val_id"
     26                         tal:attributes="value file/name" />
     27          </td>
     28          <td><a tal:attributes="href python: 'download?filename=' + file.name"
     29                 tal:content="file/name">FILENAME</a></td>
     30          <td tal:content="file/size">12 k</td>
     31          <td tal:content="file/uploaddate">Mar 12, 2008</td>
     32        </tr>
     33      </tbody>
     34    </table>
     35      <div tal:condition="view/availableActions">
     36        <span tal:repeat="action view/actions" tal:omit-tag="">
     37          <input tal:replace="structure action/render"/>
     38        </span>
     39      </div>
     40</form>
Note: See TracChangeset for help on using the changeset viewer.