|
From: <sub...@co...> - 2005-11-18 21:57:57
|
Author: test
Date: 2005-11-18 21:57:50 +0000 (Fri, 18 Nov 2005)
New Revision: 1292
Modified:
FormEncode/trunk/formencode/validators.py
Log:
Added FileUploadKeeper (forgot from last commit)
Modified: FormEncode/trunk/formencode/validators.py
===================================================================
--- FormEncode/trunk/formencode/validators.py 2005-11-18 21:55:10 UTC (rev 1291)
+++ FormEncode/trunk/formencode/validators.py 2005-11-18 21:57:50 UTC (rev 1292)
@@ -30,10 +30,10 @@
urlparse = None
from interfaces import *
from api import *
-sha = random = None
-
-import cgi
-
+sha = random = None
+
+import cgi
+
import fieldstorage
True, False = (1==1), (0==1)
@@ -1127,20 +1127,80 @@
if match.group(4):
result = result + " ext.%s" % match.group(4)
return result
-
-class FieldStorageUploadConverter(FancyValidator):
-
- """
- Converts a cgi.FieldStorage instance to
- a value that FormEncode can use for file
- uploads.
- """
- def _to_python(self, value, state):
- if isinstance(value, cgi.FieldStorage):
- return fieldstorage.convert_fieldstorage(value)
- else:
- return value
+class FieldStorageUploadConverter(FancyValidator):
+
+ """
+ Converts a cgi.FieldStorage instance to
+ a value that FormEncode can use for file
+ uploads.
+ """
+ def _to_python(self, value, state):
+ if isinstance(value, cgi.FieldStorage):
+ return fieldstorage.convert_fieldstorage(value)
+ else:
+ return value
+
+class FileUploadKeeper(FancyValidator):
+ """
+ Takes two inputs (a dictionary with keys ``static`` and
+ ``upload``) and converts them into one value on the Python side (a
+ dictionary with ``filename`` and ``content`` keys). The upload
+ takes priority over the static value. The filename may be None if
+ it can't be discovered.
+
+ Handles uploads of both text and ``cgi.FieldStorage`` upload
+ values.
+
+ This is basically for use when you have an upload field, and you
+ want to keep the upload around even if the rest of the form
+ submission fails. When converting *back* to the form submission,
+ there may be extra values ``'original_filename'`` and
+ ``'original_content'``, which may want to use in your form to show
+ the user you still have their content around.
+ """
+
+ upload_key = 'upload'
+ static_key = 'static'
+
+ def _to_python(self, value, state):
+ upload = value.get(self.upload_key)
+ static = value.get(self.static_key, '').strip()
+ filename = content = None
+ if isinstance(upload, cgi.FieldStorage):
+ filename = upload.filename
+ content = upload.value
+ elif isinstance(upload, str) and upload:
+ filename = None
+ content = upload
+ if not content and static:
+ filename, content = static.split(None, 1)
+ if filename == '-':
+ filename = ''
+ else:
+ filename = filename.decode('base64')
+ content = content.decode('base64')
+ return {'filename': filename, 'content': content}
+
+ def _from_python(self, value, state):
+ filename = value.get('filename', '')
+ content = value.get('content', '')
+ if filename or content:
+ result = self.pack_content(filename, content)
+ return {self.upload_key: '',
+ self.static_key: result,
+ 'original_filename': filename,
+ 'original_content': content}
+ else:
+ return {self.upload_key: '',
+ self.static_key: ''}
+
+ def pack_content(self, filename, content):
+ enc_filename = self.base64encode(filename) or '-'
+ enc_content = (content or '').encode('base64')
+ result = '%s %s' % (enc_filename, enc_content)
+ return result
+
class DateConverter(FancyValidator):
"""
|