Merge lp:~cjwatson/lazr.restful/datetime-unmarshall into lp:lazr.restful

Proposed by Colin Watson
Status: Merged
Merged at revision: 296
Proposed branch: lp:~cjwatson/lazr.restful/datetime-unmarshall
Merge into: lp:lazr.restful
Diff against target: 86 lines (+25/-4)
4 files modified
NEWS.rst (+6/-0)
src/lazr/restful/_resource.py (+0/-4)
src/lazr/restful/docs/webservice-marshallers.rst (+12/-0)
src/lazr/restful/marshallers.py (+7/-0)
To merge this branch: bzr merge lp:~cjwatson/lazr.restful/datetime-unmarshall
Reviewer Review Type Date Requested Status
Cristian Gonzalez (community) Approve
Ioana Lasc (community) Approve
Review via email: mp+402905@code.launchpad.net

Commit message

Implement DateTimeFieldMarshaller.unmarshall.

Description of the change

This is cleaner than special-casing `date` and `datetime` objects in `ResourceJSONEncoder.default`.

To post a comment you must log in.
Revision history for this message
Ioana Lasc (ilasc) :
review: Approve
Revision history for this message
Cristian Gonzalez (cristiangsp) wrote :

Looks good!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'NEWS.rst'
2--- NEWS.rst 2021-05-14 12:46:54 +0000
3+++ NEWS.rst 2021-05-18 15:16:23 +0000
4@@ -2,6 +2,12 @@
5 NEWS for lazr.restful
6 =====================
7
8+1.0.3
9+=====
10+
11+Give ``DateTimeFieldMarshaller`` an ``unmarshall`` method, rather than
12+special-casing it elsewhere.
13+
14 1.0.2 (2021-05-14)
15 ==================
16
17
18=== modified file 'src/lazr/restful/_resource.py'
19--- src/lazr/restful/_resource.py 2021-05-14 09:10:28 +0000
20+++ src/lazr/restful/_resource.py 2021-05-18 15:16:23 +0000
21@@ -29,8 +29,6 @@
22 'WADL_SCHEMA_FILE',
23 ]
24
25-
26-from datetime import datetime, date
27 from email.utils import formatdate
28 import copy
29 from operator import itemgetter
30@@ -222,8 +220,6 @@
31
32 def default(self, obj):
33 """Convert the given object to a simple data structure."""
34- if isinstance(obj, datetime) or isinstance(obj, date):
35- return obj.isoformat()
36 if isProxy(obj):
37 # We have a security-proxied version of a built-in
38 # type. We create a new version of the type by copying the
39
40=== modified file 'src/lazr/restful/docs/webservice-marshallers.rst'
41--- src/lazr/restful/docs/webservice-marshallers.rst 2021-01-21 11:42:18 +0000
42+++ src/lazr/restful/docs/webservice-marshallers.rst 2021-05-18 15:16:23 +0000
43@@ -385,6 +385,12 @@
44 ...
45 ValueError: Value doesn't look like a date.
46
47+The unmarshall() method returns the ISO 8601 representation of the value.
48+
49+ >>> marshaller.unmarshall(
50+ ... None, marshaller.marshall_from_json_data('2009-07-07T13:45:00Z'))
51+ '2009-07-07T13:45:00+00:00'
52+
53 Date
54 ----
55
56@@ -406,6 +412,12 @@
57 >>> marshaller.marshall_from_json_data('2009-07-07T13:15:00+0000')
58 datetime.date(2009, 7, 7)
59
60+The unmarshall() method returns the ISO 8601 representation of the value.
61+
62+ >>> marshaller.unmarshall(
63+ ... None, marshaller.marshall_from_json_data('2009-07-09'))
64+ '2009-07-09'
65+
66 Text
67 ----
68
69
70=== modified file 'src/lazr/restful/marshallers.py'
71--- src/lazr/restful/marshallers.py 2021-01-21 11:42:18 +0000
72+++ src/lazr/restful/marshallers.py 2021-05-18 15:16:23 +0000
73@@ -409,6 +409,13 @@
74 # JSON will serialize '20090131' as a number
75 raise ValueError("Value doesn't look like a date.")
76
77+ def unmarshall(self, entry, value):
78+ """See `IFieldMarshaller`.
79+
80+ Represent the datetime in ISO 8601 format.
81+ """
82+ return None if value is None else value.isoformat()
83+
84
85 class DateFieldMarshaller(DateTimeFieldMarshaller):
86 """A marshaller that transforms its value into a date object."""

Subscribers

People subscribed via source and target branches