Skip to content

Commit 18bf0d6

Browse files
committed
Split all rest action into 2 type
element uri and collection uri committer: Ralf Schlatterbeck <[email protected]>
1 parent 1fc52dd commit 18bf0d6

File tree

1 file changed

+55
-68
lines changed

1 file changed

+55
-68
lines changed

roundup/rest.py

Lines changed: 55 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -14,56 +14,39 @@
1414

1515
class RestfulInstance(object):
1616
"""Dummy Handler for REST
17-
WARNING: Very ugly !!!!, cleaned & better organized in progress (next commit)
1817
"""
1918

2019
def __init__(self, db):
2120
# TODO: database, translator and instance.actions
2221
self.db = db
2322

24-
def action_get(self, resource_uri, input):
25-
# TODO: split this into collection URI and resource URI
26-
class_name = resource_uri
27-
try:
28-
class_obj = self.db.getclass(class_name)
29-
"""prop_name = class_obj.labelprop()
30-
result = [class_obj.get(item_id, prop_name)"""
31-
result = [{'id': item_id}
32-
for item_id in class_obj.list()
33-
if self.db.security.hasPermission('View',
34-
self.db.getuid(),
35-
class_name,
36-
None,
37-
item_id)
38-
]
39-
result = json.JSONEncoder().encode(result)
40-
# result = `len(dict(result))` + ' ' + `len(result)`
41-
except KeyError:
42-
pass
43-
44-
try:
45-
class_name, item_id = hyperdb.splitDesignator(resource_uri)
46-
class_obj = self.db.getclass(class_name)
47-
props = class_obj.properties.keys()
48-
props.sort()
49-
result = [(prop_name, class_obj.get(item_id, prop_name))
50-
for prop_name in props
51-
if self.db.security.hasPermission('View',
52-
self.db.getuid(),
53-
class_name,
54-
prop_name,
55-
item_id)
56-
]
57-
# Note: is this a bug by having an extra indent in xmlrpc ?
58-
result = json.JSONEncoder().encode(dict(result))
59-
except hyperdb.DesignatorError:
60-
pass
23+
def get_collection(self, class_name, input):
24+
class_obj = self.db.getclass(class_name)
25+
prop_name = class_obj.labelprop()
26+
result = [{'id': item_id, 'name': class_obj.get(item_id, prop_name)}
27+
for item_id in class_obj.list()
28+
if self.db.security.hasPermission('View', self.db.getuid(),
29+
class_name, None, item_id)
30+
]
31+
result = json.JSONEncoder().encode(result)
6132

6233
return result
6334

64-
def action_post(self, resource_uri, input):
65-
class_name = resource_uri
35+
def get_element(self, class_name, item_id, input):
36+
class_obj = self.db.getclass(class_name)
37+
props = class_obj.properties.keys()
38+
props.sort() # sort properties
39+
result = [(prop_name, class_obj.get(item_id, prop_name))
40+
for prop_name in props
41+
if self.db.security.hasPermission('View', self.db.getuid(),
42+
class_name, prop_name,
43+
item_id)
44+
]
45+
result = json.JSONEncoder().encode(dict(result))
46+
47+
return result
6648

49+
def post_collection(self, class_name, input):
6750
if not self.db.security.hasPermission('Create', self.db.getuid(),
6851
class_name):
6952
raise Unauthorised('Permission to create %s denied' % class_name)
@@ -92,60 +75,64 @@ def action_post(self, resource_uri, input):
9275
raise xmlrpc.UsageError, message
9376
return result
9477

95-
def action_put(self, resource_uri, input):
78+
def post_element(self, class_name, item_id, input):
9679
raise NotImplementedError
9780

98-
def action_delete(self, resource_uri, input):
81+
def put_collection(self, class_name, input):
82+
raise NotImplementedError
83+
84+
def put_element(self, class_name, item_id, input):
85+
raise NotImplementedError
86+
87+
def delete_collection(self, class_name, input):
9988
# TODO: should I allow user to delete the whole collection ?
89+
raise NotImplementedError
90+
91+
def delete_element(self, class_name, item_id, input):
10092
# TODO: BUG with DELETE without form data. Working with random data
10193
# crash at line self.form = cgi.FieldStorage(fp=request.rfile, environ=env)
102-
class_name = resource_uri
103-
try:
104-
class_obj = self.db.getclass(class_name)
105-
raise NotImplementedError
106-
except KeyError:
107-
pass
108-
10994
try:
110-
class_name, item_id = hyperdb.splitDesignator(resource_uri)
111-
print class_name
112-
print item_id
11395
self.db.destroynode(class_name, item_id)
11496
result = 'OK'
11597
except IndexError:
11698
result = 'Error'
117-
except hyperdb.DesignatorError:
118-
pass
11999

120100
return result
121101

122-
def action_patch(self, resource_uri, input):
102+
def patch_collection(self, class_name, input):
103+
raise NotImplementedError
104+
105+
def patch_element(self, class_name, item_id, input):
123106
raise NotImplementedError
124107

125108
def dispatch(self, method, uri, input):
126109
print "METHOD: " + method + " URI: " + uri
127110
print type(input)
128111
pprint.pprint(input)
129-
130-
# PATH is split to multiple pieces
131-
# 0 - rest
132-
# 1 - resource
133-
#
134-
# Example: rest/issue - collection uri
135-
# Example: rest/issue573 - element uri
136-
uri_path = uri.split("/")
137-
input_form = ["%s=%s" % (item.name, item.value) for item in input]
138112
# TODO: process input_form directly instead of making a new array
139113
# TODO: rest server
140114
# TODO: check roundup/actions.py
141115
# TODO: if uri_path has more than 2 child, return 404
142116
# TODO: custom JSONEncoder to handle other data type
143117
# TODO: catch all error and display error.
118+
119+
# PATH is split to multiple pieces
120+
# 0 - rest
121+
# 1 - resource
122+
123+
resource_uri = uri.split("/")[1]
124+
input_data = ["%s=%s" % (item.name, item.value) for item in input]
125+
144126
try:
145-
output = getattr(self, "action_%s" % method.lower())(uri_path[1], input_form)
127+
if resource_uri in self.db.classes:
128+
output = getattr(self, "%s_collection" % method.lower())(resource_uri, input_data)
129+
else:
130+
class_name, item_id = hyperdb.splitDesignator(resource_uri)
131+
output = getattr(self, "%s_element" % method.lower())(class_name, item_id, input_data)
132+
except hyperdb.DesignatorError:
133+
pass # invalid URI
146134
except AttributeError:
147-
raise NotImplementedError
135+
raise NotImplementedError # Error: method is invalid
148136

149-
print "Response Length: %s - Response Content (First 50 char): %s" %\
150-
(len(output), output[:50])
137+
print "Length: %s - Content(50 char): %s" % (len(output), output[:50])
151138
return output

0 commit comments

Comments
 (0)