2008-07-19 09:22:26 +08:00
|
|
|
from django.forms.models import ModelFormMetaclass, ModelForm
|
2008-07-19 03:45:00 +08:00
|
|
|
from django.template import RequestContext, loader
|
2006-07-22 14:13:49 +08:00
|
|
|
from django.http import Http404, HttpResponse, HttpResponseRedirect
|
2008-07-19 03:45:00 +08:00
|
|
|
from django.core.xheaders import populate_xheaders
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.core.exceptions import ObjectDoesNotExist, ImproperlyConfigured
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
from django.utils.translation import ugettext
|
2008-07-19 03:45:00 +08:00
|
|
|
from django.contrib.auth.views import redirect_to_login
|
|
|
|
from django.views.generic import GenericViewError
|
2009-12-10 00:57:23 +08:00
|
|
|
from django.contrib import messages
|
2008-07-19 03:45:00 +08:00
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
import warnings
|
|
|
|
warnings.warn(
|
|
|
|
'Function-based generic views have been deprecated; use class-based views instead.',
|
2011-04-02 16:44:04 +08:00
|
|
|
DeprecationWarning
|
2010-10-18 21:34:47 +08:00
|
|
|
)
|
|
|
|
|
2008-07-19 03:45:00 +08:00
|
|
|
|
|
|
|
def apply_extra_context(extra_context, context):
|
|
|
|
"""
|
|
|
|
Adds items from extra_context dict to context. If a value in extra_context
|
|
|
|
is callable, then it is called and the result is added to context.
|
|
|
|
"""
|
|
|
|
for key, value in extra_context.iteritems():
|
|
|
|
if callable(value):
|
|
|
|
context[key] = value()
|
|
|
|
else:
|
|
|
|
context[key] = value
|
|
|
|
|
|
|
|
def get_model_and_form_class(model, form_class):
|
|
|
|
"""
|
|
|
|
Returns a model and form class based on the model and form_class
|
|
|
|
parameters that were passed to the generic view.
|
|
|
|
|
|
|
|
If ``form_class`` is given then its associated model will be returned along
|
|
|
|
with ``form_class`` itself. Otherwise, if ``model`` is given, ``model``
|
|
|
|
itself will be returned along with a ``ModelForm`` class created from
|
|
|
|
``model``.
|
|
|
|
"""
|
|
|
|
if form_class:
|
|
|
|
return form_class._meta.model, form_class
|
|
|
|
if model:
|
|
|
|
# The inner Meta class fails if model = model is used for some reason.
|
|
|
|
tmp_model = model
|
|
|
|
# TODO: we should be able to construct a ModelForm without creating
|
|
|
|
# and passing in a temporary inner class.
|
|
|
|
class Meta:
|
|
|
|
model = tmp_model
|
|
|
|
class_name = model.__name__ + 'Form'
|
|
|
|
form_class = ModelFormMetaclass(class_name, (ModelForm,), {'Meta': Meta})
|
|
|
|
return model, form_class
|
|
|
|
raise GenericViewError("Generic view must be called with either a model or"
|
|
|
|
" form_class argument.")
|
|
|
|
|
|
|
|
def redirect(post_save_redirect, obj):
|
|
|
|
"""
|
|
|
|
Returns a HttpResponseRedirect to ``post_save_redirect``.
|
|
|
|
|
|
|
|
``post_save_redirect`` should be a string, and can contain named string-
|
|
|
|
substitution place holders of ``obj`` field names.
|
|
|
|
|
|
|
|
If ``post_save_redirect`` is None, then redirect to ``obj``'s URL returned
|
|
|
|
by ``get_absolute_url()``. If ``obj`` has no ``get_absolute_url`` method,
|
|
|
|
then raise ImproperlyConfigured.
|
|
|
|
|
2008-07-19 07:52:18 +08:00
|
|
|
This function is meant to handle the post_save_redirect parameter to the
|
2008-07-19 03:45:00 +08:00
|
|
|
``create_object`` and ``update_object`` views.
|
|
|
|
"""
|
|
|
|
if post_save_redirect:
|
|
|
|
return HttpResponseRedirect(post_save_redirect % obj.__dict__)
|
|
|
|
elif hasattr(obj, 'get_absolute_url'):
|
|
|
|
return HttpResponseRedirect(obj.get_absolute_url())
|
|
|
|
else:
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
"No URL to redirect to. Either pass a post_save_redirect"
|
|
|
|
" parameter to the generic view or define a get_absolute_url"
|
|
|
|
" method on the Model.")
|
|
|
|
|
|
|
|
def lookup_object(model, object_id, slug, slug_field):
|
|
|
|
"""
|
|
|
|
Return the ``model`` object with the passed ``object_id``. If
|
2009-05-18 00:45:28 +08:00
|
|
|
``object_id`` is None, then return the object whose ``slug_field``
|
2008-07-19 03:45:00 +08:00
|
|
|
equals the passed ``slug``. If ``slug`` and ``slug_field`` are not passed,
|
|
|
|
then raise Http404 exception.
|
|
|
|
"""
|
|
|
|
lookup_kwargs = {}
|
|
|
|
if object_id:
|
|
|
|
lookup_kwargs['%s__exact' % model._meta.pk.name] = object_id
|
|
|
|
elif slug and slug_field:
|
|
|
|
lookup_kwargs['%s__exact' % slug_field] = slug
|
|
|
|
else:
|
|
|
|
raise GenericViewError(
|
|
|
|
"Generic view must be called with either an object_id or a"
|
|
|
|
" slug/slug_field.")
|
|
|
|
try:
|
|
|
|
return model.objects.get(**lookup_kwargs)
|
|
|
|
except ObjectDoesNotExist:
|
|
|
|
raise Http404("No %s found for %s"
|
|
|
|
% (model._meta.verbose_name, lookup_kwargs))
|
2005-07-25 06:21:09 +08:00
|
|
|
|
2008-07-19 03:45:00 +08:00
|
|
|
def create_object(request, model=None, template_name=None,
|
2006-06-03 21:37:34 +08:00
|
|
|
template_loader=loader, extra_context=None, post_save_redirect=None,
|
Fixed #7830 -- Removed all of the remaining, deprecated, non-oldforms features:
* Support for representing files as strings was removed. Use `django.core.files.base.ContentFile` instead.
* Support for representing uploaded files as dictionaries was removed. Use `django.core.files.uploadedfile.SimpleUploadedFile` instead.
* The `filename`, `file_name`, `file_size`, and `chuck` properties of `UploadedFile` were removed. Use the `name`, `name`, `size`, and `chunks` properties instead, respectively.
* The `get_FIELD_filename`, `get_FIELD_url`, `get_FIELD_size`, and `save_FIELD_file` methods for Models with `FileField` fields were removed. Instead, use the `path`, `url`, and `size` attributes and `save` method on the field itself, respectively.
* The `get_FIELD_width` and `get_FIELD_height` methods for Models with `ImageField` fields were removed. Use the `width` and `height` attributes on the field itself instead.
* The dispatcher `connect`, `disconnect`, `send`, and `sendExact` functions were removed. Use the signal object's own `connect`, `disconnect`, `send`, and `send` methods instead, respectively.
* The `form_for_model` and `form_for_instance` functions were removed. Use a `ModelForm` subclass instead.
* Support for importing `django.newforms` was removed. Use `django.forms` instead.
* Support for importing `django.utils.images` was removed. Use `django.core.files.images` instead.
* Support for the `follow` argument in the `create_object` and `update_object` generic views was removed. Use the `django.forms` package and the new `form_class` argument instead.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8291 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-08-11 05:10:47 +08:00
|
|
|
login_required=False, context_processors=None, form_class=None):
|
2005-07-25 06:21:09 +08:00
|
|
|
"""
|
|
|
|
Generic object-creation function.
|
2005-12-01 14:36:47 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
Templates: ``<app_label>/<model_name>_form.html``
|
2005-07-25 06:21:09 +08:00
|
|
|
Context:
|
|
|
|
form
|
2008-07-19 03:45:00 +08:00
|
|
|
the form for the object
|
2005-07-25 06:21:09 +08:00
|
|
|
"""
|
2006-06-03 21:37:34 +08:00
|
|
|
if extra_context is None: extra_context = {}
|
2006-07-19 10:09:26 +08:00
|
|
|
if login_required and not request.user.is_authenticated():
|
2005-08-12 22:19:34 +08:00
|
|
|
return redirect_to_login(request.path)
|
2005-12-01 14:36:47 +08:00
|
|
|
|
2008-07-19 03:45:00 +08:00
|
|
|
model, form_class = get_model_and_form_class(model, form_class)
|
|
|
|
if request.method == 'POST':
|
|
|
|
form = form_class(request.POST, request.FILES)
|
|
|
|
if form.is_valid():
|
|
|
|
new_object = form.save()
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2009-12-10 00:57:23 +08:00
|
|
|
msg = ugettext("The %(verbose_name)s was created successfully.") %\
|
|
|
|
{"verbose_name": model._meta.verbose_name}
|
|
|
|
messages.success(request, msg, fail_silently=True)
|
2008-07-19 03:45:00 +08:00
|
|
|
return redirect(post_save_redirect, new_object)
|
2005-07-25 06:21:09 +08:00
|
|
|
else:
|
2008-07-19 03:45:00 +08:00
|
|
|
form = form_class()
|
2005-12-01 14:36:47 +08:00
|
|
|
|
2008-07-19 03:45:00 +08:00
|
|
|
# Create the template, context, response
|
2005-07-25 06:21:09 +08:00
|
|
|
if not template_name:
|
2006-05-02 09:31:56 +08:00
|
|
|
template_name = "%s/%s_form.html" % (model._meta.app_label, model._meta.object_name.lower())
|
2005-07-25 06:21:09 +08:00
|
|
|
t = template_loader.get_template(template_name)
|
2006-05-02 09:31:56 +08:00
|
|
|
c = RequestContext(request, {
|
2005-12-24 12:39:59 +08:00
|
|
|
'form': form,
|
|
|
|
}, context_processors)
|
2008-07-19 03:45:00 +08:00
|
|
|
apply_extra_context(extra_context, c)
|
2007-06-22 15:15:04 +08:00
|
|
|
return HttpResponse(t.render(c))
|
2005-07-25 06:21:09 +08:00
|
|
|
|
2008-07-19 03:45:00 +08:00
|
|
|
def update_object(request, model=None, object_id=None, slug=None,
|
2007-08-12 20:59:41 +08:00
|
|
|
slug_field='slug', template_name=None, template_loader=loader,
|
Fixed #7830 -- Removed all of the remaining, deprecated, non-oldforms features:
* Support for representing files as strings was removed. Use `django.core.files.base.ContentFile` instead.
* Support for representing uploaded files as dictionaries was removed. Use `django.core.files.uploadedfile.SimpleUploadedFile` instead.
* The `filename`, `file_name`, `file_size`, and `chuck` properties of `UploadedFile` were removed. Use the `name`, `name`, `size`, and `chunks` properties instead, respectively.
* The `get_FIELD_filename`, `get_FIELD_url`, `get_FIELD_size`, and `save_FIELD_file` methods for Models with `FileField` fields were removed. Instead, use the `path`, `url`, and `size` attributes and `save` method on the field itself, respectively.
* The `get_FIELD_width` and `get_FIELD_height` methods for Models with `ImageField` fields were removed. Use the `width` and `height` attributes on the field itself instead.
* The dispatcher `connect`, `disconnect`, `send`, and `sendExact` functions were removed. Use the signal object's own `connect`, `disconnect`, `send`, and `send` methods instead, respectively.
* The `form_for_model` and `form_for_instance` functions were removed. Use a `ModelForm` subclass instead.
* Support for importing `django.newforms` was removed. Use `django.forms` instead.
* Support for importing `django.utils.images` was removed. Use `django.core.files.images` instead.
* Support for the `follow` argument in the `create_object` and `update_object` generic views was removed. Use the `django.forms` package and the new `form_class` argument instead.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8291 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-08-11 05:10:47 +08:00
|
|
|
extra_context=None, post_save_redirect=None, login_required=False,
|
|
|
|
context_processors=None, template_object_name='object',
|
|
|
|
form_class=None):
|
2005-07-25 06:21:09 +08:00
|
|
|
"""
|
|
|
|
Generic object-update function.
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
Templates: ``<app_label>/<model_name>_form.html``
|
2005-07-25 06:21:09 +08:00
|
|
|
Context:
|
|
|
|
form
|
2008-07-19 03:45:00 +08:00
|
|
|
the form for the object
|
2005-07-25 06:21:09 +08:00
|
|
|
object
|
|
|
|
the original object being edited
|
|
|
|
"""
|
2006-06-03 21:37:34 +08:00
|
|
|
if extra_context is None: extra_context = {}
|
2006-07-19 10:09:26 +08:00
|
|
|
if login_required and not request.user.is_authenticated():
|
2005-08-12 22:19:34 +08:00
|
|
|
return redirect_to_login(request.path)
|
2005-07-25 06:21:09 +08:00
|
|
|
|
2008-07-19 03:45:00 +08:00
|
|
|
model, form_class = get_model_and_form_class(model, form_class)
|
|
|
|
obj = lookup_object(model, object_id, slug, slug_field)
|
2005-12-01 14:36:47 +08:00
|
|
|
|
2008-07-19 03:45:00 +08:00
|
|
|
if request.method == 'POST':
|
|
|
|
form = form_class(request.POST, request.FILES, instance=obj)
|
|
|
|
if form.is_valid():
|
|
|
|
obj = form.save()
|
2009-12-10 00:57:23 +08:00
|
|
|
msg = ugettext("The %(verbose_name)s was updated successfully.") %\
|
|
|
|
{"verbose_name": model._meta.verbose_name}
|
|
|
|
messages.success(request, msg, fail_silently=True)
|
2008-07-19 03:45:00 +08:00
|
|
|
return redirect(post_save_redirect, obj)
|
2005-07-25 06:21:09 +08:00
|
|
|
else:
|
2008-07-19 03:45:00 +08:00
|
|
|
form = form_class(instance=obj)
|
2005-12-01 14:36:47 +08:00
|
|
|
|
2005-07-25 06:21:09 +08:00
|
|
|
if not template_name:
|
2006-05-02 09:31:56 +08:00
|
|
|
template_name = "%s/%s_form.html" % (model._meta.app_label, model._meta.object_name.lower())
|
2005-07-25 06:21:09 +08:00
|
|
|
t = template_loader.get_template(template_name)
|
2006-05-02 09:31:56 +08:00
|
|
|
c = RequestContext(request, {
|
2005-12-24 12:39:59 +08:00
|
|
|
'form': form,
|
2008-07-19 03:45:00 +08:00
|
|
|
template_object_name: obj,
|
2005-12-24 12:39:59 +08:00
|
|
|
}, context_processors)
|
2008-07-19 03:45:00 +08:00
|
|
|
apply_extra_context(extra_context, c)
|
2007-06-22 15:15:04 +08:00
|
|
|
response = HttpResponse(t.render(c))
|
2008-07-19 03:45:00 +08:00
|
|
|
populate_xheaders(request, response, model, getattr(obj, obj._meta.pk.attname))
|
2005-07-25 06:21:09 +08:00
|
|
|
return response
|
|
|
|
|
2008-07-19 03:45:00 +08:00
|
|
|
def delete_object(request, model, post_delete_redirect, object_id=None,
|
|
|
|
slug=None, slug_field='slug', template_name=None,
|
|
|
|
template_loader=loader, extra_context=None, login_required=False,
|
|
|
|
context_processors=None, template_object_name='object'):
|
2005-07-25 06:21:09 +08:00
|
|
|
"""
|
|
|
|
Generic object-delete function.
|
2005-12-01 14:36:47 +08:00
|
|
|
|
|
|
|
The given template will be used to confirm deletetion if this view is
|
2005-07-25 06:21:09 +08:00
|
|
|
fetched using GET; for safty, deletion will only be performed if this
|
|
|
|
view is POSTed.
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
Templates: ``<app_label>/<model_name>_confirm_delete.html``
|
2005-07-25 06:21:09 +08:00
|
|
|
Context:
|
|
|
|
object
|
|
|
|
the original object being deleted
|
|
|
|
"""
|
2006-06-03 21:37:34 +08:00
|
|
|
if extra_context is None: extra_context = {}
|
2006-07-19 10:09:26 +08:00
|
|
|
if login_required and not request.user.is_authenticated():
|
2005-08-12 22:19:34 +08:00
|
|
|
return redirect_to_login(request.path)
|
2005-07-25 06:21:09 +08:00
|
|
|
|
2008-07-19 03:45:00 +08:00
|
|
|
obj = lookup_object(model, object_id, slug, slug_field)
|
2005-12-01 14:36:47 +08:00
|
|
|
|
2006-06-20 12:34:13 +08:00
|
|
|
if request.method == 'POST':
|
2008-07-19 03:45:00 +08:00
|
|
|
obj.delete()
|
2009-12-10 00:57:23 +08:00
|
|
|
msg = ugettext("The %(verbose_name)s was deleted.") %\
|
|
|
|
{"verbose_name": model._meta.verbose_name}
|
|
|
|
messages.success(request, msg, fail_silently=True)
|
2005-07-25 06:21:09 +08:00
|
|
|
return HttpResponseRedirect(post_delete_redirect)
|
|
|
|
else:
|
|
|
|
if not template_name:
|
2006-05-02 09:31:56 +08:00
|
|
|
template_name = "%s/%s_confirm_delete.html" % (model._meta.app_label, model._meta.object_name.lower())
|
2005-07-25 06:21:09 +08:00
|
|
|
t = template_loader.get_template(template_name)
|
2006-05-02 09:31:56 +08:00
|
|
|
c = RequestContext(request, {
|
2008-07-19 03:45:00 +08:00
|
|
|
template_object_name: obj,
|
2005-12-24 12:39:59 +08:00
|
|
|
}, context_processors)
|
2008-07-19 03:45:00 +08:00
|
|
|
apply_extra_context(extra_context, c)
|
2007-06-22 15:15:04 +08:00
|
|
|
response = HttpResponse(t.render(c))
|
2008-07-19 03:45:00 +08:00
|
|
|
populate_xheaders(request, response, model, getattr(obj, obj._meta.pk.attname))
|
2005-07-25 06:21:09 +08:00
|
|
|
return response
|