PageRenderTime 49ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Lib/site-packages/django/views/generic/edit.py

https://gitlab.com/suyesh/Djangotest
Python | 334 lines | 178 code | 52 blank | 104 comment | 31 complexity | f0dec32a2345ee9208ba9d9e0fdb34c6 MD5 | raw file
  1. import inspect
  2. import re
  3. import warnings
  4. from django.core.exceptions import ImproperlyConfigured
  5. from django.forms import models as model_forms
  6. from django.http import HttpResponseRedirect
  7. from django.utils import six
  8. from django.utils.deprecation import RemovedInDjango110Warning
  9. from django.utils.encoding import force_text
  10. from django.views.generic.base import ContextMixin, TemplateResponseMixin, View
  11. from django.views.generic.detail import (
  12. BaseDetailView, SingleObjectMixin, SingleObjectTemplateResponseMixin,
  13. )
  14. PERCENT_PLACEHOLDER_REGEX = re.compile(r'%\([^\)]+\)') # RemovedInDjango110Warning
  15. class FormMixinBase(type):
  16. def __new__(cls, name, bases, attrs):
  17. get_form = attrs.get('get_form')
  18. if get_form and inspect.isfunction(get_form):
  19. try:
  20. inspect.getcallargs(get_form, None)
  21. except TypeError:
  22. warnings.warn(
  23. "`%s.%s.get_form` method must define a default value for "
  24. "its `form_class` argument." % (attrs['__module__'], name),
  25. RemovedInDjango110Warning, stacklevel=2
  26. )
  27. def get_form_with_form_class(self, form_class=None):
  28. if form_class is None:
  29. form_class = self.get_form_class()
  30. return get_form(self, form_class=form_class)
  31. attrs['get_form'] = get_form_with_form_class
  32. return super(FormMixinBase, cls).__new__(cls, name, bases, attrs)
  33. class FormMixin(six.with_metaclass(FormMixinBase, ContextMixin)):
  34. """
  35. A mixin that provides a way to show and handle a form in a request.
  36. """
  37. initial = {}
  38. form_class = None
  39. success_url = None
  40. prefix = None
  41. def get_initial(self):
  42. """
  43. Returns the initial data to use for forms on this view.
  44. """
  45. return self.initial.copy()
  46. def get_prefix(self):
  47. """
  48. Returns the prefix to use for forms on this view
  49. """
  50. return self.prefix
  51. def get_form_class(self):
  52. """
  53. Returns the form class to use in this view
  54. """
  55. return self.form_class
  56. def get_form(self, form_class=None):
  57. """
  58. Returns an instance of the form to be used in this view.
  59. """
  60. if form_class is None:
  61. form_class = self.get_form_class()
  62. return form_class(**self.get_form_kwargs())
  63. def get_form_kwargs(self):
  64. """
  65. Returns the keyword arguments for instantiating the form.
  66. """
  67. kwargs = {
  68. 'initial': self.get_initial(),
  69. 'prefix': self.get_prefix(),
  70. }
  71. if self.request.method in ('POST', 'PUT'):
  72. kwargs.update({
  73. 'data': self.request.POST,
  74. 'files': self.request.FILES,
  75. })
  76. return kwargs
  77. def get_success_url(self):
  78. """
  79. Returns the supplied success URL.
  80. """
  81. if self.success_url:
  82. # Forcing possible reverse_lazy evaluation
  83. url = force_text(self.success_url)
  84. else:
  85. raise ImproperlyConfigured(
  86. "No URL to redirect to. Provide a success_url.")
  87. return url
  88. def form_valid(self, form):
  89. """
  90. If the form is valid, redirect to the supplied URL.
  91. """
  92. return HttpResponseRedirect(self.get_success_url())
  93. def form_invalid(self, form):
  94. """
  95. If the form is invalid, re-render the context data with the
  96. data-filled form and errors.
  97. """
  98. return self.render_to_response(self.get_context_data(form=form))
  99. class ModelFormMixin(FormMixin, SingleObjectMixin):
  100. """
  101. A mixin that provides a way to show and handle a modelform in a request.
  102. """
  103. fields = None
  104. def get_form_class(self):
  105. """
  106. Returns the form class to use in this view.
  107. """
  108. if self.fields is not None and self.form_class:
  109. raise ImproperlyConfigured(
  110. "Specifying both 'fields' and 'form_class' is not permitted."
  111. )
  112. if self.form_class:
  113. return self.form_class
  114. else:
  115. if self.model is not None:
  116. # If a model has been explicitly provided, use it
  117. model = self.model
  118. elif hasattr(self, 'object') and self.object is not None:
  119. # If this view is operating on a single object, use
  120. # the class of that object
  121. model = self.object.__class__
  122. else:
  123. # Try to get a queryset and extract the model class
  124. # from that
  125. model = self.get_queryset().model
  126. if self.fields is None:
  127. raise ImproperlyConfigured(
  128. "Using ModelFormMixin (base class of %s) without "
  129. "the 'fields' attribute is prohibited." % self.__class__.__name__
  130. )
  131. return model_forms.modelform_factory(model, fields=self.fields)
  132. def get_form_kwargs(self):
  133. """
  134. Returns the keyword arguments for instantiating the form.
  135. """
  136. kwargs = super(ModelFormMixin, self).get_form_kwargs()
  137. if hasattr(self, 'object'):
  138. kwargs.update({'instance': self.object})
  139. return kwargs
  140. def get_success_url(self):
  141. """
  142. Returns the supplied URL.
  143. """
  144. if self.success_url:
  145. # force_text can be removed with deprecation warning
  146. self.success_url = force_text(self.success_url)
  147. if PERCENT_PLACEHOLDER_REGEX.search(self.success_url):
  148. warnings.warn(
  149. "%()s placeholder style in success_url is deprecated. "
  150. "Please replace them by the {} Python format syntax.",
  151. RemovedInDjango110Warning, stacklevel=2
  152. )
  153. url = self.success_url % self.object.__dict__
  154. else:
  155. url = self.success_url.format(**self.object.__dict__)
  156. else:
  157. try:
  158. url = self.object.get_absolute_url()
  159. except AttributeError:
  160. raise ImproperlyConfigured(
  161. "No URL to redirect to. Either provide a url or define"
  162. " a get_absolute_url method on the Model.")
  163. return url
  164. def form_valid(self, form):
  165. """
  166. If the form is valid, save the associated model.
  167. """
  168. self.object = form.save()
  169. return super(ModelFormMixin, self).form_valid(form)
  170. class ProcessFormView(View):
  171. """
  172. A mixin that renders a form on GET and processes it on POST.
  173. """
  174. def get(self, request, *args, **kwargs):
  175. """
  176. Handles GET requests and instantiates a blank version of the form.
  177. """
  178. form = self.get_form()
  179. return self.render_to_response(self.get_context_data(form=form))
  180. def post(self, request, *args, **kwargs):
  181. """
  182. Handles POST requests, instantiating a form instance with the passed
  183. POST variables and then checked for validity.
  184. """
  185. form = self.get_form()
  186. if form.is_valid():
  187. return self.form_valid(form)
  188. else:
  189. return self.form_invalid(form)
  190. # PUT is a valid HTTP verb for creating (with a known URL) or editing an
  191. # object, note that browsers only support POST for now.
  192. def put(self, *args, **kwargs):
  193. return self.post(*args, **kwargs)
  194. class BaseFormView(FormMixin, ProcessFormView):
  195. """
  196. A base view for displaying a form
  197. """
  198. class FormView(TemplateResponseMixin, BaseFormView):
  199. """
  200. A view for displaying a form, and rendering a template response.
  201. """
  202. class BaseCreateView(ModelFormMixin, ProcessFormView):
  203. """
  204. Base view for creating an new object instance.
  205. Using this base class requires subclassing to provide a response mixin.
  206. """
  207. def get(self, request, *args, **kwargs):
  208. self.object = None
  209. return super(BaseCreateView, self).get(request, *args, **kwargs)
  210. def post(self, request, *args, **kwargs):
  211. self.object = None
  212. return super(BaseCreateView, self).post(request, *args, **kwargs)
  213. class CreateView(SingleObjectTemplateResponseMixin, BaseCreateView):
  214. """
  215. View for creating a new object instance,
  216. with a response rendered by template.
  217. """
  218. template_name_suffix = '_form'
  219. class BaseUpdateView(ModelFormMixin, ProcessFormView):
  220. """
  221. Base view for updating an existing object.
  222. Using this base class requires subclassing to provide a response mixin.
  223. """
  224. def get(self, request, *args, **kwargs):
  225. self.object = self.get_object()
  226. return super(BaseUpdateView, self).get(request, *args, **kwargs)
  227. def post(self, request, *args, **kwargs):
  228. self.object = self.get_object()
  229. return super(BaseUpdateView, self).post(request, *args, **kwargs)
  230. class UpdateView(SingleObjectTemplateResponseMixin, BaseUpdateView):
  231. """
  232. View for updating an object,
  233. with a response rendered by template.
  234. """
  235. template_name_suffix = '_form'
  236. class DeletionMixin(object):
  237. """
  238. A mixin providing the ability to delete objects
  239. """
  240. success_url = None
  241. def delete(self, request, *args, **kwargs):
  242. """
  243. Calls the delete() method on the fetched object and then
  244. redirects to the success URL.
  245. """
  246. self.object = self.get_object()
  247. success_url = self.get_success_url()
  248. self.object.delete()
  249. return HttpResponseRedirect(success_url)
  250. # Add support for browsers which only accept GET and POST for now.
  251. def post(self, request, *args, **kwargs):
  252. return self.delete(request, *args, **kwargs)
  253. def get_success_url(self):
  254. if self.success_url:
  255. # force_text can be removed with deprecation warning
  256. self.success_url = force_text(self.success_url)
  257. if PERCENT_PLACEHOLDER_REGEX.search(self.success_url):
  258. warnings.warn(
  259. "%()s placeholder style in success_url is deprecated. "
  260. "Please replace them by the {} Python format syntax.",
  261. RemovedInDjango110Warning, stacklevel=2
  262. )
  263. return self.success_url % self.object.__dict__
  264. else:
  265. return self.success_url.format(**self.object.__dict__)
  266. else:
  267. raise ImproperlyConfigured(
  268. "No URL to redirect to. Provide a success_url.")
  269. class BaseDeleteView(DeletionMixin, BaseDetailView):
  270. """
  271. Base view for deleting an object.
  272. Using this base class requires subclassing to provide a response mixin.
  273. """
  274. class DeleteView(SingleObjectTemplateResponseMixin, BaseDeleteView):
  275. """
  276. View for deleting an object retrieved with `self.get_object()`,
  277. with a response rendered by template.
  278. """
  279. template_name_suffix = '_confirm_delete'