Skip to content
This repository
Browse code

Adding a test for Field layout object and MultiWidget field

This tests @beezz's changes to {% crispy_field %} tag for MultiWidget subclasses.
HTML attributes in `Field` layout object turn now underscores into hyphens.
  • Loading branch information...
commit f9ec42ad0af53e24c327a1141fea31711e874e73 1 parent 6dd58b0
Miguel Araujo authored
5 crispy_forms/layout.py
... ... @@ -1,6 +1,8 @@
1 1 from django.conf import settings
2 2 from django.template import Context, Template
3 3 from django.template.loader import render_to_string
  4 +from django.utils.html import conditional_escape
  5 +
4 6
5 7 from utils import render_field, flatatt
6 8
@@ -301,7 +303,8 @@ def __init__(self, field, *args, **kwargs):
301 303 if kwargs.has_key('template'):
302 304 self.template = kwargs.pop('template')
303 305
304   - self.attrs.update(kwargs)
  306 + # We use kwargs as HTML attributes, turning data_id='test' into data-id='test'
  307 + self.attrs.update(dict([(k.replace('_', '-'), conditional_escape(v)) for k,v in kwargs.items()]))
305 308
306 309 def render(self, form, form_style, context):
307 310 return render_field(self.field, form, form_style, context, template=self.template, attrs=self.attrs)
1  crispy_forms/templatetags/crispy_forms_field.py
@@ -40,7 +40,6 @@ def render(self, context):
40 40 attrs = [self.attrs] * len(widgets)
41 41
42 42 for widget, attr in zip(widgets, attrs):
43   -
44 43 class_name = widget.__class__.__name__.lower()
45 44 class_name = class_converter.get(class_name, class_name)
46 45 css_class = widget.attrs.get('class', '')
36 crispy_forms/tests/tests.py
@@ -3,6 +3,7 @@
3 3 from django.conf import settings
4 4 from django.core.urlresolvers import reverse
5 5 from django.forms.models import formset_factory
  6 +from django.forms.widgets import MultiWidget, TextInput
6 7 from django.template import Context, Template, TemplateSyntaxError
7 8 from django.template.loader import get_template_from_string
8 9 from django.template.loader import render_to_string
@@ -12,7 +13,18 @@
12 13
13 14 from crispy_forms.helper import FormHelper, FormHelpersException
14 15 from crispy_forms.layout import Submit, Reset, Hidden, Button
15   -from crispy_forms.layout import Layout, Fieldset, MultiField, Row, Column, HTML, ButtonHolder, Div
  16 +from crispy_forms.layout import Layout, Fieldset, MultiField, Row, Column, HTML, ButtonHolder, Div, Field
  17 +
  18 +
  19 +class AddressWidget(MultiWidget):
  20 + def __init__(self, attrs=None):
  21 + widgets = (TextInput, TextInput, TextInput)
  22 + super(AddressWidget, self).__init__(widgets, attrs)
  23 +
  24 + def decompress(self, value):
  25 + if value:
  26 + return value.split(',')
  27 + return [None, None, None]
16 28
17 29
18 30 class TestForm(forms.Form):
@@ -22,6 +34,7 @@ class TestForm(forms.Form):
22 34 password2 = forms.CharField(label="re-enter password", max_length=30, required=True, widget=forms.PasswordInput())
23 35 first_name = forms.CharField(label="first name", max_length=30, required=True, widget=forms.TextInput())
24 36 last_name = forms.CharField(label="last name", max_length=30, required=True, widget=forms.TextInput())
  37 + address = forms.CharField(widget=AddressWidget)
25 38
26 39 def clean(self):
27 40 super(TestForm, self).clean()
@@ -612,6 +625,27 @@ def test_formset_layout(self):
612 625 self.assertEqual(html.count('Note for first form only'), 1)
613 626 self.assertEqual(html.count('formRow'), 3)
614 627
  628 + def test_multiwidget_Field(self):
  629 + template = get_template_from_string(u"""
  630 + {% load crispy_forms_tags %}
  631 + {% crispy form form_helper %}
  632 + """)
  633 +
  634 + form_helper = FormHelper()
  635 + form_helper.add_layout(
  636 + Layout(
  637 + Field('address', data_name='test')
  638 + )
  639 + )
  640 +
  641 + c = Context({
  642 + 'form': TestForm(),
  643 + 'form_helper': form_helper,
  644 + })
  645 + html = template.render(c)
  646 + self.assertEqual(html.count('data-name="test"'), 3)
  647 + self.assertEqual(html.count('class="textinput textInput"'), 3)
  648 +
615 649 def test_i18n(self):
616 650 template = get_template_from_string(u"""
617 651 {% load crispy_forms_tags %}
5 crispy_forms/utils.py
@@ -51,6 +51,7 @@ def render_field(field, form, form_style, context, template=None, labelclass=Non
51 51 raise Exception("Field '%s' is using forbidden unicode characters" % field)
52 52
53 53 try:
  54 + # Injecting HTML attributes into field's widget, Django handles rendering these
54 55 field_instance = form.fields[field]
55 56 if attrs is not None:
56 57 field_instance.widget.attrs.update(attrs)
@@ -81,8 +82,8 @@ def render_field(field, form, form_style, context, template=None, labelclass=Non
81 82
82 83 # We save the Layout object's bound fields in the layout object's `bound_fields` list
83 84 if layout_object is not None:
84   - layout_object.bound_fields.append(bound_field)
85   -
  85 + layout_object.bound_fields.append(bound_field)
  86 +
86 87 context.update({'field': bound_field, 'labelclass': labelclass, 'flat_attrs': flatatt(attrs or {})})
87 88 html = template.render(context)
88 89

0 comments on commit f9ec42a

Please sign in to comment.
Something went wrong with that request. Please try again.