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
|
|
|
|
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
|
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.
|
|
|
|
"""
|
2012-07-01 03:19:07 +08:00
|
|
|
httpresponse_kwargs = {'content_type': 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
|
|
|
|
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):
|
|
|
|
"""
|
|
|
|
Returns an HttpResponseRedirect to the apropriate URL for the arguments
|
|
|
|
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
|
|
|
|
2009-03-21 21:09:13 +08:00
|
|
|
# If it's a model, use get_absolute_url()
|
|
|
|
if hasattr(to, 'get_absolute_url'):
|
|
|
|
return redirect_class(to.get_absolute_url())
|
2010-12-22 01:18:41 +08:00
|
|
|
|
2009-03-21 21:09:13 +08:00
|
|
|
# Next try a reverse URL resolution.
|
|
|
|
try:
|
|
|
|
return redirect_class(urlresolvers.reverse(to, args=args, kwargs=kwargs))
|
|
|
|
except urlresolvers.NoReverseMatch:
|
2010-01-11 02:44:39 +08:00
|
|
|
# If this is a callable, re-raise.
|
|
|
|
if callable(to):
|
|
|
|
raise
|
2009-03-21 21:09:13 +08:00
|
|
|
# If this doesn't "feel" like a URL, re-raise.
|
|
|
|
if '/' not in to and '.' not in to:
|
|
|
|
raise
|
2010-12-22 01:18:41 +08:00
|
|
|
|
2009-03-21 21:09:13 +08:00
|
|
|
# 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)
|
2010-12-22 01:18:41 +08:00
|
|
|
return obj_list
|
|
|
|
|