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