205
206
def __call__(self, func):
206
207
really_wrapped_func = PATCHWrapper(func)
207
208
return super(PATCH, self).__call__(really_wrapped_func)
212
"""Get and set attributes on an object.
214
Most attributes are fairly simple - a getattr() or setattr() on the object
215
does the trick, with the appropriate encoding or decoding on the way in
216
and out. Encoding doesn't happen here though; the standard JSON library
217
handles most types, but see ExtendedEncoder for additional support.
219
Others are more complicated since they aren't kept in the model as direct
220
columns in the database. These will use subclasses of this base class.
221
Read-only attributes will have a decoder which always raises ValueError.
224
def __init__(self, decoder=None):
225
"""Create a getter/setter for a specific attribute.
227
:param decoder: The callable for decoding a web request value string
228
into the specific data type needed by the object's attribute. Use
229
None to indicate a read-only attribute. The callable should raise
230
ValueError when the web request value cannot be converted.
231
:type decoder: callable
233
self.decoder = decoder
235
def get(self, obj, attribute):
236
"""Return the named object attribute value.
238
:param obj: The object to access.
240
:param attribute: The attribute name.
241
:type attribute: string
242
:return: The attribute value, ready for JSON encoding.
245
return getattr(obj, attribute)
247
def put(self, obj, attribute, value):
248
"""Set the named object attribute value.
250
:param obj: The object to change.
252
:param attribute: The attribute name.
253
:type attribute: string
254
:param value: The new value for the attribute.
256
setattr(obj, attribute, value)
258
def __call__(self, value):
259
"""Convert the value to its internal format.
261
:param value: The web request value to convert.
263
:return: The converted value.
266
if self.decoder is None:
268
return self.decoder(value)