From 971fba2ae13ce1ca136a75f0760605061e86f5e1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Mon, 8 Mar 2021 09:17:26 +0100
Subject: [PATCH] manager: take snapshots after page import (#51475)

---
 combo/data/models.py   |  4 +++-
 combo/data/utils.py    |  7 +++++--
 combo/manager/views.py |  7 +++++--
 tests/test_manager.py  | 10 +++++++++-
 4 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/combo/data/models.py b/combo/data/models.py
index 1059b237..5bd095a1 100644
--- a/combo/data/models.py
+++ b/combo/data/models.py
@@ -515,10 +515,12 @@ class Page(models.Model):
     @classmethod
     def load_serialized_pages(cls, json_site, request=None):
         cells = []
+        pages = []
         for json_page in json_site:
-            cls.load_serialized_page(json_page, request=request)
+            pages.append(cls.load_serialized_page(json_page, request=request))
             cells.extend(json_page.get('cells'))
         cls.load_serialized_cells(cells)
+        return pages
 
     @classmethod
     def export_all_for_json(cls):
diff --git a/combo/data/utils.py b/combo/data/utils.py
index 5fa15c9c..253b5685 100644
--- a/combo/data/utils.py
+++ b/combo/data/utils.py
@@ -120,7 +120,7 @@ def import_site(data, if_empty=False, clean=False, request=None):
         try:
             MapLayer.load_serialized_objects(data.get('map-layers') or [])
             Asset.load_serialized_objects(data.get('assets') or [])
-            Page.load_serialized_pages(data.get('pages') or [], request=request)
+            pages = Page.load_serialized_pages(data.get('pages') or [], request=request)
 
             if data.get('pwa'):
                 PwaSettings.load_serialized_settings(data['pwa'].get('settings'))
@@ -137,6 +137,8 @@ def import_site(data, if_empty=False, clean=False, request=None):
             except IndexError:
                 raise ImportSiteError(message)
             raise ImportSiteError(_('Unknown page "%s".') % page_slug)
+        else:
+            return pages
 
 
 def export_site_tar(fd, export_kwargs=None):
@@ -164,5 +166,6 @@ def import_site_tar(fd, if_empty=False, clean=False, overwrite=False, request=No
     json_site = tar.extractfile(tarinfo).read()
     data = json.loads(json_site.decode('utf-8'))
     data.update(untar_assets_files(tar, overwrite=overwrite))
-    import_site(data, if_empty=if_empty, clean=clean, request=request)
+    pages = import_site(data, if_empty=if_empty, clean=clean, request=request)
     tar.close()
+    return pages
diff --git a/combo/manager/views.py b/combo/manager/views.py
index 51ec2af6..bdab78a2 100644
--- a/combo/manager/views.py
+++ b/combo/manager/views.py
@@ -129,12 +129,15 @@ class SiteImportView(FormView):
             fd.seek(0)
         try:
             if format == 'json':
-                import_site(json_site, request=self.request)
+                pages = import_site(json_site, request=self.request)
             else:
-                import_site_tar(fd, request=self.request)
+                pages = import_site_tar(fd, request=self.request)
         except ImportSiteError as e:
             form.add_error('site_file', force_text(e))
             return self.form_invalid(form)
+        else:
+            for page in pages:
+                PageSnapshot.take(page, request=self.request, comment=_('imported'))
 
         return super(SiteImportView, self).form_valid(form)
 
diff --git a/tests/test_manager.py b/tests/test_manager.py
index 49ab96e2..8d34928f 100644
--- a/tests/test_manager.py
+++ b/tests/test_manager.py
@@ -833,9 +833,15 @@ def test_site_export_import_json(app, admin_user):
     assert resp.headers['content-type'] == 'application/json'
     site_export = resp.body
 
+    resp = app.get('/manage/')
+    resp = resp.click('Import Site')
+    resp.form['site_file'] = Upload('site-export.json', site_export, 'application/json')
+    resp = resp.form.submit()
+    assert Page.objects.count() == 4
+    assert PageSnapshot.objects.all().count() == 4
+
     Page.objects.all().delete()
     assert LinkCell.objects.count() == 0
-    app = login(app)
     resp = app.get('/manage/')
     resp = resp.click('Import Site')
     resp.form['site_file'] = Upload('site-export.json', site_export, 'application/json')
@@ -844,6 +850,7 @@ def test_site_export_import_json(app, admin_user):
     assert LinkCell.objects.count() == 2
     assert LinkCell.objects.get(page__slug='one').link_page.slug == 'two'
     assert LinkCell.objects.get(page__slug='two').link_page.slug == 'one'
+    assert PageSnapshot.objects.all().count() == 8
 
     resp = app.get('/manage/')
     resp = resp.click('Export Site')
@@ -899,6 +906,7 @@ def test_site_export_import_tar(app, admin_user):
     resp.form['site_file'] = Upload('site-export.json', site_export, 'application/json')
     resp = resp.form.submit()
     assert Page.objects.count() == 1
+    assert PageSnapshot.objects.all().count() == 1
     assert TextCell.objects.count() == 1
     assert Asset.objects.filter(key='collectivity:banner').count() == 1
     assert open('%s/assets/test.png' % path, 'r').read() == 'foo'
-- 
2.30.1

