~ubuntu-branches/debian/sid/python-django/sid

« back to all changes in this revision

Viewing changes to django/shortcuts/__init__.py

  • Committer: Package Import Robot
  • Author(s): Raphaël Hertzog
  • Date: 2014-09-17 14:15:11 UTC
  • mfrom: (1.3.17) (6.2.18 experimental)
  • Revision ID: package-import@ubuntu.com-20140917141511-icneokthe9ww5sk4
Tags: 1.7-2
* Release to unstable.
* Add a migrate-south sample script to help users apply their South
  migrations. Thanks to Brian May.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
import warnings
7
 
 
8
 
from django.template import loader, RequestContext
9
 
from django.http import HttpResponse, Http404
10
 
from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect
11
 
from django.db.models.base import ModelBase
12
 
from django.db.models.manager import Manager
13
 
from django.db.models.query import QuerySet
14
 
from django.core import urlresolvers
15
 
 
16
 
def render_to_response(*args, **kwargs):
17
 
    """
18
 
    Returns a HttpResponse whose content is filled with the result of calling
19
 
    django.template.loader.render_to_string() with the passed arguments.
20
 
    """
21
 
    httpresponse_kwargs = {'content_type': kwargs.pop('content_type', None)}
22
 
 
23
 
    mimetype = kwargs.pop('mimetype', None)
24
 
    if mimetype:
25
 
        warnings.warn("The mimetype keyword argument is deprecated, use "
26
 
            "content_type instead", DeprecationWarning, stacklevel=2)
27
 
        httpresponse_kwargs['content_type'] = mimetype
28
 
 
29
 
    return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
30
 
 
31
 
def render(request, *args, **kwargs):
32
 
    """
33
 
    Returns a HttpResponse whose content is filled with the result of calling
34
 
    django.template.loader.render_to_string() with the passed arguments.
35
 
    Uses a RequestContext by default.
36
 
    """
37
 
    httpresponse_kwargs = {
38
 
        'content_type': kwargs.pop('content_type', None),
39
 
        'status': kwargs.pop('status', None),
40
 
    }
41
 
 
42
 
    if 'context_instance' in kwargs:
43
 
        context_instance = kwargs.pop('context_instance')
44
 
        if kwargs.get('current_app', None):
45
 
            raise ValueError('If you provide a context_instance you must '
46
 
                             'set its current_app before calling render()')
47
 
    else:
48
 
        current_app = kwargs.pop('current_app', None)
49
 
        context_instance = RequestContext(request, current_app=current_app)
50
 
 
51
 
    kwargs['context_instance'] = context_instance
52
 
 
53
 
    return HttpResponse(loader.render_to_string(*args, **kwargs),
54
 
                        **httpresponse_kwargs)
55
 
 
56
 
def redirect(to, *args, **kwargs):
57
 
    """
58
 
    Returns an HttpResponseRedirect to the appropriate URL for the arguments
59
 
    passed.
60
 
 
61
 
    The arguments could be:
62
 
 
63
 
        * A model: the model's `get_absolute_url()` function will be called.
64
 
 
65
 
        * A view name, possibly with arguments: `urlresolvers.reverse()` will
66
 
          be used to reverse-resolve the name.
67
 
 
68
 
        * A URL, which will be used as-is for the redirect location.
69
 
 
70
 
    By default issues a temporary redirect; pass permanent=True to issue a
71
 
    permanent redirect
72
 
    """
73
 
    if kwargs.pop('permanent', False):
74
 
        redirect_class = HttpResponsePermanentRedirect
75
 
    else:
76
 
        redirect_class = HttpResponseRedirect
77
 
 
78
 
    return redirect_class(resolve_url(to, *args, **kwargs))
79
 
 
80
 
def _get_queryset(klass):
81
 
    """
82
 
    Returns a QuerySet from a Model, Manager, or QuerySet. Created to make
83
 
    get_object_or_404 and get_list_or_404 more DRY.
84
 
 
85
 
    Raises a ValueError if klass is not a Model, Manager, or QuerySet.
86
 
    """
87
 
    if isinstance(klass, QuerySet):
88
 
        return klass
89
 
    elif isinstance(klass, Manager):
90
 
        manager = klass
91
 
    elif isinstance(klass, ModelBase):
92
 
        manager = klass._default_manager
93
 
    else:
94
 
        klass__name = klass.__name__ if isinstance(klass, type) \
95
 
                      else klass.__class__.__name__
96
 
        raise ValueError("Object is of type '%s', but must be a Django Model, "
97
 
                         "Manager, or QuerySet" % klass__name)
98
 
    return manager.all()
99
 
 
100
 
def get_object_or_404(klass, *args, **kwargs):
101
 
    """
102
 
    Uses get() to return an object, or raises a Http404 exception if the object
103
 
    does not exist.
104
 
 
105
 
    klass may be a Model, Manager, or QuerySet object. All other passed
106
 
    arguments and keyword arguments are used in the get() query.
107
 
 
108
 
    Note: Like with get(), an MultipleObjectsReturned will be raised if more than one
109
 
    object is found.
110
 
    """
111
 
    queryset = _get_queryset(klass)
112
 
    try:
113
 
        return queryset.get(*args, **kwargs)
114
 
    except queryset.model.DoesNotExist:
115
 
        raise Http404('No %s matches the given query.' % queryset.model._meta.object_name)
116
 
 
117
 
def get_list_or_404(klass, *args, **kwargs):
118
 
    """
119
 
    Uses filter() to return a list of objects, or raise a Http404 exception if
120
 
    the list is empty.
121
 
 
122
 
    klass may be a Model, Manager, or QuerySet object. All other passed
123
 
    arguments and keyword arguments are used in the filter() query.
124
 
    """
125
 
    queryset = _get_queryset(klass)
126
 
    obj_list = list(queryset.filter(*args, **kwargs))
127
 
    if not obj_list:
128
 
        raise Http404('No %s matches the given query.' % queryset.model._meta.object_name)
129
 
    return obj_list
130
 
 
131
 
def resolve_url(to, *args, **kwargs):
132
 
    """
133
 
    Return a URL appropriate for the arguments passed.
134
 
 
135
 
    The arguments could be:
136
 
 
137
 
        * A model: the model's `get_absolute_url()` function will be called.
138
 
 
139
 
        * A view name, possibly with arguments: `urlresolvers.reverse()` will
140
 
          be used to reverse-resolve the name.
141
 
 
142
 
        * A URL, which will be returned as-is.
143
 
 
144
 
    """
145
 
    # If it's a model, use get_absolute_url()
146
 
    if hasattr(to, 'get_absolute_url'):
147
 
        return to.get_absolute_url()
148
 
 
149
 
    # Next try a reverse URL resolution.
150
 
    try:
151
 
        return urlresolvers.reverse(to, args=args, kwargs=kwargs)
152
 
    except urlresolvers.NoReverseMatch:
153
 
        # If this is a callable, re-raise.
154
 
        if callable(to):
155
 
            raise
156
 
        # If this doesn't "feel" like a URL, re-raise.
157
 
        if '/' not in to and '.' not in to:
158
 
            raise
159
 
 
160
 
    # Finally, fall back and assume it's a URL
161
 
    return to