1.1 --- a/XSLForms/Fields.py Sat Nov 29 02:11:56 2008 +0100
1.2 +++ b/XSLForms/Fields.py Sat Mar 21 00:29:05 2009 +0100
1.3 @@ -5,7 +5,7 @@
1.4 Interpretation of field collections from sources such as HTTP request parameter
1.5 dictionaries.
1.6
1.7 -Copyright (C) 2005, 2006, 2007, 2008 Paul Boddie <paul@boddie.org.uk>
1.8 +Copyright (C) 2005, 2006, 2007, 2008, 2009 Paul Boddie <paul@boddie.org.uk>
1.9
1.10 This program is free software; you can redistribute it and/or modify it under
1.11 the terms of the GNU Lesser General Public License as published by the Free
1.12 @@ -59,6 +59,8 @@
1.13 except NameError:
1.14 from sets import Set as set
1.15
1.16 +FILE_NAMESPACE = "http://www.boddie.org.uk/ns/xmltools/file-upload"
1.17 +
1.18 class FieldsError(Exception):
1.19 pass
1.20
1.21 @@ -88,8 +90,14 @@
1.22
1.23 """
1.24 Complete the given 'documents' using the 'fields' items list.
1.25 +
1.26 + Include a special entry in 'documents' for the key None,
1.27 + referencing a list of file upload tuples of the form
1.28 + (filename, content).
1.29 """
1.30
1.31 + files = []
1.32 +
1.33 for field, value in fields:
1.34
1.35 # Ignore selectors.
1.36 @@ -119,15 +127,7 @@
1.37 if self.values_are_lists:
1.38 value = value[0]
1.39
1.40 - # Convert the value to Unicode if necessary.
1.41 -
1.42 - if type(value) == type(""):
1.43 - value = unicode(value, encoding=self.encoding)
1.44 -
1.45 - # Remove CR characters, ignoring non-textual parameters.
1.46 -
1.47 - if isinstance(value, (str, unicode)):
1.48 - node.setAttributeNS(EMPTY_NAMESPACE, t[0], value.replace("\r", ""))
1.49 + self._set_attribute(node, t[0], value, files)
1.50 break
1.51
1.52 elif len(t) == 2:
1.53 @@ -156,16 +156,33 @@
1.54 name = t[0]
1.55 for subvalue in values:
1.56 subnode = self._append_element(node, name)
1.57 + self._set_attribute(subnode, t[2], subvalue, files)
1.58
1.59 - # Convert the value to Unicode if necessary.
1.60 + documents[None] = files
1.61 +
1.62 + def _set_attribute(self, node, name, value, files):
1.63 +
1.64 + """
1.65 + Set an attribute on 'node' having the given 'name' and 'value', adding
1.66 + entries to the 'files' list if file upload fields are detected.
1.67 + """
1.68 +
1.69 + # Convert the value to Unicode if necessary.
1.70
1.71 - if type(subvalue) == type(""):
1.72 - subvalue = unicode(subvalue, encoding=self.encoding)
1.73 + if type(value) == type(""):
1.74 + value = unicode(value, encoding=self.encoding)
1.75 +
1.76 + # Remove CR characters, ignoring non-textual parameters.
1.77
1.78 - # Remove CR characters, ignoring non-textual parameters.
1.79 + if isinstance(value, (str, unicode)):
1.80 + node.setAttributeNS(EMPTY_NAMESPACE, name, value.replace("\r", ""))
1.81 +
1.82 + # Handle file uploads having certain attributes.
1.83
1.84 - if isinstance(subvalue, (str, unicode)):
1.85 - subnode.setAttributeNS(EMPTY_NAMESPACE, t[2], subvalue.replace("\r", ""))
1.86 + elif hasattr(value, "content") and hasattr(value, "filename"):
1.87 + node.setAttributeNS(FILE_NAMESPACE, "file:" + name, str(len(files)))
1.88 + node.setAttributeNS(EMPTY_NAMESPACE, name, value.filename)
1.89 + files.append((value.filename, value.content))
1.90
1.91 def complete_selectors(self, selectors, fields, documents, create):
1.92
1.93 @@ -466,6 +483,15 @@
1.94 parameters.append((parameter_name, value))
1.95 return FieldProcessor.get_selectors(self, parameters, self.documents, create)
1.96
1.97 + def get_files(self):
1.98 +
1.99 + """
1.100 + Get the uploaded file details as a list of tuples of the form
1.101 + (filename, content).
1.102 + """
1.103 +
1.104 + return self.documents.get(None, [])
1.105 +
1.106 def new_instance(self, name):
1.107
1.108 """