2014-10-15 02:56:39 +08:00
|
|
|
import inspect
|
2015-01-12 19:00:27 +08:00
|
|
|
import re
|
2014-10-15 02:56:39 +08:00
|
|
|
import warnings
|
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2014-02-27 05:48:20 +08:00
|
|
|
from django.forms import models as model_forms
|
2010-10-18 21:34:47 +08:00
|
|
|
from django.http import HttpResponseRedirect
|
2014-10-15 02:56:39 +08:00
|
|
|
from django.utils import six
|
|
|
|
from django.utils.deprecation import RemovedInDjango20Warning
|
2012-12-04 20:18:57 +08:00
|
|
|
from django.utils.encoding import force_text
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.views.generic.base import ContextMixin, TemplateResponseMixin, View
|
|
|
|
from django.views.generic.detail import (
|
|
|
|
BaseDetailView, SingleObjectMixin, SingleObjectTemplateResponseMixin,
|
|
|
|
)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2015-01-26 18:23:48 +08:00
|
|
|
PERCENT_PLACEHOLDER_REGEX = re.compile(r'%\([^\)]+\)') # RemovedInDjango20Warning
|
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2014-10-15 02:56:39 +08:00
|
|
|
class FormMixinBase(type):
|
|
|
|
def __new__(cls, name, bases, attrs):
|
|
|
|
get_form = attrs.get('get_form')
|
|
|
|
if get_form and inspect.isfunction(get_form):
|
|
|
|
try:
|
|
|
|
inspect.getcallargs(get_form, None)
|
|
|
|
except TypeError:
|
|
|
|
warnings.warn(
|
|
|
|
"`%s.%s.get_form` method must define a default value for "
|
|
|
|
"its `form_class` argument." % (attrs['__module__'], name),
|
|
|
|
RemovedInDjango20Warning, stacklevel=2
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_form_with_form_class(self, form_class=None):
|
|
|
|
if form_class is None:
|
|
|
|
form_class = self.get_form_class()
|
|
|
|
return get_form(self, form_class=form_class)
|
|
|
|
attrs['get_form'] = get_form_with_form_class
|
|
|
|
return super(FormMixinBase, cls).__new__(cls, name, bases, attrs)
|
|
|
|
|
|
|
|
|
|
|
|
class FormMixin(six.with_metaclass(FormMixinBase, ContextMixin)):
|
2010-10-18 21:34:47 +08:00
|
|
|
"""
|
|
|
|
A mixin that provides a way to show and handle a form in a request.
|
|
|
|
"""
|
|
|
|
|
|
|
|
initial = {}
|
|
|
|
form_class = None
|
|
|
|
success_url = None
|
2013-06-22 19:12:43 +08:00
|
|
|
prefix = None
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def get_initial(self):
|
|
|
|
"""
|
|
|
|
Returns the initial data to use for forms on this view.
|
|
|
|
"""
|
2012-03-18 06:31:03 +08:00
|
|
|
return self.initial.copy()
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2013-06-22 19:12:43 +08:00
|
|
|
def get_prefix(self):
|
|
|
|
"""
|
|
|
|
Returns the prefix to use for forms on this view
|
|
|
|
"""
|
|
|
|
return self.prefix
|
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
def get_form_class(self):
|
|
|
|
"""
|
|
|
|
Returns the form class to use in this view
|
|
|
|
"""
|
|
|
|
return self.form_class
|
|
|
|
|
2014-10-15 02:56:39 +08:00
|
|
|
def get_form(self, form_class=None):
|
2010-10-18 21:34:47 +08:00
|
|
|
"""
|
|
|
|
Returns an instance of the form to be used in this view.
|
|
|
|
"""
|
2014-10-15 02:56:39 +08:00
|
|
|
if form_class is None:
|
|
|
|
form_class = self.get_form_class()
|
2010-12-10 11:51:30 +08:00
|
|
|
return form_class(**self.get_form_kwargs())
|
|
|
|
|
|
|
|
def get_form_kwargs(self):
|
|
|
|
"""
|
2012-04-29 01:18:16 +08:00
|
|
|
Returns the keyword arguments for instantiating the form.
|
2010-12-10 11:51:30 +08:00
|
|
|
"""
|
2013-06-22 19:12:43 +08:00
|
|
|
kwargs = {
|
|
|
|
'initial': self.get_initial(),
|
|
|
|
'prefix': self.get_prefix(),
|
|
|
|
}
|
|
|
|
|
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_success_url(self):
|
2012-06-11 16:34:00 +08:00
|
|
|
"""
|
|
|
|
Returns the supplied success URL.
|
|
|
|
"""
|
2010-10-18 21:34:47 +08:00
|
|
|
if self.success_url:
|
2012-12-04 20:18:57 +08:00
|
|
|
# Forcing possible reverse_lazy evaluation
|
|
|
|
url = force_text(self.success_url)
|
2010-10-18 21:34:47 +08:00
|
|
|
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):
|
2012-06-11 16:34:00 +08:00
|
|
|
"""
|
|
|
|
If the form is valid, redirect to the supplied URL.
|
|
|
|
"""
|
2010-10-18 21:34:47 +08:00
|
|
|
return HttpResponseRedirect(self.get_success_url())
|
|
|
|
|
|
|
|
def form_invalid(self, form):
|
2012-06-11 16:34:00 +08:00
|
|
|
"""
|
|
|
|
If the form is invalid, re-render the context data with the
|
|
|
|
data-filled form and errors.
|
|
|
|
"""
|
2010-10-18 21:34:47 +08:00
|
|
|
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.
|
|
|
|
"""
|
2013-06-24 05:43:09 +08:00
|
|
|
fields = None
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def get_form_class(self):
|
|
|
|
"""
|
2012-06-11 16:34:00 +08:00
|
|
|
Returns the form class to use in this view.
|
2010-10-18 21:34:47 +08:00
|
|
|
"""
|
2014-11-15 19:17:55 +08:00
|
|
|
if self.fields is not None and self.form_class:
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
"Specifying both 'fields' and 'form_class' is not permitted."
|
|
|
|
)
|
2010-10-18 21:34:47 +08:00
|
|
|
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
|
2013-02-22 05:56:55 +08:00
|
|
|
|
2013-06-24 05:43:09 +08:00
|
|
|
if self.fields is None:
|
2014-03-22 08:44:34 +08:00
|
|
|
raise ImproperlyConfigured(
|
|
|
|
"Using ModelFormMixin (base class of %s) without "
|
|
|
|
"the 'fields' attribute is prohibited." % self.__class__.__name__
|
|
|
|
)
|
2013-02-22 05:56:55 +08:00
|
|
|
|
2013-06-24 05:43:09 +08:00
|
|
|
return model_forms.modelform_factory(model, fields=self.fields)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2010-12-10 11:51:30 +08:00
|
|
|
def get_form_kwargs(self):
|
2010-10-18 21:34:47 +08:00
|
|
|
"""
|
2012-04-29 01:18:16 +08:00
|
|
|
Returns the keyword arguments for instantiating 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()
|
2014-02-09 18:53:23 +08:00
|
|
|
if hasattr(self, 'object'):
|
|
|
|
kwargs.update({'instance': self.object})
|
2010-12-10 11:51:30 +08:00
|
|
|
return kwargs
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def get_success_url(self):
|
2012-06-11 16:34:00 +08:00
|
|
|
"""
|
|
|
|
Returns the supplied URL.
|
|
|
|
"""
|
2010-10-18 21:34:47 +08:00
|
|
|
if self.success_url:
|
2015-01-26 18:09:50 +08:00
|
|
|
# force_text can be removed with deprecation warning
|
|
|
|
self.success_url = force_text(self.success_url)
|
2015-01-26 18:23:48 +08:00
|
|
|
if PERCENT_PLACEHOLDER_REGEX.search(self.success_url):
|
2015-01-12 19:00:27 +08:00
|
|
|
warnings.warn(
|
|
|
|
"%()s placeholder style in success_url is deprecated. "
|
|
|
|
"Please replace them by the {} Python format syntax.",
|
|
|
|
RemovedInDjango20Warning, stacklevel=2
|
|
|
|
)
|
|
|
|
url = self.success_url % self.object.__dict__
|
|
|
|
else:
|
|
|
|
url = self.success_url.format(**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):
|
2012-06-11 16:34:00 +08:00
|
|
|
"""
|
|
|
|
If the form is valid, save the associated model.
|
|
|
|
"""
|
2010-10-18 21:34:47 +08:00
|
|
|
self.object = form.save()
|
|
|
|
return super(ModelFormMixin, self).form_valid(form)
|
|
|
|
|
|
|
|
|
|
|
|
class ProcessFormView(View):
|
|
|
|
"""
|
2012-06-11 16:34:00 +08:00
|
|
|
A mixin that renders a form on GET and processes it on POST.
|
2010-10-18 21:34:47 +08:00
|
|
|
"""
|
|
|
|
def get(self, request, *args, **kwargs):
|
2012-06-11 16:34:00 +08:00
|
|
|
"""
|
|
|
|
Handles GET requests and instantiates a blank version of the form.
|
|
|
|
"""
|
2014-10-15 02:56:39 +08:00
|
|
|
form = self.get_form()
|
2010-10-18 21:34:47 +08:00
|
|
|
return self.render_to_response(self.get_context_data(form=form))
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
2012-06-11 16:34:00 +08:00
|
|
|
"""
|
|
|
|
Handles POST requests, instantiating a form instance with the passed
|
|
|
|
POST variables and then checked for validity.
|
|
|
|
"""
|
2014-10-15 02:56:39 +08:00
|
|
|
form = self.get_form()
|
2010-10-18 21:34:47 +08:00
|
|
|
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):
|
|
|
|
"""
|
2012-06-11 16:34:00 +08:00
|
|
|
View for creating a new object instance,
|
2010-10-18 21:34:47 +08:00
|
|
|
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,
|
2012-06-11 16:34:00 +08:00
|
|
|
with a response rendered by template.
|
2010-10-18 21:34:47 +08:00
|
|
|
"""
|
|
|
|
template_name_suffix = '_form'
|
|
|
|
|
|
|
|
|
|
|
|
class DeletionMixin(object):
|
|
|
|
"""
|
|
|
|
A mixin providing the ability to delete objects
|
|
|
|
"""
|
|
|
|
success_url = None
|
|
|
|
|
|
|
|
def delete(self, request, *args, **kwargs):
|
2012-06-11 16:34:00 +08:00
|
|
|
"""
|
|
|
|
Calls the delete() method on the fetched object and then
|
|
|
|
redirects to the success URL.
|
|
|
|
"""
|
2010-10-20 08:21:47 +08:00
|
|
|
self.object = self.get_object()
|
2013-02-11 15:39:14 +08:00
|
|
|
success_url = self.get_success_url()
|
2010-10-18 21:34:47 +08:00
|
|
|
self.object.delete()
|
2013-02-11 15:39:14 +08:00
|
|
|
return HttpResponseRedirect(success_url)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
# Add support for browsers which only accept GET and POST for now.
|
2013-06-11 18:40:06 +08:00
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
return self.delete(request, *args, **kwargs)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
if self.success_url:
|
2015-01-26 18:09:50 +08:00
|
|
|
# force_text can be removed with deprecation warning
|
|
|
|
self.success_url = force_text(self.success_url)
|
2015-01-26 18:23:48 +08:00
|
|
|
if PERCENT_PLACEHOLDER_REGEX.search(self.success_url):
|
2015-01-12 19:00:27 +08:00
|
|
|
warnings.warn(
|
|
|
|
"%()s placeholder style in success_url is deprecated. "
|
|
|
|
"Please replace them by the {} Python format syntax.",
|
|
|
|
RemovedInDjango20Warning, stacklevel=2
|
|
|
|
)
|
|
|
|
return self.success_url % self.object.__dict__
|
|
|
|
else:
|
|
|
|
return self.success_url.format(**self.object.__dict__)
|
2010-10-18 21:34:47 +08:00
|
|
|
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'
|