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.
|
|
|
|
"""
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.http import (
|
|
|
|
Http404,
|
|
|
|
HttpResponse,
|
|
|
|
HttpResponsePermanentRedirect,
|
|
|
|
HttpResponseRedirect,
|
|
|
|
)
|
2015-09-04 10:01:30 +08:00
|
|
|
from django.template import loader
|
2015-12-30 23:51:16 +08:00
|
|
|
from django.urls import NoReverseMatch, reverse
|
2015-01-10 05:18:34 +08:00
|
|
|
from django.utils.functional import Promise
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2015-09-04 10:01:30 +08:00
|
|
|
def render(
|
|
|
|
request, template_name, context=None, content_type=None, status=None, using=None
|
|
|
|
):
|
2010-12-22 01:18:41 +08:00
|
|
|
"""
|
2021-10-19 00:06:00 +08:00
|
|
|
Return an HttpResponse whose content is filled with the result of calling
|
2010-12-22 01:18:41 +08:00
|
|
|
django.template.loader.render_to_string() with the passed arguments.
|
|
|
|
"""
|
2015-09-04 10:01:30 +08:00
|
|
|
content = loader.render_to_string(template_name, context, request, using=using)
|
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
|
|
|
|
2017-02-02 00:41:56 +08:00
|
|
|
def redirect(to, *args, permanent=False, **kwargs):
|
2009-03-21 21:09:13 +08:00
|
|
|
"""
|
2017-01-25 04:37:33 +08:00
|
|
|
Return 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
|
|
|
|
2015-12-30 23:51:16 +08:00
|
|
|
* A view name, possibly with arguments: `urls.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
|
|
|
|
2017-09-29 02:39:01 +08:00
|
|
|
Issues a temporary redirect by default; pass permanent=True to issue a
|
|
|
|
permanent redirect.
|
2009-03-21 21:09:13 +08:00
|
|
|
"""
|
2017-02-02 00:41:56 +08:00
|
|
|
redirect_class = (
|
|
|
|
HttpResponsePermanentRedirect if permanent else HttpResponseRedirect
|
2022-02-04 03:24:19 +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):
|
|
|
|
"""
|
2016-03-28 04:35:57 +08:00
|
|
|
Return a QuerySet or a Manager.
|
|
|
|
Duck typing in action: any class with a `get()` method (for
|
|
|
|
get_object_or_404) or a `filter()` method (for get_list_or_404) might do
|
|
|
|
the job.
|
2007-07-22 11:41:11 +08:00
|
|
|
"""
|
2016-03-28 04:35:57 +08:00
|
|
|
# If it is a model class or anything else with ._default_manager
|
|
|
|
if hasattr(klass, "_default_manager"):
|
|
|
|
return klass._default_manager.all()
|
|
|
|
return klass
|
2007-07-22 11:41:11 +08:00
|
|
|
|
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
|
|
|
"""
|
2021-10-19 00:06:00 +08:00
|
|
|
Use get() to return an object, or raise an 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.
|
|
|
|
|
2017-11-07 11:11:39 +08:00
|
|
|
Like with QuerySet.get(), MultipleObjectsReturned is raised if more than
|
|
|
|
one object is found.
|
2007-07-22 11:09:24 +08:00
|
|
|
"""
|
2007-07-22 11:41:11 +08:00
|
|
|
queryset = _get_queryset(klass)
|
2018-02-17 16:27:46 +08:00
|
|
|
if not hasattr(queryset, "get"):
|
2016-03-28 04:35:57 +08:00
|
|
|
klass__name = (
|
|
|
|
klass.__name__ if isinstance(klass, type) else klass.__class__.__name__
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2016-03-28 04:35:57 +08:00
|
|
|
raise ValueError(
|
|
|
|
"First argument to get_object_or_404() must be a Model, Manager, "
|
|
|
|
"or QuerySet, not '%s'." % klass__name
|
|
|
|
)
|
2018-02-17 16:27:46 +08:00
|
|
|
try:
|
|
|
|
return queryset.get(*args, **kwargs)
|
2007-07-22 11:41:11 +08:00
|
|
|
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
|
|
|
"""
|
2021-10-19 00:06:00 +08:00
|
|
|
Use filter() to return a list of objects, or raise an 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)
|
2018-02-17 16:27:46 +08:00
|
|
|
if not hasattr(queryset, "filter"):
|
2016-03-28 04:35:57 +08:00
|
|
|
klass__name = (
|
|
|
|
klass.__name__ if isinstance(klass, type) else klass.__class__.__name__
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2016-03-28 04:35:57 +08:00
|
|
|
raise ValueError(
|
|
|
|
"First argument to get_list_or_404() must be a Model, Manager, or "
|
|
|
|
"QuerySet, not '%s'." % klass__name
|
|
|
|
)
|
2018-02-17 16:27:46 +08:00
|
|
|
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.
|
|
|
|
|
2015-12-30 23:51:16 +08:00
|
|
|
* A view name, possibly with arguments: `urls.reverse()` will be used
|
|
|
|
to reverse-resolve the name.
|
2012-09-09 06:55:29 +08:00
|
|
|
|
|
|
|
* 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.
|
2017-04-22 01:52:26 +08:00
|
|
|
to = str(to)
|
2015-01-10 05:18:34 +08:00
|
|
|
|
2020-09-25 00:37:55 +08:00
|
|
|
# Handle relative URLs
|
|
|
|
if isinstance(to, str) and to.startswith(("./", "../")):
|
|
|
|
return to
|
2014-02-15 20:54:35 +08:00
|
|
|
|
2012-09-09 06:55:29 +08:00
|
|
|
# Next try a reverse URL resolution.
|
|
|
|
try:
|
2015-12-30 23:51:16 +08:00
|
|
|
return reverse(to, args=args, kwargs=kwargs)
|
|
|
|
except NoReverseMatch:
|
2012-09-09 06:55:29 +08:00
|
|
|
# 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
|