From c50fb5452c9f3a7c68104b42615591cbc316d70f Mon Sep 17 00:00:00 2001
From: Bryan Brancotte <bryan.brancotte@pasteur.fr>
Date: Thu, 3 Feb 2022 17:26:00 +0100
Subject: [PATCH] redirect to data source list when curator switch back to
 private a datasource that zhe cannot see anymore WIP #241

---
 .../locale/en/LC_MESSAGES/django.po           |  4 +++
 .../tests/test_views_data_source.py           | 34 +++++++++++++++----
 src/viralhostrange/viralhostrangedb/views.py  | 12 +++++++
 3 files changed, 43 insertions(+), 7 deletions(-)

diff --git a/src/viralhostrange/locale/en/LC_MESSAGES/django.po b/src/viralhostrange/locale/en/LC_MESSAGES/django.po
index ee24e42b..ce54fd7d 100644
--- a/src/viralhostrange/locale/en/LC_MESSAGES/django.po
+++ b/src/viralhostrange/locale/en/LC_MESSAGES/django.po
@@ -1430,6 +1430,10 @@ msgstr ""
 msgid "Update the data source"
 msgstr ""
 
+#, python-format
+msgid "Data source \"%s\" went private, you can not longer access to it."
+msgstr ""
+
 msgid "History of a data source"
 msgstr ""
 
diff --git a/src/viralhostrange/viralhostrangedb/tests/test_views_data_source.py b/src/viralhostrange/viralhostrangedb/tests/test_views_data_source.py
index 5814f15e..66940cb2 100644
--- a/src/viralhostrange/viralhostrangedb/tests/test_views_data_source.py
+++ b/src/viralhostrange/viralhostrangedb/tests/test_views_data_source.py
@@ -181,15 +181,35 @@ class DataSourceUpdateViewTestCase(ViewTestCase):
             set([(gu.user.pk, gu.can_write) for gu in granted_users]),
             set([(gu.user.pk, gu.can_write) for gu in self.public_data_source_of_user.granteduser_set.all()]))
 
-        is_curator = business_process.is_curator(self.user)
+    def test_back_to_private_by_curator(self):
+        url = reverse('viralhostrangedb:data-source-update', args=[self.public_data_source_of_user.pk])
+        granted_users = set(self.public_data_source_of_user.granteduser_set.all())
+
+        self.client.force_login(self.toto)
+        business_process.set_curator(self.toto, True)
+        form_data = dict(
+            name=self.public_data_source_of_user.name,
+            life_domain="bacteria",
+            description="a "*20,
+            public=True,
+            allowed_users_editor=
+            "\n".join(["%s %s;%i;%s" % (gu.user.last_name, gu.user.first_name, gu.user.pk, gu.can_write)
+                       for gu in granted_users]),
+        )
 
-        self.assertEqual(form_data["provider_first_name"] if is_curator else None,
-                         self.public_data_source_of_user.provider_first_name)
-        self.assertEqual(form_data["provider_last_name"] if is_curator else None,
-                         self.public_data_source_of_user.provider_last_name)
+        response = self.client.post(url, form_data)
+        self.write_in_tmp_file(response)
+        self.assertRedirects(
+            response,
+            expected_url=reverse('viralhostrangedb:data-source-detail', args=[self.public_data_source_of_user.pk])
+        )
+        form_data['public'] = False
 
-    def test_update_work_for_curator(self):
-        self.test_update_work()
+        response = self.client.post(url, form_data)
+        self.assertRedirects(
+            response,
+            expected_url=reverse('viralhostrangedb:data-source-list')
+        )
 
     def test_new_user_granted_by_email(self):
         url = reverse('viralhostrangedb:data-source-update', args=[self.public_data_source_of_user.pk])
diff --git a/src/viralhostrange/viralhostrangedb/views.py b/src/viralhostrange/viralhostrangedb/views.py
index 29dcc24a..be3f1e5c 100644
--- a/src/viralhostrange/viralhostrangedb/views.py
+++ b/src/viralhostrange/viralhostrangedb/views.py
@@ -46,6 +46,7 @@ from viralhostrangedb import forms, business_process, views_api
 from viralhostrangedb import mixins
 from viralhostrangedb import models
 from viralhostrangedb.business_process import MessageImportationObserver
+from viralhostrangedb.templatetags.viralhostrange_tags import can_see
 from viralhostrangedb.utils import order_queryset_specifically
 
 logger = logging.getLogger(__name__)
@@ -372,6 +373,11 @@ class DataSourceUpdateView(mixins.OnlyEditorOrCuratorOrOwnedMixin, UpdateView):
     form_class = forms.DataSourceUserCreateOrUpdateForm
     template_name = 'basetheme_bootstrap/small_form_host.html'
 
+    def get_success_url(self):
+        if can_see(self.request.user, self.object):
+            return super().get_success_url()
+        return reverse('viralhostrangedb:data-source-list')
+
     def get_context_data(self, *, object_list=None, **kwargs):
         context = super().get_context_data(object_list=object_list, **kwargs)
         context["page_title"] = _("Editing a data source")
@@ -396,6 +402,12 @@ class DataSourceUpdateView(mixins.OnlyEditorOrCuratorOrOwnedMixin, UpdateView):
             change_fields=changed_data,
             altered_data=business_process.DataSourceAlteredData.INNER_INFORMATION,
         )
+        if 'public' in changed_data and not self.object.public and not can_see(self.request.user, self.object):
+            messages.add_message(
+                request=self.request,
+                level=messages.SUCCESS,
+                message=ugettext('Data source "%s" went private, you can not longer access to it.') % self.object.name,
+            )
         return r
 
 
-- 
GitLab