Merge lp:~leonardr/lazr.restful/431986-field-resource-link into lp:lazr.restful

Proposed by Leonard Richardson
Status: Merged
Merged at revision: not available
Proposed branch: lp:~leonardr/lazr.restful/431986-field-resource-link
Merge into: lp:lazr.restful
Diff against target: 104 lines
5 files modified
src/lazr/restful/NEWS.txt (+6/-0)
src/lazr/restful/_resource.py (+1/-1)
src/lazr/restful/example/base/tests/field.txt (+30/-0)
src/lazr/restful/publisher.py (+9/-0)
src/lazr/restful/version.txt (+1/-1)
To merge this branch: bzr merge lp:~leonardr/lazr.restful/431986-field-resource-link
Reviewer Review Type Date Requested Status
Paul Hummer (community) Approve
Review via email: mp+12357@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Leonard Richardson (leonardr) wrote :

This branch fixes bug 431986 by making it possible to navigate to the field resource at /entry/foo_link or /entry/foo_collection_link.

Here's how it should work (and does work with this branch):

1. /bugs/1/owner points to an entry resource. If you GET that object you get a JSON dictionary. If you PATCH it you should send a JSON dictionary. (Though you should really PATCH the user's self_link so you're modifying it at the canonical location.)

2. /bugs/1/owner_link points to a field resource. If you GET that object you'll get a JSON-encoded string, containing the canonical URL of the bug's owner. If you PATCH it you can change which user is the bug's owner.

Previously, you could not navigate to owner_link at all. There was a hack (I don't remember the details) that would let you navigate to owner_link, but even then you couldn't PATCH the field because the resource class was using the internal schema name ("owner") instead of the web service schema name ("owner_link"). So you'd try to PATCH owner_link and get "No such field: owner". This branch fixes both problems.

Revision history for this message
Paul Hummer (rockstar) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/lazr/restful/NEWS.txt'
2--- src/lazr/restful/NEWS.txt 2009-09-16 16:00:46 +0000
3+++ src/lazr/restful/NEWS.txt 2009-09-24 15:45:19 +0000
4@@ -2,6 +2,12 @@
5 NEWS for lazr.restful
6 =====================
7
8+0.9.7 (2009-09-24)
9+==================
10+
11+Fixed a bug that made it impossible to navigate to a field resource if
12+the field was a link to another object.
13+
14 0.9.6 (2009-09-16)
15 ==================
16
17
18=== modified file 'src/lazr/restful/_resource.py'
19--- src/lazr/restful/_resource.py 2009-08-11 16:40:43 +0000
20+++ src/lazr/restful/_resource.py 2009-09-24 15:45:19 +0000
21@@ -1148,7 +1148,7 @@
22 value, error = self.processAsJSONDocument(media_type, representation)
23 if value is None:
24 return value, error
25- changeset = {self.context.field.__name__ : value}
26+ changeset = {self.context.name : value}
27 return self.applyChanges(changeset)
28
29 do_PATCH = do_PUT
30
31=== modified file 'src/lazr/restful/example/base/tests/field.txt'
32--- src/lazr/restful/example/base/tests/field.txt 2009-09-01 13:10:07 +0000
33+++ src/lazr/restful/example/base/tests/field.txt 2009-09-24 15:45:19 +0000
34@@ -35,6 +35,31 @@
35 ... representation).jsonBody()
36 <b>Bold description</b>
37
38+If you get a field that contains a link to another object, you'll see
39+the link, rather than the actual object.
40+
41+ >>> link_field_url = "/recipes/3/cookbook_link"
42+ >>> print webservice.get(link_field_url).jsonBody()
43+ http://.../cookbooks/James%20Beard%27s%20American%20Cookery
44+
45+ >>> collection_url = quote(
46+ ... "/cookbooks/The Joy of Cooking/recipes_collection_link")
47+ >>> print webservice.get(collection_url).jsonBody()
48+ http://.../cookbooks/The%20Joy%20of%20Cooking/recipes
49+
50+Changing a field resource that contains a link works the same way as
51+changing a field resource that contains a scalar value.
52+
53+ >>> new_value = simplejson.dumps(
54+ ... webservice.get(cookbook_url).jsonBody()['self_link'])
55+ >>> print new_value
56+ "http://.../cookbooks/The%20Joy%20of%20Cooking"
57+
58+ >>> print webservice(link_field_url, 'PATCH', new_value)
59+
60+ >>> print webservice.get(link_field_url).jsonBody()
61+ http://.../cookbooks/The%20Joy%20of%20Cooking
62+
63 The same rules for modifying a field apply whether you're modifying
64 the entry as a whole or just modifying a single field.
65
66@@ -45,6 +70,11 @@
67 ...
68 copyright_date: Value doesn't look like a date.
69
70+ >>> print webservice(collection_url, 'PATCH', new_value)
71+ HTTP/1.1 400 Bad Request
72+ ...
73+ recipes_collection_link: You tried to modify a collection attribute.
74+
75 Field resources also support GET, for when you only need part of an
76 entry. You can get either a JSON or XHTML-fragment representation.
77
78
79=== modified file 'src/lazr/restful/publisher.py'
80--- src/lazr/restful/publisher.py 2009-08-13 17:35:27 +0000
81+++ src/lazr/restful/publisher.py 2009-09-24 15:45:19 +0000
82@@ -60,6 +60,15 @@
83 except TypeError:
84 pass
85 else:
86+ if name.endswith("_link"):
87+ # The user wants to access the link resource itself,
88+ # rather than the object on the other side of the link.
89+ if name.endswith("_collection_link"):
90+ schema_name = name[:-16]
91+ else:
92+ schema_name = name[:-5]
93+ field = entry.schema.get(schema_name)
94+ return EntryField(entry, field, name)
95 field = entry.schema.get(name)
96 if ICollectionField.providedBy(field):
97 result = self._traverseToScopedCollection(
98
99=== modified file 'src/lazr/restful/version.txt'
100--- src/lazr/restful/version.txt 2009-09-16 16:00:46 +0000
101+++ src/lazr/restful/version.txt 2009-09-24 15:45:19 +0000
102@@ -1,1 +1,1 @@
103-0.9.6
104+0.9.7

Subscribers

People subscribed via source and target branches