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.
|
|
|
|
"""
|
2013-01-31 20:39:29 +08:00
|
|
|
import warnings
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2010-12-22 01:18:41 +08:00
|
|
|
from django.template import loader, RequestContext
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.http import HttpResponse, Http404
|
2009-03-21 21:09:13 +08:00
|
|
|
from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect
|
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
|
2009-03-21 21:09:13 +08:00
|
|
|
from django.core import urlresolvers
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def render_to_response(*args, **kwargs):
|
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.
|
|
|
|
"""
|
2013-01-31 20:39:29 +08:00
|
|
|
httpresponse_kwargs = {'content_type': kwargs.pop('content_type', None)}
|
|
|
|
|
|
|
|
mimetype = kwargs.pop('mimetype', None)
|
|
|
|
if mimetype:
|
|
|
|
warnings.warn("The mimetype keyword argument is deprecated, use "
|
|
|
|
"content_type instead", DeprecationWarning, stacklevel=2)
|
|
|
|
httpresponse_kwargs['content_type'] = mimetype
|
|
|
|
|
2007-09-15 05:30:13 +08:00
|
|
|
return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2010-12-22 01:18:41 +08: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 12:54:10 +08:00
|
|
|
httpresponse_kwargs = {
|
|
|
|
'content_type': kwargs.pop('content_type', None),
|
|
|
|
'status': kwargs.pop('status', None),
|
|
|
|
}
|
2011-01-06 06:41:43 +08: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-22 01:18:41 +08:00
|
|
|
return HttpResponse(loader.render_to_string(*args, **kwargs),
|
|
|
|
**httpresponse_kwargs)
|
|
|
|
|
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
|
|
|
|
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:
|
|
|
|
klass__name = klass.__name__ if isinstance(klass, type) \
|
|
|
|
else klass.__class__.__name__
|
|
|
|
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()
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
# 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
|