1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
"""
References
* `Foundation 6 fieldset <https://get.foundation/sites/docs/forms.html#fieldset-styles>`_;
* `Foundation 6 Accordion <https://get.foundation/sites/docs/accordion.html>`_;
* `Foundation 6 Tabs <https://get.foundation/sites/docs/tabs.html>`_;
""" # noqa: E501
from random import randint
from django.template.loader import render_to_string
from crispy_forms import layout as crispy_forms_layout
from crispy_forms.utils import render_field, TEMPLATE_PACK
from crispy_forms import bootstrap as crispy_forms_bootstrap
__all__ = [
'Fieldset', 'Container', 'ContainerHolder', 'TabHolder',
'VerticalTabHolder', 'TabItem', 'AccordionHolder', 'AccordionItem',
]
class Fieldset(crispy_forms_layout.Fieldset):
"""
It wraps fields in a ``<fieldset>``:
.. code-block:: python
Fieldset("Text for the legend",
'form_field_1',
'form_field_2'
)
The first parameter is the text for the fieldset legend. This text is
context aware, so you can do things like :
.. code-block:: python
Fieldset("Data for {{ user.username }}",
'form_field_1',
'form_field_2'
)
"""
template = "%s/layout/fieldset.html"
class Container(crispy_forms_bootstrap.Container):
"""
Overrides original Container element to get the "active" classname from
Class attribute ``active_css_class`` so it's compatible with Foundation
5 and 6.
"""
css_class = ""
active_css_class = "active"
def get_active_css_class(self, template_pack):
# Foundation-6 addon only which use unusual class name
if template_pack == 'foundation-6':
return "is-active"
return self.active_css_class
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK,
**kwargs):
active_classname = self.get_active_css_class(template_pack)
if self.active:
if active_classname and active_classname not in self.css_class:
self.css_class += ' ' + active_classname
else:
self.css_class = self.css_class.replace(active_classname, '')
return super(Container, self).render(form, form_style, context,
template_pack)
class ContainerHolder(crispy_forms_bootstrap.ContainerHolder):
pass
class TabHolder(crispy_forms_bootstrap.TabHolder):
"""
Tabs holder object to wrap Tab item objects in a container:
.. code-block:: python
TabHolder(
TabItem('My tab 1', 'form_field_1', 'form_field_2'),
TabItem('My tab 2', 'form_field_3')
)
``TabHolder`` direct children should allways be a ``TabItem`` layout item.
A random id is builded for the tab holder if you don't define it using
``css_id`` argument.
The first ``TabItem`` containing a field error will be marked as
*active* if any, else this will be just the first ``TabItem``.
"""
template = "%s/layout/tab-holder.html"
default_active_tab = None
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK):
"""
Re-implement almost the same code from crispy_forms but passing
``form`` instance to item ``render_link`` method.
"""
links, content = '', ''
# accordion group needs the parent div id to set `data-parent` (I don't
# know why). This needs to be a unique id
if not self.css_id:
self.css_id = "-".join(["tabsholder", str(randint(1000, 9999))])
for tab in self.fields:
tab.active = False
# Activate item
self.open_target_group_for_form(form)
for tab in self.fields:
content += render_field(
tab, form, form_style, context, template_pack=template_pack
)
links += tab.render_link(form, template_pack)
context.update({
'tabs': self,
'links': links,
'content': content
})
template = self.get_template_name(template_pack)
return render_to_string(template, context.flatten())
class VerticalTabHolder(TabHolder):
"""
VerticalTabHolder appends vertical class to TabHolder container
"""
css_class = 'vertical'
class TabItem(Container):
"""
Tab item object. It wraps fields in a div whose default class is "tabs" and
takes a name as first argument.
Tab item is also responsible of building its associated tab link with its
``render_link`` using the ``link_template`` attribute.
Example:
.. code-block:: python
TabItem('My tab', 'form_field_1', 'form_field_2', 'form_field_3')
``TabItem`` layout item has no real utility out of a ``TabHolder``.
"""
template = "%s/layout/tab-item.html"
link_template = "%s/layout/tab-link.html"
def has_errors(self, form):
"""
Find tab fields listed as invalid
"""
return any([fieldname_error for fieldname_error in form.errors.keys()
if fieldname_error in self])
def render_link(self, form, template_pack=TEMPLATE_PACK, **kwargs):
"""
Render the link for the tab-pane. It must be called after render so
``css_class`` is updated with ``active`` class name if needed.
"""
link_template = self.link_template % template_pack
return render_to_string(link_template,
{
'link': self,
'item_has_errors': self.has_errors(form)
})
class AccordionHolder(crispy_forms_bootstrap.Accordion):
"""
Accordion items holder object to wrap Accordion item objects in a
container:
.. code-block:: python
AccordionHolder(
AccordionItem("group name", "form_field_1", "form_field_2"),
AccordionItem("another group name", "form_field"),
)
``AccordionHolder`` direct children should allways be a ``AccordionItem``
layout item.
A random id is builded for the accordion holder if you don't define it
using ``css_id`` argument.
The first ``AccordionItem`` containing a field error will be marked as
*active* if any, else this will be just the first ``AccordionItem``.
"""
template = "%s/layout/accordion-holder.html"
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK,
**kwargs):
"""
Re-implement almost the same code from crispy_forms but using
``form`` instance to catch field errors.
"""
content = ''
# accordion group needs the parent div id to set `data-parent` (I don't
# know why). This needs to be a unique id
if not self.css_id:
self.css_id = "-".join(["accordion",
str(randint(1000, 9999))])
# Active first 'AccordionItem' containing a field error if any, else
# active first holder item
self.open_target_group_for_form(form)
for group in self.fields:
group.data_parent = self.css_id
group.item_has_errors = any([fieldname_error for fieldname_error in
form.errors.keys()
if fieldname_error in group])
content += render_field(
group, form, form_style, context, template_pack=template_pack,
**kwargs
)
template = self.get_template_name(template_pack)
context.update({'accordion': self, 'content': content})
return render_to_string(template, context.flatten())
class AccordionItem(crispy_forms_bootstrap.AccordionGroup):
"""
Accordion item object. It wraps given fields inside an accordion
tab. It takes accordion tab name as first argument.
The item name is also slugified to build an id for the tab if you don't
define it using ``css_id`` argument.
Example:
.. code-block:: python
AccordionItem("group name", "form_field_1", "form_field_2")
"""
template = "%s/layout/accordion-item.html"
|