From 794706003bd4e43621a02232afe8ae1ea91362ac Mon Sep 17 00:00:00 2001
From: Art Lukyanchyk <artiom.lukyanchyk@hs-hannover.de>
Date: Wed, 25 Oct 2017 17:10:22 +0200
Subject: [PATCH] Template tag bulma_form ensures that forms are only submitted
 once.

---
 hshassets/assets/js/common.js                  | 6 ++++++
 hshassets/forms/utils.py                       | 5 +++--
 hshassets/static/hshassets/js/common.js        | 7 +++++++
 hshassets/static/hshassets/js/common.min.js    | 1 +
 hshassets/templates/hshassets/layout/base.html | 2 ++
 hshassets/templatetags/bulma.py                | 4 ++--
 6 files changed, 21 insertions(+), 4 deletions(-)
 create mode 100644 hshassets/assets/js/common.js
 create mode 100644 hshassets/static/hshassets/js/common.js
 create mode 100644 hshassets/static/hshassets/js/common.min.js

diff --git a/hshassets/assets/js/common.js b/hshassets/assets/js/common.js
new file mode 100644
index 00000000..f6462f99
--- /dev/null
+++ b/hshassets/assets/js/common.js
@@ -0,0 +1,6 @@
+
+$('[data-submit-only-once="true"]').submit(function(event){
+    $(this).find("button").addClass("is-loading");
+    $(this).find(":submit").attr("disabled","disabled");
+});
+
diff --git a/hshassets/forms/utils.py b/hshassets/forms/utils.py
index efc23b88..90dd7bc4 100644
--- a/hshassets/forms/utils.py
+++ b/hshassets/forms/utils.py
@@ -103,11 +103,12 @@ class BulmaFieldMarkup(object):
         return cls.div_control(content)
 
 
-def render_form_generics(context, rendered_fields, rendered_errors, submit_text, submit_class):
+def render_form_generics(context, rendered_fields, rendered_errors, submit_text, submit_class, submit_only_once=True):
     csrf_field = defaulttags.CsrfTokenNode().render(context)
+    submit_only_once = "true" if submit_only_once else "false"
     return format_html(
         """
-        <form method="post">
+        <form method="post" data-submit-only-once="{submit_only_once}">
             {rendered_errors}
             {rendered_fields}
             {csrf_field}
diff --git a/hshassets/static/hshassets/js/common.js b/hshassets/static/hshassets/js/common.js
new file mode 100644
index 00000000..2824f197
--- /dev/null
+++ b/hshassets/static/hshassets/js/common.js
@@ -0,0 +1,7 @@
+
+$('[data-submit-only-once="true"]').submit(function(event){
+    $(this).find("button").addClass("is-loading");
+    $(this).find(":submit").attr("disabled","disabled");
+});
+
+
diff --git a/hshassets/static/hshassets/js/common.min.js b/hshassets/static/hshassets/js/common.min.js
new file mode 100644
index 00000000..894dd68b
--- /dev/null
+++ b/hshassets/static/hshassets/js/common.min.js
@@ -0,0 +1 @@
+$('[data-submit-only-once="true"]').submit(function(event){$(this).find("button").addClass("is-loading");$(this).find(":submit").attr("disabled","disabled");});
\ No newline at end of file
diff --git a/hshassets/templates/hshassets/layout/base.html b/hshassets/templates/hshassets/layout/base.html
index 80db6af1..3b0c2355 100644
--- a/hshassets/templates/hshassets/layout/base.html
+++ b/hshassets/templates/hshassets/layout/base.html
@@ -30,8 +30,10 @@
         {% block script %}
             {% if debug %}
                 <script type="text/javascript" src="{% static 'hshassets/js/jquery-3.2.1.js' %}"></script>
+                <script type="text/javascript" src="{% static 'hshassets/js/common.js' %}"></script>
             {% else %}
                 <script type="text/javascript" src="{% static 'hshassets/js/jquery-3.2.1.min.js' %}"></script>
+                <script type="text/javascript" src="{% static 'hshassets/js/common.min.js' %}"></script>
             {% endif %}
         {% endblock %}
         {% block post-script %}{% endblock %}
diff --git a/hshassets/templatetags/bulma.py b/hshassets/templatetags/bulma.py
index 7ff59906..9990c537 100644
--- a/hshassets/templatetags/bulma.py
+++ b/hshassets/templatetags/bulma.py
@@ -43,13 +43,13 @@ def bulma_form_errors(form):
 
 
 @register.simple_tag(takes_context=True)
-def bulma_form(context, form, submit_text="OK", submit_class="button is-outlined"):
+def bulma_form(context, form, submit_text="OK", submit_class="button is-outlined", submit_only_once=True):
     """Renders whole form, including errors, csrf and a submit button."""
 
     fields = bulma_form_fields(form)
     errors = bulma_form_errors(form)
 
-    return render_form_generics(context, fields, errors, submit_text, submit_class)
+    return render_form_generics(context, fields, errors, submit_text, submit_class, submit_only_once)
 
 
 def render_layout(elements, form):
-- 
GitLab