Ticket #8167: create_message.diff
File create_message.diff, 4.0 KB (added by , 16 years ago) |
---|
-
django/views/generic/create_update.py
6 6 from django.utils.translation import ugettext 7 7 from django.contrib.auth.views import redirect_to_login 8 8 from django.views.generic import GenericViewError 9 from django.template import Template, Context 9 10 10 11 11 12 def apply_extra_context(extra_context, context): … … 89 90 except ObjectDoesNotExist: 90 91 raise Http404("No %s found for %s" 91 92 % (model._meta.verbose_name, lookup_kwargs)) 93 94 def create_message(message_set, message, model, object): 95 """ 96 Creates a message via the django template system. If the message is set to none, 97 no message will be created. Also reassigns ``model._meta`` to `` model.meta`` to 98 make it accessible by the template. 99 """ 100 if message is not None: 101 model.meta = model._meta 102 template = Template(message) 103 context = Context({"model": model, "object": object}) 104 message_set.create(message=template.render(context)) 92 105 93 106 def create_object(request, model=None, template_name=None, 94 107 template_loader=loader, extra_context=None, post_save_redirect=None, 95 login_required=False, context_processors=None, form_class=None): 108 login_required=False, context_processors=None, form_class=None, 109 message="The {{ model.meta.verbose_name }} was created successfully."): 96 110 """ 97 111 Generic object-creation function. 98 112 … … 111 125 if form.is_valid(): 112 126 new_object = form.save() 113 127 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})128 create_message(request.user.message_set, message, model, new_object) 115 129 return redirect(post_save_redirect, new_object) 116 130 else: 117 131 form = form_class() … … 130 144 slug_field='slug', template_name=None, template_loader=loader, 131 145 extra_context=None, post_save_redirect=None, login_required=False, 132 146 context_processors=None, template_object_name='object', 133 form_class=None ):147 form_class=None, message="The {{ model.meta.verbose_name }} was updated successfully."): 134 148 """ 135 149 Generic object-update function. 136 150 … … 153 167 if form.is_valid(): 154 168 obj = form.save() 155 169 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})170 create_message(request.user.message_set, message, model, obj) 157 171 return redirect(post_save_redirect, obj) 158 172 else: 159 173 form = form_class(instance=obj) … … 173 187 def delete_object(request, model, post_delete_redirect, object_id=None, 174 188 slug=None, slug_field='slug', template_name=None, 175 189 template_loader=loader, extra_context=None, login_required=False, 176 context_processors=None, template_object_name='object'): 190 context_processors=None, template_object_name='object', 191 message="The {{ model.meta.verbose_name }} was deleted."): 177 192 """ 178 193 Generic object-delete function. 179 194 … … 191 206 return redirect_to_login(request.path) 192 207 193 208 obj = lookup_object(model, object_id, slug, slug_field) 209 210 from copy import deepcopy 211 old_object = deepcopy(obj) 194 212 195 213 if request.method == 'POST': 196 214 obj.delete() 197 215 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})216 create_message(request.user.message_set, message, model, old_object) 199 217 return HttpResponseRedirect(post_delete_redirect) 200 218 else: 201 219 if not template_name: