diff --git a/ippisite/ippidb/templatetags/customtags.py b/ippisite/ippidb/templatetags/customtags.py index 7e4cec696520160f55bd65b5f0efd62c7fdc4352..b62b0c21ab91ec09d5f4cfe76e345f5a88dfd4fe 100644 --- a/ippisite/ippidb/templatetags/customtags.py +++ b/ippisite/ippidb/templatetags/customtags.py @@ -21,12 +21,16 @@ def bootstrap(object): if isinstance(object, BoundField): field = object attrs = field.field.widget.attrs + # get the class specified in the code css_classes = set(attrs.get("class", "").split(" ")) + # if the field has errors, we add bootstrap classes if field.errors: css_classes.add("is-invalid") + # we propagate the "required" attribute if field.field.required: attrs['required'] = 'required' if isinstance(field.field.widget, widgets.CheckboxInput): + # if it is a checkbox, we the classes are not the same wrapping_classes = "form-check" label_classes = "form-check-label" css_classes.add("form-check-input") @@ -35,21 +39,29 @@ def bootstrap(object): # label_classes = "form-check-label" # css_classes.add("form-check-input") else: + # usual classes wrapping_classes = "input_field form-group" label_classes = "form-control-placeholder" css_classes.add("form-control") if field.name == "DELETE": + # HACK : If the field is the DELETE button of the formset attrs["onchange"] = "formsetItemDelete(this);" wrapping_classes += " formset-item-delete-host" css_classes.add("formset-item-delete") + + # overwrite the css classes attrs["class"] = " ".join(css_classes) + + # render the field ret.append('<div class="%s">' % wrapping_classes) ret.append(field.as_widget(attrs=attrs)) ret.append("""<label class="%s">%s</label>""" % (label_classes, field.label)) if field.errors: + # render its errors for e in field.errors: ret.append("""<div class="invalid-feedback">%s</div>""" % e) ret.append('</div>') + # return it as safe html code return mark_safe("".join(ret)) else: logger.error("Can't bootstrapize object of class %s" % str(type(object)))