From d0dd072ae87e44dd0491ff24f42f3b991d1755ba Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Valentin=20Grou=C3=A8s?= <valentin.groues@uni.lu>
Date: Tue, 21 Mar 2017 15:36:46 +0100
Subject: [PATCH] hide visit fields in appointment edit view - #73

---
 smash/web/forms.py                          | 1 +
 smash/web/tests/test_AppointmentEditForm.py | 9 +++++++--
 smash/web/views.py                          | 5 -----
 3 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/smash/web/forms.py b/smash/web/forms.py
index d1b4f2b7..35a319b8 100644
--- a/smash/web/forms.py
+++ b/smash/web/forms.py
@@ -145,6 +145,7 @@ class AppointmentEditForm(ModelForm):
             raise TypeError("Worker not defined for: " + user.username)
 
         super(ModelForm, self).__init__(*args, **kwargs)
+        self.fields.pop('visit')
 
     def clean_location(self):
         location = self.cleaned_data['location']
diff --git a/smash/web/tests/test_AppointmentEditForm.py b/smash/web/tests/test_AppointmentEditForm.py
index 23e816d9..e4b9bee9 100644
--- a/smash/web/tests/test_AppointmentEditForm.py
+++ b/smash/web/tests/test_AppointmentEditForm.py
@@ -19,18 +19,23 @@ class AppointmentEditFormTests(TestCase):
 
         self.sample_data = {'first_name': 'name',
                             'length': '50',
-                            'visit': self.visit.id,
                             'location': location.id,
                             'datetime_when': "2020-01-01",
                             }
 
-        add_form = AppointmentAddForm(user=self.user, data=self.sample_data)
+        self.sample_data_with_visit = self.sample_data
+        self.sample_data_with_visit['visit'] = self.visit.id
+        add_form = AppointmentAddForm(user=self.user, data=self.sample_data_with_visit)
         self.appointment = add_form.save()
 
     def test_validation(self):
         form = AppointmentEditForm(user=self.user, data=self.sample_data)
         self.assertTrue(form.is_valid())
 
+    def test_no_visit_field(self):
+        form = AppointmentEditForm(user=self.user, data=self.sample_data)
+        self.assertNotIn('visit', form.fields)
+
     def test_validation_invalid_location(self):
         self.sample_data['location'] = create_location(name="xxx").id
         form = AppointmentEditForm(user=self.user, data=self.sample_data)
diff --git a/smash/web/views.py b/smash/web/views.py
index a18ece4b..989dd33c 100644
--- a/smash/web/views.py
+++ b/smash/web/views.py
@@ -717,11 +717,6 @@ def appointment_edit(request, id):
         if appointment_form.is_valid() and subject_form.is_valid():
             appointment_form.save()
             subject_form.save()
-
-            data = appointment_form.cleaned_data
-            vis = data['visit']
-            get_object_or_404(Visit, id=vis.id)
-
             return redirect(appointments)
     else:
         appointment_form = AppointmentEditForm(instance=the_appointment, user=request.user, prefix="appointment")
-- 
GitLab