1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 """
48 This module defines a number of datatypes (dbattributes) for
49 particular purposes.
50 """
51
52 from cPickle import loads, dumps, HIGHEST_PROTOCOL
53 from string import *
54
55 from orm2.datatypes import datatype, Unicode
56 from orm2.validators import *
57 from orm2 import sql
58
60 """
61 SQL literal class for <b>valid</b> Unicode (idna) domain names and
62 email addresses.
63 """
65 if "@" in self.content:
66 local, remote = split(self.content, "@")
67 local = local.encode("ascii")
68 remote = remote.encode("idna")
69
70 s = "%s@%s" % ( local, remote, )
71 s = runner.ds.escape_string(s)
72 sql = runner.ds.string_quotes(s)
73 else:
74 s = self.content.encode("idna")
75 s = runner.ds.escape_string(s)
76 sql = runner.ds.string_quotes(s)
77
78 return sql
79
80
81
82 -class PDomain(Unicode):
83 """
84 Just like orm2.datatypes.Unicode, except that it doesn't use the
85 backend's charset to convert the Unicode string, but Python's idna
86 (Internationalized Domain Names in Applications) codec which takes
87 care of lowercasing and punicode representation and so on.
88 """
89 sql_literal_class = idna_literal
90
91 - def __init__(self, column=None, title=None, validators=(),
92 widget_specs=(), has_default=False):
100
101 - def __convert__(self, value):
102 if type(value) is not UnicodeType:
103 raise TypeError(
104 "You must set a PDomain property to a unicode value!")
105 else:
106 return value
107
108
110 """
111 Like PDomain above, but for e-Mail addresses. The local part will be
112 checked against a regular expression, the remote part will be treated
113 like a domain name by the PDomain class above.
114 """
115 sql_literal_class = idna_literal
116
117 - def __init__(self, column=None, title=None, validators=(),
118 has_default=False):
126
127
135
136
138 """
139 This datatype uses Python's pickle module to serialize (nearly)
140 arbitrary Python objects into a string representation that is then
141 stored in a regular database column. See U{http://localhost/Documentation/Python/Main/lib/module-pickle.html} for details on pickling.
142 """
143
144 - def __init__(self, pickle_protocol=HIGHEST_PROTOCOL,
145 column=None, title=None,
146 validators=(), has_default=False):
147 """
148 @param pickle_protocol: Version number of the protocol being used by
149 the pickle functions. See U{http://localhost/Documentation/Python/Main/lib/module-pickle.html} for details.
150 """
151 self.pickle_protocol = pickle_protocol
152 datatype.__init__(self, column, title, validators,
153 has_default)
154
155
162
164 """
165 Since we store the Python object 'as is', convert does nothing.
166 """
167 return value
168
170 """
171 This function takes care of converting the Python object into a
172 serialized string representation.
173 """
174 if not self.isset(dbobj):
175 msg = "This attribute has not been retrieved from the database."
176 raise AttributeError(msg)
177 else:
178 value = getattr(dbobj, self.data_attribute_name())
179
180 if value is None:
181 return sql.NULL
182 else:
183 pickled = dumps(value, self.pickle_protocol)
184 return sql.string_literal(pickled)
185
186
187
188
189
190
191