Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

New custom tag to render form elements, replacing old include snippet

Roland Geiderc094df254d06
diff --git a/manager/templates/form.html b/manager/templates/form.html
index 3ad47f04d..99efb2e1d 100644
--- a/manager/templates/form.html
+++ b/manager/templates/form.html
@@ -1,6 +1,7 @@
{% extends "base.html" %}
{% load i18n %}
-{% load staticfiles %}
+{% load staticfiles %}
+{% load manager_extras %}
{% block header %}
@@ -35,9 +36,9 @@
<table>
{% for current_field in form_fields %}
{% if current_field.name in select_lists %}
- {% include "form_snippet.html" with field=current_field css_class="ym-fbox-select" %}
+ {% yaml_form_field current_field "ym-fbox-select" %}
{% else %}
- {% include "form_snippet.html" with field=current_field %}
+ {% yaml_form_field current_field %}
{% endif %}
{% endfor %}
<tr>
diff --git a/manager/templates/set/edit.html b/manager/templates/set/edit.html
index f09138bf4..ddb97f285 100644
--- a/manager/templates/set/edit.html
+++ b/manager/templates/set/edit.html
@@ -1,6 +1,8 @@
{% extends "base.html" %}
{% load i18n %}
{% load staticfiles %}
+{% load manager_extras %}
+
<!--
Title
@@ -33,10 +35,8 @@ function custom_page_init()
{% csrf_token %}
<table>
-
- {% include "form_snippet.html" with field=set_form.exercises %}
- {% include "form_snippet.html" with field=set_form.sets %}
-
+ {% yaml_form_field set_form.exercises %}
+ {% yaml_form_field set_form.sets %}
<tr>
<td colspan="2">
<input type="submit" value="{% trans 'Save' %}" id="form-save">
diff --git a/manager/templates/setting/edit.html b/manager/templates/setting/edit.html
index 4d715cdaa..760250fd1 100644
--- a/manager/templates/setting/edit.html
+++ b/manager/templates/setting/edit.html
@@ -1,5 +1,7 @@
{% extends "base.html" %}
{% load i18n %}
+{% load manager_extras %}
+
<!--
Title
@@ -25,8 +27,7 @@
<table>
{% for form in setting_form %}
{{ form.id }} <!-- Needed for FormSets -->
- {% include "form_snippet.html" with field=form.reps %}
- <!--{% include "form_snippet.html" with field=form.comment %}-->
+ {% yaml_form_field form.reps %}
{% endfor %}
<tr>
<td><input type="submit" value="{% trans 'Save' %}" /></td>
@@ -43,4 +44,4 @@
Side bar
-->
{% block sidebar %}
-{% endblock %}
\ No newline at end of file
+{% endblock %}
diff --git a/manager/templates/form_snippet.html b/manager/templates/tags/yaml_form_element.html
similarity index 87%
rename from manager/templates/form_snippet.html
rename to manager/templates/tags/yaml_form_element.html
index 821f67426..404b0169c 100644
--- a/manager/templates/form_snippet.html
+++ b/manager/templates/tags/yaml_form_element.html
@@ -6,7 +6,7 @@
{% if field.errors %}</span></div>{% endif %}
</td>
<td>
- <div class="{{css_class|default:'ym-fbox-text'}} {% if field.errors %}ym-error{% endif %}">
+ <div class="{{css_class}} {% if field.errors %}ym-error{% endif %}">
{{ field }}
</div>
diff --git a/manager/templates/user/change_password.html b/manager/templates/user/change_password.html
index ea1ee67f8..e73967155 100644
--- a/manager/templates/user/change_password.html
+++ b/manager/templates/user/change_password.html
@@ -1,5 +1,6 @@
{% extends "base.html" %}
{% load i18n %}
+{% load manager_extras %}
{% block title %}{% trans "Change password" %}{% endblock %}
@@ -14,9 +15,9 @@
{% csrf_token %}
{{ form.non_field_errors }}
<table>
-{% include "form_snippet.html" with field=form.old_password %}
-{% include "form_snippet.html" with field=form.new_password1 %}
-{% include "form_snippet.html" with field=form.new_password2 %}
+{% yaml_form_field form.old_password %}
+{% yaml_form_field form.new_password1 %}
+{% yaml_form_field form.new_password2 %}
<tr>
<td colspan="2">
<input type="submit" value="{% trans 'Change password' %}" id="form-save">
diff --git a/manager/templates/user/login.html b/manager/templates/user/login.html
index 63cb3e3c2..9cc9428cd 100644
--- a/manager/templates/user/login.html
+++ b/manager/templates/user/login.html
@@ -1,6 +1,7 @@
{% extends "base.html" %}
{% load i18n %}
{% load staticfiles %}
+{% load manager_extras %}
{% block title %} {% trans "Login" %}{% endblock %}
@@ -23,8 +24,8 @@
</div>
<input type="hidden" name="next" value="{{next}}">
<table>
-{% include "form_snippet.html" with field=form.username %}
-{% include "form_snippet.html" with field=form.password %}
+{% yaml_form_field form.username %}
+{% yaml_form_field form.password %}
<tr>
<td colspan="2">
<input type="submit" value="{% trans 'Login' %}" id="form-save">
diff --git a/manager/templates/user/password_reset_confirm.html b/manager/templates/user/password_reset_confirm.html
index e517dd5e9..01dd3cecc 100644
--- a/manager/templates/user/password_reset_confirm.html
+++ b/manager/templates/user/password_reset_confirm.html
@@ -1,5 +1,6 @@
{% extends "base.html" %}
{% load i18n %}
+{% load manager_extras %}
{% comment %}Translators: Don't translate, this is already done by Django{% endcomment %}
{% block title %}{% trans "Password reset confirmation" %}{% endblock %}
@@ -20,8 +21,8 @@
{% csrf_token %}
<table>
-{% include "form_snippet.html" with field=form.new_password1 %}
-{% include "form_snippet.html" with field=form.new_password2 %}
+ {% yaml_form_field form.new_password1 %}
+ {% yaml_form_field form.new_password2 %}
<tr>
<td colspan="2">
{% comment %}Translators: Don't translate, this is already done by Django{% endcomment %}
diff --git a/manager/templates/user/password_reset_form.html b/manager/templates/user/password_reset_form.html
index af24d894a..f89edba1a 100644
--- a/manager/templates/user/password_reset_form.html
+++ b/manager/templates/user/password_reset_form.html
@@ -1,5 +1,6 @@
{% extends "base.html" %}
{% load i18n %}
+{% load manager_extras %}
{% comment %}Translators: Don't translate, this is already done by Django{% endcomment %}
{% block title %}{% trans "Password reset" %}{% endblock %}
@@ -15,7 +16,7 @@
{% csrf_token %}
{{ form.non_field_errors }}
<table>
-{% include "form_snippet.html" with field=form.email %}
+{% yaml_form_field form.emaill %}
<tr>
<td colspan="2">
{% comment %}Translators: Don't translate, this is already done by Django{% endcomment %}
diff --git a/manager/templates/user/preferences.html b/manager/templates/user/preferences.html
index f7bcdc9a0..b32b6aa0f 100644
--- a/manager/templates/user/preferences.html
+++ b/manager/templates/user/preferences.html
@@ -1,6 +1,7 @@
{% extends "base.html" %}
{% load i18n %}
{% load staticfiles %}
+{% load manager_extras %}
{% block title %}{% trans "Preferences" %}{% endblock %}
@@ -15,8 +16,8 @@
{% csrf_token %}
{{ form.non_field_errors }}
<table>
-{% include "form_snippet.html" with field=form.show_comments %}
-{% include "form_snippet.html" with field=form.show_english_ingredients %}
+{% yaml_form_field form.show_comments %}
+{% yaml_form_field form.show_english_ingredients %}
</table>
@@ -24,7 +25,7 @@
{{ email_form.non_field_errors }}
<table>
-{% include "form_snippet.html" with field=email_form.email %}
+{% yaml_form_field form.email_form.email %}
<tr>
<td colspan="2">
<input type="submit" value="{% trans 'Save' %}">
diff --git a/manager/templates/user/registration.html b/manager/templates/user/registration.html
index 8474c673c..dab3bbe83 100644
--- a/manager/templates/user/registration.html
+++ b/manager/templates/user/registration.html
@@ -1,5 +1,6 @@
{% extends "base.html" %}
{% load i18n %}
+{% load manager_extras %}
{% block title %} {% trans "Login" %}{% endblock %}
@@ -13,10 +14,10 @@
{{ form.non_field_errors }}
<input type="hidden" name="redirect_target" value="{{redirect_target}}">
<table>
-{% include "form_snippet.html" with field=form.username %}
-{% include "form_snippet.html" with field=form.password1 %}
-{% include "form_snippet.html" with field=form.password2 %}
-{% include "form_snippet.html" with field=form.email %}
+{% yaml_form_field form.username %}
+{% yaml_form_field form.password1 %}
+{% yaml_form_field form.password2 %}
+{% yaml_form_field form.email %}
<tr>
<td colspan="2">
<input type="submit" value="{% trans 'Save' %}" id="form-save">
diff --git a/manager/templatetags/manager_extras.py b/manager/templatetags/manager_extras.py
index dc4756b44..e14e19a13 100644
--- a/manager/templatetags/manager_extras.py
+++ b/manager/templatetags/manager_extras.py
@@ -35,3 +35,13 @@ def render_weight_log(log):
"""
return {'log': log}
+
+
[email protected]_tag('tags/yaml_form_element.html')
+def yaml_form_field(field, css_class = 'ym-fbox-text'):
+ """
+ Renders a form field in a <tr> with all necessary CSS
+ """
+
+ return {'field': field,
+ 'css_class': css_class}
diff --git a/nutrition/templates/edit_meal_item.html b/nutrition/templates/edit_meal_item.html
index 33efb98b1..d9c0d6855 100644
--- a/nutrition/templates/edit_meal_item.html
+++ b/nutrition/templates/edit_meal_item.html
@@ -1,6 +1,8 @@
{% extends "base.html" %}
{% load i18n %}
{% load staticfiles %}
+{% load manager_extras %}
+
{% block title %}{% trans "Edit meal item" %}{% endblock %}
@@ -27,11 +29,11 @@ $(document).ready(function() {
{% csrf_token %}
{{ form.non_field_errors }}
<table>
- {% include "form_snippet.html" with field=form.amount_gramm %}
+ {% yaml_form_field form.amount_gramm %}
{% if js_active %}
<input type="hidden" id="id_ingredient" name="ingredient" value="{{ingredient}}">
{% else %}
- {% include "form_snippet.html" with field=form.ingredient css_class="ym-fbox-select" %}
+ {% yaml_form_field form.amount_gramm form.ingredient "ym-fbox-select"%}
{% endif %}
<tr>
<td style="text-align:right;">
diff --git a/weight/templates/edit.html b/weight/templates/edit.html
index 10efae097..16e5f274c 100644
--- a/weight/templates/edit.html
+++ b/weight/templates/edit.html
@@ -1,6 +1,7 @@
{% extends "base.html" %}
{% load i18n %}
{% load staticfiles %}
+{% load manager_extras %}
{% block title %}{% trans "Weight entry" %}{% endblock %}
@@ -33,8 +34,8 @@ $(document).ready(function () {
{% csrf_token %}
{{ weight_form.non_field_errors }}
<table>
- {% include "form_snippet.html" with field=weight_form.weight %}
- {% include "form_snippet.html" with field=weight_form.creation_date %}
+ {% yaml_form_field weight_form.weight %}
+ {% yaml_form_field weight_form.creation_date %}
<tr>
<td><input type="submit" value="{% trans "Save" %}" id="form-save"></td>
<td></td>