2010-10-18 21:34:47 +08:00
|
|
|
from django.forms import models as model_forms
|
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
|
|
|
from django.http import HttpResponseRedirect
|
|
|
|
from django.views.generic.base import TemplateResponseMixin, View
|
2010-10-20 08:21:47 +08:00
|
|
|
from django.views.generic.detail import (SingleObjectMixin,
|
|
|
|
SingleObjectTemplateResponseMixin, BaseDetailView)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
class FormMixin(object):
|
|
|
|
"""
|
|
|
|
A mixin that provides a way to show and handle a form in a request.
|
|
|
|
"""
|
|
|
|
|
|
|
|
initial = {}
|
|
|
|
form_class = None
|
|
|
|
success_url = None
|
|
|
|
|
|
|
|
def get_initial(self):
|
|
|
|
"""
|
|
|
|
Returns the initial data to use for forms on this view.
|
|
|
|
"""
|
|
|
|
return self.initial
|
|
|
|
|
|
|
|
def get_form_class(self):
|
|
|
|
"""
|
|
|
|
Returns the form class to use in this view
|
|
|
|
"""
|
|
|
|
return self.form_class
|
|
|
|
|
|
|
|
def get_form(self, form_class):
|
|
|
|
"""
|
|
|
|
Returns an instance of the form to be used in this view.
|
|
|
|
"""
|
2010-12-10 11:51:30 +08:00
|
|
|
return form_class(**self.get_form_kwargs())
|
|
|
|
|
|
|
|
def get_form_kwargs(self):
|
|
|
|
"""
|
|
|
|
Returns the keyword arguments for instanciating the form.
|
|
|
|
"""
|
|
|
|
kwargs = {'initial': self.get_initial()}
|
2010-10-18 21:34:47 +08:00
|
|
|
if self.request.method in ('POST', 'PUT'):
|
2010-12-10 11:51:30 +08:00
|
|
|
kwargs.update({
|
|
|
|
'data': self.request.POST,
|
|
|
|
'files': self.request.FILES,
|
|
|
|
})
|
|
|
|
return kwargs
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
return kwargs
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
if self.success_url:
|
|
|
|
url = self.success_url
|
|
|
|
else:
|
|
|
|
raise ImproperlyConfigured(
|
2010-10-19 15:22:38 +08:00
|
|
|
"No URL to redirect to. Provide a success_url.")
|
2010-10-18 21:34:47 +08:00
|
|
|
return url
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
return HttpResponseRedirect(self.get_success_url())
|
|
|
|
|
|
|
|
def form_invalid(self, form):
|
|
|
|
return self.render_to_response(self.get_context_data(form=form))
|
|
|
|
|
|
|
|
|
|
|
|
class ModelFormMixin(FormMixin, SingleObjectMixin):
|
|
|
|
"""
|
|
|
|
A mixin that provides a way to show and handle a modelform in a request.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def get_form_class(self):
|
|
|
|
"""
|
|
|
|
Returns the form class to use in this view
|
|
|
|
"""
|
|
|
|
if self.form_class:
|
|
|
|
return self.form_class
|
|
|
|
else:
|
2011-02-15 16:12:29 +08:00
|
|
|
if self.model is not None:
|
|
|
|
# If a model has been explicitly provided, use it
|
|
|
|
model = self.model
|
|
|
|
elif hasattr(self, 'object') and self.object is not None:
|
|
|
|
# If this view is operating on a single object, use
|
|
|
|
# the class of that object
|
|
|
|
model = self.object.__class__
|
|
|
|
else:
|
|
|
|
# Try to get a queryset and extract the model class
|
|
|
|
# from that
|
|
|
|
model = self.get_queryset().model
|
2010-10-18 21:34:47 +08:00
|
|
|
return model_forms.modelform_factory(model)
|
|
|
|
|
2010-12-10 11:51:30 +08:00
|
|
|
def get_form_kwargs(self):
|
2010-10-18 21:34:47 +08:00
|
|
|
"""
|
2010-12-10 11:51:30 +08:00
|
|
|
Returns the keyword arguments for instanciating the form.
|
2010-10-18 21:34:47 +08:00
|
|
|
"""
|
2010-12-10 11:51:30 +08:00
|
|
|
kwargs = super(ModelFormMixin, self).get_form_kwargs()
|
|
|
|
kwargs.update({'instance': self.object})
|
|
|
|
return kwargs
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
if self.success_url:
|
2010-12-04 19:20:30 +08:00
|
|
|
url = self.success_url % self.object.__dict__
|
2010-10-18 21:34:47 +08:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
url = self.object.get_absolute_url()
|
|
|
|
except AttributeError:
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
"No URL to redirect to. Either provide a url or define"
|
|
|
|
" a get_absolute_url method on the Model.")
|
|
|
|
return url
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
self.object = form.save()
|
|
|
|
return super(ModelFormMixin, self).form_valid(form)
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = kwargs
|
|
|
|
if self.object:
|
|
|
|
context['object'] = self.object
|
|
|
|
context_object_name = self.get_context_object_name(self.object)
|
|
|
|
if context_object_name:
|
|
|
|
context[context_object_name] = self.object
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
|
class ProcessFormView(View):
|
|
|
|
"""
|
|
|
|
A mixin that processes a form on POST.
|
|
|
|
"""
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
form_class = self.get_form_class()
|
|
|
|
form = self.get_form(form_class)
|
|
|
|
return self.render_to_response(self.get_context_data(form=form))
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
form_class = self.get_form_class()
|
|
|
|
form = self.get_form(form_class)
|
|
|
|
if form.is_valid():
|
|
|
|
return self.form_valid(form)
|
|
|
|
else:
|
|
|
|
return self.form_invalid(form)
|
|
|
|
|
|
|
|
# PUT is a valid HTTP verb for creating (with a known URL) or editing an
|
|
|
|
# object, note that browsers only support POST for now.
|
2010-10-28 10:59:04 +08:00
|
|
|
def put(self, *args, **kwargs):
|
|
|
|
return self.post(*args, **kwargs)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
class BaseFormView(FormMixin, ProcessFormView):
|
|
|
|
"""
|
|
|
|
A base view for displaying a form
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class FormView(TemplateResponseMixin, BaseFormView):
|
|
|
|
"""
|
|
|
|
A view for displaying a form, and rendering a template response.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class BaseCreateView(ModelFormMixin, ProcessFormView):
|
|
|
|
"""
|
|
|
|
Base view for creating an new object instance.
|
|
|
|
|
|
|
|
Using this base class requires subclassing to provide a response mixin.
|
|
|
|
"""
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
self.object = None
|
|
|
|
return super(BaseCreateView, self).get(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
self.object = None
|
|
|
|
return super(BaseCreateView, self).post(request, *args, **kwargs)
|
|
|
|
|
2011-01-27 11:14:20 +08:00
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
class CreateView(SingleObjectTemplateResponseMixin, BaseCreateView):
|
|
|
|
"""
|
|
|
|
View for creating an new object instance,
|
|
|
|
with a response rendered by template.
|
|
|
|
"""
|
|
|
|
template_name_suffix = '_form'
|
|
|
|
|
|
|
|
|
|
|
|
class BaseUpdateView(ModelFormMixin, ProcessFormView):
|
|
|
|
"""
|
|
|
|
Base view for updating an existing object.
|
|
|
|
|
|
|
|
Using this base class requires subclassing to provide a response mixin.
|
|
|
|
"""
|
|
|
|
def get(self, request, *args, **kwargs):
|
2010-10-20 08:21:47 +08:00
|
|
|
self.object = self.get_object()
|
2010-10-18 21:34:47 +08:00
|
|
|
return super(BaseUpdateView, self).get(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
2010-10-20 08:21:47 +08:00
|
|
|
self.object = self.get_object()
|
2010-10-18 21:34:47 +08:00
|
|
|
return super(BaseUpdateView, self).post(request, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
class UpdateView(SingleObjectTemplateResponseMixin, BaseUpdateView):
|
|
|
|
"""
|
|
|
|
View for updating an object,
|
|
|
|
with a response rendered by template..
|
|
|
|
"""
|
|
|
|
template_name_suffix = '_form'
|
|
|
|
|
|
|
|
|
|
|
|
class DeletionMixin(object):
|
|
|
|
"""
|
|
|
|
A mixin providing the ability to delete objects
|
|
|
|
"""
|
|
|
|
success_url = None
|
|
|
|
|
|
|
|
def delete(self, request, *args, **kwargs):
|
2010-10-20 08:21:47 +08:00
|
|
|
self.object = self.get_object()
|
2010-10-18 21:34:47 +08:00
|
|
|
self.object.delete()
|
|
|
|
return HttpResponseRedirect(self.get_success_url())
|
|
|
|
|
|
|
|
# Add support for browsers which only accept GET and POST for now.
|
2010-10-28 10:59:04 +08:00
|
|
|
def post(self, *args, **kwargs):
|
|
|
|
return self.delete(*args, **kwargs)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
if self.success_url:
|
|
|
|
return self.success_url
|
|
|
|
else:
|
|
|
|
raise ImproperlyConfigured(
|
2010-10-19 15:22:38 +08:00
|
|
|
"No URL to redirect to. Provide a success_url.")
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2011-01-27 11:14:20 +08:00
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
class BaseDeleteView(DeletionMixin, BaseDetailView):
|
|
|
|
"""
|
|
|
|
Base view for deleting an object.
|
|
|
|
|
|
|
|
Using this base class requires subclassing to provide a response mixin.
|
|
|
|
"""
|
|
|
|
|
2011-01-27 11:14:20 +08:00
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
class DeleteView(SingleObjectTemplateResponseMixin, BaseDeleteView):
|
|
|
|
"""
|
|
|
|
View for deleting an object retrieved with `self.get_object()`,
|
|
|
|
with a response rendered by template.
|
|
|
|
"""
|
|
|
|
template_name_suffix = '_confirm_delete'
|