Ticket #17795: t17795.2.diff

File t17795.2.diff, 12.9 KB (added by Adrien Lemaire, 13 years ago)

kwargs for get_context_data() result, and factory tests

  • django/views/generic/dates.py

    diff --git a/django/views/generic/dates.py b/django/views/generic/dates.py
    index 5f5f959..d0a073b 100644
    a b class BaseDateListView(MultipleObjectMixin, DateMixin, View):  
    171171    def get(self, request, *args, **kwargs):
    172172        self.date_list, self.object_list, extra_context = self.get_dated_items()
    173173        context = self.get_context_data(object_list=self.object_list,
    174                                         date_list=self.date_list)
     174                                        date_list=self.date_list, **kwargs)
    175175        context.update(extra_context)
    176176        return self.render_to_response(context)
    177177
  • django/views/generic/detail.py

    diff --git a/django/views/generic/detail.py b/django/views/generic/detail.py
    index b9278bb..fe8460a 100644
    a b class SingleObjectMixin(object):  
    9696class BaseDetailView(SingleObjectMixin, View):
    9797    def get(self, request, *args, **kwargs):
    9898        self.object = self.get_object()
    99         context = self.get_context_data(object=self.object)
     99        context = self.get_context_data(object=self.object, **kwargs)
    100100        return self.render_to_response(context)
    101101
    102102
  • django/views/generic/edit.py

    diff --git a/django/views/generic/edit.py b/django/views/generic/edit.py
    index 3cade52..e46ff68 100644
    a b class ProcessFormView(View):  
    129129    def get(self, request, *args, **kwargs):
    130130        form_class = self.get_form_class()
    131131        form = self.get_form(form_class)
    132         return self.render_to_response(self.get_context_data(form=form))
     132        return self.render_to_response(
     133            self.get_context_data(form=form, **kwargs),
     134        )
    133135
    134136    def post(self, request, *args, **kwargs):
    135137        form_class = self.get_form_class()
  • django/views/generic/list.py

    diff --git a/django/views/generic/list.py b/django/views/generic/list.py
    index 9797356..63f2808 100644
    a b class BaseListView(MultipleObjectMixin, View):  
    116116        if not allow_empty and len(self.object_list) == 0:
    117117            raise Http404(_(u"Empty list and '%(class_name)s.allow_empty' is False.")
    118118                          % {'class_name': self.__class__.__name__})
    119         context = self.get_context_data(object_list=self.object_list)
     119        context = self.get_context_data(object_list=self.object_list, **kwargs)
    120120        return self.render_to_response(context)
    121121
    122122
  • tests/regressiontests/generic_views/dates.py

    diff --git a/tests/regressiontests/generic_views/dates.py b/tests/regressiontests/generic_views/dates.py
    index 652f66b..22f03bc 100644
    a b from __future__ import absolute_import  
    33import datetime
    44
    55from django.core.exceptions import ImproperlyConfigured
     6from django.core.urlresolvers import reverse
    67from django.test import TestCase
     8from django.test.client import RequestFactory
    79
     10from . import views
    811from .models import Book
    912
    1013
    class ArchiveIndexViewTests(TestCase):  
    2124                pubdate=base_date - datetime.timedelta(days=1))
    2225
    2326    def test_archive_view(self):
    24         res = self.client.get('/dates/books/')
     27        res = self.client.get(reverse('books_archive'))
    2528        self.assertEqual(res.status_code, 200)
    2629        self.assertEqual(res.context['date_list'], Book.objects.dates('pubdate', 'year')[::-1])
    2730        self.assertEqual(list(res.context['latest']), list(Book.objects.all()))
    class ArchiveIndexViewTests(TestCase):  
    3740
    3841    def test_empty_archive_view(self):
    3942        Book.objects.all().delete()
    40         res = self.client.get('/dates/books/')
     43        res = self.client.get(reverse('books_archive'))
    4144        self.assertEqual(res.status_code, 404)
    4245
    4346    def test_allow_empty_archive_view(self):
    class ArchiveIndexViewTests(TestCase):  
    7881        self.assertEqual(res.context['page_obj'].number, 2)
    7982        self.assertEqual(list(res.context['latest']), list(Book.objects.all()[10:20]))
    8083
     84    def test_datebaselistview_regression_t17795(self):
     85        factory = RequestFactory()
     86        request = factory.get(reverse('books_archive'))
     87        res = views.BookArchive.as_view()(request, foo='bar')
     88        self.assertEqual(res.context_data.get('foo'), 'bar',
     89            msg='context_data now contains the foo kwargs')
     90
    8191
    8292class YearArchiveViewTests(TestCase):
    8393    fixtures = ['generic-views-test-data.json']
    class YearArchiveViewTests(TestCase):  
    132142        res = self.client.get('/dates/books/no_year/')
    133143        self.assertEqual(res.status_code, 404)
    134144
     145
    135146class MonthArchiveViewTests(TestCase):
    136147    fixtures = ['generic-views-test-data.json']
    137148    urls = 'regressiontests.generic_views.urls'
    class WeekArchiveViewTests(TestCase):  
    291302        self.assertEqual(res.status_code, 200)
    292303        self.assertEqual(res.context['week'], datetime.date(2008, 9, 29))
    293304
     305
    294306class DayArchiveViewTests(TestCase):
    295307    fixtures = ['generic-views-test-data.json']
    296308    urls = 'regressiontests.generic_views.urls'
    class DayArchiveViewTests(TestCase):  
    373385        self.assertEqual(res.status_code, 404)
    374386
    375387    def test_today_view(self):
    376         res = self.client.get('/dates/books/today/')
     388        res = self.client.get(reverse('today_books'))
    377389        self.assertEqual(res.status_code, 404)
    378390        res = self.client.get('/dates/books/today/allow_empty/')
    379391        self.assertEqual(res.status_code, 200)
    380392        self.assertEqual(res.context['day'], datetime.date.today())
    381393
     394
    382395class DateDetailViewTests(TestCase):
    383396    fixtures = ['generic-views-test-data.json']
    384397    urls = 'regressiontests.generic_views.urls'
  • tests/regressiontests/generic_views/detail.py

    diff --git a/tests/regressiontests/generic_views/detail.py b/tests/regressiontests/generic_views/detail.py
    index 0b5d873..ecb4697 100644
    a b  
    11from __future__ import absolute_import
    22
    33from django.core.exceptions import ImproperlyConfigured
     4from django.core.urlresolvers import reverse
    45from django.test import TestCase
     6from django.test.client import RequestFactory
    57
     8from . import views
    69from .models import Artist, Author, Page
    710
    811
    9 class DetailViewTest(TestCase):
     12class DetailViewTests(TestCase):
    1013    fixtures = ['generic-views-test-data.json']
    1114    urls = 'regressiontests.generic_views.urls'
    1215
    class DetailViewTest(TestCase):  
    9295
    9396    def test_invalid_queryset(self):
    9497        self.assertRaises(ImproperlyConfigured, self.client.get, '/detail/author/invalid/qs/')
     98
     99    def test_detailview_regression_t17795(self):
     100        factory = RequestFactory()
     101        request = factory.get(reverse('author_detail', kwargs={'pk': 1}))
     102        res = views.AuthorDetail.as_view()(request, pk='1', foo='bar')
     103        self.assertEqual(res.context_data.get('foo'), 'bar',
     104            msg='context_data now contains the foo kwargs')
  • tests/regressiontests/generic_views/edit.py

    diff --git a/tests/regressiontests/generic_views/edit.py b/tests/regressiontests/generic_views/edit.py
    index 2bd982e..aef8ba8 100644
    a b from django.core.exceptions import ImproperlyConfigured  
    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
    class ModelFormMixinTests(TestCase):  
    1516        form_class = views.AuthorGetQuerySetFormView().get_form_class()
    1617        self.assertEqual(form_class._meta.model, Author)
    1718
     19
    1820class CreateViewTests(TestCase):
    1921    urls = 'regressiontests.generic_views.urls'
    2022
    class CreateViewTests(TestCase):  
    9294        self.assertEqual(res.status_code, 302)
    9395        self.assertRedirects(res, 'http://testserver/accounts/login/?next=/edit/authors/create/restricted/')
    9496
     97    def test_createview_regression_t17795(self):
     98        factory = RequestFactory()
     99        request = factory.get(reverse('author_create'))
     100        res = views.AuthorCreate.as_view()(request, foo='bar')
     101        self.assertEqual(res.context_data.get('foo'), 'bar',
     102            msg='context_data now contains the foo kwargs')
     103
     104
    95105class UpdateViewTests(TestCase):
    96106    urls = 'regressiontests.generic_views.urls'
    97107
    class UpdateViewTests(TestCase):  
    228238        self.assertRedirects(res, 'http://testserver/list/authors/')
    229239        self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (xkcd)>'])
    230240
     241
    231242class DeleteViewTests(TestCase):
    232243    urls = 'regressiontests.generic_views.urls'
    233244
    class DeleteViewTests(TestCase):  
    284295            self.fail('Should raise exception -- No redirect URL provided, and no get_absolute_url provided')
    285296        except ImproperlyConfigured:
    286297            pass
    287 
  • tests/regressiontests/generic_views/list.py

    diff --git a/tests/regressiontests/generic_views/list.py b/tests/regressiontests/generic_views/list.py
    index 9ad00ed..536edaf 100644
    a b  
    11from __future__ import absolute_import
    22
    33from django.core.exceptions import ImproperlyConfigured
     4from django.core.urlresolvers import reverse
    45from django.test import TestCase
     6from django.test.client import RequestFactory
    57
     8from . import views
    69from .models import Author, Artist
    710
    811
    class ListViewTests(TestCase):  
    164167        for i in range(n):
    165168            Author.objects.create(name='Author %02i' % i, slug='a%s' % i)
    166169
     170    def test_listview_regression_t17795(self):
     171        factory = RequestFactory()
     172        request = factory.get(reverse('authors_list'))
     173        res = views.AuthorList.as_view()(request, foo='bar')
     174        self.assertEqual(res.context_data.get('foo'), 'bar',
     175            msg='context_data now contains the foo kwargs')
  • tests/regressiontests/generic_views/tests.py

    diff --git a/tests/regressiontests/generic_views/tests.py b/tests/regressiontests/generic_views/tests.py
    index d387216..5693b06 100644
    a b from .base import ViewTest, TemplateViewTest, RedirectViewTest  
    44from .dates import (ArchiveIndexViewTests, YearArchiveViewTests,
    55    MonthArchiveViewTests, WeekArchiveViewTests, DayArchiveViewTests,
    66    DateDetailViewTests)
    7 from .detail import DetailViewTest
     7from .detail import DetailViewTests
    88from .edit import (ModelFormMixinTests, CreateViewTests, UpdateViewTests,
    99    DeleteViewTests)
    1010from .list import ListViewTests
  • tests/regressiontests/generic_views/urls.py

    diff --git a/tests/regressiontests/generic_views/urls.py b/tests/regressiontests/generic_views/urls.py
    index 090ec73..787676a 100644
    a b urlpatterns = patterns('',  
    2828        views.ObjectDetail.as_view()),
    2929    url(r'^detail/artist/(?P<pk>\d+)/$',
    3030        views.ArtistDetail.as_view(),
    31         name="artist_detail"),
     31        name='artist_detail'),
    3232    url(r'^detail/author/(?P<pk>\d+)/$',
    3333        views.AuthorDetail.as_view(),
    34         name="author_detail"),
     34        name='author_detail'),
    3535    (r'^detail/author/bycustompk/(?P<foo>\d+)/$',
    3636        views.AuthorDetail.as_view(pk_url_kwarg='foo')),
    3737    (r'^detail/author/byslug/(?P<slug>[\w-]+)/$',
    urlpatterns = patterns('',  
    5454        views.AuthorDetail.as_view(queryset=None)),
    5555
    5656    # Create/UpdateView
    57     (r'^edit/artists/create/$',
    58         views.ArtistCreate.as_view()),
     57    url(r'^edit/artists/create/$',
     58        views.ArtistCreate.as_view(),
     59        name='author_create'),
    5960    (r'^edit/artists/(?P<pk>\d+)/update/$',
    6061        views.ArtistUpdate.as_view()),
    6162
    urlpatterns = patterns('',  
    9495        views.SpecializedAuthorDelete.as_view()),
    9596
    9697    # ArchiveIndexView
    97     (r'^dates/books/$',
    98         views.BookArchive.as_view()),
     98    url(r'^dates/books/$',
     99        views.BookArchive.as_view(),
     100        name='books_archive'),
    99101    (r'^dates/books/context_object_name/$',
    100102        views.BookArchive.as_view(context_object_name='thingies')),
    101103    (r'^dates/books/allow_empty/$',
    urlpatterns = patterns('',  
    116118        views.DictList.as_view(paginate_by=1)),
    117119    url(r'^list/artists/$',
    118120        views.ArtistList.as_view(),
    119         name="artists_list"),
     121        name='artists_list'),
    120122    url(r'^list/authors/$',
    121123        views.AuthorList.as_view(),
    122         name="authors_list"),
     124        name='authors_list'),
    123125    (r'^list/authors/paginated/$',
    124126        views.AuthorList.as_view(paginate_by=30)),
    125127    (r'^list/authors/paginated/(?P<page>\d+)/$',
    urlpatterns = patterns('',  
    200202        views.BookDayArchive.as_view()),
    201203
    202204    # TodayArchiveView
    203     (r'dates/books/today/$',
    204         views.BookTodayArchive.as_view()),
     205    url(r'dates/books/today/$',
     206        views.BookTodayArchive.as_view(),
     207        name='today_books'),
    205208    (r'dates/books/today/allow_empty/$',
    206209        views.BookTodayArchive.as_view(allow_empty=True)),
    207210
Back to Top