Skip to content

Commit

Permalink
Fixed flake issues
Browse files Browse the repository at this point in the history
  • Loading branch information
sveetch committed Feb 1, 2017
1 parent e435902 commit b6903bf
Show file tree
Hide file tree
Showing 10 changed files with 236 additions and 117 deletions.
2 changes: 1 addition & 1 deletion crispy_forms_foundation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"""Django application to add 'django-crispy-forms' layout objects for 'Foundation for sites'"""
"""Django application to add 'django-crispy-forms' layout objects for 'Foundation for sites'""" # noqa: E501
__version__ = '0.5.4'
101 changes: 70 additions & 31 deletions crispy_forms_foundation/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,60 @@

from .layout import Submit, HTML, InlineSwitchField


class FoundationFormMixin(object):
"""
Mixin to implement the layout helper that will automatically build a form layout
Generally, you will prefer to use ``FoundationForm`` or ``FoundationModelForm`` instead.
If you still want to directly use this mixin you'll just have to execute ``FoundationFormMixin.init_helper()`` in your form init.
"""

title = None #: If set, defines the form's title
layout = None #: If set, override the default layout for the form
error_title = _("Errors :") #: Defines the error title for non field errors
form_id = None #: Defines the id of the form
classes = "foundation-form" #: Defines the classes used on the form
action = "" #: Defines the action of the form. ``reverse`` will be called on the value. On failure the value will be assigned as is
method = "post" #: Defines the method used for the action
attrs = {} #: Defines the attributes of the form
switches = True #: If True, will replace all fields checkboxes with switches
submit = True #: Adds a submit button on the form. Can be set to a Submit object or a string which will be used as the value of the submit button
title_templatestring = u"<h3 class=\"subheader\">{0}</h3>" #: Template string used to display form title (if any)
Mixin to implement the layout helper that will automatically build a form
layout
Generally, you will prefer to use ``FoundationForm`` or
``FoundationModelForm`` instead.
If you still want to directly use this mixin you'll just have to execute
``FoundationFormMixin.init_helper()`` in your form init.
**Attributes**
title
If set, defines the form's title
layout
If set, override the default layout for the form
error_title
Defines the error title for non field errors
form_id
Defines the id of the form
classes
Defines the classes used on the form
action
Defines the action of the form. ``reverse`` will be called on the
value. On failure the value will be assigned as is
method
Defines the method used for the action
attrs
Defines the attributes of the form
switches
If True, will replace all fields checkboxes with switches
submit
Adds a submit button on the form. Can be set to a Submit object or
a string which will be used as the value of the submit button
title_templatestring
Template string used to display form title (if any)
"""
title = None
layout = None
error_title = _("Errors :")
form_id = None
classes = "foundation-form"
action = ""
method = "post"
attrs = {}
switches = True
submit = True
title_templatestring = u"<h3 class=\"subheader\">{0}</h3>"

def init_helper(self):
# Put required HTML attribute on required fields so they are managed by Abide (if enabled)
# Put required HTML attribute on required fields so they are managed by
# Abide (if enabled)
if "data_abide" in self.attrs:
for field in self.fields.values():
if field.required:
Expand All @@ -46,19 +77,21 @@ def init_helper(self):
# Start from the given layout
self.helper = FormHelper()
self.helper.layout = deepcopy(self.layout)

# Try to reverse form_action url, else fallback to use it as a simple string

# Try to reverse form_action url, else fallback to use it as a simple
# string
try:
self.helper.form_action = reverse(self.action)
except NoReverseMatch:
self.helper.form_action = self.action

if self.title:
self.helper.layout.insert(0, HTML(self.title_templatestring.format(self.title)))

html = HTML(self.title_templatestring.format(self.title))
self.helper.layout.insert(0, html)

if self.form_id is not None:
self.helper.form_id = self.form_id

self.helper.form_class = self.classes
self.helper.form_method = self.method
self.helper.form_error_title = self.error_title
Expand All @@ -69,8 +102,11 @@ def init_helper(self):
layout_field_names = self.helper.layout.get_field_names()
# Transform checkbox fields to switches element
for pointer in layout_field_names:
if isinstance(self.fields[pointer[1]].widget, forms.CheckboxInput):
self.replace_layout_object(pointer[0], InlineSwitchField(pointer[1], switch_class="inline"))
if isinstance(self.fields[pointer[1]].widget,
forms.CheckboxInput):
field = InlineSwitchField(pointer[1],
switch_class="inline")
self.replace_layout_object(pointer[0], field)

if self.submit:
if isinstance(self.submit, Submit):
Expand All @@ -93,12 +129,14 @@ def replace_layout_object(self, position, instead):
else:
self.helper.layout.fields[position[0]] = instead


class FoundationForm(FoundationFormMixin, forms.Form):
"""
A **Django form** that inherit from ``FoundationFormMixin`` to automatically build a form layout
A **Django form** that inherit from ``FoundationFormMixin`` to
automatically build a form layout
Example:
.. sourcecode:: python
from django import forms
Expand All @@ -110,7 +148,7 @@ class YourForm(FoundationForm):
layout = Layout(Fieldset("Section", "my_field", "my_field_2"))
switches = False
attrs = {'data_abide': ""}
title = forms.CharField(label='Title', required=True)
slug = forms.CharField(label='Slug', required=False)
Expand All @@ -122,10 +160,11 @@ def __init__(self, *args, **kwargs):

class FoundationModelForm(FoundationFormMixin, forms.ModelForm):
"""
A **Django Model form** that inherit from ``FoundationFormMixin`` to automatically build a form layout
A **Django Model form** that inherit from ``FoundationFormMixin`` to
automatically build a form layout
Example:
.. sourcecode:: python
from crispy_forms_foundation.forms import FoundationModelForm
Expand Down
17 changes: 14 additions & 3 deletions crispy_forms_foundation/layout/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""
Layout items for Foundation components
Inherits from the default **crispy_forms** layout objects to force templates on the
right ``TEMPLATE_PACK`` (defined from ``settings.CRISPY_TEMPLATE_PACK``) and implements
Foundation components.
Inherits from the default **crispy_forms** layout objects to force templates on
the right ``TEMPLATE_PACK`` (defined from ``settings.CRISPY_TEMPLATE_PACK``)
and implements Foundation components.
"""
from __future__ import absolute_import
from django.conf import settings
Expand All @@ -21,4 +21,15 @@
VerticalTabHolder, AccordionItem,
AccordionHolder)


__all__ = [
"Div", "Panel", "Layout", "UneditableField", "HTML", "Row", "RowFluid",
"Column", "MultiWidgetField", "Field", "MultiField", "SplitDateTimeField",
"InlineField", "InlineJustifiedField", "SwitchField", "InlineSwitchField",
"ButtonHolder", "ButtonHolderPanel", "ButtonGroup", "Button", "Submit",
"Hidden", "Reset", "Container", "ContainerHolder", "Fieldset", "TabItem",
"TabHolder", "VerticalTabHolder", "AccordionItem", "AccordionHolder",
]


TEMPLATE_PACK = getattr(settings, 'CRISPY_TEMPLATE_PACK', 'foundation-5')
23 changes: 17 additions & 6 deletions crispy_forms_foundation/layout/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,30 @@
TEMPLATE_PACK = getattr(settings, 'CRISPY_TEMPLATE_PACK', 'foundation-5')


class Layout(crispy_forms_layout.Layout): pass
class UneditableField(crispy_forms_layout.HTML): pass
class HTML(crispy_forms_layout.HTML): pass
class Layout(crispy_forms_layout.Layout):
pass


class UneditableField(crispy_forms_layout.HTML):
pass


class HTML(crispy_forms_layout.HTML):
pass


class Div(crispy_forms_layout.Div):
"""
It wraps fields in a <div>
You can set ``css_id`` for a DOM id and ``css_class`` for a DOM class. Example:
You can set ``css_id`` for a DOM id and ``css_class`` for a DOM class.
Example:
.. sourcecode:: python
Div('form_field_1', 'form_field_2', css_id='div-example', css_class='divs')
Div('form_field_1', 'form_field_2', css_id='div-example',
css_class='divs')
"""
template = "{0}/layout/div.html".format(TEMPLATE_PACK)

Expand All @@ -34,7 +44,8 @@ class Panel(crispy_forms_layout.Div):
.. sourcecode:: python
Panel('form_field_1', 'form_field_2', css_id='div-example', css_class='divs')
Panel('form_field_1', 'form_field_2', css_id='div-example',
css_class='divs')
"""
def __init__(self, field, *args, **kwargs):
kwargs['css_class'] = kwargs.get('css_class', '')+' panel'
Expand Down
36 changes: 24 additions & 12 deletions crispy_forms_foundation/layout/buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,25 @@
* `Foundation buttons <http://foundation.zurb.com/sites/docs/v/5.5.3/components/buttons.html>`_ for button components;
* `Foundation button groups <http://foundation.zurb.com/sites/docs/v/5.5.3/components/button_groups.html>`_ for button groups components;
"""
""" # noqa: E501
from django.conf import settings
from django.template import Context
from django.template.loader import render_to_string

from crispy_forms.utils import render_field
from crispy_forms import layout as crispy_forms_layout


TEMPLATE_PACK = getattr(settings, 'CRISPY_TEMPLATE_PACK', 'foundation-5')


class ButtonHolder(crispy_forms_layout.ButtonHolder):
"""
It wraps fields in a ``<div class="button-holder">``
This is where you should put Layout objects that render to form buttons like Submit.
It should only hold ``HTML`` and ``BaseInput`` inherited objects.
This is where you should put Layout objects that render to form buttons
like Submit. It should only hold ``HTML`` and ``BaseInput`` inherited
objects.
Example:
Expand All @@ -47,8 +50,8 @@ class ButtonGroup(crispy_forms_layout.LayoutObject):
"""
It wraps fields in a ``<ul class="button-group">``
This is where you should put Layout objects that render to form buttons like Submit.
It should only hold `HTML` and `BaseInput` inherited objects.
This is where you should put Layout objects that render to form buttons
like Submit. It should only hold `HTML` and `BaseInput` inherited objects.
Example:
Expand All @@ -71,10 +74,14 @@ def render(self, form, form_style, context, template_pack=TEMPLATE_PACK):
field_list = []
for field in self.fields:
field_list.append(
render_field(field, form, form_style, context, template_pack=template_pack)
render_field(field, form, form_style, context,
template_pack=template_pack)
)

return render_to_string(self.template, Context({'buttongroup': self, 'field_list': field_list}))
return render_to_string(self.template, Context({
'buttongroup': self,
'field_list': field_list,
}))


class Button(crispy_forms_layout.BaseInput):
Expand All @@ -85,21 +92,24 @@ class Button(crispy_forms_layout.BaseInput):
button = Button('Button 1', 'Press Me!')
.. note:: The first argument is also slugified and turned into the id for the button.
.. note:: The first argument is also slugified and turned into the id for
the button.
"""
input_type = 'button'
field_classes = 'button'


class Submit(crispy_forms_layout.BaseInput):
"""
Used to create a Submit button descriptor for the {% crispy %} template tag:
Used to create a Submit button descriptor for the {% crispy %} template
tag:
.. sourcecode:: python
submit = Submit('Search the Site', 'search this site')
.. note:: The first argument is also slugified and turned into the id for the submit button.
.. note:: The first argument is also slugified and turned into the id for
the submit button.
"""
input_type = 'submit'
field_classes = 'submit button'
Expand All @@ -115,13 +125,15 @@ class Hidden(crispy_forms_layout.Hidden):

class Reset(crispy_forms_layout.BaseInput):
"""
Used to create a Reset button input descriptor for the {% crispy %} template tag:
Used to create a Reset button input descriptor for the ``{% crispy %}``
template tag:
.. sourcecode:: python
reset = Reset('Reset This Form', 'Revert Me!')
.. note:: The first argument is also slugified and turned into the id for the reset.
.. note:: The first argument is also slugified and turned into the id for
the reset.
"""
input_type = 'reset'
field_classes = 'reset button'
Loading

0 comments on commit b6903bf

Please sign in to comment.