Ticket #8167: create_message.diff

File create_message.diff, 4.0 KB (added by ColtonProvias, 16 years ago)
  • django/views/generic/create_update.py

     
    66from django.utils.translation import ugettext
    77from django.contrib.auth.views import redirect_to_login
    88from django.views.generic import GenericViewError
     9from django.template import Template, Context
    910
    1011
    1112def apply_extra_context(extra_context, context):
     
    8990    except ObjectDoesNotExist:
    9091        raise Http404("No %s found for %s"
    9192                      % (model._meta.verbose_name, lookup_kwargs))
     93                     
     94def 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))
    92105
    93106def create_object(request, model=None, template_name=None,
    94107        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."):
    96110    """
    97111    Generic object-creation function.
    98112
     
    111125        if form.is_valid():
    112126            new_object = form.save()
    113127            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)
    115129            return redirect(post_save_redirect, new_object)
    116130    else:
    117131        form = form_class()
     
    130144        slug_field='slug', template_name=None, template_loader=loader,
    131145        extra_context=None, post_save_redirect=None, login_required=False,
    132146        context_processors=None, template_object_name='object',
    133         form_class=None):
     147        form_class=None, message="The {{ model.meta.verbose_name }} was updated successfully."):
    134148    """
    135149    Generic object-update function.
    136150
     
    153167        if form.is_valid():
    154168            obj = form.save()
    155169            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)
    157171            return redirect(post_save_redirect, obj)
    158172    else:
    159173        form = form_class(instance=obj)
     
    173187def delete_object(request, model, post_delete_redirect, object_id=None,
    174188        slug=None, slug_field='slug', template_name=None,
    175189        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."):
    177192    """
    178193    Generic object-delete function.
    179194
     
    191206        return redirect_to_login(request.path)
    192207
    193208    obj = lookup_object(model, object_id, slug, slug_field)
     209   
     210    from copy import deepcopy
     211    old_object = deepcopy(obj)
    194212
    195213    if request.method == 'POST':
    196214        obj.delete()
    197215        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)
    199217        return HttpResponseRedirect(post_delete_redirect)
    200218    else:
    201219        if not template_name:
Back to Top