Skip to content

Commit ff2ea3a

Browse files
committed
Implement getting resource from database
committer: Ralf Schlatterbeck <[email protected]>
1 parent 9d31ec5 commit ff2ea3a

File tree

1 file changed

+54
-6
lines changed

1 file changed

+54
-6
lines changed

roundup/rest.py

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
# Restful API for Roundup
2-
#
3-
# This module is free software, you may redistribute it
4-
# and/or modify under the same terms as Python.
5-
#
1+
"""
2+
Restful API for Roundup
3+
4+
This module is free software, you may redistribute it
5+
and/or modify under the same terms as Python.
6+
"""
67

78
import json
89
import pprint
10+
from roundup import hyperdb
11+
from roundup.cgi.templating import Unauthorised
912

1013

1114
class RestfulInstance(object):
@@ -16,9 +19,54 @@ def __init__(self, db):
1619
# TODO: database, translator and instance.actions
1720
self.db = db
1821

22+
def action_get(self, resource, input):
23+
classname, itemid = hyperdb.splitDesignator(resource)
24+
cl = self.db.getclass(classname)
25+
props = cl.properties.keys()
26+
props.sort()
27+
for p in props:
28+
if not self.db.security.hasPermission('View', self.db.getuid(),
29+
classname, p, itemid):
30+
raise Unauthorised('Permission to view %s of %s denied' %
31+
(p, resource))
32+
result = [(prop, cl.get(itemid, prop)) for prop in props]
33+
34+
# print type(result)
35+
# print type(dict(result))
36+
return json.JSONEncoder().encode(dict(result))
37+
# return json.dumps(dict(result))
38+
# return dict(result)
39+
1940
def dispatch(self, method, uri, input):
2041
print method
2142
print uri
2243
print type(input)
2344
pprint.pprint(input)
24-
return ' '.join([method, uri, pprint.pformat(input)])
45+
46+
# PATH is split to multiple pieces
47+
# 0 - rest
48+
# 1 - resource
49+
#
50+
# Example: rest/issue - collection uri
51+
# Example: rest/issue573 - element uri
52+
uri_path = uri.split("/")
53+
# TODO: use named function for this instead
54+
# TODO: check roundup/actions.py
55+
# TODO: if uri_path has more than 2 child, return 404
56+
output = "METHOD is not supported"
57+
if method == "GET":
58+
output = self.action_get(uri_path[1], input)
59+
elif method == "POST":
60+
pass
61+
elif method == "PUT":
62+
pass
63+
elif method == "DELETE":
64+
pass
65+
elif method == "PATCH":
66+
pass
67+
else:
68+
pass
69+
70+
print output
71+
print len(output)
72+
return output

0 commit comments

Comments
 (0)