|
14 | 14 |
|
15 | 15 | class RestfulInstance(object): |
16 | 16 | """Dummy Handler for REST |
17 | | - WARNING: Very ugly !!!!, cleaned & better organized in progress (next commit) |
18 | 17 | """ |
19 | 18 |
|
20 | 19 | def __init__(self, db): |
21 | 20 | # TODO: database, translator and instance.actions |
22 | 21 | self.db = db |
23 | 22 |
|
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) |
61 | 32 |
|
62 | 33 | return result |
63 | 34 |
|
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 |
66 | 48 |
|
| 49 | + def post_collection(self, class_name, input): |
67 | 50 | if not self.db.security.hasPermission('Create', self.db.getuid(), |
68 | 51 | class_name): |
69 | 52 | raise Unauthorised('Permission to create %s denied' % class_name) |
@@ -92,60 +75,64 @@ def action_post(self, resource_uri, input): |
92 | 75 | raise xmlrpc.UsageError, message |
93 | 76 | return result |
94 | 77 |
|
95 | | - def action_put(self, resource_uri, input): |
| 78 | + def post_element(self, class_name, item_id, input): |
96 | 79 | raise NotImplementedError |
97 | 80 |
|
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): |
99 | 88 | # TODO: should I allow user to delete the whole collection ? |
| 89 | + raise NotImplementedError |
| 90 | + |
| 91 | + def delete_element(self, class_name, item_id, input): |
100 | 92 | # TODO: BUG with DELETE without form data. Working with random data |
101 | 93 | # 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 | | - |
109 | 94 | try: |
110 | | - class_name, item_id = hyperdb.splitDesignator(resource_uri) |
111 | | - print class_name |
112 | | - print item_id |
113 | 95 | self.db.destroynode(class_name, item_id) |
114 | 96 | result = 'OK' |
115 | 97 | except IndexError: |
116 | 98 | result = 'Error' |
117 | | - except hyperdb.DesignatorError: |
118 | | - pass |
119 | 99 |
|
120 | 100 | return result |
121 | 101 |
|
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): |
123 | 106 | raise NotImplementedError |
124 | 107 |
|
125 | 108 | def dispatch(self, method, uri, input): |
126 | 109 | print "METHOD: " + method + " URI: " + uri |
127 | 110 | print type(input) |
128 | 111 | 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] |
138 | 112 | # TODO: process input_form directly instead of making a new array |
139 | 113 | # TODO: rest server |
140 | 114 | # TODO: check roundup/actions.py |
141 | 115 | # TODO: if uri_path has more than 2 child, return 404 |
142 | 116 | # TODO: custom JSONEncoder to handle other data type |
143 | 117 | # 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 | + |
144 | 126 | 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 |
146 | 134 | except AttributeError: |
147 | | - raise NotImplementedError |
| 135 | + raise NotImplementedError # Error: method is invalid |
148 | 136 |
|
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]) |
151 | 138 | return output |
0 commit comments