Created
September 26, 2009 16:04
-
-
Save sshirokov/194293 to your computer and use it in GitHub Desktop.
@require_POST_or(url_name_or_callable): Run this method if POST, call another or redirect away otherwise.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.core.urlresolvers import reverse | |
from django.views.decorators.http import require_POST | |
from django.http import HttpResponseRedirect, HttpResponseNotAllowed | |
def require_POST_or(other): | |
def _d_closure(func): | |
def wrapped_func(request, *args, **kwargs): | |
r = require_POST(func)(request, *args, **kwargs) | |
if type(r) == HttpResponseNotAllowed: | |
if callable(other): return other(request, *args, **kwargs) | |
else: return HttpResponseRedirect(reverse(other)) | |
return r | |
wrapped_func.__name__ = func.__name__ | |
wrapped_func.__doc__ = func.__doc__ | |
return wrapped_func | |
return _d_closure |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment