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.
|
|
|
|
"""
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
from django.template import loader
|
|
|
|
from django.http import HttpResponse, Http404
|
2009-03-21 21:09:13 +08:00
|
|
|
from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect
|
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.
|
|
|
|
"""
|
2007-09-15 05:58:17 +08:00
|
|
|
httpresponse_kwargs = {'mimetype': kwargs.pop('mimetype', None)}
|
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
|
|
|
|
2009-03-21 21:09:13 +08:00
|
|
|
def redirect(to, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Returns an HttpResponseRedirect to the apropriate URL 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 used as-is for the redirect location.
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
# If it's a model, use get_absolute_url()
|
|
|
|
if hasattr(to, 'get_absolute_url'):
|
|
|
|
return redirect_class(to.get_absolute_url())
|
|
|
|
|
|
|
|
# Next try a reverse URL resolution.
|
|
|
|
try:
|
|
|
|
return redirect_class(urlresolvers.reverse(to, args=args, kwargs=kwargs))
|
|
|
|
except urlresolvers.NoReverseMatch:
|
|
|
|
# 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 redirect_class(to)
|
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
if isinstance(klass, QuerySet):
|
|
|
|
return klass
|
|
|
|
elif isinstance(klass, Manager):
|
|
|
|
manager = klass
|
|
|
|
else:
|
|
|
|
manager = klass._default_manager
|
|
|
|
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)
|
2009-03-21 21:09:13 +08:00
|
|
|
return obj_list
|