Repository: GeotrekCE/Geotrek-admin

Issue:
Synchro mobile : Problème de pictogramme sur les thèmes 
Il arrive que la synchro-mobile soit en échec lors que des thèmes n'ont pas de pictogramme associés.

Après analyse, il s'avère que certains parsers semblent avoir créés automatiquement des thèmes sans pictogramme.

Deux approches possible : 

# Première approche

Dans cette méthode : https://github.com/GeotrekCE/Geotrek-admin/blob/6cf5f1fd3c1a4ee6605c86cb8661a79c63c573a5/geotrek/api/mobile/serializers/common.py#L144-L146

Rajouter un check pour savoir si un pictogramme est présent (évite à la synchro de casser)

# Seconde approche

Empêcher avec les parseurs de créer un thème sans pictogramme (mais dans ce cas quel pictogramme utiliser par défaut ?)

# A noter: 

- Les deux peuvent être fait.
- Si possible au passage corriger : https://github.com/GeotrekCE/Geotrek-admin/blob/6cf5f1fd3c1a4ee6605c86cb8661a79c63c573a5/geotrek/api/mobile/serializers/common.py#L105-L108


Discussion:
La première approche a été validé en réu. 

Resolving patch:
diff --git a/geotrek/api/mobile/serializers/common.py b/geotrek/api/mobile/serializers/common.py
--- a/geotrek/api/mobile/serializers/common.py
+++ b/geotrek/api/mobile/serializers/common.py
@@ -103,6 +103,8 @@ class PracticeSerializer(serializers.ModelSerializer):
         pictogram = serializers.SerializerMethodField()
 
         def get_pictogram(self, obj):
+            if not obj.pictogram:
+                return None
             file_name, file_extension = os.path.splitext(str(obj.pictogram.url))
             return '{file}.png'.format(file=file_name) if file_extension == '.svg' else obj.pictogram.url
 
@@ -142,6 +144,8 @@ class ThemeSerializer(serializers.ModelSerializer):
         pictogram = serializers.SerializerMethodField()
 
         def get_pictogram(self, obj):
+            if not obj.pictogram:
+                return None
             file_name, file_extension = os.path.splitext(str(obj.pictogram.url))
             return '{file}.png'.format(file=file_name) if file_extension == '.svg' else obj.pictogram.url
 


Test patch:
diff --git a/geotrek/api/tests/test_mobile/test_serializers.py b/geotrek/api/tests/test_mobile/test_serializers.py
new file mode 100644
--- /dev/null
+++ b/geotrek/api/tests/test_mobile/test_serializers.py
@@ -0,0 +1,31 @@
+from django.test import TestCase
+
+from geotrek.api.mobile.serializers import common as api_serializers
+from geotrek.trekking.tests.factories import PracticeFactory
+from geotrek.common.tests.factories import ThemeFactory
+
+
+class PracticeSerializerTestCase(TestCase):
+
+    def test_practice_with_picto(self):
+        practice = PracticeFactory.create(pictogram='a/pictogram/path.png')
+        serialized_practice = api_serializers.PracticeSerializer(practice).data
+        self.assertEqual(serialized_practice['pictogram'], '/media/a/pictogram/path.png')
+
+    def test_practice_with_no_picto(self):
+        practice = PracticeFactory.create(pictogram=None)
+        serialized_practice = api_serializers.PracticeSerializer(practice).data
+        self.assertIsNone(serialized_practice['pictogram'])
+
+
+class ThemeSerializerTestCase(TestCase):
+
+    def test_theme_with_picto(self):
+        theme = ThemeFactory.create(pictogram='a/pictogram/path.png')
+        serialized_theme = api_serializers.ThemeSerializer(theme).data
+        self.assertEqual(serialized_theme['pictogram'], '/media/a/pictogram/path.png')
+
+    def test_theme_with_no_picto(self):
+        theme = ThemeFactory.create(pictogram=None)
+        serialized_theme = api_serializers.ThemeSerializer(theme).data
+        self.assertIsNone(serialized_theme['pictogram'])
diff --git a/geotrek/api/tests/test_mobile/test_sync_mobile.py b/geotrek/api/tests/test_mobile/test_sync_mobile.py
--- a/geotrek/api/tests/test_mobile/test_sync_mobile.py
+++ b/geotrek/api/tests/test_mobile/test_sync_mobile.py
@@ -22,7 +22,8 @@
 from geotrek.common.tests import TranslationResetMixin
 from geotrek.common.tests.factories import (AttachmentFactory,
                                             RecordSourceFactory,
-                                            TargetPortalFactory)
+                                            TargetPortalFactory,
+                                            ThemeFactory)
 from geotrek.common.utils.testdata import (get_dummy_uploaded_file,
                                            get_dummy_uploaded_image,
                                            get_dummy_uploaded_image_svg)
@@ -577,3 +578,20 @@ def test_sync_treks_informationdesk_photo_missing(self):
         management.call_command('sync_mobile', os.path.join(settings.TMP_DIR, 'sync_mobile', 'tmp_sync'), url='http://localhost:8000',
                                 skip_tiles=True, verbosity=2, stdout=output)
         self.assertIn('Done', output.getvalue())
+
+    def test_sync_treks_theme_no_picto(self):
+        theme_no_picto = ThemeFactory.create(pictogram=None)
+        trek = TrekWithPublishedPOIsFactory.create()
+        trek.themes.add(theme_no_picto)
+        output = StringIO()
+        management.call_command('sync_mobile', os.path.join(settings.TMP_DIR, 'sync_mobile', 'tmp_sync'), url='http://localhost:8000',
+                                skip_tiles=True, verbosity=2, stdout=output)
+        self.assertIn('Done', output.getvalue())
+
+    def test_sync_treks_practice_no_picto(self):
+        practice_no_picto = PracticeFactory.create(pictogram=None)
+        TrekWithPublishedPOIsFactory.create(practice=practice_no_picto)
+        output = StringIO()
+        management.call_command('sync_mobile', os.path.join(settings.TMP_DIR, 'sync_mobile', 'tmp_sync'), url='http://localhost:8000',
+                                skip_tiles=True, verbosity=2, stdout=output)
+        self.assertIn('Done', output.getvalue())