Mercurial > p > roundup > code
annotate roundup/actions.py @ 5710:0b79bfcb3312
Add support for making an idempotent POST. This allows retrying a POST
that was interrupted. It involves creating a post once only (poe) url
/rest/data/<class>/@poe/<random_token>. This url acts the same as a
post to /rest/data/<class>. However once the @poe url is used, it
can't be used for a second POST.
To make these changes:
1) Take the body of post_collection into a new post_collection_inner
function. Have post_collection call post_collection_inner.
2) Add a handler for POST to rest/data/class/@poe. This will return a
unique POE url. By default the url expires after 30 minutes. The
POE random token is only good for a specific user and is stored in
the session db.
3) Add a handler for POST to rest/data/<class>/@poe/<random token>.
The random token generated in 2 is validated for proper class (if
token is not generic) and proper user and must not have expired.
If everything is valid, call post_collection_inner to process the
input and generate the new entry.
To make recognition of 2 stable (so it's not confused with
rest/data/<:class_name>/<:item_id>), removed @ from
Routing::url_to_regex.
The current Routing.execute method stops on the first regular
expression to match the URL. Since item_id doesn't accept a POST, I
was getting 405 bad method sometimes. My guess is the order of the
regular expressions is not stable, so sometime I would get the right
regexp for /data/<class>/@poe and sometime I would get the one for
/data/<class>/<item_id>. By removing the @ from the url_to_regexp,
there was no way for the item_id case to match @poe.
There are alternate fixes we may need to look at. If a regexp matches
but the method does not, return to the regexp matching loop in
execute() looking for another match. Only once every possible match
has failed should the code return a 405 method failure.
Another fix is to implement a more sophisticated mechanism so that
@Routing.route("/data/<:class_name>/<:item_id>/<:attr_name>", 'PATCH')
has different regexps for matching <:class_name> <:item_id> and
<:attr_name>. Currently the regexp specified by url_to_regex is used
for every component.
Other fixes:
Made failure to find any props in props_from_args return an empty
dict rather than throwing an unhandled error.
Make __init__ for SimulateFieldStorageFromJson handle an empty json
doc. Useful for POSTing to rest/data/class/@poe with an empty
document.
Testing:
added testPostPOE to test/rest_common.py that I think covers
all the code that was added.
Documentation:
Add doc to rest.txt in the "Client API" section titled: Safely
Re-sending POST". Move existing section "Adding new rest endpoints" in
"Client API" to a new second level section called "Programming the
REST API". Also a minor change to the simple rest client moving the
header setting to continuation lines rather than showing one long
line.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Sun, 14 Apr 2019 21:07:11 -0400 |
| parents | ed02a1e0aa5d |
| children | 48a1f919f894 |
| rev | line source |
|---|---|
| 4083 | 1 # |
| 2 # Copyright (C) 2009 Stefan Seefeld | |
| 3 # All rights reserved. | |
| 4 # For license terms see the file COPYING.txt. | |
| 5604 | 5 # Actions used in REST and XMLRPC APIs |
| 4083 | 6 # |
| 7 | |
|
5071
a7541077cf12
Remove 'import *' statement from actions.py
John Kristensen <john@jerrykan.com>
parents:
4357
diff
changeset
|
8 from roundup.exceptions import Unauthorised |
| 4083 | 9 from roundup import hyperdb |
| 10 from roundup.i18n import _ | |
| 11 | |
| 12 class Action: | |
| 13 def __init__(self, db, translator): | |
| 14 self.db = db | |
| 15 self.translator = translator | |
| 16 | |
| 17 def handle(self, *args): | |
| 18 """Action handler procedure""" | |
| 19 raise NotImplementedError | |
| 20 | |
| 21 def execute(self, *args): | |
| 22 """Execute the action specified by this object.""" | |
| 23 | |
| 24 self.permission(*args) | |
| 25 return self.handle(*args) | |
| 26 | |
| 27 | |
| 28 def permission(self, *args): | |
| 29 """Check whether the user has permission to execute this action. | |
| 30 | |
| 31 If not, raise Unauthorised.""" | |
| 32 | |
| 33 pass | |
| 34 | |
| 35 | |
| 36 def gettext(self, msgid): | |
| 37 """Return the localized translation of msgid""" | |
| 38 return self.translator.gettext(msgid) | |
| 39 | |
| 40 | |
| 41 _ = gettext | |
| 42 | |
| 43 | |
| 5604 | 44 class PermCheck(Action): |
| 45 def permission(self, designator): | |
| 46 | |
| 47 classname, itemid = hyperdb.splitDesignator(designator) | |
| 48 perm = self.db.security.hasPermission | |
| 49 | |
| 50 if not perm('Retire', self.db.getuid(), classname=classname | |
| 51 , itemid=itemid): | |
| 52 raise Unauthorised(self._('You do not have permission to retire ' | |
| 53 'or restore the %(classname)s class.') | |
| 54 %locals()) | |
| 55 | |
| 56 class Retire(PermCheck): | |
| 4083 | 57 |
| 58 def handle(self, designator): | |
| 59 | |
| 60 classname, itemid = hyperdb.splitDesignator(designator) | |
| 61 | |
| 62 # make sure we don't try to retire admin or anonymous | |
| 63 if (classname == 'user' and | |
| 64 self.db.user.get(itemid, 'username') in ('admin', 'anonymous')): | |
|
4357
13b3155869e0
Beginnings of a big code cleanup / modernisation to make 2to3 happy
Richard Jones <richard@users.sourceforge.net>
parents:
4125
diff
changeset
|
65 raise ValueError(self._( |
|
13b3155869e0
Beginnings of a big code cleanup / modernisation to make 2to3 happy
Richard Jones <richard@users.sourceforge.net>
parents:
4125
diff
changeset
|
66 'You may not retire the admin or anonymous user')) |
| 4083 | 67 |
| 68 # do the retire | |
| 69 self.db.getclass(classname).retire(itemid) | |
| 70 self.db.commit() | |
| 71 | |
| 72 | |
| 5604 | 73 class Restore(PermCheck): |
| 74 | |
| 75 def handle(self, designator): | |
| 4083 | 76 |
| 77 classname, itemid = hyperdb.splitDesignator(designator) | |
| 78 | |
| 5604 | 79 # do the restore |
| 80 self.db.getclass(classname).restore(itemid) | |
| 81 self.db.commit() | |
| 82 |
