2007-07-22 03:09:24 +00: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.
|
|
|
|
"""
|
2010-12-21 17:18:41 +00:00
|
|
|
from django.template import loader, RequestContext
|
2006-05-02 01:31:56 +00:00
|
|
|
from django.http import HttpResponse, Http404
|
2009-03-21 13:09:13 +00:00
|
|
|
from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect
|
2012-12-12 19:34:59 +00:00
|
|
|
from django.db.models.base import ModelBase
|
2007-01-03 14:16:58 +00:00
|
|
|
from django.db.models.manager import Manager
|
2007-07-22 03:41:11 +00:00
|
|
|
from django.db.models.query import QuerySet
|
2009-03-21 13:09:13 +00:00
|
|
|
from django.core import urlresolvers
|
2014-02-15 12:54:35 +00:00
|
|
|
from django.utils import six
|
2006-05-02 01:31:56 +00:00
|
|
|
|
2013-11-02 20:12:09 +00:00
|
|
|
|
2006-05-02 01:31:56 +00:00
|
|
|
def render_to_response(*args, **kwargs):
|
2007-07-22 03:09:24 +00:00
|
|
|
"""
|
2007-07-25 03:12:31 +00:00
|
|
|
Returns a HttpResponse whose content is filled with the result of calling
|
2007-07-22 03:09:24 +00:00
|
|
|
django.template.loader.render_to_string() with the passed arguments.
|
|
|
|
"""
|
2013-01-31 12:39:29 +00:00
|
|
|
httpresponse_kwargs = {'content_type': kwargs.pop('content_type', None)}
|
|
|
|
|
2007-09-14 21:30:13 +00:00
|
|
|
return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
|
2006-05-02 01:31:56 +00:00
|
|
|
|
2013-11-02 20:12:09 +00:00
|
|
|
|
2010-12-21 17:18:41 +00:00
|
|
|
def render(request, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
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.
|
|
|
|
"""
|
2010-12-22 04:54:10 +00:00
|
|
|
httpresponse_kwargs = {
|
|
|
|
'content_type': kwargs.pop('content_type', None),
|
|
|
|
'status': kwargs.pop('status', None),
|
|
|
|
}
|
2011-01-05 22:41:43 +00:00
|
|
|
|
|
|
|
if 'context_instance' in kwargs:
|
|
|
|
context_instance = kwargs.pop('context_instance')
|
|
|
|
if kwargs.get('current_app', None):
|
|
|
|
raise ValueError('If you provide a context_instance you must '
|
|
|
|
'set its current_app before calling render()')
|
|
|
|
else:
|
|
|
|
current_app = kwargs.pop('current_app', None)
|
|
|
|
context_instance = RequestContext(request, current_app=current_app)
|
|
|
|
|
|
|
|
kwargs['context_instance'] = context_instance
|
|
|
|
|
2010-12-21 17:18:41 +00:00
|
|
|
return HttpResponse(loader.render_to_string(*args, **kwargs),
|
|
|
|
**httpresponse_kwargs)
|
|
|
|
|
2013-11-02 20:12:09 +00:00
|
|
|
|
2009-03-21 13:09:13 +00:00
|
|
|
def redirect(to, *args, **kwargs):
|
|
|
|
"""
|
2012-11-23 16:20:18 +00:00
|
|
|
Returns an HttpResponseRedirect to the appropriate URL for the arguments
|
2009-03-21 13:09:13 +00:00
|
|
|
passed.
|
2010-12-21 17:18:41 +00:00
|
|
|
|
2009-03-21 13:09:13 +00:00
|
|
|
The arguments could be:
|
2010-12-21 17:18:41 +00:00
|
|
|
|
2009-03-21 13:09:13 +00:00
|
|
|
* A model: the model's `get_absolute_url()` function will be called.
|
2010-12-21 17:18:41 +00:00
|
|
|
|
2009-03-21 13:09:13 +00:00
|
|
|
* A view name, possibly with arguments: `urlresolvers.reverse()` will
|
|
|
|
be used to reverse-resolve the name.
|
2010-12-21 17:18:41 +00:00
|
|
|
|
2009-03-21 13:09:13 +00:00
|
|
|
* A URL, which will be used as-is for the redirect location.
|
2010-12-21 17:18:41 +00:00
|
|
|
|
2009-03-21 13:09:13 +00: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-21 17:18:41 +00:00
|
|
|
|
2012-09-08 22:55:29 +00:00
|
|
|
return redirect_class(resolve_url(to, *args, **kwargs))
|
2009-03-21 13:09:13 +00:00
|
|
|
|
2013-11-02 20:12:09 +00:00
|
|
|
|
2007-07-22 03:41:11 +00:00
|
|
|
def _get_queryset(klass):
|
|
|
|
"""
|
2007-07-25 03:12:31 +00:00
|
|
|
Returns a QuerySet from a Model, Manager, or QuerySet. Created to make
|
2007-07-22 03:41:11 +00:00
|
|
|
get_object_or_404 and get_list_or_404 more DRY.
|
2012-12-12 19:34:59 +00:00
|
|
|
|
|
|
|
Raises a ValueError if klass is not a Model, Manager, or QuerySet.
|
2007-07-22 03:41:11 +00:00
|
|
|
"""
|
|
|
|
if isinstance(klass, QuerySet):
|
|
|
|
return klass
|
|
|
|
elif isinstance(klass, Manager):
|
|
|
|
manager = klass
|
2012-12-12 19:34:59 +00:00
|
|
|
elif isinstance(klass, ModelBase):
|
2007-07-22 03:41:11 +00:00
|
|
|
manager = klass._default_manager
|
2012-12-12 19:34:59 +00:00
|
|
|
else:
|
2013-09-22 12:36:53 +00:00
|
|
|
if isinstance(klass, type):
|
2013-09-22 12:01:57 +00:00
|
|
|
klass__name = klass.__name__
|
|
|
|
else:
|
|
|
|
klass__name = klass.__class__.__name__
|
2012-12-12 19:34:59 +00:00
|
|
|
raise ValueError("Object is of type '%s', but must be a Django Model, "
|
|
|
|
"Manager, or QuerySet" % klass__name)
|
2007-07-22 03:41:11 +00:00
|
|
|
return manager.all()
|
|
|
|
|
2013-11-02 20:12:09 +00:00
|
|
|
|
2006-05-31 12:34:05 +00:00
|
|
|
def get_object_or_404(klass, *args, **kwargs):
|
2007-07-22 03:09:24 +00:00
|
|
|
"""
|
2007-07-25 03:12:31 +00:00
|
|
|
Uses get() to return an object, or raises a Http404 exception if the object
|
2007-07-22 03:09:24 +00:00
|
|
|
does not exist.
|
|
|
|
|
2007-07-25 03:12:31 +00:00
|
|
|
klass may be a Model, Manager, or QuerySet object. All other passed
|
2007-07-22 03:09:24 +00:00
|
|
|
arguments and keyword arguments are used in the get() query.
|
|
|
|
|
2007-12-02 18:21:07 +00:00
|
|
|
Note: Like with get(), an MultipleObjectsReturned will be raised if more than one
|
2007-07-22 03:09:24 +00:00
|
|
|
object is found.
|
|
|
|
"""
|
2007-07-22 03:41:11 +00:00
|
|
|
queryset = _get_queryset(klass)
|
2006-05-02 01:31:56 +00:00
|
|
|
try:
|
2007-07-22 03:41:11 +00: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 01:31:56 +00:00
|
|
|
|
2013-11-02 20:12:09 +00:00
|
|
|
|
2006-05-31 12:36:01 +00:00
|
|
|
def get_list_or_404(klass, *args, **kwargs):
|
2007-07-22 03:09:24 +00:00
|
|
|
"""
|
2007-07-25 03:12:31 +00:00
|
|
|
Uses filter() to return a list of objects, or raise a Http404 exception if
|
2007-07-22 03:45:03 +00:00
|
|
|
the list is empty.
|
2007-07-22 03:09:24 +00:00
|
|
|
|
2007-07-25 03:12:31 +00:00
|
|
|
klass may be a Model, Manager, or QuerySet object. All other passed
|
2007-07-22 03:09:24 +00:00
|
|
|
arguments and keyword arguments are used in the filter() query.
|
|
|
|
"""
|
2007-07-22 03:41:11 +00:00
|
|
|
queryset = _get_queryset(klass)
|
|
|
|
obj_list = list(queryset.filter(*args, **kwargs))
|
2006-05-02 01:31:56 +00:00
|
|
|
if not obj_list:
|
2007-07-22 03:41:11 +00:00
|
|
|
raise Http404('No %s matches the given query.' % queryset.model._meta.object_name)
|
2010-12-21 17:18:41 +00:00
|
|
|
return obj_list
|
|
|
|
|
2013-11-02 20:12:09 +00:00
|
|
|
|
2012-09-08 22:55:29 +00: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()
|
|
|
|
|
2014-02-15 12:54:35 +00:00
|
|
|
if isinstance(to, six.string_types):
|
|
|
|
# Handle relative URLs
|
|
|
|
if any(to.startswith(path) for path in ('./', '../')):
|
|
|
|
return to
|
|
|
|
|
2012-09-08 22:55:29 +00: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
|