From 427a7e1a8446b5b0754b12247315a2ef1b165346 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Tue, 26 Jan 2021 10:42:50 +0100
Subject: [PATCH 05/11] agendas: desk_simple_management in import/export
 (#48924)

---
 chrono/agendas/models.py    | 11 ++++++
 tests/test_import_export.py | 67 +++++++++++++++++++++++++++++++++++++
 2 files changed, 78 insertions(+)

diff --git a/chrono/agendas/models.py b/chrono/agendas/models.py
index 38c237d..99145be 100644
--- a/chrono/agendas/models.py
+++ b/chrono/agendas/models.py
@@ -324,6 +324,7 @@ class Agenda(models.Model):
         elif self.kind == 'meetings':
             agenda['meetingtypes'] = [x.export_json() for x in self.meetingtype_set.all()]
             agenda['desks'] = [desk.export_json() for desk in self.desk_set.all()]
+            agenda['desk_simple_management'] = self.desk_simple_management
         elif self.kind == 'virtual':
             agenda['excluded_timeperiods'] = [x.export_json() for x in self.excluded_timeperiods.all()]
             agenda['real_agendas'] = [{'slug': x.slug, 'kind': x.kind} for x in self.real_agendas.all()]
@@ -352,6 +353,7 @@ class Agenda(models.Model):
             if resource_slug not in resources_by_slug:
                 raise AgendaImportError(_('Missing "%s" resource') % resource_slug)
         data = clean_import_data(cls, data)
+        desk_simple_management = data.pop('desk_simple_management', None)
         if data.get('category'):
             try:
                 data['category'] = Category.objects.get(slug=data['category'])
@@ -407,6 +409,15 @@ class Agenda(models.Model):
                 except ValidationError as exc:
                     raise AgendaImportError(' '.join(exc.messages))
 
+        if data['kind'] == 'meetings' and desk_simple_management is not None:
+            if desk_simple_management is True and not agenda.desk_simple_management:
+                if agenda.is_available_for_simple_management():
+                    agenda.desk_simple_management = True
+                    agenda.save()
+            elif desk_simple_management is False and agenda.desk_simple_management:
+                agenda.desk_simple_management = False
+                agenda.save()
+
         return created
 
     def duplicate(self, label=None):
diff --git a/tests/test_import_export.py b/tests/test_import_export.py
index 0eec30a..2dc95de 100644
--- a/tests/test_import_export.py
+++ b/tests/test_import_export.py
@@ -6,6 +6,7 @@ import copy
 import datetime
 from io import StringIO
 import json
+import mock
 import os
 import shutil
 import sys
@@ -690,3 +691,69 @@ def test_import_export_unavailability_calendar(app):
     import_site(update_payload)
     calendar.refresh_from_db()
     assert calendar.label == 'Calendar Updated'
+
+
+@mock.patch('chrono.agendas.models.Agenda.is_available_for_simple_management')
+def test_import_export_desk_simple_management(available_mock):
+    agenda = Agenda.objects.create(label='Foo bar', kind='meetings', desk_simple_management=True)
+    output = get_output_of_command('export_site')
+    payload = json.loads(output)
+    assert len(payload['agendas']) == 1
+    assert payload['agendas'][0]['desk_simple_management'] is True
+
+    # check on creation
+    for available_value in [True, False]:
+        agenda.delete()
+        available_mock.return_value = available_value
+        import_site(payload)
+        agenda = Agenda.objects.latest('pk')
+        assert agenda.desk_simple_management == available_value
+
+    payload['agendas'][0]['desk_simple_management'] = False
+    for available_value in [True, False]:
+        # always ok to set flag to False
+        agenda.delete()
+        available_mock.return_value = available_value
+        import_site(payload)
+        agenda = Agenda.objects.latest('pk')
+        assert agenda.desk_simple_management is False
+
+    # check on update
+    payload['agendas'][0]['desk_simple_management'] = True
+    for available_value in [True, False]:
+        agenda.desk_simple_management = False
+        agenda.save()
+        available_mock.return_value = available_value
+        import_site(payload)
+        agenda.refresh_from_db()
+        assert agenda.desk_simple_management == available_value
+
+        # already True, no change
+        agenda.desk_simple_management = True
+        agenda.save()
+        available_mock.return_value = available_value
+        import_site(payload)
+        agenda.refresh_from_db()
+        assert agenda.desk_simple_management is True
+
+    payload['agendas'][0]['desk_simple_management'] = False
+    for available_value in [True, False]:
+        # always ok to set flag to False
+        for old_value in [True, False]:
+            agenda.desk_simple_management = old_value
+            agenda.save()
+            available_mock.return_value = available_value
+            import_site(payload)
+            agenda.refresh_from_db()
+            assert agenda.desk_simple_management is False
+
+    # not in payload, no changed
+    del payload['agendas'][0]['desk_simple_management']
+    for available_value in [True, False]:
+        for old_value in [True, False]:
+            agenda.desk_simple_management = old_value
+            agenda.save()
+            available_mock.return_value = available_value
+            import_site(payload)
+            agenda.refresh_from_db()
+            assert agenda.desk_simple_management == old_value
-- 
2.29.2

