From 7c7cbf0f96f94fa0fca67ab1f41b54758a03e1e4 Mon Sep 17 00:00:00 2001
From: Carlos Vega <carlos.vega@uni.lu>
Date: Tue, 11 Dec 2018 16:28:41 +0100
Subject: [PATCH] finished visits now have read only details form

---
 smash/web/forms/forms.py                                  | 8 ++++++++
 .../templates/includes/visit_appointment_types_field.html | 2 +-
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/smash/web/forms/forms.py b/smash/web/forms/forms.py
index 3106fba6..acead32d 100644
--- a/smash/web/forms/forms.py
+++ b/smash/web/forms/forms.py
@@ -60,6 +60,14 @@ class VisitDetailForm(ModelForm):
     appointment_types = forms.ModelMultipleChoiceField(required=False, widget=forms.CheckboxSelectMultiple,
                                                        queryset=AppointmentType.objects.all())
 
+    def __init__(self, *args, **kwargs):
+        super(VisitDetailForm, self).__init__(*args, **kwargs)
+        instance = getattr(self, 'instance', None)
+        if instance.is_finished: #set form as readonly
+            for key in self.fields.keys():
+                self.fields[key].widget.attrs['readonly'] = True
+
+
     class Meta:
         model = Visit
         exclude = ['is_finished', 'visit_number']
diff --git a/smash/web/templates/includes/visit_appointment_types_field.html b/smash/web/templates/includes/visit_appointment_types_field.html
index 4e167755..8cd1998f 100644
--- a/smash/web/templates/includes/visit_appointment_types_field.html
+++ b/smash/web/templates/includes/visit_appointment_types_field.html
@@ -13,7 +13,7 @@
                                          background: orange;
                                      {% endif %}{% endif %}{% endfor %}{% endfor %}
                                      "></div>
-                        <input {% if readonly %}disabled="disabled"{% endif %}
+                        <input {% if field.field.widget.attrs.readonly %}disabled="disabled"{% endif %}
                                {% for option in field.value %}{% if option == pk|slugify or option == pk %}checked="checked"
                                {% endif %}{% endfor %} type="checkbox"
                                id="id_{{ field.name }}_{{ forloop.counter0 }}"
-- 
GitLab