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