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.
|
|
|
|
"""
|
2014-12-15 00:48:51 +08:00
|
|
|
|
|
|
|
import warnings
|
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.core import urlresolvers
|
2012-12-13 03:34:59 +08:00
|
|
|
from django.db.models.base import ModelBase
|
2007-01-03 22:16:58 +08:00
|
|
|
from django.db.models.manager import Manager
|
2007-07-22 11:41:11 +08:00
|
|
|
from django.db.models.query import QuerySet
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.http import (
|
|
|
|
Http404, HttpResponse, HttpResponsePermanentRedirect, HttpResponseRedirect,
|
|
|
|
)
|
|
|
|
from django.template import RequestContext, loader
|
|
|
|
from django.template.context import _current_app_undefined
|
|
|
|
from django.template.engine import (
|
|
|
|
_context_instance_undefined, _dictionary_undefined, _dirs_undefined,
|
|
|
|
)
|
2014-02-15 20:54:35 +08:00
|
|
|
from django.utils import six
|
2015-06-23 01:54:35 +08:00
|
|
|
from django.utils.deprecation import RemovedInDjango110Warning
|
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
|
|
|
|
2014-12-14 17:17:18 +08:00
|
|
|
def render_to_response(template_name, context=None,
|
2014-12-14 00:41:00 +08:00
|
|
|
context_instance=_context_instance_undefined,
|
2014-12-14 17:17:18 +08:00
|
|
|
content_type=None, status=None, dirs=_dirs_undefined,
|
2015-01-27 04:57:10 +08:00
|
|
|
dictionary=_dictionary_undefined, using=None):
|
2007-07-22 11:09:24 +08:00
|
|
|
"""
|
2007-07-25 11:12:31 +08:00
|
|
|
Returns 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.
|
|
|
|
"""
|
2014-12-14 17:17:18 +08:00
|
|
|
if (context_instance is _context_instance_undefined
|
|
|
|
and dirs is _dirs_undefined
|
|
|
|
and dictionary is _dictionary_undefined):
|
|
|
|
# No deprecated arguments were passed - use the new code path
|
2015-01-27 04:57:10 +08:00
|
|
|
content = loader.render_to_string(template_name, context, using=using)
|
2014-12-14 17:17:18 +08:00
|
|
|
|
|
|
|
else:
|
|
|
|
# Some deprecated arguments were passed - use the legacy code path
|
|
|
|
content = loader.render_to_string(
|
2015-01-27 04:57:10 +08:00
|
|
|
template_name, context, context_instance, dirs, dictionary,
|
|
|
|
using=using)
|
2014-11-29 06:50:34 +08:00
|
|
|
|
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
|
|
|
|
2014-12-14 17:17:18 +08:00
|
|
|
def render(request, template_name, context=None,
|
2014-12-14 00:41:00 +08:00
|
|
|
context_instance=_context_instance_undefined,
|
|
|
|
content_type=None, status=None, current_app=_current_app_undefined,
|
2015-01-27 04:57:10 +08:00
|
|
|
dirs=_dirs_undefined, dictionary=_dictionary_undefined,
|
|
|
|
using=None):
|
2010-12-22 01:18:41 +08:00
|
|
|
"""
|
|
|
|
Returns a HttpResponse whose content is filled with the result of calling
|
|
|
|
django.template.loader.render_to_string() with the passed arguments.
|
|
|
|
Uses a RequestContext by default.
|
|
|
|
"""
|
2014-12-14 17:17:18 +08:00
|
|
|
if (context_instance is _context_instance_undefined
|
|
|
|
and current_app is _current_app_undefined
|
|
|
|
and dirs is _dirs_undefined
|
|
|
|
and dictionary is _dictionary_undefined):
|
|
|
|
# No deprecated arguments were passed - use the new code path
|
2015-06-23 01:54:35 +08:00
|
|
|
# In Django 1.10, request should become a positional argument.
|
2015-01-27 04:57:10 +08:00
|
|
|
content = loader.render_to_string(
|
|
|
|
template_name, context, request=request, using=using)
|
2011-01-06 06:41:43 +08:00
|
|
|
|
2014-12-14 17:17:18 +08:00
|
|
|
else:
|
|
|
|
# Some deprecated arguments were passed - use the legacy code path
|
|
|
|
if context_instance is not _context_instance_undefined:
|
|
|
|
if current_app is not _current_app_undefined:
|
|
|
|
raise ValueError('If you provide a context_instance you must '
|
|
|
|
'set its current_app before calling render()')
|
|
|
|
else:
|
2014-12-15 00:48:51 +08:00
|
|
|
context_instance = RequestContext(request)
|
|
|
|
if current_app is not _current_app_undefined:
|
|
|
|
warnings.warn(
|
|
|
|
"The current_app argument of render is deprecated. "
|
|
|
|
"Set the current_app attribute of request instead.",
|
2015-06-23 01:54:35 +08:00
|
|
|
RemovedInDjango110Warning, stacklevel=2)
|
2014-12-15 00:48:51 +08:00
|
|
|
request.current_app = current_app
|
|
|
|
# Directly set the private attribute to avoid triggering the
|
|
|
|
# warning in RequestContext.__init__.
|
|
|
|
context_instance._current_app = current_app
|
2014-12-14 17:17:18 +08:00
|
|
|
|
|
|
|
content = loader.render_to_string(
|
2015-01-27 04:57:10 +08:00
|
|
|
template_name, context, context_instance, dirs, dictionary,
|
|
|
|
using=using)
|
2014-11-29 06:50:34 +08:00
|
|
|
|
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
|
|
|
|
2009-03-21 21:09:13 +08:00
|
|
|
def redirect(to, *args, **kwargs):
|
|
|
|
"""
|
2012-11-24 00:20:18 +08:00
|
|
|
Returns 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
|
|
|
|
2009-03-21 21:09:13 +08:00
|
|
|
* A view name, possibly with arguments: `urlresolvers.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
|
|
|
|
"""
|
|
|
|
if kwargs.pop('permanent', False):
|
|
|
|
redirect_class = HttpResponsePermanentRedirect
|
|
|
|
else:
|
|
|
|
redirect_class = HttpResponseRedirect
|
2010-12-22 01:18:41 +08:00
|
|
|
|
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):
|
|
|
|
"""
|
2007-07-25 11:12:31 +08:00
|
|
|
Returns a QuerySet from a Model, Manager, or QuerySet. Created to make
|
2007-07-22 11:41:11 +08:00
|
|
|
get_object_or_404 and get_list_or_404 more DRY.
|
2012-12-13 03:34:59 +08:00
|
|
|
|
|
|
|
Raises a ValueError if klass is not a Model, Manager, or QuerySet.
|
2007-07-22 11:41:11 +08:00
|
|
|
"""
|
|
|
|
if isinstance(klass, QuerySet):
|
|
|
|
return klass
|
|
|
|
elif isinstance(klass, Manager):
|
|
|
|
manager = klass
|
2012-12-13 03:34:59 +08:00
|
|
|
elif isinstance(klass, ModelBase):
|
2007-07-22 11:41:11 +08:00
|
|
|
manager = klass._default_manager
|
2012-12-13 03:34:59 +08:00
|
|
|
else:
|
2013-09-22 20:36:53 +08:00
|
|
|
if isinstance(klass, type):
|
2013-09-22 20:01:57 +08:00
|
|
|
klass__name = klass.__name__
|
|
|
|
else:
|
|
|
|
klass__name = klass.__class__.__name__
|
2012-12-13 03:34:59 +08:00
|
|
|
raise ValueError("Object is of type '%s', but must be a Django Model, "
|
|
|
|
"Manager, or QuerySet" % klass__name)
|
2007-07-22 11:41:11 +08:00
|
|
|
return manager.all()
|
|
|
|
|
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
|
|
|
"""
|
2007-07-25 11:12:31 +08:00
|
|
|
Uses get() to return an object, or raises 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)
|
|
|
|
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
|
|
|
"""
|
2007-07-25 11:12:31 +08:00
|
|
|
Uses 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)
|
|
|
|
obj_list = list(queryset.filter(*args, **kwargs))
|
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.
|
|
|
|
|
|
|
|
* A view name, possibly with arguments: `urlresolvers.reverse()` will
|
|
|
|
be used to reverse-resolve the name.
|
|
|
|
|
|
|
|
* 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)
|
|
|
|
|
2014-02-15 20:54:35 +08:00
|
|
|
if isinstance(to, six.string_types):
|
|
|
|
# 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:
|
|
|
|
return urlresolvers.reverse(to, args=args, kwargs=kwargs)
|
|
|
|
except urlresolvers.NoReverseMatch:
|
|
|
|
# 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
|