Changeset 4852 for waeup


Ignore:
Timestamp:
18 Jan 2010, 09:06:22 (15 years ago)
Author:
uli
Message:

When uploading files, insert username before filename extension.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • waeup/branches/ulif-importers/src/waeup/browser/pages.py

    r4848 r4852  
    66import grok
    77import os
     8import re
    89import sys
    910from hurry import yui
     
    270271        try:
    271272            filename = uploadfile.filename
    272             target = os.path.join(self.context.storage, filename)
     273            target = os.path.join(self.context.storage,
     274                                  self.getNormalizedFileName(filename))
    273275            open(target, 'wb').write(uploadfile.read())
    274276        except IOError:
     
    278280        self.redirect(self.url(self.context))
    279281
     282    def getNormalizedFileName(self, filename):
     283        """Build sane filename.
     284
     285        An uploaded file foo.csv will be stored as foo_USERNAME.csv
     286        where username is the principal id of the currently logged in
     287        user.
     288
     289        Spaces in filename are replaced by underscore.
     290        """
     291        username = self.request.principal.id
     292        filename = filename.replace(' ', '_')
     293        # Only accept typical filname chars...
     294        filtered_username = ''.join(re.findall('[a-zA-Z0-9_\.\-]', username))
     295        base, ext = os.path.splitext(filename)
     296        return '%s_%s%s' % (base, filtered_username, ext.lower())
    280297
    281298class DataCenterImportCSVPage(WAeUPPage):
Note: See TracChangeset for help on using the changeset viewer.