From 83c464acb63a41af2bc7f168b67f29fb762bbb1f Mon Sep 17 00:00:00 2001
From: Bryan Brancotte <bryan.brancotte@pasteur.fr>
Date: Wed, 19 Feb 2020 11:58:33 +0100
Subject: [PATCH] Fixing #93, test_type value controls show/hide of
 cell_line_name, and emptiness during clean

---
 ippisite/ippidb/forms.py                               | 10 ++++++++++
 .../ippidb/static/js/ActivityDescriptionFormSet.js     |  8 ++++++++
 .../ippidb/static/js/TestCytotoxDescriptionFormSet.js  |  0
 ippisite/ippidb/static/js/TestPKDescriptionFormSet.js  |  0
 .../templates/wizard_formset_with_nested_formset.html  |  5 +++++
 ippisite/ippidb/templatetags/customtags.py             |  4 ++++
 6 files changed, 27 insertions(+)
 create mode 100644 ippisite/ippidb/static/js/ActivityDescriptionFormSet.js
 create mode 100644 ippisite/ippidb/static/js/TestCytotoxDescriptionFormSet.js
 create mode 100644 ippisite/ippidb/static/js/TestPKDescriptionFormSet.js

diff --git a/ippisite/ippidb/forms.py b/ippisite/ippidb/forms.py
index da0b21f8..428ea402 100644
--- a/ippisite/ippidb/forms.py
+++ b/ippisite/ippidb/forms.py
@@ -969,6 +969,10 @@ class TestActivityDescriptionForm(forms.ModelForm):
             "is_primary": forms.HiddenInput(),
         }
 
+    def __init__(self,*args,**kwargs):
+        super().__init__(*args,**kwargs)
+        self.fields["test_type"].widget.attrs["onchange"]="test_type_changed(this);"
+
     def has_changed(self):
         """
         Test if the form has changed, we consider that it has not changed if it is not linked to an actual instance and
@@ -984,6 +988,12 @@ class TestActivityDescriptionForm(forms.ModelForm):
             return False
         return super().has_changed()
 
+    def clean(self):
+        cleaned_data = super().clean()
+        if "test_type" in cleaned_data and cleaned_data["test_type"] is not "CELL":
+            cleaned_data["cell_line_name"] = ""
+        return cleaned_data
+
     def save(self, commit=True):
         """
         Before an actual save, we set the foreign key that have been created in the meantime from unique identifier
diff --git a/ippisite/ippidb/static/js/ActivityDescriptionFormSet.js b/ippisite/ippidb/static/js/ActivityDescriptionFormSet.js
new file mode 100644
index 00000000..a2a69f9d
--- /dev/null
+++ b/ippisite/ippidb/static/js/ActivityDescriptionFormSet.js
@@ -0,0 +1,8 @@
+function test_type_changed(source){
+     var cell_line_input = document.getElementById(source.id.replace("test_type","cell_line_name")).parentNode;
+     if (source.value=="CELL"){
+        cell_line_input.style.display = '';
+     }else{
+        cell_line_input.style.display = 'none';
+     }
+}
\ No newline at end of file
diff --git a/ippisite/ippidb/static/js/TestCytotoxDescriptionFormSet.js b/ippisite/ippidb/static/js/TestCytotoxDescriptionFormSet.js
new file mode 100644
index 00000000..e69de29b
diff --git a/ippisite/ippidb/static/js/TestPKDescriptionFormSet.js b/ippisite/ippidb/static/js/TestPKDescriptionFormSet.js
new file mode 100644
index 00000000..e69de29b
diff --git a/ippisite/ippidb/templates/wizard_formset_with_nested_formset.html b/ippisite/ippidb/templates/wizard_formset_with_nested_formset.html
index 4628120d..e60cec2e 100644
--- a/ippisite/ippidb/templates/wizard_formset_with_nested_formset.html
+++ b/ippisite/ippidb/templates/wizard_formset_with_nested_formset.html
@@ -6,6 +6,11 @@
 {% block extra_js %}
 {{block.super}}
 <script src="{% sstatic '/js/wizard_formset_with_nested_formset.js'%}"></script>
+{% with wizard.management_form|get_item:"current_step" as current_step %}
+{% with "/js/"|add:current_step.value|add:".js" as js_url %}
+<script src="{% sstatic js_url %}"></script>
+{%endwith%}
+{%endwith%}
 {% endblock extra_js %}
 
 
diff --git a/ippisite/ippidb/templatetags/customtags.py b/ippisite/ippidb/templatetags/customtags.py
index 6a5c0b25..283da0d1 100644
--- a/ippisite/ippidb/templatetags/customtags.py
+++ b/ippisite/ippidb/templatetags/customtags.py
@@ -34,6 +34,10 @@ def bootstrap(object):
     return mark_safe("".join(bootstrap_core(object)))
 
 
+@register.filter
+def get_item(d, key):
+    return d[key]
+
 def bootstrap_core(object):
     ret = []
     if isinstance(object, forms.BoundField):
-- 
GitLab