Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix for csrf_token settings issues over multiple django versions, bet…

…ter testing, and some more documentation
  • Loading branch information...
commit d1f0c9ed8972ce91b421e292a2ea49a5b20043a7 1 parent f201dba
@pydanny pydanny authored
View
17 test_project/settings.py
@@ -3,6 +3,8 @@
import os.path
from os.path import join, abspath, dirname
+from django import get_version # TODO: remove when pre-CSRF token templatetags are no longer supported
+
PROJECT_ROOT = abspath(dirname(__file__))
PROJECT_ROOT = PROJECT_ROOT.replace('uni_form/tests/test_project','')
@@ -64,11 +66,18 @@
# 'django.template.loaders.eggs.load_template_source',
)
+
+
MIDDLEWARE_CLASSES = (
- 'django.middleware.common.CommonMiddleware',
- 'django.contrib.sessions.middleware.SessionMiddleware',
- 'django.contrib.auth.middleware.AuthenticationMiddleware',
-)
+ 'django.middleware.common.CommonMiddleware',
+ 'django.contrib.sessions.middleware.SessionMiddleware',
+ 'django.contrib.auth.middleware.AuthenticationMiddleware',
+ )
+
+django_version = get_version()
+if django_version.startswith('1.1.2') or django_version.startswith('1.2'):
+ MIDDLEWARE_CLASSES += ('django.middleware.csrf.CsrfViewMiddleware',)
+
ROOT_URLCONF = 'test_project.urls'
View
11 test_project/test_app/templates/test_app/generic_form_test.html
@@ -0,0 +1,11 @@
+{% extends "test_app/base.html" %}
+{% load uni_form_tags %}
+
+{% block body%}
+<h1>
+ {{ title }}
+</h1>
+
+{% uni_form form helper %}
+
+{% endblock %}
View
1  test_project/test_app/urls.py
@@ -10,6 +10,7 @@
url(r'^view_helper_set_action/$', "test_app.views.view_helper_set_action", name='set_action_test'),
url(r'^lacking_form_tag/$', "test_app.views.lacking_form_tag", name='lacking_form_tag'),
url(r'^message_response/$', "test_app.views.message_response", name='message_response'),
+ url(r'^csrf_token_test/$', "test_app.views.csrf_token_test", name='csrf_token_test'),
)
View
7 uni_form/templatetags/uni_form_tags.py
@@ -1,4 +1,4 @@
-from django import get_version
+from django import get_version # TODO: remove when pre-CSRF token templatetags are no longer supported
from django.conf import settings
from django.template import Context, Template
from django.template.loader import get_template
@@ -11,7 +11,7 @@
register = template.Library()
# csrf token fix hack.
-# Remove when csrf_token is valid in all versions of supported Django.
+# TODO: remove when pre-CSRF token templatetags are no longer supported
django_version = get_version()
is_old_django = True
if django_version.startswith('1.1.2') or django_version.startswith('1.2'):
@@ -132,7 +132,7 @@ def get_render(self, context):
'toggle_fields': final_toggle_fields
}
- if not is_old_django:
+ if not is_old_django: # TODO: remove when pre-CSRF token templatetags are no longer supported
if use_csrf_protection and context.has_key('csrf_token'):
response_dict['csrf_token'] = context['csrf_token']
@@ -222,6 +222,7 @@ def render(self,context):
return template.render(c)
+# TODO: remove when pre-CSRF token templatetags are no longer supported
if is_old_django:
# csrf token fix hack.
View
21 uni_form/tests/tests.py
@@ -126,6 +126,23 @@ def test_uni_form_helper_form_attributes(self):
self.assertFalse("""id="this-form-rocks">""" in html)
self.assertFalse("""class="uniForm forms-that-rock" """ in html)
self.assertFalse("""method="get" """ in html)
- self.assertFalse("""id="this-form-rocks">""" in html)
+ self.assertFalse("""id="this-form-rocks">""" in html)
-
+ """
+ def test_csrf_token(self):
+
+ template = get_template_from_string("""
+ {% load uni_form_tags %}
+ {% uni_form form form_helper %}
+ """)
+
+ # First we build a standard form helper
+ form_helper = FormHelper()
+
+ # Now we set the CSRF token to bew true
+ form_helper.use_csrf_protection = True
+
+ # Render the text
+ c = Context({'form':TestForm(),'form_helper':form_helper})
+ html = template.render(c)
+ """
View
4 uni_form/util.py
@@ -13,6 +13,4 @@ class Toggle(object):
A container for holder toggled items such as fields and buttons.
"""
- fields = []
-
-
+ fields = []
Please sign in to comment.
Something went wrong with that request. Please try again.