测试gitnore
This commit is contained in:
@@ -4,21 +4,16 @@ of MVC. In other words, these functions/classes introduce controlled coupling
|
||||
for convenience's sake.
|
||||
"""
|
||||
from django.http import (
|
||||
Http404,
|
||||
HttpResponse,
|
||||
HttpResponsePermanentRedirect,
|
||||
HttpResponseRedirect,
|
||||
Http404, HttpResponse, HttpResponsePermanentRedirect, HttpResponseRedirect,
|
||||
)
|
||||
from django.template import loader
|
||||
from django.urls import NoReverseMatch, reverse
|
||||
from django.utils.functional import Promise
|
||||
|
||||
|
||||
def render(
|
||||
request, template_name, context=None, content_type=None, status=None, using=None
|
||||
):
|
||||
def render(request, template_name, context=None, content_type=None, status=None, using=None):
|
||||
"""
|
||||
Return an HttpResponse whose content is filled with the result of calling
|
||||
Return a HttpResponse whose content is filled with the result of calling
|
||||
django.template.loader.render_to_string() with the passed arguments.
|
||||
"""
|
||||
content = loader.render_to_string(template_name, context, request, using=using)
|
||||
@@ -42,9 +37,7 @@ def redirect(to, *args, permanent=False, **kwargs):
|
||||
Issues a temporary redirect by default; pass permanent=True to issue a
|
||||
permanent redirect.
|
||||
"""
|
||||
redirect_class = (
|
||||
HttpResponsePermanentRedirect if permanent else HttpResponseRedirect
|
||||
)
|
||||
redirect_class = HttpResponsePermanentRedirect if permanent else HttpResponseRedirect
|
||||
return redirect_class(resolve_url(to, *args, **kwargs))
|
||||
|
||||
|
||||
@@ -56,14 +49,14 @@ def _get_queryset(klass):
|
||||
the job.
|
||||
"""
|
||||
# If it is a model class or anything else with ._default_manager
|
||||
if hasattr(klass, "_default_manager"):
|
||||
if hasattr(klass, '_default_manager'):
|
||||
return klass._default_manager.all()
|
||||
return klass
|
||||
|
||||
|
||||
def get_object_or_404(klass, *args, **kwargs):
|
||||
"""
|
||||
Use get() to return an object, or raise an Http404 exception if the object
|
||||
Use get() to return an object, or raise a Http404 exception if the object
|
||||
does not exist.
|
||||
|
||||
klass may be a Model, Manager, or QuerySet object. All other passed
|
||||
@@ -73,10 +66,8 @@ def get_object_or_404(klass, *args, **kwargs):
|
||||
one object is found.
|
||||
"""
|
||||
queryset = _get_queryset(klass)
|
||||
if not hasattr(queryset, "get"):
|
||||
klass__name = (
|
||||
klass.__name__ if isinstance(klass, type) else klass.__class__.__name__
|
||||
)
|
||||
if not hasattr(queryset, 'get'):
|
||||
klass__name = klass.__name__ if isinstance(klass, type) else klass.__class__.__name__
|
||||
raise ValueError(
|
||||
"First argument to get_object_or_404() must be a Model, Manager, "
|
||||
"or QuerySet, not '%s'." % klass__name
|
||||
@@ -84,33 +75,27 @@ def get_object_or_404(klass, *args, **kwargs):
|
||||
try:
|
||||
return queryset.get(*args, **kwargs)
|
||||
except queryset.model.DoesNotExist:
|
||||
raise Http404(
|
||||
"No %s matches the given query." % queryset.model._meta.object_name
|
||||
)
|
||||
raise Http404('No %s matches the given query.' % queryset.model._meta.object_name)
|
||||
|
||||
|
||||
def get_list_or_404(klass, *args, **kwargs):
|
||||
"""
|
||||
Use filter() to return a list of objects, or raise an Http404 exception if
|
||||
Use filter() to return a list of objects, or raise a Http404 exception if
|
||||
the list is empty.
|
||||
|
||||
klass may be a Model, Manager, or QuerySet object. All other passed
|
||||
arguments and keyword arguments are used in the filter() query.
|
||||
"""
|
||||
queryset = _get_queryset(klass)
|
||||
if not hasattr(queryset, "filter"):
|
||||
klass__name = (
|
||||
klass.__name__ if isinstance(klass, type) else klass.__class__.__name__
|
||||
)
|
||||
if not hasattr(queryset, 'filter'):
|
||||
klass__name = klass.__name__ if isinstance(klass, type) else klass.__class__.__name__
|
||||
raise ValueError(
|
||||
"First argument to get_list_or_404() must be a Model, Manager, or "
|
||||
"QuerySet, not '%s'." % klass__name
|
||||
)
|
||||
obj_list = list(queryset.filter(*args, **kwargs))
|
||||
if not obj_list:
|
||||
raise Http404(
|
||||
"No %s matches the given query." % queryset.model._meta.object_name
|
||||
)
|
||||
raise Http404('No %s matches the given query.' % queryset.model._meta.object_name)
|
||||
return obj_list
|
||||
|
||||
|
||||
@@ -128,7 +113,7 @@ def resolve_url(to, *args, **kwargs):
|
||||
* A URL, which will be returned as-is.
|
||||
"""
|
||||
# If it's a model, use get_absolute_url()
|
||||
if hasattr(to, "get_absolute_url"):
|
||||
if hasattr(to, 'get_absolute_url'):
|
||||
return to.get_absolute_url()
|
||||
|
||||
if isinstance(to, Promise):
|
||||
@@ -137,7 +122,7 @@ def resolve_url(to, *args, **kwargs):
|
||||
to = str(to)
|
||||
|
||||
# Handle relative URLs
|
||||
if isinstance(to, str) and to.startswith(("./", "../")):
|
||||
if isinstance(to, str) and to.startswith(('./', '../')):
|
||||
return to
|
||||
|
||||
# Next try a reverse URL resolution.
|
||||
@@ -148,7 +133,7 @@ def resolve_url(to, *args, **kwargs):
|
||||
if callable(to):
|
||||
raise
|
||||
# If this doesn't "feel" like a URL, re-raise.
|
||||
if "/" not in to and "." not in to:
|
||||
if '/' not in to and '.' not in to:
|
||||
raise
|
||||
|
||||
# Finally, fall back and assume it's a URL
|
||||
|
||||
Reference in New Issue
Block a user