From 35710a2feea82d9e94cc443c0dd2d0133898922a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Tue, 14 Sep 2021 12:13:42 +0200
Subject: [PATCH] utils: do not require maps and pwa apps for export/import
 support (#56930)

---
 combo/data/utils.py | 54 ++++++++++++++++++++++++++++++++++++---------
 1 file changed, 43 insertions(+), 11 deletions(-)

diff --git a/combo/data/utils.py b/combo/data/utils.py
index 2a7631d1..0780c985 100644
--- a/combo/data/utils.py
+++ b/combo/data/utils.py
@@ -26,8 +26,6 @@ from django.utils.translation import ugettext_lazy as _
 
 from combo.apps.assets.models import Asset
 from combo.apps.assets.utils import add_tar_content, clean_assets_files, tar_assets_files, untar_assets_files
-from combo.apps.maps.models import MapLayer
-from combo.apps.pwa.models import PwaNavigationEntry, PwaSettings
 
 from .models import Page
 
@@ -53,6 +51,16 @@ def export_site(pages=True, cartography=True, pwa=True, assets=True, payment=Tru
     else:
         payment = False
 
+    if 'combo.apps.maps' in settings.INSTALLED_APPS:
+        from combo.apps.maps.models import MapLayer
+    else:
+        cartography = False
+
+    if 'combo.apps.pwa' in settings.INSTALLED_APPS:
+        from combo.apps.pwa.models import PwaNavigationEntry, PwaSettings
+    else:
+        pwa = False
+
     export = {}
     if pages:
         export['pages'] = Page.export_all_for_json()
@@ -79,9 +87,6 @@ def import_site(data, if_empty=False, clean=False, request=None):
         # format.
         data = {'pages': data}
 
-    if if_empty and (Page.objects.count() or MapLayer.objects.count()):
-        return
-
     if 'combo.apps.lingo' in settings.INSTALLED_APPS:
         from combo.apps.lingo.models import PaymentBackend, Regie
 
@@ -89,6 +94,23 @@ def import_site(data, if_empty=False, clean=False, request=None):
     else:
         payment_support = False
 
+    if 'combo.apps.maps' in settings.INSTALLED_APPS:
+        from combo.apps.maps.models import MapLayer
+
+        cartography_support = True
+    else:
+        cartography_support = False
+
+    if 'combo.apps.pwa' in settings.INSTALLED_APPS:
+        from combo.apps.pwa.models import PwaNavigationEntry, PwaSettings
+
+        pwa_support = True
+    else:
+        pwa_support = False
+
+    if if_empty and (Page.objects.count() or (cartography_support and MapLayer.objects.count())):
+        return
+
     # check groups used in access control are all available.
     groups = set()
     for page in data.get('pages') or []:
@@ -105,21 +127,24 @@ def import_site(data, if_empty=False, clean=False, request=None):
 
     with transaction.atomic():
         if clean:
-            MapLayer.objects.all().delete()
+            if cartography_support:
+                MapLayer.objects.all().delete()
             Asset.objects.all().delete()
             Page.objects.all().delete()
-            PwaSettings.objects.all().delete()
-            PwaNavigationEntry.objects.all().delete()
+            if pwa_support:
+                PwaSettings.objects.all().delete()
+                PwaNavigationEntry.objects.all().delete()
             if payment_support:
                 PaymentBackend.objects.all().delete()
                 Regie.objects.all().delete()
 
         try:
-            MapLayer.load_serialized_objects(data.get('map-layers') or [])
+            if data.get('map-layers') and cartography_support:
+                MapLayer.load_serialized_objects(data.get('map-layers'))
             Asset.load_serialized_objects(data.get('assets') or [])
             pages = Page.load_serialized_pages(data.get('pages') or [], request=request)
 
-            if data.get('pwa'):
+            if data.get('pwa') and pwa_support:
                 PwaSettings.load_serialized_settings(data['pwa'].get('settings'))
                 PwaNavigationEntry.load_serialized_objects(data['pwa'].get('navigation'))
             if data.get('payment') and payment_support:
@@ -153,7 +178,14 @@ def import_site_tar(fd, if_empty=False, clean=False, overwrite=False, request=No
         except KeyError:
             raise ImportSiteError(_('TAR file should provide _site.json file'))
 
-        if if_empty and (Page.objects.count() or MapLayer.objects.count()):
+        if 'combo.apps.maps' in settings.INSTALLED_APPS:
+            from combo.apps.maps.models import MapLayer
+
+            cartography_support = True
+        else:
+            cartography_support = False
+
+        if if_empty and (Page.objects.count() or (cartography_support and MapLayer.objects.count())):
             return
 
         if clean:
-- 
2.33.0

