2007-07-22 11:09:24 +08:00
|
|
|
"""
|
|
|
|
This module collects helper functions and classes that "span" multiple levels
|
|
|
|
of MVC. In other words, these functions/classes introduce controlled coupling
|
|
|
|
for convenience's sake.
|
|
|
|
"""
|
2017-02-11 18:49:04 +08:00
|
|
|
import warnings
|
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.http import (
|
|
|
|
Http404, HttpResponse, HttpResponsePermanentRedirect, HttpResponseRedirect,
|
|
|
|
)
|
2015-09-04 10:01:30 +08:00
|
|
|
from django.template import loader
|
2015-12-30 23:51:16 +08:00
|
|
|
from django.urls import NoReverseMatch, reverse
|
2017-02-11 18:49:04 +08:00
|
|
|
from django.utils.deprecation import RemovedInDjango30Warning
|
2015-01-10 05:18:34 +08:00
|
|
|
from django.utils.encoding import force_text
|
|
|
|
from django.utils.functional import Promise
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2015-09-04 10:01:30 +08:00
|
|
|
def render_to_response(template_name, context=None, content_type=None, status=None, using=None):
|
2007-07-22 11:09:24 +08:00
|
|
|
"""
|
2017-01-25 04:37:33 +08:00
|
|
|
Return a HttpResponse whose content is filled with the result of calling
|
2007-07-22 11:09:24 +08:00
|
|
|
django.template.loader.render_to_string() with the passed arguments.
|
|
|
|
"""
|
2017-02-11 18:49:04 +08:00
|
|
|
warnings.warn(
|
|
|
|
'render_to_response() is deprecated in favor render(). It has the '
|
|
|
|
'same signature except that it also requires a request.',
|
|
|
|
RemovedInDjango30Warning, stacklevel=2,
|
|
|
|
)
|
2015-09-04 10:01:30 +08:00
|
|
|
content = loader.render_to_string(template_name, context, using=using)
|
2014-12-14 17:17:18 +08:00
|
|
|
return HttpResponse(content, content_type, status)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2015-09-04 10:01:30 +08:00
|
|
|
def render(request, template_name, context=None, content_type=None, status=None, using=None):
|
2010-12-22 01:18:41 +08:00
|
|
|
"""
|
2017-01-25 04:37:33 +08:00
|
|
|
Return a HttpResponse whose content is filled with the result of calling
|
2010-12-22 01:18:41 +08:00
|
|
|
django.template.loader.render_to_string() with the passed arguments.
|
|
|
|
"""
|
2015-09-04 10:01:30 +08:00
|
|
|
content = loader.render_to_string(template_name, context, request, using=using)
|
2014-12-14 00:41:00 +08:00
|
|
|
return HttpResponse(content, content_type, status)
|
2010-12-22 01:18:41 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2017-02-02 00:41:56 +08:00
|
|
|
def redirect(to, *args, permanent=False, **kwargs):
|
2009-03-21 21:09:13 +08:00
|
|
|
"""
|
2017-01-25 04:37:33 +08:00
|
|
|
Return an HttpResponseRedirect to the appropriate URL for the arguments
|
2009-03-21 21:09:13 +08:00
|
|
|
passed.
|
2010-12-22 01:18:41 +08:00
|
|
|
|
2009-03-21 21:09:13 +08:00
|
|
|
The arguments could be:
|
2010-12-22 01:18:41 +08:00
|
|
|
|
2009-03-21 21:09:13 +08:00
|
|
|
* A model: the model's `get_absolute_url()` function will be called.
|
2010-12-22 01:18:41 +08:00
|
|
|
|
2015-12-30 23:51:16 +08:00
|
|
|
* A view name, possibly with arguments: `urls.reverse()` will be used
|
|
|
|
to reverse-resolve the name.
|
2010-12-22 01:18:41 +08:00
|
|
|
|
2009-03-21 21:09:13 +08:00
|
|
|
* A URL, which will be used as-is for the redirect location.
|
2010-12-22 01:18:41 +08:00
|
|
|
|
2009-03-21 21:09:13 +08:00
|
|
|
By default issues a temporary redirect; pass permanent=True to issue a
|
|
|
|
permanent redirect
|
|
|
|
"""
|
2017-02-02 00:41:56 +08:00
|
|
|
redirect_class = HttpResponsePermanentRedirect if permanent else HttpResponseRedirect
|
2012-09-09 06:55:29 +08:00
|
|
|
return redirect_class(resolve_url(to, *args, **kwargs))
|
2009-03-21 21:09:13 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2007-07-22 11:41:11 +08:00
|
|
|
def _get_queryset(klass):
|
|
|
|
"""
|
2016-03-28 04:35:57 +08:00
|
|
|
Return a QuerySet or a Manager.
|
|
|
|
Duck typing in action: any class with a `get()` method (for
|
|
|
|
get_object_or_404) or a `filter()` method (for get_list_or_404) might do
|
|
|
|
the job.
|
2007-07-22 11:41:11 +08:00
|
|
|
"""
|
2016-03-28 04:35:57 +08:00
|
|
|
# If it is a model class or anything else with ._default_manager
|
|
|
|
if hasattr(klass, '_default_manager'):
|
|
|
|
return klass._default_manager.all()
|
|
|
|
return klass
|
2007-07-22 11:41:11 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2006-05-31 20:34:05 +08:00
|
|
|
def get_object_or_404(klass, *args, **kwargs):
|
2007-07-22 11:09:24 +08:00
|
|
|
"""
|
2017-01-25 04:37:33 +08:00
|
|
|
Use get() to return an object, or raise a Http404 exception if the object
|
2007-07-22 11:09:24 +08:00
|
|
|
does not exist.
|
|
|
|
|
2007-07-25 11:12:31 +08:00
|
|
|
klass may be a Model, Manager, or QuerySet object. All other passed
|
2007-07-22 11:09:24 +08:00
|
|
|
arguments and keyword arguments are used in the get() query.
|
|
|
|
|
2007-12-03 02:21:07 +08:00
|
|
|
Note: Like with get(), an MultipleObjectsReturned will be raised if more than one
|
2007-07-22 11:09:24 +08:00
|
|
|
object is found.
|
|
|
|
"""
|
2007-07-22 11:41:11 +08:00
|
|
|
queryset = _get_queryset(klass)
|
2006-05-02 09:31:56 +08:00
|
|
|
try:
|
2007-07-22 11:41:11 +08:00
|
|
|
return queryset.get(*args, **kwargs)
|
2016-03-28 04:35:57 +08:00
|
|
|
except AttributeError:
|
|
|
|
klass__name = klass.__name__ if isinstance(klass, type) else klass.__class__.__name__
|
|
|
|
raise ValueError(
|
|
|
|
"First argument to get_object_or_404() must be a Model, Manager, "
|
|
|
|
"or QuerySet, not '%s'." % klass__name
|
|
|
|
)
|
2007-07-22 11:41:11 +08:00
|
|
|
except queryset.model.DoesNotExist:
|
|
|
|
raise Http404('No %s matches the given query.' % queryset.model._meta.object_name)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2006-05-31 20:36:01 +08:00
|
|
|
def get_list_or_404(klass, *args, **kwargs):
|
2007-07-22 11:09:24 +08:00
|
|
|
"""
|
2017-01-25 04:37:33 +08:00
|
|
|
Use filter() to return a list of objects, or raise a Http404 exception if
|
2007-07-22 11:45:03 +08:00
|
|
|
the list is empty.
|
2007-07-22 11:09:24 +08:00
|
|
|
|
2007-07-25 11:12:31 +08:00
|
|
|
klass may be a Model, Manager, or QuerySet object. All other passed
|
2007-07-22 11:09:24 +08:00
|
|
|
arguments and keyword arguments are used in the filter() query.
|
|
|
|
"""
|
2007-07-22 11:41:11 +08:00
|
|
|
queryset = _get_queryset(klass)
|
2016-03-28 04:35:57 +08:00
|
|
|
try:
|
|
|
|
obj_list = list(queryset.filter(*args, **kwargs))
|
|
|
|
except AttributeError:
|
|
|
|
klass__name = klass.__name__ if isinstance(klass, type) else klass.__class__.__name__
|
|
|
|
raise ValueError(
|
|
|
|
"First argument to get_list_or_404() must be a Model, Manager, or "
|
|
|
|
"QuerySet, not '%s'." % klass__name
|
|
|
|
)
|
2006-05-02 09:31:56 +08:00
|
|
|
if not obj_list:
|
2007-07-22 11:41:11 +08:00
|
|
|
raise Http404('No %s matches the given query.' % queryset.model._meta.object_name)
|
2010-12-22 01:18:41 +08:00
|
|
|
return obj_list
|
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2012-09-09 06:55:29 +08:00
|
|
|
def resolve_url(to, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Return a URL appropriate for the arguments passed.
|
|
|
|
|
|
|
|
The arguments could be:
|
|
|
|
|
|
|
|
* A model: the model's `get_absolute_url()` function will be called.
|
|
|
|
|
2015-12-30 23:51:16 +08:00
|
|
|
* A view name, possibly with arguments: `urls.reverse()` will be used
|
|
|
|
to reverse-resolve the name.
|
2012-09-09 06:55:29 +08:00
|
|
|
|
|
|
|
* A URL, which will be returned as-is.
|
|
|
|
"""
|
|
|
|
# If it's a model, use get_absolute_url()
|
|
|
|
if hasattr(to, 'get_absolute_url'):
|
|
|
|
return to.get_absolute_url()
|
|
|
|
|
2015-01-10 05:18:34 +08:00
|
|
|
if isinstance(to, Promise):
|
|
|
|
# Expand the lazy instance, as it can cause issues when it is passed
|
|
|
|
# further to some Python functions like urlparse.
|
|
|
|
to = force_text(to)
|
|
|
|
|
2016-12-29 23:27:49 +08:00
|
|
|
if isinstance(to, str):
|
2014-02-15 20:54:35 +08:00
|
|
|
# Handle relative URLs
|
2015-03-14 03:08:04 +08:00
|
|
|
if to.startswith(('./', '../')):
|
2014-02-15 20:54:35 +08:00
|
|
|
return to
|
|
|
|
|
2012-09-09 06:55:29 +08:00
|
|
|
# Next try a reverse URL resolution.
|
|
|
|
try:
|
2015-12-30 23:51:16 +08:00
|
|
|
return reverse(to, args=args, kwargs=kwargs)
|
|
|
|
except NoReverseMatch:
|
2012-09-09 06:55:29 +08:00
|
|
|
# If this is a callable, re-raise.
|
|
|
|
if callable(to):
|
|
|
|
raise
|
|
|
|
# If this doesn't "feel" like a URL, re-raise.
|
|
|
|
if '/' not in to and '.' not in to:
|
|
|
|
raise
|
|
|
|
|
|
|
|
# Finally, fall back and assume it's a URL
|
|
|
|
return to
|