Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add simple templatetag for bootstrap forms
Michal Ociepkaa30dadf4d581
diff --git a/saleor/templates/bootstrap/_field.html b/saleor/templates/bootstrap/_field.htmlnew file mode 100644index 0000000000..a175bc54b0--- /dev/null+++ b/saleor/templates/bootstrap/_field.html@@ -0,0 +1,60 @@+{% if bound_field.is_hidden %}+{{ bound_field }}+{% else %}+<div class="control-group {{ bound_field.html_name }}{% if bound_field.errors %} error{% endif %}{% if bound_field.field.required %} required{% endif %}">+ {% if show_label %}+ <label class="control-label" for="{{ bound_field.html_name }}_id">{{ bound_field.label }}</label>+ {% endif %}+ <div class="controls">+ {% if input_type == "input" %}+ <input type="{{ bound_field.field.widget.input_type }}"+ value="{{ bound_field.value|default_if_none:"" }}"+ name="{{ bound_field.html_name }}"+ id="id_{{ bound_field.html_name }}"+ />+ {% endif %}+ {% if input_type == "textarea" %}+ <textarea name="{{ bound_field.html_name }}"+ id="id_{{ bound_field.html_name }}"+ cols="40"+ rows="10"+ >{{ bound_field.value|default_if_none:"" }}</textarea>+ {% endif %}+ {% if input_type == "checkbox" %}+ <label class="checkbox" for="id_{{ bound_field.html_name }}">+ <input type="checkbox"+ {% if bound_field.value %}+ checked="checked"+ {% endif %}+ name="{{ bound_field.html_name }}"+ id="id_{{ bound_field.html_name }}"+ >+ {{ bound_field.label }}+ </label>+ {% endif %}+ {% if input_type == "radio" %}+ {% for value, name in bound_field.field.choices %}+ {% if value %}+ <label class="radio" for="id_{{ bound_field.html_name }}_{{ forloop.counter0 }}">+ <input type="radio"+ value="{{ value }}"+ name="{{ bound_field.html_name }}"+ id="id_{{ bound_field.html_name }}_{{ forloop.counter0 }}"+ >+ {{ name }}+ </label>+ {% endif %}+ {% endfor %}+ {% endif %}+ {% if input_type == "select" %}+ {{ bound_field }}+ {% endif %}++ {% if bound_field.errors %}+ {% for error in bound_field.errors %}+ <span class="help-inline error">{{ error }}</span>+ {% endfor %}+ {% endif %}+ </div>+</div>+{% endif %}diff --git a/saleor/templates/bootstrap/_non_field_errors.html b/saleor/templates/bootstrap/_non_field_errors.htmlnew file mode 100644index 0000000000..b4526450be--- /dev/null+++ b/saleor/templates/bootstrap/_non_field_errors.html@@ -0,0 +1,5 @@+<div class="alert alert-block alert-error">+ {% for error in errors %}+ <p>{{ error }}</p>+ {% endfor %}+</div>diff --git a/saleor/templatetags/__init__.py b/saleor/templatetags/__init__.pynew file mode 100644index 0000000000..e69de29bb2diff --git a/saleor/templatetags/bootstrap.py b/saleor/templatetags/bootstrap.pynew file mode 100644index 0000000000..9257cb6894--- /dev/null+++ b/saleor/templatetags/bootstrap.py@@ -0,0 +1,54 @@+from django import forms+from django.forms.forms import BoundField, BaseForm+from django.forms.util import ErrorList+from django.template import Library, Context, TemplateSyntaxError, loader+from django.utils.safestring import mark_safe+++register = Library()+field_template = loader.get_template('bootstrap/_field.html')+errors_template = loader.get_template('bootstrap/_non_field_errors.html')+++def render_non_field_errors(errors):+ if errors:+ context = Context({'errors': errors})+ return errors_template.render(context)++ return ''+++def render_field(bound_field, show_label):+ widget = bound_field.field.widget++ if isinstance(widget, forms.RadioSelect):+ input_type = 'radio'+ elif isinstance(widget, forms.Select):+ input_type = 'select'+ elif isinstance(widget, forms.Textarea):+ input_type = 'textarea'+ elif isinstance(widget, forms.CheckboxInput):+ input_type = 'checkbox'+ else:+ input_type = 'input'++ context = Context({'bound_field': bound_field,+ 'input_type': input_type,+ 'show_label': show_label})++ return field_template.render(context)+++def as_bootstrap(obj, show_label=True):+ if isinstance(obj, BoundField):+ return render_field(obj, show_label)+ elif isinstance(obj, ErrorList):+ return render_non_field_errors(obj)+ elif isinstance(obj, BaseForm):+ non_field_errors = render_non_field_errors(obj.non_field_errors())+ form = ''.join([render_field(field, show_label) for field in obj])+ return mark_safe(non_field_errors + form)+ else:+ raise TemplateSyntaxError('Filter accepts form, field and non fields '+ 'errors.')