|
1 from django.forms.models import ModelFormMetaclass, ModelForm |
|
2 from django.template import RequestContext, loader |
|
3 from django.http import Http404, HttpResponse, HttpResponseRedirect |
|
4 from django.core.xheaders import populate_xheaders |
|
5 from django.core.exceptions import ObjectDoesNotExist, ImproperlyConfigured |
|
6 from django.utils.translation import ugettext |
|
7 from django.contrib.auth.views import redirect_to_login |
|
8 from django.views.generic import GenericViewError |
|
9 |
|
10 |
|
11 def apply_extra_context(extra_context, context): |
|
12 """ |
|
13 Adds items from extra_context dict to context. If a value in extra_context |
|
14 is callable, then it is called and the result is added to context. |
|
15 """ |
|
16 for key, value in extra_context.iteritems(): |
|
17 if callable(value): |
|
18 context[key] = value() |
|
19 else: |
|
20 context[key] = value |
|
21 |
|
22 def get_model_and_form_class(model, form_class): |
|
23 """ |
|
24 Returns a model and form class based on the model and form_class |
|
25 parameters that were passed to the generic view. |
|
26 |
|
27 If ``form_class`` is given then its associated model will be returned along |
|
28 with ``form_class`` itself. Otherwise, if ``model`` is given, ``model`` |
|
29 itself will be returned along with a ``ModelForm`` class created from |
|
30 ``model``. |
|
31 """ |
|
32 if form_class: |
|
33 return form_class._meta.model, form_class |
|
34 if model: |
|
35 # The inner Meta class fails if model = model is used for some reason. |
|
36 tmp_model = model |
|
37 # TODO: we should be able to construct a ModelForm without creating |
|
38 # and passing in a temporary inner class. |
|
39 class Meta: |
|
40 model = tmp_model |
|
41 class_name = model.__name__ + 'Form' |
|
42 form_class = ModelFormMetaclass(class_name, (ModelForm,), {'Meta': Meta}) |
|
43 return model, form_class |
|
44 raise GenericViewError("Generic view must be called with either a model or" |
|
45 " form_class argument.") |
|
46 |
|
47 def redirect(post_save_redirect, obj): |
|
48 """ |
|
49 Returns a HttpResponseRedirect to ``post_save_redirect``. |
|
50 |
|
51 ``post_save_redirect`` should be a string, and can contain named string- |
|
52 substitution place holders of ``obj`` field names. |
|
53 |
|
54 If ``post_save_redirect`` is None, then redirect to ``obj``'s URL returned |
|
55 by ``get_absolute_url()``. If ``obj`` has no ``get_absolute_url`` method, |
|
56 then raise ImproperlyConfigured. |
|
57 |
|
58 This function is meant to handle the post_save_redirect parameter to the |
|
59 ``create_object`` and ``update_object`` views. |
|
60 """ |
|
61 if post_save_redirect: |
|
62 return HttpResponseRedirect(post_save_redirect % obj.__dict__) |
|
63 elif hasattr(obj, 'get_absolute_url'): |
|
64 return HttpResponseRedirect(obj.get_absolute_url()) |
|
65 else: |
|
66 raise ImproperlyConfigured( |
|
67 "No URL to redirect to. Either pass a post_save_redirect" |
|
68 " parameter to the generic view or define a get_absolute_url" |
|
69 " method on the Model.") |
|
70 |
|
71 def lookup_object(model, object_id, slug, slug_field): |
|
72 """ |
|
73 Return the ``model`` object with the passed ``object_id``. If |
|
74 ``object_id`` is None, then return the object whose ``slug_field`` |
|
75 equals the passed ``slug``. If ``slug`` and ``slug_field`` are not passed, |
|
76 then raise Http404 exception. |
|
77 """ |
|
78 lookup_kwargs = {} |
|
79 if object_id: |
|
80 lookup_kwargs['%s__exact' % model._meta.pk.name] = object_id |
|
81 elif slug and slug_field: |
|
82 lookup_kwargs['%s__exact' % slug_field] = slug |
|
83 else: |
|
84 raise GenericViewError( |
|
85 "Generic view must be called with either an object_id or a" |
|
86 " slug/slug_field.") |
|
87 try: |
|
88 return model.objects.get(**lookup_kwargs) |
|
89 except ObjectDoesNotExist: |
|
90 raise Http404("No %s found for %s" |
|
91 % (model._meta.verbose_name, lookup_kwargs)) |
|
92 |
|
93 def create_object(request, model=None, template_name=None, |
|
94 template_loader=loader, extra_context=None, post_save_redirect=None, |
|
95 login_required=False, context_processors=None, form_class=None): |
|
96 """ |
|
97 Generic object-creation function. |
|
98 |
|
99 Templates: ``<app_label>/<model_name>_form.html`` |
|
100 Context: |
|
101 form |
|
102 the form for the object |
|
103 """ |
|
104 if extra_context is None: extra_context = {} |
|
105 if login_required and not request.user.is_authenticated(): |
|
106 return redirect_to_login(request.path) |
|
107 |
|
108 model, form_class = get_model_and_form_class(model, form_class) |
|
109 if request.method == 'POST': |
|
110 form = form_class(request.POST, request.FILES) |
|
111 if form.is_valid(): |
|
112 new_object = form.save() |
|
113 if request.user.is_authenticated(): |
|
114 request.user.message_set.create(message=ugettext("The %(verbose_name)s was created successfully.") % {"verbose_name": model._meta.verbose_name}) |
|
115 return redirect(post_save_redirect, new_object) |
|
116 else: |
|
117 form = form_class() |
|
118 |
|
119 # Create the template, context, response |
|
120 if not template_name: |
|
121 template_name = "%s/%s_form.html" % (model._meta.app_label, model._meta.object_name.lower()) |
|
122 t = template_loader.get_template(template_name) |
|
123 c = RequestContext(request, { |
|
124 'form': form, |
|
125 }, context_processors) |
|
126 apply_extra_context(extra_context, c) |
|
127 return HttpResponse(t.render(c)) |
|
128 |
|
129 def update_object(request, model=None, object_id=None, slug=None, |
|
130 slug_field='slug', template_name=None, template_loader=loader, |
|
131 extra_context=None, post_save_redirect=None, login_required=False, |
|
132 context_processors=None, template_object_name='object', |
|
133 form_class=None): |
|
134 """ |
|
135 Generic object-update function. |
|
136 |
|
137 Templates: ``<app_label>/<model_name>_form.html`` |
|
138 Context: |
|
139 form |
|
140 the form for the object |
|
141 object |
|
142 the original object being edited |
|
143 """ |
|
144 if extra_context is None: extra_context = {} |
|
145 if login_required and not request.user.is_authenticated(): |
|
146 return redirect_to_login(request.path) |
|
147 |
|
148 model, form_class = get_model_and_form_class(model, form_class) |
|
149 obj = lookup_object(model, object_id, slug, slug_field) |
|
150 |
|
151 if request.method == 'POST': |
|
152 form = form_class(request.POST, request.FILES, instance=obj) |
|
153 if form.is_valid(): |
|
154 obj = form.save() |
|
155 if request.user.is_authenticated(): |
|
156 request.user.message_set.create(message=ugettext("The %(verbose_name)s was updated successfully.") % {"verbose_name": model._meta.verbose_name}) |
|
157 return redirect(post_save_redirect, obj) |
|
158 else: |
|
159 form = form_class(instance=obj) |
|
160 |
|
161 if not template_name: |
|
162 template_name = "%s/%s_form.html" % (model._meta.app_label, model._meta.object_name.lower()) |
|
163 t = template_loader.get_template(template_name) |
|
164 c = RequestContext(request, { |
|
165 'form': form, |
|
166 template_object_name: obj, |
|
167 }, context_processors) |
|
168 apply_extra_context(extra_context, c) |
|
169 response = HttpResponse(t.render(c)) |
|
170 populate_xheaders(request, response, model, getattr(obj, obj._meta.pk.attname)) |
|
171 return response |
|
172 |
|
173 def delete_object(request, model, post_delete_redirect, object_id=None, |
|
174 slug=None, slug_field='slug', template_name=None, |
|
175 template_loader=loader, extra_context=None, login_required=False, |
|
176 context_processors=None, template_object_name='object'): |
|
177 """ |
|
178 Generic object-delete function. |
|
179 |
|
180 The given template will be used to confirm deletetion if this view is |
|
181 fetched using GET; for safty, deletion will only be performed if this |
|
182 view is POSTed. |
|
183 |
|
184 Templates: ``<app_label>/<model_name>_confirm_delete.html`` |
|
185 Context: |
|
186 object |
|
187 the original object being deleted |
|
188 """ |
|
189 if extra_context is None: extra_context = {} |
|
190 if login_required and not request.user.is_authenticated(): |
|
191 return redirect_to_login(request.path) |
|
192 |
|
193 obj = lookup_object(model, object_id, slug, slug_field) |
|
194 |
|
195 if request.method == 'POST': |
|
196 obj.delete() |
|
197 if request.user.is_authenticated(): |
|
198 request.user.message_set.create(message=ugettext("The %(verbose_name)s was deleted.") % {"verbose_name": model._meta.verbose_name}) |
|
199 return HttpResponseRedirect(post_delete_redirect) |
|
200 else: |
|
201 if not template_name: |
|
202 template_name = "%s/%s_confirm_delete.html" % (model._meta.app_label, model._meta.object_name.lower()) |
|
203 t = template_loader.get_template(template_name) |
|
204 c = RequestContext(request, { |
|
205 template_object_name: obj, |
|
206 }, context_processors) |
|
207 apply_extra_context(extra_context, c) |
|
208 response = HttpResponse(t.render(c)) |
|
209 populate_xheaders(request, response, model, getattr(obj, obj._meta.pk.attname)) |
|
210 return response |