Self hosted FLOSS fitness/workout, nutrition and weight tracker
Start replacing own form logic with crispy forms
Rolandde08dc746f91
diff --git a/requirements.txt b/requirements.txtindex 9d79570e0..b739a5fe3 100644--- a/requirements.txt+++ b/requirements.txt@@ -12,6 +12,7 @@ django-bower==5.2.0django-formtools~=2.0django-recaptcha==2.0.6Django~=3.0+django-crispy-forms~=1.9django_compressor~=2.4django_extensions~=2.1django-sortedm2m~=3.0diff --git a/wger/core/static/js/wger-core.js b/wger/core/static/js/wger-core.jsindex 4ea5597cb..ab6292a30 100644--- a/wger/core/static/js/wger-core.js+++ b/wger/core/static/js/wger-core.js@@ -326,7 +326,7 @@ function wgerFormModalDialog() {$('#ajax-info-title').html('Loading...');$('#wger-ajax-info').modal('show');- $ajaxInfoContent.load(targetUrl + ' .form-horizontal',+ $ajaxInfoContent.load(targetUrl + ' .wger-form',function (responseText, textStatus, XMLHttpRequest) {var $ajaxInfoTitle;var modalTitle;@@ -362,7 +362,7 @@ function wgerFormModalDialog() {// If there isn't assume all was saved correctly and load that result into the// page's main DIV (#main-content). All this must be done like this because there// doesn't seem to be any reliable and easy way to detect redirects with AJAX.- if ($(responseText).find('.form-horizontal').length > 0) {+ if ($(responseText).find('.wger-form').length > 0) {modalDialogFormEdit();}});diff --git a/wger/core/templates/delete.html b/wger/core/templates/delete.htmlindex fe14aaa4a..3c8f73121 100644--- a/wger/core/templates/delete.html+++ b/wger/core/templates/delete.html@@ -13,7 +13,7 @@{% block content %}<form action="{{form_action}}"method="post"- class="form-horizontal">+ class="wger-form">{% csrf_token %}<div class="form-group">diff --git a/wger/core/templates/form.html b/wger/core/templates/form.htmlindex c6897f054..5c997eab7 100644--- a/wger/core/templates/form.html+++ b/wger/core/templates/form.html@@ -1,29 +1,19 @@{% extends extend_template %}-{% load i18n static wger_extras %}+{% load i18n static crispy_forms_tags %}{% block header %}--{{ form.media }}--{% if custom_js %}-<script type="text/javascript">- {{custom_js|safe}}-</script>-{% endif %}+ {{ form.media }}+ {% if custom_js %}+ <script type="text/javascript">+ {{custom_js|safe}}+ </script>+ {% endif %}{% endblock %}-{% block title %}{{title}}{% endblock %}-{% block content %}-<form action="{{form_action}}"- method="post"- class="form-horizontal"- role="form"- {% if enctype %}enctype="{{enctype}}"{% endif %}>- {% render_form_fields form submit_text %}-</form>+ {% crispy form %}{% endblock %}diff --git a/wger/core/templates/rest_framework/api.html b/wger/core/templates/rest_framework/api.htmlindex e40be2f31..19f6ca8d7 100644--- a/wger/core/templates/rest_framework/api.html+++ b/wger/core/templates/rest_framework/api.html@@ -112,7 +112,7 @@{% if post_form or raw_data_post_form %}<h4>Edit form</h4>{% with form=raw_data_post_form %}- <form action="{{ request.get_full_path }}" method="POST" class="form-horizontal" role="form">+ <form action="{{ request.get_full_path }}" method="POST" class="wger-form" role="form"><fieldset>{% render_form_fields form 'POST' %}</fieldset>@@ -123,7 +123,7 @@{% if put_form or raw_data_put_form or raw_data_patch_form %}<h4>Edit form</h4>{% with form=raw_data_put_or_patch_form %}- <form action="{{ request.get_full_path }}" method="POST" class="form-horizontal" role="form">+ <form action="{{ request.get_full_path }}" method="POST" class="wger-form" role="form"><fieldset>{% csrf_token %}{% for field in form %}diff --git a/wger/core/templates/user/change_password.html b/wger/core/templates/user/change_password.htmlindex c6bd36a22..4795a4f7f 100644--- a/wger/core/templates/user/change_password.html+++ b/wger/core/templates/user/change_password.html@@ -7,7 +7,7 @@{% block content %}<form action="{% url 'core:user:change-password' %}"method="post"- class="form-horizontal"+ class="wger-form"role="form">{% trans 'Change password' as submit_text %}diff --git a/wger/core/templates/user/delete_account.html b/wger/core/templates/user/delete_account.htmlindex 24e5735b0..bd311e737 100644--- a/wger/core/templates/user/delete_account.html+++ b/wger/core/templates/user/delete_account.html@@ -11,7 +11,7 @@<form action="{{form_action}}"method="post"- class="form-horizontal"+ class="wger-form"role="form"><div class="panel panel-danger"><div class="panel-heading">diff --git a/wger/core/templates/user/login.html b/wger/core/templates/user/login.htmlindex 8a141a702..0ac174336 100644--- a/wger/core/templates/user/login.html+++ b/wger/core/templates/user/login.html@@ -10,7 +10,7 @@{% block content %}<form action="{% url 'core:user:login' %}{% if next %}?next={{next}}{% endif %}"method="post"- class="form-horizontal">+ class="wger-form">{% trans 'Login' as submit_text %}{% render_form_fields form submit_text %}</form>diff --git a/wger/core/templates/user/password_reset_confirm.html b/wger/core/templates/user/password_reset_confirm.htmlindex 0faf7cefb..c2ff32cc0 100644--- a/wger/core/templates/user/password_reset_confirm.html+++ b/wger/core/templates/user/password_reset_confirm.html@@ -13,7 +13,7 @@<form action=""method="post"- class="form-horizonal">+ class="wger-form">{% trans 'Change my password' as submit_text %}{% render_form_fields form submit_text %}</form>diff --git a/wger/core/templates/user/password_reset_form.html b/wger/core/templates/user/password_reset_form.htmlindex 34dfb7739..42368421a 100644--- a/wger/core/templates/user/password_reset_form.html+++ b/wger/core/templates/user/password_reset_form.html@@ -7,7 +7,7 @@{% block content %}<form action=""method="post"- class="form-horizonal">+ class="wger-form">{% trans 'Change my password' as submit_text %}{% render_form_fields form submit_text %}</form>diff --git a/wger/core/templates/user/preferences.html b/wger/core/templates/user/preferences.htmlindex 315e60a17..d1ddd2872 100644--- a/wger/core/templates/user/preferences.html+++ b/wger/core/templates/user/preferences.html@@ -18,7 +18,7 @@ $(function() {{% block content %}<form action="{% url 'core:user:preferences' %}"method="post"- class="form-horizontal">+ class="wger-form">{% csrf_token %}{{ form.non_field_errors }}diff --git a/wger/email/templates/email/gym/form.html b/wger/email/templates/email/gym/form.htmlindex ec6f20c2d..62634234e 100644--- a/wger/email/templates/email/gym/form.html+++ b/wger/email/templates/email/gym/form.html@@ -18,7 +18,7 @@{% block content %}<form action="{{ form_action }}"method="post"- class="form-horizontal">+ class="wger-form">{% render_form_fields form "Vorschau erzeugen" %}</form>{% endblock %}@@ -30,4 +30,4 @@<li>{% trans "You will receive a preview email for you to review" %}</li><li>{% trans "You can update and correct the content as often as you need before sending the emails" %}</li></ol>-{% endblock %}\ No newline at end of file+{% endblock %}diff --git a/wger/email/templates/email/gym/preview.html b/wger/email/templates/email/gym/preview.htmlindex 70aa29ac0..d6fce0461 100644--- a/wger/email/templates/email/gym/preview.html+++ b/wger/email/templates/email/gym/preview.html@@ -51,7 +51,7 @@<h3>{% trans 'Correction' %}</h3><form action="{{ form_action }}"method="post"- class="form-horizontal">+ class="wger-form">{% render_form_fields form 'Correction' %}</form>{% endblock %}@@ -63,4 +63,4 @@<li>{% trans "You will receive a preview email for you to review" %}</li><li>{% trans "You can update and correct the content as often as you need before sending the emails" %}</li></ol>-{% endblock %}\ No newline at end of file+{% endblock %}diff --git a/wger/manager/templates/day/log.html b/wger/manager/templates/day/log.htmlindex fd1ee6d40..130035d66 100644--- a/wger/manager/templates/day/log.html+++ b/wger/manager/templates/day/log.html@@ -36,7 +36,7 @@ $(document).ready(function () {{# Content #}{# #}{% block content %}-<form action="{{form_action}}" method="POST" class="form-horizontal">+<form action="{{form_action}}" method="POST" class="wger-form">{% csrf_token %}{{formset.management_form}}diff --git a/wger/manager/templates/mobile/schedule/view.html b/wger/manager/templates/mobile/schedule/view.htmlindex 1844a7963..23d3ed9c7 100644--- a/wger/manager/templates/mobile/schedule/view.html+++ b/wger/manager/templates/mobile/schedule/view.html@@ -193,7 +193,7 @@ an appointment for each training day with the appropriate exercises.{% endblockt<h4 class="modal-title">{% trans "Download as PDF" %}</h4></div><div class="modal-body">- <form class="form-horizontal">+ <form class="wger-form"><div id="pdf-download-info" data-schedule-id="{{ schedule.id }}" data-uid="{{ uid }}" data-token="{{ token }}"></div><div class="form-group"><div>diff --git a/wger/manager/templates/mobile/set/add.html b/wger/manager/templates/mobile/set/add.htmlindex 981ab29a4..82b10b781 100644--- a/wger/manager/templates/mobile/set/add.html+++ b/wger/manager/templates/mobile/set/add.html@@ -27,7 +27,7 @@ $(document).ready(function() {<form action="{{form_action}}"method="post"- class="form-horizontal">+ class="wger-form">{% csrf_token %}{% render_form_field form.categories_list %}diff --git a/wger/manager/templates/mobile/workout/view.html b/wger/manager/templates/mobile/workout/view.htmlindex 996766313..f6ed3117d 100644--- a/wger/manager/templates/mobile/workout/view.html+++ b/wger/manager/templates/mobile/workout/view.html@@ -159,7 +159,7 @@ an appointment for each training day with the appropriate exercises.{% endblockt<h4 class="modal-title">{% trans "Download as PDF" %}</h4></div><div class="modal-body">- <form class="form-horizontal">+ <form class="wger-form"><div id="pdf-download-info" data-workout-id="{{ workout.id }}" data-uid="{{ uid }}" data-token="{{ token }}"></div><div class="form-group"><div>diff --git a/wger/manager/templates/schedule/view.html b/wger/manager/templates/schedule/view.htmlindex 0233de3be..c3a9bed4f 100644--- a/wger/manager/templates/schedule/view.html+++ b/wger/manager/templates/schedule/view.html@@ -257,7 +257,7 @@ an appointment for each training day with the appropriate exercises.{% endblockt<h4 class="modal-title">{% trans "Download as PDF" %}</h4></div><div class="modal-body">- <form class="form-horizontal">+ <form class="wger-form"><div id="pdf-download-info" data-schedule-id="{{ schedule.id }}" data-uid="{{ uid }}" data-token="{{ token }}"></div><div class="form-group"><div>diff --git a/wger/manager/templates/set/add.html b/wger/manager/templates/set/add.htmlindex 2172985e5..5ba95c3e9 100644--- a/wger/manager/templates/set/add.html+++ b/wger/manager/templates/set/add.html@@ -31,7 +31,7 @@ function wgerCustomPageInit()<form action="{{form_action}}"method="post"- class="form-horizontal">+ class="wger-form">{% csrf_token %}{% render_form_field form.exercises %}diff --git a/wger/manager/templates/set/edit.html b/wger/manager/templates/set/edit.htmlindex 5d056a697..62c80ade6 100644--- a/wger/manager/templates/set/edit.html+++ b/wger/manager/templates/set/edit.html@@ -16,7 +16,7 @@{% block content %}<form action="{{form_action}}"method="post"- class="form-horizontal">+ class="wger-form">{% csrf_token %}<div class="form-group">diff --git a/wger/manager/templates/workout/view.html b/wger/manager/templates/workout/view.htmlindex ebd595529..abb1e3e7a 100644--- a/wger/manager/templates/workout/view.html+++ b/wger/manager/templates/workout/view.html@@ -152,7 +152,7 @@ an appointment for each training day with the appropriate exercises.{% endblockt<h4 class="modal-title">{% trans "Download as PDF" %}</h4></div><div class="modal-body">- <form class="form-horizontal">+ <form class="wger-form"><div id="pdf-download-info" data-workout-id="{{ workout.id }}" data-uid="{{ uid }}" data-token="{{ token }}"></div><div class="form-group"><div>diff --git a/wger/nutrition/templates/bmi/form.html b/wger/nutrition/templates/bmi/form.htmlindex 782a899a6..32a40f2cd 100644--- a/wger/nutrition/templates/bmi/form.html+++ b/wger/nutrition/templates/bmi/form.html@@ -57,7 +57,7 @@ $(document).ready(function () {<form action="{% url 'nutrition:bmi:calculate' %}"method="post"- class="form-horizontal"+ class="wger-form"id="bmi-form">{% trans 'Calculate' as submit_text %}{% render_form_fields form submit_text %}diff --git a/wger/nutrition/templates/ingredient/view.html b/wger/nutrition/templates/ingredient/view.htmlindex ea5f661c9..9b707e72a 100644--- a/wger/nutrition/templates/ingredient/view.html+++ b/wger/nutrition/templates/ingredient/view.html@@ -53,7 +53,7 @@ the search.{% endblocktrans %}<div itemscope itemtype="http://schema.org/NutritionInformation"><meta itemprop="name" content="{{ingredient.name}}" />-<form class="form-horizontal"+<form class="wger-form"method="GET"action="{% url 'api-ingredient-get-values' ingredient.id %}"id="nutritional-values-form"diff --git a/wger/nutrition/templates/meal_item/edit.html b/wger/nutrition/templates/meal_item/edit.htmlindex 7b0e44ac5..df621d094 100644--- a/wger/nutrition/templates/meal_item/edit.html+++ b/wger/nutrition/templates/meal_item/edit.html@@ -22,7 +22,7 @@ $(document).ready(function() {{% block content %}<form action="{{ form_action }}"method="post"- class="form-horizontal">+ class="wger-form">{% csrf_token %}{% render_form_errors form %}{{form.ingredient}}diff --git a/wger/nutrition/templates/rate/form.html b/wger/nutrition/templates/rate/form.htmlindex 676c17a4d..0aa147b05 100644--- a/wger/nutrition/templates/rate/form.html+++ b/wger/nutrition/templates/rate/form.html@@ -28,7 +28,7 @@ at rest. This is the minimum energy needed for the body to function, without anyadditional physical activity.{% endblocktrans %}</p><form action="{% url 'nutrition:calories:bmr' %}"method="post"- class="calories-autoform form-horizontal"+ class="calories-autoform wger-form"id="bmr-form">{% csrf_token %}{% render_form_errors form %}@@ -55,7 +55,7 @@ the additional physical activity and is used to calculate the total energyrequired per day.{% endblocktrans %}</p><form action="{% url 'nutrition:calories:activities' %}"method="post"- class="form-horizontal calories-autoform"+ class="wger-form calories-autoform"id="activities-form">{% csrf_token %}{% render_form_errors form_activities %}@@ -88,7 +88,7 @@ required per day.{% endblocktrans %}</p>needs based on your level of physical activity.{% endblocktrans %}</p><form action=""method="post"- class="form-horizontal calories-autoform"+ class="wger-form calories-autoform"id="base-calories-form">{% csrf_token %}<table class="table">@@ -112,7 +112,7 @@ you might just enter yourself the amount of calories if you know the value.{% en<form action=""method="post"- class="form-horizontal"+ class="wger-form"id="total-calories-form">{% csrf_token %}diff --git a/wger/settings_global.py b/wger/settings_global.pyindex 12d8084e0..e2a7ccaa2 100644--- a/wger/settings_global.py+++ b/wger/settings_global.py@@ -74,6 +74,9 @@ INSTALLED_APPS = (# CSS/JS compressor'compressor',+ # Form renderer helper+ 'crispy_forms',+# REST-API'tastypie','rest_framework',@@ -231,7 +234,6 @@ LOCALE_PATHS = (os.path.join(SITE_ROOT, 'locale'),)-FLAVOURS_STORAGE_BACKEND = 'session'#@@ -279,6 +281,10 @@ CACHES = {}}+#+# Django Crispy Templates+#+CRISPY_TEMPLATE_PACK = 'bootstrap4'## Easy thumbnailsdiff --git a/wger/utils/generic_views.py b/wger/utils/generic_views.pyindex b1f938b85..8942a6420 100644--- a/wger/utils/generic_views.py+++ b/wger/utils/generic_views.py@@ -18,6 +18,8 @@import logging# Django+from crispy_forms.helper import FormHelper+from crispy_forms.layout import Submitfrom django.contrib import messagesfrom django.contrib.auth.mixins import PermissionRequiredMixinfrom django.http import (@@ -146,42 +148,21 @@ class WgerFormMixin(ModelFormMixin):messages = '''''- A message to display on sucess+ A message to display on success'''def get_context_data(self, **kwargs):'''- Set necessary template data to correctly render the form+ Set context data'''- # Call the base implementation first to get a contextcontext = super(WgerFormMixin, self).get_context_data(**kwargs)-- # CSRF token- context.update(csrf(self.request))-context['sidebar'] = self.sidebar- # TODO: change template so it iterates through form and not formfields- context['form_fields'] = context['form']+ context['title'] = self.title# Custom JS code on form (autocompleter, editor, etc.)context['custom_js'] = self.custom_js- # When viewing the page on it's own, this is not necessary, but when- # opening it on a modal dialog, we need to make sure the POST request- # reaches the correct controller- if self.form_action_urlname:- context['form_action'] = reverse(self.form_action_urlname,- kwargs={'pk': self.object.id})- elif self.form_action:- context['form_action'] = self.form_action-- # Set the title- context['title'] = self.title-- # Text used in the submit button- context['submit_text'] = self.submit_text-# Template to extend. For AJAX requests we don't need the rest of the# template, only the formcontext['extend_template'] = 'base_empty.html' if self.request.is_ajax() else 'base.html'@@ -226,6 +207,28 @@ class WgerFormMixin(ModelFormMixin):'''return self.messages+ def get_form(self, form_class=None):+ """Return an instance of the form to be used in this view."""+ form = super(WgerFormMixin, self).get_form(form_class)+ if not hasattr(form, "helper"):+ form.helper = FormHelper()++ form.helper.form_id = 'id-personal-data-form'+ form.helper.form_method = 'post'++ # When viewing the page on it's own, this is not necessary, but when+ # opening it on a modal dialog, we need to make sure the POST request+ # reaches the correct controller+ if self.form_action_urlname:+ form.helper.form_action = reverse(self.form_action_urlname,+ kwargs={'pk': self.object.id})+ elif self.form_action:+ form.helper.form_action = self.form_action++ form.helper.add_input(Submit('submit', self.submit_text, css_class='btn-success btn-block'))+ form.helper.form_class = 'wger-form'+ return form+def form_invalid(self, form):'''Log form errors to the consolediff --git a/wger/weight/templates/import_csv_form.html b/wger/weight/templates/import_csv_form.htmlindex 2dae96c35..c23985d60 100644--- a/wger/weight/templates/import_csv_form.html+++ b/wger/weight/templates/import_csv_form.html@@ -7,7 +7,7 @@{% block content %}<form action="{{form_action}}"method="post"- class="form-horizontal">+ class="wger-form">{% trans 'Submit' as submit_text %}{% render_form_fields form submit_text %}diff --git a/wger/weight/templates/import_csv_preview.html b/wger/weight/templates/import_csv_preview.htmlindex e667d0635..5b3c9e45a 100644--- a/wger/weight/templates/import_csv_preview.html+++ b/wger/weight/templates/import_csv_preview.html@@ -64,7 +64,7 @@ will simply be ignored. It's up to you to decide whether they are useful<form action="{{form_action}}"method="post"- class="form-horizontal">+ class="wger-form"><input type="hidden" name="{{ stage_field }}" value="2" /><input type="hidden" name="{{ hash_field }}" value="{{ hash_value }}" />