1 | """
|
---|
2 | This module collects helper functions and classes that "span" multiple levels
|
---|
3 | of MVC. In other words, these functions/classes introduce controlled coupling
|
---|
4 | for convenience's sake.
|
---|
5 | """
|
---|
6 |
|
---|
7 | from django.template import loader
|
---|
8 | from django.http import HttpResponse, Http404
|
---|
9 | from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect
|
---|
10 | from django.db.models.manager import Manager
|
---|
11 | from django.db.models.query import QuerySet
|
---|
12 | from django.core import urlresolvers
|
---|
13 |
|
---|
14 | def render_to_response(*args, **kwargs):
|
---|
15 | """
|
---|
16 | Returns a HttpResponse whose content is filled with the result of calling
|
---|
17 | django.template.loader.render_to_string() with the passed arguments.
|
---|
18 | """
|
---|
19 | httpresponse_kwargs = {'mimetype': kwargs.pop('mimetype', None)}
|
---|
20 | return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
|
---|
21 |
|
---|
22 | def redirect(to, *args, **kwargs):
|
---|
23 | """
|
---|
24 | Returns an HttpResponseRedirect to the apropriate URL for the arguments
|
---|
25 | passed.
|
---|
26 |
|
---|
27 | The arguments could be:
|
---|
28 |
|
---|
29 | * A model: the model's `get_absolute_url()` function will be called.
|
---|
30 |
|
---|
31 | * A view name, possibly with arguments: `urlresolvers.reverse()` will
|
---|
32 | be used to reverse-resolve the name.
|
---|
33 |
|
---|
34 | * A URL, which will be used as-is for the redirect location.
|
---|
35 |
|
---|
36 | By default issues a temporary redirect; pass permanent=True to issue a
|
---|
37 | permanent redirect
|
---|
38 | """
|
---|
39 | if kwargs.pop('permanent', False):
|
---|
40 | redirect_class = HttpResponsePermanentRedirect
|
---|
41 | else:
|
---|
42 | redirect_class = HttpResponseRedirect
|
---|
43 |
|
---|
44 | # If it's a model, use get_absolute_url()
|
---|
45 | if hasattr(to, 'get_absolute_url'):
|
---|
46 | return redirect_class(to.get_absolute_url())
|
---|
47 |
|
---|
48 | # Next try a reverse URL resolution.
|
---|
49 | try:
|
---|
50 | return redirect_class(urlresolvers.reverse(to, args=args, kwargs=kwargs))
|
---|
51 | except urlresolvers.NoReverseMatch:
|
---|
52 | # If this is a callable, re-raise.
|
---|
53 | if callable(to):
|
---|
54 | raise
|
---|
55 | # If this doesn't "feel" like a URL, re-raise.
|
---|
56 | if '/' not in to and '.' not in to:
|
---|
57 | raise
|
---|
58 |
|
---|
59 | # Finally, fall back and assume it's a URL
|
---|
60 | return redirect_class(to)
|
---|
61 |
|
---|
62 | def _get_queryset(klass):
|
---|
63 | """
|
---|
64 | Returns a QuerySet from a Model, Manager, or QuerySet. Created to make
|
---|
65 | get_object_or_404 and get_list_or_404 more DRY.
|
---|
66 | """
|
---|
67 | if isinstance(klass, QuerySet):
|
---|
68 | return klass
|
---|
69 | elif isinstance(klass, Manager):
|
---|
70 | manager = klass
|
---|
71 | else:
|
---|
72 | manager = klass._default_manager
|
---|
73 | return manager.all()
|
---|
74 |
|
---|
75 | def get_object_or_404(klass, *args, **kwargs):
|
---|
76 | """
|
---|
77 | Uses get() to return an object, or raises a Http404 exception if the object
|
---|
78 | does not exist.
|
---|
79 |
|
---|
80 | klass may be a Model, Manager, or QuerySet object. All other passed
|
---|
81 | arguments and keyword arguments are used in the get() query.
|
---|
82 |
|
---|
83 | Note: Like with get(), an MultipleObjectsReturned will be raised if more than one
|
---|
84 | object is found.
|
---|
85 | """
|
---|
86 | queryset = _get_queryset(klass)
|
---|
87 | try:
|
---|
88 | return queryset.get(*args, **kwargs)
|
---|
89 | except queryset.model.DoesNotExist:
|
---|
90 | raise Http404('No %s matches the given query.' % queryset.model._meta.object_name)
|
---|
91 |
|
---|
92 | def get_objects_or_404(klass, *args, **kwargs):
|
---|
93 | """
|
---|
94 | Get a set of filtered objects
|
---|
95 |
|
---|
96 | Uses filter() to return objects, or raise a Http404 exception if
|
---|
97 | no objects matches.
|
---|
98 |
|
---|
99 | klass may be a Model, Manager, or QuerySet object. All other passed
|
---|
100 | arguments and keyword arguments are used in the filter() query.
|
---|
101 | """
|
---|
102 | queryset = _get_queryset(klass)
|
---|
103 | objects = queryset.filter(*args, **kwargs)
|
---|
104 | if not objects:
|
---|
105 | raise Http404('No %s matches the given query.' % queryset.model._meta.object_name)
|
---|
106 | return objects
|
---|
107 |
|
---|
108 | def get_list_or_404(klass, *args, **kwargs):
|
---|
109 | """
|
---|
110 | Get a list of filtered objects
|
---|
111 |
|
---|
112 | Uses get_object_or_404 to get a set of objects, which will raise a Http404 exception if
|
---|
113 | no objects matches, then returns that set as a list.
|
---|
114 |
|
---|
115 | klass may be a Model, Manager, or QuerySet object. All other passed
|
---|
116 | arguments and keyword arguments are used in the filter() query
|
---|
117 | with get_objects_or_404.
|
---|
118 | """
|
---|
119 | return list(get_objects_or_404(klass, *args, **kwargs))
|
---|