Ticket #16744: 16744-2.diff

File 16744-2.diff, 7.9 KB (added by Claude Paroz, 13 years ago)

Updated patch

  • django/views/generic/base.py

    diff --git a/django/views/generic/base.py b/django/views/generic/base.py
    index c32a58a..f1e786c 100644
    a b class ContextMixin(object):  
    1515    """
    1616
    1717    def get_context_data(self, **kwargs):
     18        if 'view' not in kwargs:
     19            kwargs['view'] = self
    1820        return kwargs
    1921
    2022
  • django/views/generic/edit.py

    diff --git a/django/views/generic/edit.py b/django/views/generic/edit.py
    index 1f488cb..ab27a89 100644
    a b class FormMixin(ContextMixin):  
    3535
    3636    def get_form_kwargs(self):
    3737        """
    38         Returns the keyword arguments for instanciating the form.
     38        Returns the keyword arguments for instantiating the form.
    3939        """
    4040        kwargs = {'initial': self.get_initial()}
    4141        if self.request.method in ('POST', 'PUT'):
  • docs/ref/class-based-views.txt

    diff --git a/docs/ref/class-based-views.txt b/docs/ref/class-based-views.txt
    index 5223aee..005f38f 100644
    a b View  
    884884        The default implementation returns ``HttpResponseNotAllowed`` with list
    885885        of allowed methods in plain text.
    886886
     887    **Context**
     888
     889    .. versionadded:: 1.5
     890
     891    * ``view``: The template context of all class-based generic views include
     892      a ``view`` variable that points to the ``View`` instance.
     893
     894      .. admonition:: Use ``alters_data`` where appropriate
     895
     896          Note that having the view instance in the template context may expose
     897          potentially hazardous methods to template authors.  To prevent methods
     898          like this from being called in the template, set ``alters_data=True``
     899          on those methods.  For more information, read the documentation on
     900          :ref:`rendering a template context <alters-data-description>`.
     901
     902
    887903TemplateView
    888904~~~~~~~~~~~~
    889905.. class:: TemplateView()
  • docs/ref/templates/api.txt

    diff --git a/docs/ref/templates/api.txt b/docs/ref/templates/api.txt
    index 03fef1d..2bb20f9 100644
    a b straight lookups. Here are some things to keep in mind:  
    195195* A variable can only be called if it has no required arguments. Otherwise,
    196196  the system will return an empty string.
    197197
     198.. _alters-data-description:
     199
    198200* Obviously, there can be side effects when calling some variables, and
    199201  it'd be either foolish or a security hole to allow the template system
    200202  to access them.
  • docs/releases/1.5.txt

    diff --git a/docs/releases/1.5.txt b/docs/releases/1.5.txt
    index 84459f9..731ff02 100644
    a b Django 1.5 does not run on Jython.  
    3333What's new in Django 1.5
    3434========================
    3535
     36New ``view`` variable in class-based views context
     37~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     38In all :doc:`generic class-based views </topics/class-based-views>`
     39(or any class-based view inheriting from ``ContextMixin``), the context dictionary
     40contains a ``view`` variable that points to the ``View`` instance.
     41
    3642Backwards incompatible changes in 1.5
    3743=====================================
    3844
  • tests/regressiontests/generic_views/base.py

    diff --git a/tests/regressiontests/generic_views/base.py b/tests/regressiontests/generic_views/base.py
    index e18ed2a..af1f48c 100644
    a b class TemplateViewTest(TestCase):  
    224224        response = self.client.get('/template/simple/bar/')
    225225        self.assertEqual(response.status_code, 200)
    226226        self.assertEqual(response.context['params'], {'foo': 'bar'})
     227        self.assertTrue(isinstance(response.context['view'], View))
    227228
    228229    def test_extra_template_params(self):
    229230        """
    class TemplateViewTest(TestCase):  
    233234        self.assertEqual(response.status_code, 200)
    234235        self.assertEqual(response.context['params'], {'foo': 'bar'})
    235236        self.assertEqual(response.context['key'], 'value')
     237        self.assertTrue(isinstance(response.context['view'], View))
    236238
    237239    def test_cached_views(self):
    238240        """
  • tests/regressiontests/generic_views/detail.py

    diff --git a/tests/regressiontests/generic_views/detail.py b/tests/regressiontests/generic_views/detail.py
    index 0b5d873..24c73f3 100644
    a b from __future__ import absolute_import  
    22
    33from django.core.exceptions import ImproperlyConfigured
    44from django.test import TestCase
     5from django.views.generic.base import View
    56
    67from .models import Artist, Author, Page
    78
    class DetailViewTest(TestCase):  
    1415        res = self.client.get('/detail/obj/')
    1516        self.assertEqual(res.status_code, 200)
    1617        self.assertEqual(res.context['object'], {'foo': 'bar'})
     18        self.assertTrue(isinstance(res.context['view'], View))
    1719        self.assertTemplateUsed(res, 'generic_views/detail.html')
    1820
    1921    def test_detail_by_pk(self):
  • tests/regressiontests/generic_views/edit.py

    diff --git a/tests/regressiontests/generic_views/edit.py b/tests/regressiontests/generic_views/edit.py
    index 651e14f..16f4da8 100644
    a b from django.core.urlresolvers import reverse  
    55from django import forms
    66from django.test import TestCase
    77from django.utils.unittest import expectedFailure
     8from django.views.generic.base import View
    89from django.views.generic.edit import FormMixin
    910
    1011from . import views
    class CreateViewTests(TestCase):  
    3132        res = self.client.get('/edit/authors/create/')
    3233        self.assertEqual(res.status_code, 200)
    3334        self.assertTrue(isinstance(res.context['form'], forms.ModelForm))
     35        self.assertTrue(isinstance(res.context['view'], View))
    3436        self.assertFalse('object' in res.context)
    3537        self.assertFalse('author' in res.context)
    3638        self.assertTemplateUsed(res, 'generic_views/author_form.html')
    class CreateViewTests(TestCase):  
    101103        self.assertEqual(res.status_code, 302)
    102104        self.assertRedirects(res, 'http://testserver/accounts/login/?next=/edit/authors/create/restricted/')
    103105
     106
    104107class UpdateViewTests(TestCase):
    105108    urls = 'regressiontests.generic_views.urls'
    106109
    class UpdateViewTests(TestCase):  
    226229        res = self.client.get('/edit/author/update/')
    227230        self.assertEqual(res.status_code, 200)
    228231        self.assertTrue(isinstance(res.context['form'], forms.ModelForm))
     232        self.assertTrue(isinstance(res.context['view'], View))
    229233        self.assertEqual(res.context['object'], Author.objects.get(pk=a.pk))
    230234        self.assertEqual(res.context['author'], Author.objects.get(pk=a.pk))
    231235        self.assertTemplateUsed(res, 'generic_views/author_form.html')
    class UpdateViewTests(TestCase):  
    237241        self.assertRedirects(res, 'http://testserver/list/authors/')
    238242        self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (xkcd)>'])
    239243
     244
    240245class DeleteViewTests(TestCase):
    241246    urls = 'regressiontests.generic_views.urls'
    242247
  • tests/regressiontests/generic_views/list.py

    diff --git a/tests/regressiontests/generic_views/list.py b/tests/regressiontests/generic_views/list.py
    index 9ad00ed..47d6b18 100644
    a b from __future__ import absolute_import  
    22
    33from django.core.exceptions import ImproperlyConfigured
    44from django.test import TestCase
     5from django.views.generic.base import View
    56
    67from .models import Author, Artist
    78
    class ListViewTests(TestCase):  
    2122        self.assertEqual(res.status_code, 200)
    2223        self.assertTemplateUsed(res, 'generic_views/author_list.html')
    2324        self.assertEqual(list(res.context['object_list']), list(Author.objects.all()))
     25        self.assertTrue(isinstance(res.context['view'], View))
    2426        self.assertIs(res.context['author_list'], res.context['object_list'])
    2527        self.assertIsNone(res.context['paginator'])
    2628        self.assertIsNone(res.context['page_obj'])
Back to Top