Skip to content

Commit 4fcc591

Browse files
committed
Cleanup, fixed a bug with delete action
Change the returned type of every action from JSON to list/object committer: Ralf Schlatterbeck <[email protected]>
1 parent 18bf0d6 commit 4fcc591

File tree

2 files changed

+39
-40
lines changed

2 files changed

+39
-40
lines changed

roundup/cgi/client.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -488,10 +488,6 @@ def handle_xmlrpc(self):
488488
self.write(output)
489489

490490
def handle_rest(self):
491-
# Pull the parameters data out of the form. The "value" attribute
492-
# will be the raw content of the request.
493-
input = self.form.value
494-
495491
# Set the charset and language
496492
self.determine_charset()
497493
self.determine_language()
@@ -502,7 +498,8 @@ def handle_rest(self):
502498

503499
# Call rest library to handle the request
504500
handler = rest.RestfulInstance(self.db)
505-
output = handler.dispatch(self.env['REQUEST_METHOD'], self.path, input)
501+
output = handler.dispatch(self.env['REQUEST_METHOD'], self.path,
502+
self.form)
506503

507504
# self.setHeader("Content-Type", "text/xml")
508505
self.setHeader("Content-Length", str(len(output)))

roundup/rest.py

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import json
99
import pprint
1010
from roundup import hyperdb
11-
from roundup.cgi.templating import Unauthorised
11+
from roundup.exceptions import *
1212
from roundup import xmlrpc
1313

1414

@@ -21,28 +21,32 @@ def __init__(self, db):
2121
self.db = db
2222

2323
def get_collection(self, class_name, input):
24+
if not self.db.security.hasPermission('View', self.db.getuid(),
25+
class_name):
26+
raise Unauthorised('Permission to view %s denied' % class_name)
2427
class_obj = self.db.getclass(class_name)
2528
prop_name = class_obj.labelprop()
2629
result = [{'id': item_id, 'name': class_obj.get(item_id, prop_name)}
2730
for item_id in class_obj.list()
2831
if self.db.security.hasPermission('View', self.db.getuid(),
29-
class_name, None, item_id)
30-
]
31-
result = json.JSONEncoder().encode(result)
32-
32+
class_name,
33+
itemid=item_id)]
3334
return result
3435

3536
def get_element(self, class_name, item_id, input):
37+
if not self.db.security.hasPermission('View', self.db.getuid(),
38+
class_name, itemid=item_id):
39+
raise Unauthorised('Permission to view %s item %d denied' %
40+
(class_name, item_id))
3641
class_obj = self.db.getclass(class_name)
3742
props = class_obj.properties.keys()
3843
props.sort() # sort properties
3944
result = [(prop_name, class_obj.get(item_id, prop_name))
4045
for prop_name in props
4146
if self.db.security.hasPermission('View', self.db.getuid(),
4247
class_name, prop_name,
43-
item_id)
44-
]
45-
result = json.JSONEncoder().encode(dict(result))
48+
item_id)]
49+
result = dict(result)
4650

4751
return result
4852

@@ -54,12 +58,13 @@ def post_collection(self, class_name, input):
5458
class_obj = self.db.getclass(class_name)
5559

5660
# convert types
57-
props = xmlrpc.props_from_args(self.db, class_obj, input)
61+
input_data = ["%s=%s" % (item.name, item.value) for item in input.value]
62+
props = xmlrpc.props_from_args(self.db, class_obj, input_data)
5863

5964
# check for the key property
6065
key = class_obj.getkey()
6166
if key and key not in props:
62-
raise xmlrpc.UsageError, 'Must provide the "%s" property.' % key
67+
raise UsageError('Must provide the "%s" property.' % key)
6368

6469
for key in props:
6570
if not self.db.security.hasPermission('Create', self.db.getuid(),
@@ -69,10 +74,12 @@ def post_collection(self, class_name, input):
6974

7075
# do the actual create
7176
try:
72-
result = class_obj.create(**props)
77+
item_id = class_obj.create(**props)
7378
self.db.commit()
7479
except (TypeError, IndexError, ValueError), message:
75-
raise xmlrpc.UsageError, message
80+
raise UsageError(message)
81+
82+
result = {id: item_id}
7683
return result
7784

7885
def post_element(self, class_name, item_id, input):
@@ -89,13 +96,15 @@ def delete_collection(self, class_name, input):
8996
raise NotImplementedError
9097

9198
def delete_element(self, class_name, item_id, input):
92-
# TODO: BUG with DELETE without form data. Working with random data
93-
# crash at line self.form = cgi.FieldStorage(fp=request.rfile, environ=env)
94-
try:
95-
self.db.destroynode(class_name, item_id)
96-
result = 'OK'
97-
except IndexError:
98-
result = 'Error'
99+
if not self.db.security.hasPermission('Delete', self.db.getuid(),
100+
class_name, itemid=item_id):
101+
raise Unauthorised('Permission to delete %s %s denied' %
102+
(class_name, item_id))
103+
if item_id != input['id'].value:
104+
raise UsageError('Must provide id key as confirmation')
105+
self.db.destroynode(class_name, item_id)
106+
self.db.commit()
107+
result = {"status": "ok"}
99108

100109
return result
101110

@@ -106,33 +115,26 @@ def patch_element(self, class_name, item_id, input):
106115
raise NotImplementedError
107116

108117
def dispatch(self, method, uri, input):
109-
print "METHOD: " + method + " URI: " + uri
110-
print type(input)
111-
pprint.pprint(input)
112-
# TODO: process input_form directly instead of making a new array
113-
# TODO: rest server
114-
# TODO: check roundup/actions.py
115-
# TODO: if uri_path has more than 2 child, return 404
116-
# TODO: custom JSONEncoder to handle other data type
117-
# TODO: catch all error and display error.
118-
119118
# PATH is split to multiple pieces
120119
# 0 - rest
121120
# 1 - resource
122-
123121
resource_uri = uri.split("/")[1]
124-
input_data = ["%s=%s" % (item.name, item.value) for item in input]
125122

123+
output = None
126124
try:
127125
if resource_uri in self.db.classes:
128-
output = getattr(self, "%s_collection" % method.lower())(resource_uri, input_data)
126+
output = getattr(self, "%s_collection" % method.lower())(
127+
resource_uri, input)
129128
else:
130129
class_name, item_id = hyperdb.splitDesignator(resource_uri)
131-
output = getattr(self, "%s_element" % method.lower())(class_name, item_id, input_data)
130+
output = getattr(self, "%s_element" % method.lower())(
131+
class_name, item_id, input)
132132
except hyperdb.DesignatorError:
133-
pass # invalid URI
133+
raise NotImplementedError('Invalid URI')
134134
except AttributeError:
135-
raise NotImplementedError # Error: method is invalid
135+
raise NotImplementedError('Method is invalid')
136+
finally:
137+
output = json.JSONEncoder().encode(output)
136138

137139
print "Length: %s - Content(50 char): %s" % (len(output), output[:50])
138140
return output

0 commit comments

Comments
 (0)