| 10 | from django import newforms as forms |
| 11 | from django.newforms.models import ModelFormMetaclass, ModelForm |
| 12 | |
| 13 | |
| 14 | # New-sytle, class based generic views ####################################### |
| 15 | |
| 16 | class BaseView(object): |
| 17 | """ |
| 18 | Base class for generic object creation and update view. |
| 19 | |
| 20 | Templates: ``<app_label>/<model_name>_form.html`` |
| 21 | Context: |
| 22 | form |
| 23 | the ``ModelForm`` instance for the object |
| 24 | """ |
| 25 | def __init__(self, model, post_save_redirect=None): |
| 26 | self.model = model |
| 27 | self.post_save_redirect = None |
| 28 | |
| 29 | def __call__(self, request): |
| 30 | return self.main(request, self.get_instance(request)) |
| 31 | |
| 32 | def main(self, request, instance): |
| 33 | Form = self.get_form(request) |
| 34 | if request.POST: |
| 35 | form = Form(request.POST, request.FILES, instance=instance) |
| 36 | if form.is_valid(): |
| 37 | new_object = self.save(request, form) |
| 38 | return self.on_success(request, new_object) |
| 39 | else: |
| 40 | form = Form() |
| 41 | rendered_template = self.get_rendered_template(request, instance, form) |
| 42 | return HttpResponse(rendered_template) |
| 43 | |
| 44 | def get_form(self, request): |
| 45 | """ |
| 46 | Returns a ``ModelForm`` class to be used in this view. |
| 47 | """ |
| 48 | # TODO: we should be able to construct a ModelForm without creating |
| 49 | # and passing in a temporary inner class |
| 50 | class Meta: |
| 51 | model = self.model |
| 52 | class_name = self.model.__name__ + 'Form' |
| 53 | return ModelFormMetaclass(class_name, (ModelForm,), {'Meta': Meta}) |
| 54 | |
| 55 | def get_context(self, request, instance, form=None): |
| 56 | """ |
| 57 | Returns a ``Context`` instance to be used when rendering this view. |
| 58 | """ |
| 59 | return RequestContext(request, {'form': form, 'object': instance}) |
| 60 | |
| 61 | def get_template(self, request): |
| 62 | """ |
| 63 | Returns the template to be used when rendering this view. Those who |
| 64 | wish to use a custom template loader should do so here. |
| 65 | """ |
| 66 | opts = self.model._meta |
| 67 | template_name = "%s/%s_form.html" % (opts.app_label, opts.object_name.lower()) |
| 68 | return loader.get_template(template_name) |
| 69 | |
| 70 | def get_rendered_template(self, request, instance, form=None): |
| 71 | """ |
| 72 | Returns a rendered template. This will be passed as the sole argument |
| 73 | to HttpResponse() |
| 74 | """ |
| 75 | template = self.get_template(request) |
| 76 | context = self.get_context(request, instance, form) |
| 77 | return template.render(context) |
| 78 | |
| 79 | def save(self, request, form): |
| 80 | """ |
| 81 | Saves the object represented by the given ``form``. This method will |
| 82 | only be called if the form is valid, and should in most cases return |
| 83 | an HttpResponseRediect. It's return value will be the return value |
| 84 | for the view on success. |
| 85 | """ |
| 86 | return form.save() |
| 87 | |
| 88 | def on_success(self, request, new_object): |
| 89 | """ |
| 90 | Returns an HttpResonse, generally an HttpResponse redirect. This will |
| 91 | be the final return value of the view and will only be called if the |
| 92 | object was saved successfuly. |
| 93 | """ |
| 94 | if request.user.is_authenticated(): |
| 95 | message = self.get_message(request, new_object) |
| 96 | request.user.message_set.create(message=message) |
| 97 | # Redirect to the new object: first by trying post_save_redirect, |
| 98 | # then by obj.get_absolute_url; fail if neither works. |
| 99 | if self.post_save_redirect: |
| 100 | return HttpResponseRedirect(post_save_redirect % new_object.__dict__) |
| 101 | elif hasattr(new_object, 'get_absolute_url'): |
| 102 | return HttpResponseRedirect(new_object.get_absolute_url()) |
| 103 | else: |
| 104 | raise ImproperlyConfigured("No URL to redirect to from generic create view.") |
| 105 | |
| 106 | class AddView(BaseView): |
| 107 | def get_instance(self, request): |
| 108 | """ |
| 109 | Returns the object instance to create. |
| 110 | """ |
| 111 | return self.model() |
| 112 | |
| 113 | def get_message(self, request, new_object): |
| 114 | return ugettext("The %(verbose_name)s was created successfully.") % {"verbose_name": self.model._meta.verbose_name} |
| 115 | |
| 116 | class ChangeView(BaseView): |
| 117 | def __init__(self, model, post_save_redirect=None, slug_field='slug'): |
| 118 | self.slug_field = slug_field |
| 119 | super(ChangeView, self).__init__(model, post_save_redirect=post_save_redirect) |
| 120 | |
| 121 | def __call__(self, request, object_id=None, slug=None): |
| 122 | return self.main(request, self.get_instance(request, object_id, slug)) |
| 123 | |
| 124 | def get_query_set(self, request): |
| 125 | """ |
| 126 | Returns a queryset to use when trying to look up the object to change. |
| 127 | """ |
| 128 | return self.model._default_manager.get_query_set() |
| 129 | |
| 130 | def get_instance(self, request, object_id=None, slug=None): |
| 131 | """ |
| 132 | Returns the object to be changed, or raises a 404 if it doesn't exist. |
| 133 | """ |
| 134 | # Look up the object to be edited |
| 135 | lookup_kwargs = {} |
| 136 | if object_id: |
| 137 | lookup_kwargs['pk'] = object_id |
| 138 | elif slug and self.slug_field: |
| 139 | lookup_kwargs['%s__exact' % slug_field] = slug |
| 140 | else: |
| 141 | raise AttributeError("Generic view must be called with either an object_id or a slug/slug_field") |
| 142 | try: |
| 143 | return self.get_query_set(request).get(**lookup_kwargs) |
| 144 | except ObjectDoesNotExist: |
| 145 | raise Http404, "No %s found for %s" % (self.model._meta.verbose_name, lookup_kwargs) |
| 146 | |
| 147 | def get_message(self, request, new_object): |
| 148 | return ugettext("The %(verbose_name)s was updated successfully.") % {"verbose_name": self.model._meta.verbose_name} |
| 149 | |
| 150 | class DeleteView(ChangeView): |
| 151 | def __init__(self, model, post_save_redirect=None, slug_field='slug'): |
| 152 | self.model = model |
| 153 | self.slug_field = slug_field |
| 154 | self.post_save_redirect = post_save_redirect |
| 155 | |
| 156 | def main(self, request, instance): |
| 157 | if request.method == 'POST': |
| 158 | self.delete(instance) |
| 159 | return self.on_success(request, instance) |
| 160 | rendered_template = self.get_rendered_template(request, instance) |
| 161 | response = HttpResponse(rendered_template) |
| 162 | populate_xheaders(request, response, self.model, instance.pk) |
| 163 | return response |
| 164 | |
| 165 | def get_context(self, request, instance, form=None): |
| 166 | """ |
| 167 | Returns a ``Context`` instance to be used when rendering this view. |
| 168 | """ |
| 169 | return RequestContext(request, {'object': instance}) |
| 170 | |
| 171 | def get_template(self, request): |
| 172 | opts = self.model._meta |
| 173 | template_name = "%s/%s_confirm_delete.html" % (opts.app_label, opts.object_name.lower()) |
| 174 | return loader.get_template(template_name) |
| 175 | |
| 176 | def delete(request, instance): |
| 177 | """ |
| 178 | Deletes the given instance. Subclasses that wish to veto deletion |
| 179 | should do so here. |
| 180 | """ |
| 181 | instance.delete() |
| 182 | |
| 183 | def on_success(self, request, new_object): |
| 184 | """ |
| 185 | Redirects to self.post_save_redirect after setting a message if the |
| 186 | user is logged in. |
| 187 | |
| 188 | This method is only called if saving the object was successful. |
| 189 | """ |
| 190 | if request.user.is_authenticated(): |
| 191 | message = self.get_message(request, new_object) |
| 192 | request.user.message_set.create(message=message) |
| 193 | return HttpResponseRedirect(self.post_save_redirect) |
| 194 | |
| 195 | def get_message(self, request, new_object): |
| 196 | return ugettext("The %(verbose_name)s was deleted.") % {"verbose_name": model._meta.verbose_name} |
| 197 | |
| 198 | |
| 199 | # Classic generic views ###################################################### |