Code

Ticket #3639: create_update_newforms5.diff

File create_update_newforms5.diff, 22.2 KB (added by brosner, 6 years ago)

updated to r7140 and is now backward compatible by accepting a Model instance.

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