[Checkins] SVN: z3c.form/trunk/src/z3c/form/util. Fix
extractFileName method, now it doesn't remove dots from a filename
Roger Ineichen
roger at projekt01.ch
Thu Jun 7 20:07:55 EDT 2007
Log message for revision 76482:
Fix extractFileName method, now it doesn't remove dots from a filename
Added a couple tests for extractFileName
Changed:
U z3c.form/trunk/src/z3c/form/util.py
U z3c.form/trunk/src/z3c/form/util.txt
-=-
Modified: z3c.form/trunk/src/z3c/form/util.py
===================================================================
--- z3c.form/trunk/src/z3c/form/util.py 2007-06-07 21:16:56 UTC (rev 76481)
+++ z3c.form/trunk/src/z3c/form/util.py 2007-06-08 00:07:54 UTC (rev 76482)
@@ -22,6 +22,7 @@
import zope.contenttype
from z3c.form import interfaces
+from z3c.i18n import MessageFactory as _
_identifier = re.compile('[A-Za-z][a-zA-Z0-9_]*$')
@@ -84,25 +85,24 @@
return zope.contenttype.guess_content_type(widget.filename)[0]
-def extractFileName(form, id, cleanup=True):
+def extractFileName(form, id, cleanup=True, allowEmtpyPostFix=False):
"""Knows how to extract a filename if a IBytes/IFileWidget was used.
Uploads from win/IE need some cleanup because the filename includes also
the path. cleanUp=True will do this for you.
"""
widget = getWidgetById(form, id)
+ if not allowEmtpyPostFix or cleanup:
+ # we need to strip out the path part even if we not reomve them later,
+ # because we just need ot check the filename extension
+ cleanFileName = widget.filename.split('\\')[-1]
+ cleanFileName = cleanFileName.split('/')[-1]
+ dottedParts = cleanFileName.split('.')
+ if not allowEmtpyPostFix:
+ if len(dottedParts) <= 1:
+ raise ValueError(_('Missing filename extension.'))
if cleanup:
- # we try to use a better file name. Uploads from win/IE need some
- # cleanup because the filename includes also the path.
- path = widget.filename.split('.')
- if len(path) > 1:
- postfix = path.pop(-1)
- # now remove the rest of the path
- pathStr = path.pop(-1)
- cleanFileName = pathStr.split('\\')[-1]
- cleanFileName = cleanFileName.split('/')[-1]
- filename = cleanFileName +'.'+ postfix
- return filename
+ return cleanFileName
return widget.filename
Modified: z3c.form/trunk/src/z3c/form/util.txt
===================================================================
--- z3c.form/trunk/src/z3c/form/util.txt 2007-06-07 21:16:56 UTC (rev 76481)
+++ z3c.form/trunk/src/z3c/form/util.txt 2007-06-08 00:07:54 UTC (rev 76482)
@@ -46,3 +46,138 @@
If the widget is not found but the prefix is correct, ``None`` is returned:
>>> util.getWidgetById(addPerson, 'form.widgets.myname')
+
+
+extractFileName
+---------------
+
+Test the filename extraction method:
+
+ >>> class IDocument(zope.interface.Interface):
+ ... data = zope.schema.Bytes(title=u'Data')
+
+Define a widgets stub and a upload widget stub class and setup them as a
+faked form:
+
+ >>> class FileUploadWidgetStub(object):
+ ... def __init__(self):
+ ... self.filename = None
+
+ >>> class WidgetsStub(object):
+ ... def __init__(self):
+ ... self.data = FileUploadWidgetStub()
+ ... self.prefix = 'widgets.'
+ ... def get(self, name, default):
+ ... return self.data
+
+ >>> class FileUploadFormStub(form.AddForm):
+ ... def __init__(self):
+ ... self.widgets = WidgetsStub()
+ ...
+ ... def setFakeFileName(self, filename):
+ ... self.widgets.data.filename = filename
+
+Now we can setup the stub form. Note this form is just a fake it's not a real
+implementation. We just provide a form like class which simulates the
+FileUpload object in the a widget. See z3c.form.browser.file.txt for a real
+file upload test uscase:
+
+ >>> uploadForm = FileUploadFormStub()
+ >>> uploadForm.setFakeFileName('foo.txt')
+
+And extract the filename
+
+ >>> util.extractFileName(uploadForm, 'form.widgets.data', cleanup=True)
+ 'foo.txt'
+
+Test a unicode filename:
+
+ >>> uploadForm.setFakeFileName(u'foo.txt')
+ >>> util.extractFileName(uploadForm, 'form.widgets.data', cleanup=True)
+ u'foo.txt'
+
+Test a windows IE uploaded filename:
+
+ >>> uploadForm.setFakeFileName(u'D:\\some\\folder\\foo.txt')
+ >>> util.extractFileName(uploadForm, 'form.widgets.data', cleanup=True)
+ u'foo.txt'
+
+Test another filename:
+
+ >>> uploadForm.setFakeFileName(u'D:/some/folder/foo.txt')
+ >>> util.extractFileName(uploadForm, 'form.widgets.data', cleanup=True)
+ u'foo.txt'
+
+Test another filename:
+
+ >>> uploadForm.setFakeFileName(u'/tmp/folder/foo.txt')
+ >>> util.extractFileName(uploadForm, 'form.widgets.data', cleanup=True)
+ u'foo.txt'
+
+Test special characters in filename, e.g. dots:
+
+ >>> uploadForm.setFakeFileName(u'/tmp/foo.bar.txt')
+ >>> util.extractFileName(uploadForm, 'form.widgets.data', cleanup=True)
+ u'foo.bar.txt'
+
+Test some other special characters in filename:
+
+ >>> uploadForm.setFakeFileName(u'/tmp/foo-bar.v.0.1.txt')
+ >>> util.extractFileName(uploadForm, 'form.widgets.data', cleanup=True)
+ u'foo-bar.v.0.1.txt'
+
+Test special characters in file path of filename:
+
+ >>> uploadForm.setFakeFileName(u'/tmp-v.1.0/foo-bar.v.0.1.txt')
+ >>> util.extractFileName(uploadForm, 'form.widgets.data', cleanup=True)
+ u'foo-bar.v.0.1.txt'
+
+Test optional keyword arguments. But remember it's hard for Zope to guess the
+content type for filenames without extensions:
+
+ >>> uploadForm.setFakeFileName(u'minimal')
+ >>> util.extractFileName(uploadForm, 'form.widgets.data', cleanup=True,
+ ... allowEmtpyPostFix=True)
+ u'minimal'
+
+ >>> uploadForm.setFakeFileName(u'/tmp/minimal')
+ >>> util.extractFileName(uploadForm, 'form.widgets.data', cleanup=True,
+ ... allowEmtpyPostFix=True)
+ u'minimal'
+
+ >>> uploadForm.setFakeFileName(u'D:\\some\\folder\\minimal')
+ >>> util.extractFileName(uploadForm, 'form.widgets.data', cleanup=True,
+ ... allowEmtpyPostFix=True)
+ u'minimal'
+
+There will be a ValueError if we get a empty filename by default:
+
+ >>> uploadForm.setFakeFileName(u'/tmp/minimal')
+ >>> util.extractFileName(uploadForm, 'form.widgets.data', cleanup=True)
+ Traceback (most recent call last):
+ ...
+ ValueError: Missing filename extension.
+
+We also can skip removing a path from a upload. Note only IE will upload a
+path in a upload ``<input type="file" ...>`` field:
+
+ >>> uploadForm.setFakeFileName(u'/tmp/foo.txt')
+ >>> util.extractFileName(uploadForm, 'form.widgets.data', cleanup=False)
+ u'/tmp/foo.txt'
+
+ >>> uploadForm.setFakeFileName(u'/tmp-v.1.0/foo-bar.v.0.1.txt')
+ >>> util.extractFileName(uploadForm, 'form.widgets.data', cleanup=False)
+ u'/tmp-v.1.0/foo-bar.v.0.1.txt'
+
+ >>> uploadForm.setFakeFileName(u'D:\\some\\folder\\foo.txt')
+ >>> util.extractFileName(uploadForm, 'form.widgets.data', cleanup=False)
+ u'D:\\some\\folder\\foo.txt'
+
+And missing filename extensions are also not allowed by deafault if we skip
+the filename:
+
+ >>> uploadForm.setFakeFileName(u'/tmp/minimal')
+ >>> util.extractFileName(uploadForm, 'form.widgets.data', cleanup=False)
+ Traceback (most recent call last):
+ ...
+ ValueError: Missing filename extension.
More information about the Checkins
mailing list