diff --git a/CHANGELOG.md b/CHANGELOG.md index 6609c55..793250d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,9 @@ ## 0.7 - Added support for django 1.10 and 1.11 +- Dropped support for django 1.3 - 1.7 - Fixed issue where django version would be forced to 1.8 when installing django-parsley. More details here. +- Added parsleyfy template tag ## 0.6 diff --git a/parsley/decorators.py b/parsley/decorators.py index 81bf7b0..811ad88 100644 --- a/parsley/decorators.py +++ b/parsley/decorators.py @@ -82,6 +82,7 @@ class ParsleyChoiceFieldRenderer(ParsleyChoiceFieldRendererMixin, field.widget.r if error_message: attrs["{prefix}-type-message".format(field_type, prefix=prefix)] = error_message + def parsley_form(form): prefix = getattr(getattr(form, 'Meta', None), 'parsley_namespace', 'data-parsley') for _, field in form.fields.items(): @@ -100,8 +101,12 @@ def parsley_form(form): attrs["{prefix}-%s".format(prefix=prefix) % key] = value return form + def parsleyfy(klass): - "A decorator to add {prefix}-* attributes to your form.fields" + """ + A decorator to add {prefix}-* attributes to your form.fields + """ + old_init = klass.__init__ def new_init(self, *args, **kwargs): diff --git a/parsley/tests/forms.py b/parsley/tests/forms.py index 93936f8..57b51ef 100644 --- a/parsley/tests/forms.py +++ b/parsley/tests/forms.py @@ -6,7 +6,6 @@ class TextForm(forms.Form): - "A simple form" name = forms.CharField(required=True,) university = forms.CharField(required=False) diff --git a/parsley/tests/tests.py b/parsley/tests/tests.py index 7ac85de..80bb113 100644 --- a/parsley/tests/tests.py +++ b/parsley/tests/tests.py @@ -2,10 +2,11 @@ import six from django import forms +from django.template import Context, Template from django.test import TestCase from django.utils.translation import ugettext_lazy as _ -from parsley.decorators import parsleyfy +from parsley.decorators import parsleyfy, parsley_form from .forms import (TextForm, TextForm2, FieldTypeForm, ExtraDataForm, ExtraDataMissingFieldForm, FormWithWidgets, StudentModelForm, @@ -35,8 +36,7 @@ def test_basic(self): form = TextForm() self.assertEqual(form.fields["name"].widget.attrs, {}) self.assertEqual(form.fields["university"].widget.attrs, {}) - ParsleyForm = parsleyfy(TextForm) - form = ParsleyForm() + form = parsley_form(TextForm()) self.assertAttrsEqual(form.fields["name"].widget.attrs, { "data-parsley-required": "true", "data-parsley-required-message": _("This field is required.") @@ -117,17 +117,17 @@ def test_widgets(self): class TestMetadata(ParsleyTestCase): def test_docstring(self): form1 = TextForm() - form2 = parsleyfy(TextForm)() + form2 = parsley_form(TextForm()) self.assertEqual(form1.__doc__, form2.__doc__) def test_module(self): form1 = TextForm() - form2 = parsleyfy(TextForm)() + form2 = parsley_form(TextForm()) self.assertEqual(form1.__module__, form2.__module__) def test_name(self): form1 = TextForm() - form2 = parsleyfy(TextForm)() + form2 = parsley_form(TextForm()) self.assertEqual(form1.__class__.__name__, form2.__class__.__name__) @@ -270,10 +270,11 @@ def test_override_default_message(self): "data-parsley-required-message": "Favorite color is required" }) + class TestCustomPrefix(TestCase): def test_default_prefix(self): - form = TextForm() + form = TextForm2() attrs = form.fields['name'].widget.attrs self.assertTrue('data-parsley-required' in attrs) @@ -281,3 +282,29 @@ def test_custom_prefix(self): form = CustomPrefixForm() attrs = form.fields['name'].widget.attrs self.assertTrue('custom-required' in attrs) + + +class TemplateTagTest(ParsleyTestCase): + def test_basic(self): + """ + Tests that parsleyfy will work using the template tag + """ + + template = Template("{% load parsley %}") + form = TextForm() + context = Context({'form': form}) + template.render(context) + + self.assertEqual(context['form'].fields["name"].widget.attrs, {}) + self.assertEqual(context['form'].fields["university"].widget.attrs, {}) + + template = Template("{% load parsley %}{% parsleyfy form as form %}") + form = TextForm() + context = Context({'form': form}) + template.render(context) + + self.assertAttrsEqual(context['form'].fields["name"].widget.attrs, { + "data-parsley-required": "true", + "data-parsley-required-message": _("This field is required.") + }) + self.assertEqual(context['form'].fields["university"].widget.attrs, {}) \ No newline at end of file diff --git a/runtests.py b/runtests.py index 3d26a2f..ba38d46 100755 --- a/runtests.py +++ b/runtests.py @@ -21,6 +21,12 @@ 'parsley', ], STATIC_URL="/static/", + TEMPLATES=[ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'APP_DIRS': True, + }, + ] ) # Setup Django 1.7+ (AppRegistryNotReady).