Ticket #17557: improve-cbv-104.patch

File improve-cbv-104.patch, 9.5 KB (added by Travis Swicegood, 13 years ago)
  • django/views/generic/edit.py

    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):  
    2727        """
    2828        return self.form_class
    2929
    30     def get_form(self, form_class):
     30    def get_form(self, form_class=None):
    3131        """
    3232        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.
    3336        """
     37        if not form_class:
     38            form_class = self.get_form_class()
    3439        return form_class(**self.get_form_kwargs())
    3540
    3641    def get_form_kwargs(self):
  • tests/regressiontests/generic_views/edit.py

    diff --git a/tests/regressiontests/generic_views/edit.py b/tests/regressiontests/generic_views/edit.py
    index 182615a..4f22364 100644
    a b  
    44from django.core.urlresolvers import reverse
    55from django import forms
    66from django.test import TestCase
     7from django.test.client import RequestFactory
    78from django.utils.unittest import expectedFailure
    89
    910from . import views
    1011from .models import Artist, Author
    1112
    1213
     14class 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
    1332class ModelFormMixinTests(TestCase):
    1433    def test_get_form(self):
    1534        form_class = views.AuthorGetQuerySetFormView().get_form_class()
  • tests/regressiontests/generic_views/forms.py

    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):  
    1111
    1212    class Meta:
    1313        model = Author
     14
     15
     16class ExpandedAuthorForm(forms.ModelForm):
     17    name = forms.CharField()
     18    slug = forms.SlugField()
     19    extra = forms.CharField()
     20
     21    class Meta:
     22        model = Author
  • tests/regressiontests/generic_views/tests.py

    diff --git a/tests/regressiontests/generic_views/tests.py b/tests/regressiontests/generic_views/tests.py
    index d387216..495f35e 100644
    a b  
    66    DateDetailViewTests)
    77from .detail import DetailViewTest
    88from .edit import (ModelFormMixinTests, CreateViewTests, UpdateViewTests,
    9     DeleteViewTests)
     9    DeleteViewTests, FormMixinTestCase)
    1010from .list import ListViewTests
  • tests/regressiontests/generic_views/views.py

    diff --git a/tests/regressiontests/generic_views/views.py b/tests/regressiontests/generic_views/views.py
    index 5ff9cf0..7be0dce 100644
    a b  
    55from django.core.urlresolvers import reverse
    66from django.utils.decorators import method_decorator
    77from django.views import generic
     8from django.views.generic.edit import FormMixin
    89
    9 from .forms import AuthorForm
     10from .forms import AuthorForm, ExpandedAuthorForm
    1011from .models import Artist, Author, Book, Page
    1112
    1213
    class AuthorCreate(generic.CreateView):  
    8990    success_url = '/list/authors/'
    9091
    9192
     93class ExpandedAuthorFormMixin(FormMixin):
     94    form_class = ExpandedAuthorForm
     95
     96
    9297class SpecializedAuthorCreate(generic.CreateView):
    9398    model = Author
    9499    form_class = AuthorForm
  • django/views/generic/edit.py

    -- 
    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):  
    132132    A mixin that processes a form on POST.
    133133    """
    134134    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()
    137136        return self.render_to_response(self.get_context_data(form=form))
    138137
    139138    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()
    142140        if form.is_valid():
    143141            return self.form_valid(form)
    144142        else:
  • django/views/generic/edit.py

    -- 
    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):  
    5151        return kwargs
    5252
    5353    def get_context_data(self, **kwargs):
     54        if not "form" in kwargs:
     55            kwargs["form"] = self.get_form()
    5456        return kwargs
    5557
    5658    def get_success_url(self):
  • tests/regressiontests/generic_views/edit.py

    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):  
    2828        form = self.get_mixin().get_form(views.AuthorForm)
    2929        self.assertEqual(form.__class__, views.AuthorForm)
    3030
     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
    3142
    3243class ModelFormMixinTests(TestCase):
    3344    def test_get_form(self):
  • django/views/generic/edit.py

    -- 
    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):  
    120120        return super(ModelFormMixin, self).form_valid(form)
    121121
    122122    def get_context_data(self, **kwargs):
    123         context = kwargs
     123        context = super(ModelFormMixin, self).get_context_data(**kwargs)
    124124        if self.object:
    125125            context['object'] = self.object
    126126            context_object_name = self.get_context_object_name(self.object)
    class ProcessFormView(View):  
    134134    A mixin that processes a form on POST.
    135135    """
    136136    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())
    139138
    140139    def post(self, request, *args, **kwargs):
    141140        form = self.get_form()
  • tests/regressiontests/generic_views/edit.py

    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):  
    4141
    4242
    4343class 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
    4450    def test_get_form(self):
    4551        form_class = views.AuthorGetQuerySetFormView().get_form_class()
    4652        self.assertEqual(form_class._meta.model, Author)
    4753
     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
    4861class CreateViewTests(TestCase):
    4962    urls = 'regressiontests.generic_views.urls'
    5063
Back to Top