Code

Ticket #3639: r6635_create_update_newforms3.diff

File r6635_create_update_newforms3.diff, 19.7 KB (added by brosner, 6 years ago)

created save_callback for custom saving of form data

Line 
1Index: django/views/generic/create_update.py
2===================================================================
3--- django/views/generic/create_update.py       (revision 6635)
4+++ django/views/generic/create_update.py       (working copy)
5@@ -1,7 +1,6 @@
6 from django.core.xheaders import populate_xheaders
7 from django.template import loader
8-from django import oldforms
9-from django.db.models import FileField
10+from django import newforms as forms
11 from django.contrib.auth.views import redirect_to_login
12 from django.template import RequestContext
13 from django.http import Http404, HttpResponse, HttpResponseRedirect
14@@ -10,7 +9,8 @@
15 
16 def create_object(request, model, template_name=None,
17         template_loader=loader, extra_context=None, post_save_redirect=None,
18-        login_required=False, follow=None, context_processors=None):
19+        login_required=False, context_processors=None,
20+        save_callback=lambda form, request: form.save(), **kwargs):
21     """
22     Generic object-creation function.
23 
24@@ -23,22 +23,14 @@
25     if login_required and not request.user.is_authenticated():
26         return redirect_to_login(request.path)
27 
28-    manipulator = model.AddManipulator(follow=follow)
29-    if request.POST:
30-        # If data was POSTed, we're trying to create a new object
31-        new_data = request.POST.copy()
32-
33-        if model._meta.has_field_type(FileField):
34-            new_data.update(request.FILES)
35-
36-        # Check for errors
37-        errors = manipulator.get_validation_errors(new_data)
38-        manipulator.do_html2python(new_data)
39-
40-        if not errors:
41-            # No errors -- this means we can save the data!
42-            new_object = manipulator.save(new_data)
43-
44+    ModelForm = forms.form_for_model(model, **kwargs)
45+   
46+    if request.method == 'POST':
47+        form = ModelForm(request.POST, request.FILES)
48+       
49+        if form.is_valid():
50+            new_object = save_callback(form, request)
51+           
52             if request.user.is_authenticated():
53                 request.user.message_set.create(message=ugettext("The %(verbose_name)s was created successfully.") % {"verbose_name": model._meta.verbose_name})
54 
55@@ -51,12 +43,9 @@
56             else:
57                 raise ImproperlyConfigured("No URL to redirect to from generic create view.")
58     else:
59-        # No POST, so we want a brand new form without any data or errors
60-        errors = {}
61-        new_data = manipulator.flatten_data()
62+        form = ModelForm()
63 
64-    # Create the FormWrapper, template, context, response
65-    form = oldforms.FormWrapper(manipulator, new_data, errors)
66+    # Create the template, context, response
67     if not template_name:
68         template_name = "%s/%s_form.html" % (model._meta.app_label, model._meta.object_name.lower())
69     t = template_loader.get_template(template_name)
70@@ -73,8 +62,9 @@
71 def update_object(request, model, object_id=None, slug=None,
72         slug_field='slug', template_name=None, template_loader=loader,
73         extra_context=None, post_save_redirect=None,
74-        login_required=False, follow=None, context_processors=None,
75-        template_object_name='object'):
76+        login_required=False, context_processors=None,
77+        template_object_name='object',
78+        save_callback=lambda form, request: form.save(), **kwargs):
79     """
80     Generic object-update function.
81 
82@@ -102,16 +92,12 @@
83     except ObjectDoesNotExist:
84         raise Http404, "No %s found for %s" % (model._meta.verbose_name, lookup_kwargs)
85 
86-    manipulator = model.ChangeManipulator(getattr(object, object._meta.pk.attname), follow=follow)
87+    ModelForm = forms.form_for_instance(object, **kwargs)
88 
89-    if request.POST:
90-        new_data = request.POST.copy()
91-        if model._meta.has_field_type(FileField):
92-            new_data.update(request.FILES)
93-        errors = manipulator.get_validation_errors(new_data)
94-        manipulator.do_html2python(new_data)
95-        if not errors:
96-            object = manipulator.save(new_data)
97+    if request.method == 'POST':
98+        form = ModelForm(request.POST, request.FILES)
99+        if form.is_valid():
100+            object = save_callback(form, request)
101 
102             if request.user.is_authenticated():
103                 request.user.message_set.create(message=ugettext("The %(verbose_name)s was updated successfully.") % {"verbose_name": model._meta.verbose_name})
104@@ -124,11 +110,8 @@
105             else:
106                 raise ImproperlyConfigured("No URL to redirect to from generic create view.")
107     else:
108-        errors = {}
109-        # This makes sure the form acurate represents the fields of the place.
110-        new_data = manipulator.flatten_data()
111+        form = ModelForm()
112 
113-    form = oldforms.FormWrapper(manipulator, new_data, errors)
114     if not template_name:
115         template_name = "%s/%s_form.html" % (model._meta.app_label, model._meta.object_name.lower())
116     t = template_loader.get_template(template_name)
117Index: tests/regressiontests/views/tests/__init__.py
118===================================================================
119--- tests/regressiontests/views/tests/__init__.py       (revision 6635)
120+++ tests/regressiontests/views/tests/__init__.py       (working copy)
121@@ -1,4 +1,5 @@
122 from defaults import *
123 from i18n import *
124 from static import *
125-from generic.date_based import *
126\ No newline at end of file
127+from generic.date_based import *
128+from generic.create_update import *
129\ No newline at end of file
130Index: tests/regressiontests/views/tests/generic/create_update.py
131===================================================================
132--- tests/regressiontests/views/tests/generic/create_update.py  (revision 0)
133+++ tests/regressiontests/views/tests/generic/create_update.py  (revision 0)
134@@ -0,0 +1,102 @@
135+
136+import datetime
137+from django.test import TestCase
138+from django.contrib.auth.views import redirect_to_login
139+from regressiontests.views.models import Article, Author
140+
141+class CreateObjectTest(TestCase):
142+    fixtures = ['testdata.json']
143+   
144+    def test_not_logged_in(self):
145+        """Verifies the user is logged in through the login_required kwarg"""
146+        view_url = '/views/create_update/member/create/article/'
147+        response = self.client.get(view_url)
148+        self.assertRedirects(response, 'http://testserver/accounts/login/?next=%s' % view_url)
149+   
150+    def test_create_article_display_page(self):
151+        """Ensures the generic view returned the page and contains a form."""
152+        view_url = '/views/create_update/create/article/'
153+        response = self.client.get(view_url)
154+        self.assertEqual(response.status_code, 200)
155+        if not response.context.get('form'):
156+            self.fail('No form found in the response.')
157+   
158+    def test_create_article_with_errors(self):
159+        """POSTs a form that contain validation errors."""
160+        view_url = '/views/create_update/create/article/'
161+        response = self.client.post(view_url, {
162+            'title': 'My First Article',
163+        })
164+        self.assertFormError(response, 'form', 'slug', [u'This field is required.'])
165+   
166+    def test_create_article(self):
167+        """Creates a new article through the generic view and ensures it gets
168+        redirected to the correct URL defined by post_save_redirect"""
169+        view_url = '/views/create_update/create/article/'
170+        response = self.client.post(view_url, {
171+            'title': 'My First Article',
172+            'slug': 'my-first-article',
173+            'author': '1',
174+            'date_created': datetime.datetime(2007, 6, 25),
175+        })
176+        self.assertRedirects(response,
177+            'http://testserver/views/create_update/view/article/my-first-article/',
178+            target_status_code=404)
179+   
180+    def test_create_custom_save_article(self):
181+        """Creates a new article with a custom save_callback to adjust the slug
182+        before committing to the database."""
183+        view_url = '/views/create_update/create_custom/article/'
184+        response = self.client.post(view_url, {
185+            'title': 'Test Article',
186+        })
187+        self.assertRedirects(response,
188+            'http://testserver/views/create_update/view/article/some-other-slug/',
189+            target_status_code=404)
190+
191+class UpdateDeleteObjectTest(TestCase):
192+    fixtures = ['testdata.json']
193+   
194+    def test_update_object_form_display(self):
195+        """Verifies that the form was created properly and with initial values."""
196+        response = self.client.get('/views/create_update/update/article/old_article/')
197+        self.assertEquals(unicode(response.context['form']['title']),
198+            u'<input id="id_title" type="text" name="title" value="Old Article" maxlength="100" />')
199+   
200+    def test_update_object(self):
201+        """Verifies the form POST data and performs a redirect to
202+        post_save_redirect"""
203+        response = self.client.post('/views/create_update/update/article/old_article/', {
204+            'title': 'Another Article',
205+            'slug': 'another-article-slug',
206+            'author': 1,
207+            'date_created': datetime.datetime(2007, 6, 25),
208+        })
209+        self.assertRedirects(response,
210+            'http://testserver/views/create_update/view/article/another-article-slug/',
211+            target_status_code=404)
212+        article = Article.objects.get(pk=1)
213+        self.assertEquals(article.title, "Another Article")
214+   
215+    def test_delete_object_confirm(self):
216+        """Verifies the confirm deletion page is displayed using a GET."""
217+        response = self.client.get('/views/create_update/delete/article/old_article/')
218+        self.assertTemplateUsed(response, 'views/article_confirm_delete.html')
219+   
220+    def test_delete_object_redirect(self):
221+        """Verifies that post_delete_redirect works properly."""
222+        response = self.client.post('/views/create_update/delete/article/old_article/')
223+        self.assertRedirects(response,
224+            'http://testserver/views/create_update/',
225+            target_status_code=404)
226+   
227+    def test_delete_object(self):
228+        """Verifies the object actually gets deleted on a POST."""
229+        response = self.client.post('/views/create_update/delete/article/old_article/')
230+        try:
231+            Article.objects.get(slug='old_article')
232+        except Article.DoesNotExist:
233+            pass
234+        else:
235+            self.fail('Object was not deleted.')
236+       
237\ No newline at end of file
238Index: tests/regressiontests/views/views.py
239===================================================================
240--- tests/regressiontests/views/views.py        (revision 6635)
241+++ tests/regressiontests/views/views.py        (working copy)
242@@ -1,7 +1,24 @@
243+import datetime
244 from django.http import HttpResponse
245 from django.template import RequestContext
246+from django.views.generic.create_update import create_object
247 
248+from models import Author
249+
250 def index_page(request):
251     """Dummy index page"""
252     return HttpResponse('<html><body>Dummy page</body></html>')
253 
254+def custom_slug(request, **kwargs):
255+    def mysave(form, request):
256+        instance = form.save(commit=False)
257+        instance.slug = 'some-other-slug'
258+        instance.author = Author.objects.get(pk=1)
259+        # this would normally be done through the default value of the field
260+        # this is just here to prove that it can be done in a save_callback
261+        instance.date_created = datetime.datetime.now()
262+        instance.save()
263+        return instance
264+    return create_object(request, save_callback=mysave,
265+        fields=('title',), **kwargs)
266+
267Index: tests/regressiontests/views/urls.py
268===================================================================
269--- tests/regressiontests/views/urls.py (revision 6635)
270+++ tests/regressiontests/views/urls.py (working copy)
271@@ -20,6 +20,10 @@
272     'month_format': '%m',
273 }
274 
275+crud_info_dict = {
276+    'model': Article,
277+}
278+
279 urlpatterns = patterns('',
280     (r'^$', views.index_page),
281     
282@@ -44,5 +48,17 @@
283         dict(allow_future=True, slug_field='slug', **date_based_info_dict)),
284     (r'^date_based/archive_month/(?P<year>\d{4})/(?P<month>\d{1,2})/$',
285         'django.views.generic.date_based.archive_month',
286-        date_based_info_dict),     
287+        date_based_info_dict),
288+   
289+    # crud generic views
290+    (r'^create_update/member/create/article/$', 'django.views.generic.create_update.create_object',
291+        dict(login_required=True, **crud_info_dict)),
292+    (r'^create_update/create/article/$', 'django.views.generic.create_update.create_object',
293+        dict(post_save_redirect='/views/create_update/view/article/%(slug)s/', **crud_info_dict)),
294+    (r'^create_update/create_custom/article/$', views.custom_slug,
295+        dict(post_save_redirect='/views/create_update/view/article/%(slug)s/', **crud_info_dict)),
296+    (r'create_update/update/article/(?P<slug>[-\w]+)/$', 'django.views.generic.create_update.update_object',
297+        dict(post_save_redirect='/views/create_update/view/article/%(slug)s/', slug_field='slug', **crud_info_dict)),
298+    (r'create_update/delete/article/(?P<slug>[-\w]+)/$', 'django.views.generic.create_update.delete_object',
299+        dict(post_delete_redirect='/views/create_update/', slug_field='slug', **crud_info_dict)),
300 )
301Index: tests/templates/views/article_confirm_delete.html
302===================================================================
303--- tests/templates/views/article_confirm_delete.html   (revision 0)
304+++ tests/templates/views/article_confirm_delete.html   (revision 0)
305@@ -0,0 +1 @@
306+This template intentionally left blank
307\ No newline at end of file
308Index: tests/templates/views/article_form.html
309===================================================================
310--- tests/templates/views/article_form.html     (revision 0)
311+++ tests/templates/views/article_form.html     (revision 0)
312@@ -0,0 +1 @@
313+This template intentionally left blank
314\ No newline at end of file
315Index: docs/generic_views.txt
316===================================================================
317--- docs/generic_views.txt      (revision 6635)
318+++ docs/generic_views.txt      (working copy)
319@@ -894,14 +894,21 @@
320 The ``django.views.generic.create_update`` module contains a set of functions
321 for creating, editing and deleting objects.
322 
323+**New in Django development version:**
324+
325+``django.views.generic.create_update.create_object`` and
326+``django.views.generic.create_update.update_object`` now use `newforms`_ to
327+build and display the form.
328+
329+.. _newforms: ../newforms/
330+
331 ``django.views.generic.create_update.create_object``
332 ----------------------------------------------------
333 
334 **Description:**
335 
336 A page that displays a form for creating an object, redisplaying the form with
337-validation errors (if there are any) and saving the object. This uses the
338-automatic manipulators that come with Django models.
339+validation errors (if there are any) and saving the object.
340 
341 **Required arguments:**
342 
343@@ -938,6 +945,26 @@
344     * ``context_processors``: A list of template-context processors to apply to
345       the view's template. See the `RequestContext docs`_.
346 
347+    * **New in Django development version:** ``save_callback``: A callback
348+      function to override how the form gets saved to the database.
349+     
350+      The ``save_callback`` function will be given a ``Form`` instance with
351+      the cleaned data and the ``HttpRequest`` instance passed to the view.
352+   
353+    * **New in Django development version:** ``form``: A ``BaseForm`` subclass
354+      to allow form customization and validation. See an example
355+      `using an alternate base class`_ to fully understand the usage of this
356+      argument.
357+
358+    * **New in Django development version:** ``fields``: A list of fields to
359+      include in the ``Form`` object. See an example `using a subset of fields`_
360+      for more information.
361+
362+    * **New in Django development version:** ``formfield_callback``: A callback
363+      function to easily customize the default fields and widgets displayed
364+      by the form. See an example `how to override default field types`_ for
365+      more information.
366+
367 **Template name:**
368 
369 If ``template_name`` isn't specified, this view will use the template
370@@ -947,22 +974,25 @@
371 
372 In addition to ``extra_context``, the template's context will be:
373 
374-    * ``form``: A ``django.oldforms.FormWrapper`` instance representing the form
375+    * ``form``: A ``django.newforms.Form`` instance representing the form
376       for editing the object. This lets you refer to form fields easily in the
377       template system.
378 
379       For example, if ``model`` has two fields, ``name`` and ``address``::
380 
381           <form action="" method="post">
382-          <p><label for="id_name">Name:</label> {{ form.name }}</p>
383-          <p><label for="id_address">Address:</label> {{ form.address }}</p>
384+          <p>{{ form.name.label_tag }} {{ form.name }}</p>
385+          <p>{{ form.address.label_tag }} {{ form.address }}</p>
386           </form>
387 
388-      See the `manipulator and formfield documentation`_ for more information
389-      about using ``FormWrapper`` objects in templates.
390+      See the `newforms documentation`_ for more information about using
391+      ``Form`` objects in templates.
392 
393 .. _authentication system: ../authentication/
394-.. _manipulator and formfield documentation: ../forms/
395+.. _using an alternate base class: ../newforms/#using-an-alternate-base-class
396+.. _using a subset of fields: ../newforms/#using-a-subset-of-fields-on-the-form
397+.. _how to override default field types: ../newforms/#overriding-the-default-field-types
398+.. _newforms documentation: ../newforms/
399 
400 ``django.views.generic.create_update.update_object``
401 ----------------------------------------------------
402@@ -1019,6 +1049,26 @@
403 
404     * ``template_object_name``:  Designates the name of the template variable
405       to use in the template context. By default, this is ``'object'``.
406+   
407+    * **New in Django development version:** ``save_callback``: A callback
408+      function to override how the form gets saved to the database.
409+     
410+      The ``save_callback`` function will be given a ``Form`` instance with
411+      the cleaned data and the ``HttpRequest`` instance passed to the view.
412+   
413+    * **New in Django development version:** ``form``: A ``BaseForm`` subclass
414+      to allow form customization and validation. See an example
415+      `using an alternate base class`_ to fully understand the usage of this
416+      argument.
417+   
418+    * **New in Django development version:** ``fields``: A list of fields to
419+      include in the ``Form`` object. See an example `using a subset of fields`_
420+      for more information.
421+   
422+    * **New in Django development version:** ``formfield_callback``: A callback
423+      function to easily customize the default fields and widgets displayed
424+      by the form. See an example `how to override default field types`_ for
425+      more information.
426 
427 **Template name:**
428 
429@@ -1029,20 +1079,20 @@
430 
431 In addition to ``extra_context``, the template's context will be:
432 
433-    * ``form``: A ``django.oldforms.FormWrapper`` instance representing the form
434+    * ``form``: A ``django.newforms.Form`` instance representing the form
435       for editing the object. This lets you refer to form fields easily in the
436       template system.
437 
438       For example, if ``model`` has two fields, ``name`` and ``address``::
439 
440           <form action="" method="post">
441-          <p><label for="id_name">Name:</label> {{ form.name }}</p>
442-          <p><label for="id_address">Address:</label> {{ form.address }}</p>
443+          <p>{{ form.name.label_tag }} {{ form.name }}</p>
444+          <p>{{ form.address.label_tag }} {{ form.address }}</p>
445           </form>
446+     
447+      See the `newforms documentation`_ for more information about using
448+      ``Form`` objects in templates.
449 
450-      See the `manipulator and formfield documentation`_ for more information
451-      about using ``FormWrapper`` objects in templates.
452-
453     * ``object``: The original object being edited. This variable's name
454       depends on the ``template_object_name`` parameter, which is ``'object'``
455       by default. If ``template_object_name`` is ``'foo'``, this variable's