b6a8be49762d — Gustavo Andres Morero 7 years ago
changes to allow passing a list of forms/ids to inclusion template tag.
M form_guard/templates/form_guard/js_snippet.html +26 -10
@@ 1,14 1,30 @@ 
 <script type="text/javascript">
-  var anti_spam_{{ FORM_GUARD_FIELD_ID }} = function() {
-      if (document.getElementById("{{ FORM_GUARD_FIELD_ID }}")) {
-          a = document.getElementById("{{ FORM_GUARD_FIELD_ID }}");
-          if (isNaN(a.value) == true) {
-              a.value = 0;
-          } else {
-              a.value = parseInt(a.value) + 1;
-          }
+  var field_ids = {{ field_ids|safe }};
+
+  var update_field_value = function(field_id) {
+    if (document.getElementById(field_id)) {
+      a = document.getElementById(field_id);
+      if (isNaN(a.value) == true) {
+        a.value = 0;
+      } else {
+        a.value = parseInt(a.value) + 1;
       }
-      setTimeout("anti_spam_{{ FORM_GUARD_FIELD_ID }}()", 1000);
+    }
   }
-  anti_spam_{{ FORM_GUARD_FIELD_ID }}();
+
+  if (field_ids.length == 1) {
+    var anti_spam_{{ field_ids.0 }} = function() {
+      update_field_value("{{ field_ids.0 }}");
+      setTimeout("anti_spam_{{ field_ids.0 }}()", 1000);
+    }
+    anti_spam_{{ field_ids.0 }}();
+  } else {
+    var anti_spam = function() {
+      for (var i=0, l=field_ids.length; i<l; i++) { 
+        update_field_value(field_ids[i]);
+      }
+      setTimeout("anti_spam()", 1000);
+    }
+    anti_spam();
+  }
 </script>

          
M form_guard/templatetags/form_guard_tags.py +12 -5
@@ 11,9 11,16 @@ def is_string(obj):
         return isinstance(obj, str)
 
 
+def get_form_field_id(obj):
+    if is_string(obj):
+        return obj
+    else:
+        return obj.fields[obj.FORM_GUARD_FIELD_NAME].widget.attrs.get('id')
+
+
 @register.inclusion_tag('form_guard/js_snippet.html', takes_context=True)
-def form_guard_js(context, form):
-    if is_string(form):
-        return {'FORM_GUARD_FIELD_ID': form}
-    field_id = form.fields[form.FORM_GUARD_FIELD_NAME].widget.attrs.get('id')
-    return {'FORM_GUARD_FIELD_ID': field_id}
+def form_guard_js(context, *args):
+    if not args:
+        raise template.TemplateSyntaxError(
+            'No argument provided for form_guard_js tag.')
+    return {'field_ids': [str(get_form_field_id(arg)) for arg in args]}

          
M form_guard/tests.py +9 -2
@@ 5,6 5,7 @@ from django.test import TestCase
 from django.forms.fields import CharField
 from django.test.utils import override_settings
 from django.core.exceptions import PermissionDenied
+from django.template import TemplateSyntaxError
 
 from .forms import FormGuardForm
 from .settings import FORM_GUARD_MIN_VALUE

          
@@ 66,9 67,15 @@ class TestFormGuard(TestCase):
     def test_template_tag(self):
         # argument is antispam field id (string)
         ret = form_guard_js({}, 'field_id')
-        self.assertEqual(ret, {'FORM_GUARD_FIELD_ID': 'field_id'})
+        self.assertEqual(ret, {'field_ids': ['field_id']})
         # argument is a form instance
         form = FormGuardForm()
         ret = form_guard_js({}, form)
         fid = form.fields[form.FORM_GUARD_FIELD_NAME].widget.attrs.get('id')
-        self.assertEqual(ret, {'FORM_GUARD_FIELD_ID': fid})
+        self.assertEqual(ret, {'field_ids': [fid]})
+        # mixed arguments
+        ret = form_guard_js({}, 'field_id', form)
+        self.assertEqual(ret, {'field_ids': ['field_id', fid]})
+        # no arguments
+        self.assertRaises(
+            TemplateSyntaxError, lambda: form_guard_js({}))