Add a method=? parameter rest_dispatch to support PATCH-via-POST

(imported from commit 66f7011d6920639abc307ca85708bc958f3bb0e3)
This commit is contained in:
Leo Franchi 2013-10-21 12:54:57 -04:00
parent a9bd2743f0
commit 13b452ddf8
2 changed files with 30 additions and 3 deletions

View File

@ -917,6 +917,27 @@ class BotTest(AuthedTestCase):
bot = self.get_bot() bot = self.get_bot()
self.assertEqual('Fred', bot['full_name']) self.assertEqual('Fred', bot['full_name'])
def test_patch_bot_via_post(self):
self.login("hamlet@zulip.com")
bot_info = {
'full_name': 'The Bot of Hamlet',
'short_name': 'hambot',
}
result = self.client.post("/json/create_bot", bot_info)
self.assert_json_success(result)
bot_info = {
'full_name': 'Fred',
'method': 'PATCH'
}
result = self.client.post("/json/bots/hambot-bot@zulip.com", bot_info)
self.assert_json_success(result)
full_name = ujson.loads(result.content)['full_name']
self.assertEqual('Fred', full_name)
bot = self.get_bot()
self.assertEqual('Fred', bot['full_name'])
def test_patch_bogus_bot(self): def test_patch_bogus_bot(self):
# Deleting a bogus bot will succeed silently. # Deleting a bogus bot will succeed silently.
self.login("hamlet@zulip.com") self.login("hamlet@zulip.com")

View File

@ -225,8 +225,14 @@ def rest_dispatch(request, **kwargs):
if arg in METHODS: if arg in METHODS:
supported_methods[arg] = kwargs[arg] supported_methods[arg] = kwargs[arg]
del kwargs[arg] del kwargs[arg]
if request.method in supported_methods.keys():
target_function = globals()[supported_methods[request.method]] # Override requested method if magic method=??? parameter exists
method_to_use = request.method
if request.POST and 'method' in request.POST:
method_to_use = request.POST['method']
if method_to_use in supported_methods.keys():
target_function = globals()[supported_methods[method_to_use]]
# Set request._query for update_activity_user(), which is called # Set request._query for update_activity_user(), which is called
# by some of the later wrappers. # by some of the later wrappers.
@ -246,7 +252,7 @@ def rest_dispatch(request, **kwargs):
# Wrap function with decorator to authenticate the user before # Wrap function with decorator to authenticate the user before
# proceeding # proceeding
target_function = authenticated_rest_api_view(target_function) target_function = authenticated_rest_api_view(target_function)
if request.method not in ["GET", "POST"]: if method_to_use not in ["GET", "POST"]:
# process_as_post needs to be the outer decorator, because # process_as_post needs to be the outer decorator, because
# otherwise we might access and thus cache a value for # otherwise we might access and thus cache a value for
# request.REQUEST. # request.REQUEST.