forked from django/django
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add predicate decorator to not match specific view
Predicates is concept brought back from Pyramid, Pylon. It has the goal of allowing a better organizatio of your function based view and get rid of redundant code as `if request.method == 'POST` Improve the doc about predicates Change title about predicate in the docs Fix predicate explanation in docs
- Loading branch information
Showing
7 changed files
with
118 additions
and
3 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
""" | ||
Decorators for views based on HTTP headers. | ||
""" | ||
|
||
import logging | ||
from functools import wraps | ||
|
||
from django.utils.decorators import decorator_from_middleware, available_attrs | ||
from django.utils.http import http_date | ||
from django.middleware.http import ConditionalGetMiddleware | ||
|
||
logger = logging.getLogger('django.request') | ||
|
||
|
||
def url_predicates(predicates_list): | ||
""" | ||
Decorators to make urls matchable if predicates return True | ||
Usage:: | ||
url_pattern += url('/', my_view_GET) | ||
url_pattern += url('/', my_view_POST) | ||
... | ||
predicate_GET = lambda request: request.method == 'GET' | ||
predicate_POST = lambda request: request.method == 'POST' | ||
@url_predicates([predicate_GET]) | ||
def my_view_GET(request): | ||
# I can assume now that only GET requests get match to the url | ||
# associated to this view | ||
# ... | ||
@url_predicates([predicate_POST]) | ||
def my_view_POST(request): | ||
# I can assume now that only POST requests get match to the url | ||
# associated to this view | ||
# ... | ||
""" | ||
def decorator(func): | ||
@wraps(func, assigned=available_attrs(func)) | ||
def inner(request, *args, **kwargs): | ||
return func(request, *args, **kwargs) | ||
inner.predicates = predicates_list | ||
return inner | ||
return decorator | ||
|
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
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
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
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