Repository: GeotrekCE/Geotrek-admin

Issue:
Control which picture will be used as main illustration (trek, POI)
- Could be a new attribute in attachment model
- Could be a foreign key in trek/poi/.. i.e. `PicturesMixin`



Discussion:

Resolving patch:
diff --git a/geotrek/common/mixins.py b/geotrek/common/mixins.py
--- a/geotrek/common/mixins.py
+++ b/geotrek/common/mixins.py
@@ -85,7 +85,8 @@ def pictures(self):
         """
         if hasattr(self, '_pictures'):
             return self._pictures
-        return [a for a in self.attachments.all() if a.is_image
+        all_attachments = self.attachments.order_by('-starred').all()
+        return [a for a in all_attachments if a.is_image
                 and a.title != 'mapimage']
 
     @pictures.setter


Test patch:
diff --git a/geotrek/common/tests.py b/geotrek/common/tests.py
--- a/geotrek/common/tests.py
+++ b/geotrek/common/tests.py
@@ -12,6 +12,7 @@
 from mapentity.factories import UserFactory
 
 from geotrek.settings import EnvIniReader
+from geotrek.common.utils.testdata import get_dummy_uploaded_image
 from geotrek.authent.tests import AuthentFixturesTest
 from .utils import almostequal, sampling, sql_extent, uniquify
 from .utils.postgresql import debug_pg_notices
@@ -113,3 +114,35 @@ def test_helpers(self):
 
     def tearDown(self):
         os.remove(self.ini_file)
+
+
+class DefaultPictureTest(TestCase):
+    def setUp(self):
+        from geotrek.trekking.factories import TrekFactory
+        self.trek = TrekFactory.create()
+        self.add_attachment()
+
+    def add_attachment(self, attachment=None):
+        from geotrek.common.factories import AttachmentFactory
+        return AttachmentFactory.create(obj=self.trek, attachment_file=attachment)
+
+    def test_default_thumbnail_is_empty(self):
+        self.assertEqual(len(self.trek.attachments), 1)
+        self.assertEqual(self.trek.thumbnail, None)
+        self.assertEqual(self.trek.pictures, [])
+
+    def test_picture_is_taken_among_attachments(self):
+        self.add_attachment(attachment=get_dummy_uploaded_image())
+        self.assertEqual(len(self.trek.attachments), 2)
+        self.assertEqual(len(self.trek.pictures), 1)
+        self.assertNotEqual(self.trek.thumbnail, None)
+
+    def test_the_starred_picture_is_taken_among_attachments(self):
+        self.add_attachment(attachment=get_dummy_uploaded_image())
+        starred = self.add_attachment(attachment=get_dummy_uploaded_image())
+        self.add_attachment(attachment=get_dummy_uploaded_image())
+        starred.starred = True
+        starred.save()
+        self.assertEqual(len(self.trek.attachments), 4)
+        self.assertEqual(len(self.trek.pictures), 3)
+        self.assertTrue(starred.attachment_file.name in self.trek.thumbnail.name)
diff --git a/geotrek/trekking/tests/test_models.py b/geotrek/trekking/tests/test_models.py
--- a/geotrek/trekking/tests/test_models.py
+++ b/geotrek/trekking/tests/test_models.py
@@ -5,8 +5,6 @@
 from bs4 import BeautifulSoup
 
 from geotrek.core.factories import PathFactory, PathAggregationFactory
-from geotrek.common.factories import AttachmentFactory
-from geotrek.common.utils.testdata import get_dummy_uploaded_image
 from geotrek.zoning.factories import DistrictFactory, CityFactory
 from geotrek.trekking.factories import (POIFactory, TrekFactory, TrekWithPOIsFactory)
 from geotrek.trekking.models import Trek
@@ -210,15 +208,3 @@ def test_city_departure(self):
                                                               (3, 9), (3, 3)))))
         self.assertEqual(trek.cities, [city1, city2])
         self.assertEqual(trek.city_departure, unicode(city1))
-
-    def test_picture(self):
-        trek = TrekFactory.create()
-        AttachmentFactory.create(obj=trek)
-        self.assertEqual(len(trek.attachments), 1)
-        self.assertEqual(trek.thumbnail, None)
-        self.assertEqual(trek.pictures, [])
-
-        AttachmentFactory.create(obj=trek, attachment_file=get_dummy_uploaded_image())
-        self.assertEqual(len(trek.attachments), 2)
-        self.assertEqual(len(trek.pictures), 1)
-        self.assertNotEqual(trek.thumbnail, None)