Skip to content

Commit

Permalink
add test for template tag, bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
Jared Proffitt committed May 26, 2017
1 parent b31aded commit bb135ae
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="https://github.com/agiliq/Django-parsley/issues/72" target="_blank">here</a>.
- Added parsleyfy template tag

## 0.6

Expand Down
7 changes: 6 additions & 1 deletion parsley/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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):
Expand Down
1 change: 0 additions & 1 deletion parsley/tests/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@


class TextForm(forms.Form):
"A simple form"
name = forms.CharField(required=True,)
university = forms.CharField(required=False)

Expand Down
41 changes: 34 additions & 7 deletions parsley/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.")
Expand Down Expand Up @@ -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__)


Expand Down Expand Up @@ -270,14 +270,41 @@ 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)

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, {})
6 changes: 6 additions & 0 deletions runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
'parsley',
],
STATIC_URL="/static/",
TEMPLATES=[
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'APP_DIRS': True,
},
]
)

# Setup Django 1.7+ (AppRegistryNotReady).
Expand Down

0 comments on commit bb135ae

Please sign in to comment.