From 1e369ec1fb5442607647666be31069796e213ba1 Mon Sep 17 00:00:00 2001
From: Travis Swicegood <development@domain51.com>
Date: Tue, 17 Jan 2012 10:55:38 -0600
Subject: [PATCH 1/4] Simplify get_form() usage
---
django/views/generic/edit.py | 7 ++++++-
tests/regressiontests/generic_views/edit.py | 19 +++++++++++++++++++
tests/regressiontests/generic_views/forms.py | 9 +++++++++
tests/regressiontests/generic_views/tests.py | 2 +-
tests/regressiontests/generic_views/views.py | 7 ++++++-
5 files changed, 41 insertions(+), 3 deletions(-)
diff --git a/django/views/generic/edit.py b/django/views/generic/edit.py
index 3cade52..8f7ce0c 100644
a
|
b
|
def get_form_class(self):
|
27 | 27 | """ |
28 | 28 | return self.form_class |
29 | 29 | |
30 | | def get_form(self, form_class): |
| 30 | def get_form(self, form_class=None): |
31 | 31 | """ |
32 | 32 | Returns an instance of the form to be used in this view. |
| 33 | |
| 34 | The form class defaults to ``get_form_class`` if no ``form_class`` |
| 35 | argument is provided. |
33 | 36 | """ |
| 37 | if not form_class: |
| 38 | form_class = self.get_form_class() |
34 | 39 | return form_class(**self.get_form_kwargs()) |
35 | 40 | |
36 | 41 | def get_form_kwargs(self): |
diff --git a/tests/regressiontests/generic_views/edit.py b/tests/regressiontests/generic_views/edit.py
index 182615a..4f22364 100644
a
|
b
|
|
4 | 4 | from django.core.urlresolvers import reverse |
5 | 5 | from django import forms |
6 | 6 | from django.test import TestCase |
| 7 | from django.test.client import RequestFactory |
7 | 8 | from django.utils.unittest import expectedFailure |
8 | 9 | |
9 | 10 | from . import views |
10 | 11 | from .models import Artist, Author |
11 | 12 | |
12 | 13 | |
| 14 | class FormMixinTestCase(TestCase): |
| 15 | def setUp(self): |
| 16 | self.factory = RequestFactory() |
| 17 | |
| 18 | def get_mixin(self): |
| 19 | mixin = views.ExpandedAuthorFormMixin() |
| 20 | mixin.request = self.factory.get("/any/url/works") |
| 21 | return mixin |
| 22 | |
| 23 | def test_get_form_without_form_class(self): |
| 24 | form = self.get_mixin().get_form() |
| 25 | self.assertEqual(form.__class__, views.ExpandedAuthorForm) |
| 26 | |
| 27 | def test_get_form_with_form_class(self): |
| 28 | form = self.get_mixin().get_form(views.AuthorForm) |
| 29 | self.assertEqual(form.__class__, views.AuthorForm) |
| 30 | |
| 31 | |
13 | 32 | class ModelFormMixinTests(TestCase): |
14 | 33 | def test_get_form(self): |
15 | 34 | form_class = views.AuthorGetQuerySetFormView().get_form_class() |
diff --git a/tests/regressiontests/generic_views/forms.py b/tests/regressiontests/generic_views/forms.py
index a78242f..3f97aa9 100644
a
|
b
|
class AuthorForm(forms.ModelForm):
|
11 | 11 | |
12 | 12 | class Meta: |
13 | 13 | model = Author |
| 14 | |
| 15 | |
| 16 | class ExpandedAuthorForm(forms.ModelForm): |
| 17 | name = forms.CharField() |
| 18 | slug = forms.SlugField() |
| 19 | extra = forms.CharField() |
| 20 | |
| 21 | class Meta: |
| 22 | model = Author |
diff --git a/tests/regressiontests/generic_views/tests.py b/tests/regressiontests/generic_views/tests.py
index d387216..495f35e 100644
a
|
b
|
|
6 | 6 | DateDetailViewTests) |
7 | 7 | from .detail import DetailViewTest |
8 | 8 | from .edit import (ModelFormMixinTests, CreateViewTests, UpdateViewTests, |
9 | | DeleteViewTests) |
| 9 | DeleteViewTests, FormMixinTestCase) |
10 | 10 | from .list import ListViewTests |
diff --git a/tests/regressiontests/generic_views/views.py b/tests/regressiontests/generic_views/views.py
index 5ff9cf0..7be0dce 100644
a
|
b
|
|
5 | 5 | from django.core.urlresolvers import reverse |
6 | 6 | from django.utils.decorators import method_decorator |
7 | 7 | from django.views import generic |
| 8 | from django.views.generic.edit import FormMixin |
8 | 9 | |
9 | | from .forms import AuthorForm |
| 10 | from .forms import AuthorForm, ExpandedAuthorForm |
10 | 11 | from .models import Artist, Author, Book, Page |
11 | 12 | |
12 | 13 | |
… |
… |
class AuthorCreate(generic.CreateView):
|
89 | 90 | success_url = '/list/authors/' |
90 | 91 | |
91 | 92 | |
| 93 | class ExpandedAuthorFormMixin(FormMixin): |
| 94 | form_class = ExpandedAuthorForm |
| 95 | |
| 96 | |
92 | 97 | class SpecializedAuthorCreate(generic.CreateView): |
93 | 98 | model = Author |
94 | 99 | form_class = AuthorForm |
--
1.7.5.4
From a4d67b8c1764cce63eca19056ef668894f2fb821 Mon Sep 17 00:00:00 2001
From: Travis Swicegood <development@domain51.com>
Date: Tue, 17 Jan 2012 10:56:54 -0600
Subject: [PATCH 2/4] First step in simplifying these handlers
---
django/views/generic/edit.py | 6 ++----
1 files changed, 2 insertions(+), 4 deletions(-)
diff --git a/django/views/generic/edit.py b/django/views/generic/edit.py
index 8f7ce0c..48dd4c6 100644
a
|
b
|
class ProcessFormView(View):
|
132 | 132 | A mixin that processes a form on POST. |
133 | 133 | """ |
134 | 134 | def get(self, request, *args, **kwargs): |
135 | | form_class = self.get_form_class() |
136 | | form = self.get_form(form_class) |
| 135 | form = self.get_form() |
137 | 136 | return self.render_to_response(self.get_context_data(form=form)) |
138 | 137 | |
139 | 138 | def post(self, request, *args, **kwargs): |
140 | | form_class = self.get_form_class() |
141 | | form = self.get_form(form_class) |
| 139 | form = self.get_form() |
142 | 140 | if form.is_valid(): |
143 | 141 | return self.form_valid(form) |
144 | 142 | else: |
--
1.7.5.4
From 37b4c5740b31f7f63718c51a32ca0b0f24bc0231 Mon Sep 17 00:00:00 2001
From: Travis Swicegood <development@domain51.com>
Date: Tue, 17 Jan 2012 11:35:35 -0600
Subject: [PATCH 3/4] Add a form element if not provided
---
django/views/generic/edit.py | 2 ++
tests/regressiontests/generic_views/edit.py | 11 +++++++++++
2 files changed, 13 insertions(+), 0 deletions(-)
diff --git a/django/views/generic/edit.py b/django/views/generic/edit.py
index 48dd4c6..fd7b42c 100644
a
|
b
|
def get_form_kwargs(self):
|
51 | 51 | return kwargs |
52 | 52 | |
53 | 53 | def get_context_data(self, **kwargs): |
| 54 | if not "form" in kwargs: |
| 55 | kwargs["form"] = self.get_form() |
54 | 56 | return kwargs |
55 | 57 | |
56 | 58 | def get_success_url(self): |
diff --git a/tests/regressiontests/generic_views/edit.py b/tests/regressiontests/generic_views/edit.py
index 4f22364..afc19e1 100644
a
|
b
|
def test_get_form_with_form_class(self):
|
28 | 28 | form = self.get_mixin().get_form(views.AuthorForm) |
29 | 29 | self.assertEqual(form.__class__, views.AuthorForm) |
30 | 30 | |
| 31 | def test_get_context_data_without_form(self): |
| 32 | context_data = self.get_mixin().get_context_data() |
| 33 | self.assertTrue("form" in context_data) |
| 34 | self.assertEqual(context_data["form"].__class__, |
| 35 | views.ExpandedAuthorForm) |
| 36 | |
| 37 | def test_get_context_data_with_form(self): |
| 38 | context_data = self.get_mixin().get_context_data( |
| 39 | form=views.AuthorForm()) |
| 40 | self.assertEqual(context_data["form"].__class__, views.AuthorForm) |
| 41 | |
31 | 42 | |
32 | 43 | class ModelFormMixinTests(TestCase): |
33 | 44 | def test_get_form(self): |
--
1.7.5.4
From 63cf5107880327b4664428ef7afeb832b97213fd Mon Sep 17 00:00:00 2001
From: Travis Swicegood <development@domain51.com>
Date: Tue, 17 Jan 2012 11:42:27 -0600
Subject: [PATCH 4/4] Simplify get_context_data usage based on previous commit
---
django/views/generic/edit.py | 5 ++---
tests/regressiontests/generic_views/edit.py | 13 +++++++++++++
2 files changed, 15 insertions(+), 3 deletions(-)
diff --git a/django/views/generic/edit.py b/django/views/generic/edit.py
index fd7b42c..fab8e2a 100644
a
|
b
|
def form_valid(self, form):
|
120 | 120 | return super(ModelFormMixin, self).form_valid(form) |
121 | 121 | |
122 | 122 | def get_context_data(self, **kwargs): |
123 | | context = kwargs |
| 123 | context = super(ModelFormMixin, self).get_context_data(**kwargs) |
124 | 124 | if self.object: |
125 | 125 | context['object'] = self.object |
126 | 126 | context_object_name = self.get_context_object_name(self.object) |
… |
… |
class ProcessFormView(View):
|
134 | 134 | A mixin that processes a form on POST. |
135 | 135 | """ |
136 | 136 | def get(self, request, *args, **kwargs): |
137 | | form = self.get_form() |
138 | | return self.render_to_response(self.get_context_data(form=form)) |
| 137 | return self.render_to_response(self.get_context_data()) |
139 | 138 | |
140 | 139 | def post(self, request, *args, **kwargs): |
141 | 140 | form = self.get_form() |
diff --git a/tests/regressiontests/generic_views/edit.py b/tests/regressiontests/generic_views/edit.py
index afc19e1..b65b0f9 100644
a
|
b
|
def test_get_context_data_with_form(self):
|
41 | 41 | |
42 | 42 | |
43 | 43 | class ModelFormMixinTests(TestCase): |
| 44 | def get_testable_view(self): |
| 45 | view = views.AuthorGetQuerySetFormView() |
| 46 | view.request = RequestFactory().get("/any/url/works") |
| 47 | view.object = None |
| 48 | return view |
| 49 | |
44 | 50 | def test_get_form(self): |
45 | 51 | form_class = views.AuthorGetQuerySetFormView().get_form_class() |
46 | 52 | self.assertEqual(form_class._meta.model, Author) |
47 | 53 | |
| 54 | def test_get_context_data_adds_form_if_not_present(self): |
| 55 | view = self.get_testable_view() |
| 56 | context_data = view.get_context_data() |
| 57 | self.assertTrue("form" in context_data) |
| 58 | self.assertEqual(context_data["form"]._meta.model, Author) |
| 59 | |
| 60 | |
48 | 61 | class CreateViewTests(TestCase): |
49 | 62 | urls = 'regressiontests.generic_views.urls' |
50 | 63 | |