22 |
# $Id$ |
# $Id$ |
23 |
|
|
24 |
import types |
import types |
25 |
from mx import DateTime |
import mx.DateTime |
26 |
|
import mx.DateTime.ISO |
27 |
|
|
28 |
# ============================================================================= |
# ============================================================================= |
29 |
# Instance class |
# Instance class |
41 |
self._classdef = classdef |
self._classdef = classdef |
42 |
|
|
43 |
# --------------------------------------------------------------------------- |
# --------------------------------------------------------------------------- |
44 |
# Get the value of a property |
# Get the value of a property and convert it into RPC-transportable datatype |
45 |
# --------------------------------------------------------------------------- |
# --------------------------------------------------------------------------- |
46 |
|
|
47 |
def _getValue (self, propertyname): |
def _getValue (self, propertyname): |
48 |
propertydef = self._classdef.properties [propertyname] |
propertydef = self._classdef.properties [propertyname] |
49 |
|
value = self._record.getField (propertydef.column) |
50 |
|
|
51 |
|
# Always return an empty string for NULL |
52 |
|
if value is None: |
53 |
|
return "" |
54 |
|
|
55 |
|
# String property (the id is actually a string, too) |
56 |
if propertydef.gnue_type == "id" or \ |
if propertydef.gnue_type == "id" or \ |
57 |
propertydef.gnue_type == "string": |
propertydef.gnue_type == "string": |
58 |
# String property (the id is actually a string, too) |
if type (value) == types.UnicodeType: |
|
s = self._record.getField (propertydef.column) |
|
|
if s is None: |
|
|
return "" |
|
|
elif type (s) == types.UnicodeType: |
|
59 |
# encode unicode values to utf-8 (normal case) |
# encode unicode values to utf-8 (normal case) |
60 |
return s.encode ('utf-8') |
return value.encode ('utf-8') |
61 |
elif type (s) == types.StringType: |
elif type (value) == types.StringType: |
62 |
# if not UnicodeType then return normal string |
# if not UnicodeType then return normal string |
63 |
return s |
return value |
64 |
else: |
else: |
65 |
raise Exception, ("Database returned invalid value '%s' for " + \ |
raise Exception, ("Database returned invalid value '%s' for " + \ |
66 |
"property '%s'") % (s, propertyname) |
"property '%s'") % (repr (value), propertyname) |
67 |
|
|
68 |
|
|
69 |
|
# Number property |
70 |
elif propertydef.gnue_type == "number": |
elif propertydef.gnue_type == "number": |
71 |
# Number property |
try: |
72 |
s = self._record.getField (propertydef.column) |
if propertydef.gnue_scale == 0: |
73 |
if s is None: |
# ... without fractional part |
74 |
return "" |
return int (value) |
75 |
elif propertydef.gnue_scale == 0: |
else: |
76 |
# ... without fractional part |
# ... with fractional part |
77 |
try: |
return float (value) |
78 |
return int (s) |
except ValueError: |
79 |
except ValueError: |
raise Exception, ("Database returned invalid value '%s' for " + \ |
80 |
raise Exception, ("Database returned invalid value '%s' for " + \ |
"property '%s'") % (repr (value), propertyname) |
|
"property '%s'") % (s, propertyname) |
|
|
else: |
|
|
# ... with fractional part |
|
|
try: |
|
|
return float (s) |
|
|
except ValueError: |
|
|
raise Exception, ("Database returned invalid value '%s' for " + \ |
|
|
"property '%s'") % (s, propertyname) |
|
81 |
|
|
82 |
|
# Date property |
83 |
elif propertydef.gnue_type == "date": |
elif propertydef.gnue_type == "date": |
84 |
s = self._record.getField (propertydef.column) |
if type (value) == mx.DateTime.DateTimeType: # DateTime DB field |
85 |
if s is None: # NULL |
return value.date |
86 |
return "" |
elif type (value) == types.UnicodeType: # String DB field w/ Unicode |
|
elif type (s) == types.UnicodeType: # String DB field w/ Unicode |
|
87 |
# TODO: Check if valid date |
# TODO: Check if valid date |
88 |
return s.encode ('utf-8') |
return value.encode ('utf-8') |
89 |
elif type (s) == types.StringType: # String DB field w/o Unicode |
elif type (value) == types.StringType: # String DB field w/o Unicode |
90 |
# TODO: Check if valid date |
# TODO: Check if valid date |
91 |
return s |
return value |
|
elif type (s) == DateTime.DateTimeType: # DateTime DB field |
|
|
return s.date |
|
92 |
else: |
else: |
93 |
raise Exception, ("Database returned invalid value '%s' for " + \ |
raise Exception, ("Database returned invalid value '%s' for " + \ |
94 |
"property '%s'") % (s, propertyname) |
"property '%s'") % (repr (value), propertyname) |
95 |
|
|
96 |
|
# Time property |
97 |
elif propertydef.gnue_type == "time": |
elif propertydef.gnue_type == "time": |
98 |
s = self._record.getField (propertydef.column) |
if type (value) == mx.DateTime.DateTimeType: # DateTime DB field |
99 |
if s is None: # NULL |
return value.time |
100 |
return "" |
elif type (value) == types.UnicodeType: # String DB field w/ Unicode |
|
elif type (s) == types.UnicodeType: # String DB field w/ Unicode |
|
101 |
# TODO: Check if valid time |
# TODO: Check if valid time |
102 |
return s.encode ('utf-8') |
return value.encode ('utf-8') |
103 |
elif type (s) == types.StringType: # String DB field w/o Unicode |
elif type (value) == types.StringType: # String DB field w/o Unicode |
104 |
# TODO: Check if valid time |
# TODO: Check if valid time |
105 |
return s |
return value |
|
elif type (s) == DateTime.DateTimeType: # DateTime DB field |
|
|
return s.time |
|
106 |
else: |
else: |
107 |
raise Exception, ("Database returned invalid value '%s' for " + \ |
raise Exception, ("Database returned invalid value '%s' for " + \ |
108 |
"property '%s'") % (s, propertyname) |
"property '%s'") % (repr (value), propertyname) |
109 |
|
|
110 |
|
# Date/Time property |
111 |
elif propertydef.gnue_type == "datetime": |
elif propertydef.gnue_type == "datetime": |
112 |
s = self._record.getField (propertydef.column) |
if type (value) == mx.DateTime.DateTimeType: # DateTime DB field |
113 |
if s is None: # NULL |
return value.date + " " + value.time |
114 |
return "" |
elif type (value) == types.UnicodeType: # String DB field w/ Unicode |
|
elif type (s) == types.UnicodeType: # String DB field w/ Unicode |
|
115 |
# TODO: Check if valid datetime |
# TODO: Check if valid datetime |
116 |
return s.encode ('utf-8') |
return value.encode ('utf-8') |
117 |
elif type (s) == types.StringType: # String DB field w/o Unicode |
elif type (value) == types.StringType: # String DB field w/o Unicode |
118 |
# TODO: Check if valid datetime |
# TODO: Check if valid datetime |
119 |
return s |
return value |
|
elif type (s) == DateTime.DateTimeType: # DateTime DB field |
|
|
return s.date + " " + s.time |
|
120 |
else: |
else: |
121 |
raise Exception, ("Database returned invalid value '%s' for " + \ |
raise Exception, ("Database returned invalid value '%s' for " + \ |
122 |
"property '%s'") % (s, propertyname) |
"property '%s'") % (repr (value), propertyname) |
123 |
|
|
124 |
|
# Boolean property |
125 |
elif propertydef.gnue_type == "boolean": |
elif propertydef.gnue_type == "boolean": |
126 |
# Boolean property |
if value in [0, "0", "f", "F", "false", "FALSE", "n", "N", "no", "NO"]: |
|
s = self._record.getField (propertydef.column) |
|
|
if s is None: |
|
|
return "" |
|
|
elif s in [0, "0", "f", "F", "false", "FALSE", "n", "N", "no", "NO"]: |
|
127 |
return 0 |
return 0 |
128 |
elif s in [1, "1", "t", "T", "true", "true", "y", "Y", "yes", "YES"]: |
elif value in [1, "1", "t", "T", "true", "true", "y", "Y", "yes", "YES"]: |
129 |
return 1 |
return 1 |
130 |
else: |
else: |
131 |
raise Exception, ("Database returned invalid value '%s' for " + \ |
raise Exception, ("Database returned invalid value '%s' for " + \ |
132 |
"property '%s'") % (s, propertyname) |
"property '%s'") % (repr (value), propertyname) |
133 |
|
|
134 |
|
# Reference property: gnue_type is a classname |
135 |
elif self._classdef.classes.has_key (propertydef.gnue_type): |
elif self._classdef.classes.has_key (propertydef.gnue_type): |
136 |
# Reference property: gnue_type is a classname |
return value |
|
s = self._record.getField (propertydef.column) |
|
|
if s is None: |
|
|
return "" |
|
|
else: |
|
|
return s |
|
137 |
|
|
138 |
# TODO: Missing property types: |
# TODO: Missing property types: |
139 |
# * list properties |
# * list properties |
140 |
# * calculated properties (must have triggers working first) |
# * calculated properties (must have triggers working first) |
141 |
|
|
142 |
else: |
else: |
143 |
raise Exception, "Field type '%s' not defined." % propertydef.gnue_type |
raise Exception, "Property type '%s' not defined" % propertydef.gnue_type |
144 |
|
|
145 |
# --------------------------------------------------------------------------- |
# --------------------------------------------------------------------------- |
146 |
# Get the values of a list of properties |
# Get the values of a list of properties |
153 |
return result |
return result |
154 |
|
|
155 |
# --------------------------------------------------------------------------- |
# --------------------------------------------------------------------------- |
156 |
# Set the value of a property |
# Set the value of a property and convert it into native datatype |
157 |
# --------------------------------------------------------------------------- |
# --------------------------------------------------------------------------- |
158 |
|
|
159 |
def _putValue (self, propertyname, value): |
def _putValue (self, propertyname, value): |
160 |
propertydef = self._classdef.properties [propertyname] |
propertydef = self._classdef.properties [propertyname] |
161 |
|
|
162 |
# TODO: from property type: add more tests to see if the new value is valid |
# TODO: add more tests to see if the new value is valid, wrt max length |
163 |
|
|
164 |
|
# NULL |
165 |
if value == "": |
if value == "": |
166 |
value = None |
value = None |
167 |
|
|
168 |
|
# String property (the id is actually a string, too) |
169 |
elif propertydef.gnue_type == "id" or \ |
elif propertydef.gnue_type == "id" or \ |
170 |
propertydef.gnue_type == "string": |
propertydef.gnue_type == "string": |
|
# String property (the id is actually a string, too) |
|
171 |
if type (value) is not types.StringType: |
if type (value) is not types.StringType: |
172 |
value = str (value) |
value = str (value) |
173 |
# decode unicode values from utf-8 |
# decode unicode values from utf-8 |
174 |
value = unicode (value, 'utf-8') |
value = unicode (value, 'utf-8') |
175 |
|
|
176 |
|
# Number property |
177 |
elif propertydef.gnue_type == "number": |
elif propertydef.gnue_type == "number": |
178 |
# TODO: check if value is a valid number, probably convert into int/float |
try: |
179 |
pass |
if propertydef.gnue_scale == 0: |
180 |
|
# ... without fractional part |
181 |
|
value = int (value) |
182 |
|
else: |
183 |
|
# ... with fractional part |
184 |
|
value = float (value) |
185 |
|
except ValueError: |
186 |
|
raise Exception, "Invalid value '%s' for property '%s'" % \ |
187 |
|
(value, propertyname) |
188 |
|
|
189 |
|
# Date property |
190 |
elif propertydef.gnue_type == "date": |
elif propertydef.gnue_type == "date": |
191 |
# TODO: check if value is a valid date, probably convert into DateTime |
try: |
192 |
pass |
# Workaround: gnue-common's dbdriver for appserver post a datetime |
193 |
|
# value for date properties. So silently accept that. |
194 |
|
value = mx.DateTime.ISO.ParseDateTime (value) |
195 |
|
except ValueError: |
196 |
|
raise Exception, "Invalid value '%s' for property '%s'" % \ |
197 |
|
(value, propertyname) |
198 |
|
|
199 |
|
# Time property |
200 |
elif propertydef.gnue_type == "time": |
elif propertydef.gnue_type == "time": |
201 |
# TODO: check if value is a valid time, probably convert into DateTime |
try: |
202 |
pass |
# Workaround: gnue-common's dbdriver for appserver post a datetime |
203 |
|
# value for time properties. So silently accept that. |
204 |
|
value = mx.DateTime.ISO.ParseDateTime (value) |
205 |
|
except ValueError: |
206 |
|
raise Exception, "Invalid value '%s' for property '%s'" % \ |
207 |
|
(value, propertyname) |
208 |
|
|
209 |
|
# Date/Time property |
210 |
elif propertydef.gnue_type == "datetime": |
elif propertydef.gnue_type == "datetime": |
211 |
# TODO: check if value is valid, probably convert into DateTime |
try: |
212 |
pass |
value = mx.DateTime.ISO.ParseDateTime (value) |
213 |
|
except ValueError: |
214 |
|
raise Exception, "Invalid value '%s' for property '%s'" % \ |
215 |
|
(value, propertyname) |
216 |
|
|
217 |
|
# Boolean property |
218 |
elif propertydef.gnue_type == "boolean": |
elif propertydef.gnue_type == "boolean": |
|
# Boolean property |
|
219 |
if value in [0, "0", "f", "F", "false", "FALSE", "n", "N", "no", "NO"]: |
if value in [0, "0", "f", "F", "false", "FALSE", "n", "N", "no", "NO"]: |
220 |
value = 0 |
value = 0 |
221 |
elif value in [1, "1", "t", "T", "true", "true", "y", "Y", "yes", "YES"]: |
elif value in [1, "1", "t", "T", "true", "true", "y", "Y", "yes", "YES"]: |