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
=== modified file 'NEWS.rst'
--- NEWS.rst 2021-05-14 12:46:54 +0000
+++ NEWS.rst 2021-05-18 15:16:23 +0000
@@ -2,6 +2,12 @@
2NEWS for lazr.restful2NEWS for lazr.restful
3=====================3=====================
44
51.0.3
6=====
7
8Give ``DateTimeFieldMarshaller`` an ``unmarshall`` method, rather than
9special-casing it elsewhere.
10
51.0.2 (2021-05-14)111.0.2 (2021-05-14)
6==================12==================
713
814
=== modified file 'src/lazr/restful/_resource.py'
--- src/lazr/restful/_resource.py 2021-05-14 09:10:28 +0000
+++ src/lazr/restful/_resource.py 2021-05-18 15:16:23 +0000
@@ -29,8 +29,6 @@
29 'WADL_SCHEMA_FILE',29 'WADL_SCHEMA_FILE',
30 ]30 ]
3131
32
33from datetime import datetime, date
34from email.utils import formatdate32from email.utils import formatdate
35import copy33import copy
36from operator import itemgetter34from operator import itemgetter
@@ -222,8 +220,6 @@
222220
223 def default(self, obj):221 def default(self, obj):
224 """Convert the given object to a simple data structure."""222 """Convert the given object to a simple data structure."""
225 if isinstance(obj, datetime) or isinstance(obj, date):
226 return obj.isoformat()
227 if isProxy(obj):223 if isProxy(obj):
228 # We have a security-proxied version of a built-in224 # We have a security-proxied version of a built-in
229 # type. We create a new version of the type by copying the225 # type. We create a new version of the type by copying the
230226
=== modified file 'src/lazr/restful/docs/webservice-marshallers.rst'
--- src/lazr/restful/docs/webservice-marshallers.rst 2021-01-21 11:42:18 +0000
+++ src/lazr/restful/docs/webservice-marshallers.rst 2021-05-18 15:16:23 +0000
@@ -385,6 +385,12 @@
385 ...385 ...
386 ValueError: Value doesn't look like a date.386 ValueError: Value doesn't look like a date.
387387
388The unmarshall() method returns the ISO 8601 representation of the value.
389
390 >>> marshaller.unmarshall(
391 ... None, marshaller.marshall_from_json_data('2009-07-07T13:45:00Z'))
392 '2009-07-07T13:45:00+00:00'
393
388Date394Date
389----395----
390396
@@ -406,6 +412,12 @@
406 >>> marshaller.marshall_from_json_data('2009-07-07T13:15:00+0000')412 >>> marshaller.marshall_from_json_data('2009-07-07T13:15:00+0000')
407 datetime.date(2009, 7, 7)413 datetime.date(2009, 7, 7)
408414
415The unmarshall() method returns the ISO 8601 representation of the value.
416
417 >>> marshaller.unmarshall(
418 ... None, marshaller.marshall_from_json_data('2009-07-09'))
419 '2009-07-09'
420
409Text421Text
410----422----
411423
412424
=== modified file 'src/lazr/restful/marshallers.py'
--- src/lazr/restful/marshallers.py 2021-01-21 11:42:18 +0000
+++ src/lazr/restful/marshallers.py 2021-05-18 15:16:23 +0000
@@ -409,6 +409,13 @@
409 # JSON will serialize '20090131' as a number409 # JSON will serialize '20090131' as a number
410 raise ValueError("Value doesn't look like a date.")410 raise ValueError("Value doesn't look like a date.")
411411
412 def unmarshall(self, entry, value):
413 """See `IFieldMarshaller`.
414
415 Represent the datetime in ISO 8601 format.
416 """
417 return None if value is None else value.isoformat()
418
412419
413class DateFieldMarshaller(DateTimeFieldMarshaller):420class DateFieldMarshaller(DateTimeFieldMarshaller):
414 """A marshaller that transforms its value into a date object."""421 """A marshaller that transforms its value into a date object."""

Subscribers

People subscribed via source and target branches