Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 134 lines (114 sloc) 5.124 kb
d74f19cb »
2010-03-21 Start jamming templates in.
1 import colander
2
3 from deform import widget
cabbf309 »
2010-04-22 Use internatl.
4 from deform.i18n import _
d74f19cb »
2010-03-21 Start jamming templates in.
5
bdbe41d7 »
2010-05-08 Remove most schema-related APIs in order to make the task of document…
6 default_widget_makers = {
7 colander.Mapping: widget.MappingWidget,
8 colander.Sequence: widget.SequenceWidget,
9 colander.String: widget.TextInputWidget,
10 colander.Integer: widget.TextInputWidget,
11 colander.Float: widget.TextInputWidget,
12 colander.Decimal: widget.TextInputWidget,
13 colander.Boolean: widget.CheckboxWidget,
14 colander.Date: widget.DatePartsWidget,
15 colander.Tuple: widget.Widget, # null
16 }
0106c22a »
2010-05-03 Add TextAreaCSVWidget.
17
ad124874 »
2010-03-29 Add schema elements for file data.
18 class FileData(object):
19 """
20 A type representing file data; used to shuttle data back and forth
22fad465 »
2010-03-29 Tweak.
21 between an application and the
22 :class:`deform.widget.FileUploadWidget` widget.
ad124874 »
2010-03-29 Add schema elements for file data.
23
24 This type passes the value obtained during deserialization back to
25 the caller unchanged. It serializes from a dictionary containing
26 partial file data info into a dictionary containing full file data
27 info, serializing the full file data (the widget receives the full
28 file data).
29 """
30
31 # We cant use FileUploadWidget as the default_widget_maker for
32 # this schema node because it requires a tmpstore argument, and
33 # a tmpstore cannot be generally defaulted.
34
35 def serialize(self, node, value):
36 """
37 Serialize a dictionary representing partial file information
38 to a dictionary containing information expected by a file
39 upload widget.
40
41 The file data dictionary passed as ``value`` to this
42 ``serialize`` method *must* include:
43
44 filename
45 Filename of this file (not a full filesystem path, just the
46 filename itself).
f68b3eb4 »
2010-04-01 Docstring.
47
ad124874 »
2010-03-29 Add schema elements for file data.
48 uid
49 Unique string id for this file. Needs to be unique enough to
50 disambiguate it from other files that may use the same
51 temporary storage mechanism before a successful validation,
52 and must be adequate for the calling code to reidentify it
53 after deserialization.
54
55 A fully populated dictionary *may* also include the following
56 values:
57
58 fp
59 File-like object representing this file's content or
60 ``None``. ``None`` indicates this file has already been
61 committed to permanent storage. When serializing a
62 'committed' file, the ``fp`` value should ideally not be
63 passed or it should be passed as ``None``; ``None`` as an
64 ``fp`` value is a signifier to the file upload widget that
65 the file data has already been committed. Using ``None``
66 as an ``fp`` value helps prevent unnecessary data copies
67 to temporary storage when a form is rendered, however its
68 use requires cooperation from the calling code; in
69 particular, the calling code must be willing to translate
70 a ``None`` ``fp`` value returned from a deserialization
71 into the file data via the ``uid`` in the deserialization.
f68b3eb4 »
2010-04-01 Docstring.
72
ad124874 »
2010-03-29 Add schema elements for file data.
73 mimetype
74 File content type (e.g. ``application/octet-stream``).
f68b3eb4 »
2010-04-01 Docstring.
75
ad124874 »
2010-03-29 Add schema elements for file data.
76 size
77 File content length (integer).
f68b3eb4 »
2010-04-01 Docstring.
78
ad124874 »
2010-03-29 Add schema elements for file data.
79 preview_url
80 URL which provides an image preview of this file's data.
81
82 If a ``size`` is not provided, the widget will have no access
83 to size display data. If ``preview_url`` is not provided, the
84 widget will not be able to show a file preview. If
85 ``mimetype`` is not provided, the widget will not be able to
86 display mimetype information.
87 """
88 if not hasattr(value, 'get'):
cabbf309 »
2010-04-22 Use internatl.
89 mapping = {'value':repr(value)}
90 raise colander.Invalid(
91 node,
92 _('${value} is not a dictionary', mapping=mapping)
93 )
ad124874 »
2010-03-29 Add schema elements for file data.
94 for n in ('filename', 'uid'):
95 if not n in value:
cabbf309 »
2010-04-22 Use internatl.
96 mapping = {'value':repr(value), 'key':n}
97 raise colander.Invalid(
98 node,
99 _('${value} has no ${key} key', mapping=mapping)
100 )
ad124874 »
2010-03-29 Add schema elements for file data.
101 result = {}
102 result['filename'] = value['filename']
103 result['uid'] = value['uid']
104 result['mimetype'] = value.get('mimetype')
105 result['size'] = value.get('size')
106 result['fp'] = value.get('fp')
107 result['preview_url'] = value.get('preview_url')
108 return result
109
110 def deserialize(self, node, value):
111 if not value and node.required:
cabbf309 »
2010-04-22 Use internatl.
112 raise colander.Invalid(node, _('Required'))
ad124874 »
2010-03-29 Add schema elements for file data.
113 return value
114
7330dda4 »
2010-04-12 Make checkbox choices work by adding a Set schema type.
115 class Set(object):
ea8a5c05 »
2010-05-04 Make docs render.
116 """ A type representing a non-overlapping set of items.
117 Deserializes an iterable to a ``set`` object. """
bdbe41d7 »
2010-05-08 Remove most schema-related APIs in order to make the task of document…
118 widget_maker = widget.CheckboxChoiceWidget
7330dda4 »
2010-04-12 Make checkbox choices work by adding a Set schema type.
119 def deserialize(self, node, value):
120 if not hasattr(value, '__iter__'):
a43b4b6f »
2010-04-13 i18n-ize the messages owned by deform.
121 raise colander.Invalid(
122 node,
cabbf309 »
2010-04-22 Use internatl.
123 _('${value} is not iterable', mapping={'value':value})
a43b4b6f »
2010-04-13 i18n-ize the messages owned by deform.
124 )
7330dda4 »
2010-04-12 Make checkbox choices work by adding a Set schema type.
125 if not value:
126 if node.required:
cabbf309 »
2010-04-22 Use internatl.
127 raise colander.Invalid(node, _('Required'))
7330dda4 »
2010-04-12 Make checkbox choices work by adding a Set schema type.
128 value = node.default
129 return set(value)
130
131 def serialize(self, node, value):
132 return value
133
Something went wrong with that request. Please try again.