summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFrédéric Péters <fpeters@entrouvert.com>2020-09-08 08:21:47 (GMT)
committerFrédéric Péters <fpeters@entrouvert.com>2020-09-11 05:59:11 (GMT)
commite72dc51a7469815f0c0047a0eba8046a02d327db (patch)
treef66e06c581ee3334becc6c00747fcd7a82630e66
parentf475bc7c3f019112eec6d1a0605a1645f3392441 (diff)
downloadgadjo-e72dc51a7469815f0c0047a0eba8046a02d327db.zip
gadjo-e72dc51a7469815f0c0047a0eba8046a02d327db.tar.gz
gadjo-e72dc51a7469815f0c0047a0eba8046a02d327db.tar.bz2
misc: add django widget type as css class (#46440)v1.6
-rw-r--r--gadjo/templates/gadjo/widget.html3
-rw-r--r--gadjo/templatetags/gadjo.py13
2 files changed, 15 insertions, 1 deletions
diff --git a/gadjo/templates/gadjo/widget.html b/gadjo/templates/gadjo/widget.html
index a937698..899cf3e 100644
--- a/gadjo/templates/gadjo/widget.html
+++ b/gadjo/templates/gadjo/widget.html
@@ -1,6 +1,7 @@
-{% load i18n %}
+{% load gadjo i18n %}
<div class="widget
{{ field.css_classes }}
+ django-{{ field|field_class_name }}
{% if field.errors %}widget-with-error{% endif %}
{% if field.field.required %}widget-required{% else %}widget-optional{% endif %}"
id="{{field.id_for_label}}_p">
diff --git a/gadjo/templatetags/gadjo.py b/gadjo/templatetags/gadjo.py
index 685de2d..4e551c4 100644
--- a/gadjo/templatetags/gadjo.py
+++ b/gadjo/templatetags/gadjo.py
@@ -7,6 +7,7 @@ from xstatic.main import XStatic
from django import template
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
+from django.forms import BoundField
from django.utils.html import escape
from django.utils.http import urlencode
@@ -122,3 +123,15 @@ def querystring(parser, token):
def with_template(form):
form_template = template.loader.get_template('gadjo/form.html')
return form_template.render({'form': form})
+
+
+# pattern to transform Django camel case class names to CSS class names with
+# dashes. (CheckboxInput -> checkbox-input)
+class_name_pattern = re.compile(r'(?<!^)(?=[A-Z])')
+
+
+@register.filter
+def field_class_name(field):
+ if isinstance(field, BoundField):
+ field = field.field
+ return class_name_pattern.sub('-', field.widget.__class__.__name__).lower()