Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Simplify form rendering logic

Patryk Zawadzkicd77f090679b
diff --git a/cart/forms.py b/cart/forms.py
index 092db2cffa..d2e93af2b4 100644
--- a/cart/forms.py
+++ b/cart/forms.py
@@ -1,10 +1,12 @@
-from . import InsufficientStockException
from decimal import Decimal
+
from django import forms
from django.forms.formsets import BaseFormSet
from django.utils.translation import pgettext, ugettext
from product.models import StockedProduct
+from . import InsufficientStockException
+
class QuantityField(forms.DecimalField):
diff --git a/saleor/templates/bootstrap/_field_horizontal.html b/saleor/templates/bootstrap/_field_horizontal.html
index 44a29f7d7c..c0de09d4fc 100644
--- a/saleor/templates/bootstrap/_field_horizontal.html
+++ b/saleor/templates/bootstrap/_field_horizontal.html
@@ -1,3 +1,4 @@
+{% load render_widget from bootstrap %}
{% if bound_field.is_hidden %}
{{ bound_field }}
{% else %}
@@ -6,52 +7,24 @@
<label class="col-lg-2 control-label" for="{{ bound_field.html_name }}_id">{{ bound_field.label }}</label>
{% endif %}
<div class="col-lg-10{% if not show_label %} col-lg-offset-2{% endif %}">
- {% 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 }}"
- class="form-control"
- />
- {% endif %}
- {% if input_type == "textarea" %}
- <textarea name="{{ bound_field.html_name }}"
- id="id_{{ bound_field.html_name }}"
- cols="40"
- rows="10"
- class="form-control"
- >{{ bound_field.value|default_if_none:"" }}</textarea>
+ {% if input_type == "input" or input_type == "textarea" or input_type == "select" %}
+ {% render_widget bound_field class="form-control" %}
{% 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 }}"
- class="form-control"
- >
+ {% render_widget bound_field class="form-control" %}
{{ bound_field.label }}
- </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 }}"
- class="form-control"
- >
+ <input type="radio" value="{{ value }}" name="{{ bound_field.html_name }}" id="id_{{ bound_field.html_name }}_{{ forloop.counter0 }}" class="form-control">
{{ name }}
- </label>
+ </label>
{% endif %}
- {% endfor %}
- {% endif %}
- {% if input_type == "select" %}
- {{ bound_field }}
+ {% endfor %}
{% endif %}
{% if bound_field.help_text %}
diff --git a/saleor/templates/bootstrap/_field_vertical.html b/saleor/templates/bootstrap/_field_vertical.html
index 0240042423..daf2d1ac33 100644
--- a/saleor/templates/bootstrap/_field_vertical.html
+++ b/saleor/templates/bootstrap/_field_vertical.html
@@ -1,3 +1,4 @@
+{% load render_widget from bootstrap %}
{% if bound_field.is_hidden %}
{{ bound_field }}
{% else %}
@@ -6,52 +7,24 @@
<label class="control-label" for="{{ bound_field.html_name }}_id">{{ bound_field.label }}</label>
{% endif %}
<div>
- {% 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 }}"
- class="form-control"
- />
- {% endif %}
- {% if input_type == "textarea" %}
- <textarea name="{{ bound_field.html_name }}"
- id="id_{{ bound_field.html_name }}"
- cols="40"
- rows="10"
- class="form-control"
- >{{ bound_field.value|default_if_none:"" }}</textarea>
+ {% if input_type == "input" or input_type == "textarea" or input_type == "select" %}
+ {% render_widget bound_field class="form-control" %}
{% 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 }}"
- class="form-control"
- >
+ {% render_widget bound_field class="form-control" %}
{{ bound_field.label }}
- </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 }}"
- class="form-control"
- >
+ <input type="radio" value="{{ value }}" name="{{ bound_field.html_name }}" id="id_{{ bound_field.html_name }}_{{ forloop.counter0 }}" class="form-control">
{{ name }}
- </label>
+ </label>
{% endif %}
- {% endfor %}
- {% endif %}
- {% if input_type == "select" %}
- {{ bound_field }}
+ {% endfor %}
{% endif %}
{% if bound_field.help_text %}
diff --git a/saleor/templatetags/bootstrap.py b/saleor/templatetags/bootstrap.py
index 9531ae9642..d6eb304e8c 100644
--- a/saleor/templatetags/bootstrap.py
+++ b/saleor/templatetags/bootstrap.py
@@ -64,3 +64,7 @@ def as_horizontal_form(obj, show_label=True):
def as_vertical_form(obj, show_label=True):
return as_bootstrap(obj=obj, show_label=show_label,
template=TEMPLATE_VERTICAL)
+
+def render_widget(obj, **attrs):
+ return obj.as_widget(attrs=attrs)