Skip to content

Commit

Permalink
Merge pull request #2770 from GeotrekCE/exclude_POIs_on_site_page
Browse files Browse the repository at this point in the history
Hide excluded POIs on Outdoor Site and Course detail pages
  • Loading branch information
Chatewgne committed Sep 24, 2021
2 parents 735acdc + 95a49bf commit 81b0135
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ CHANGELOG
**Bug fixes**

* Fix dynamic forms on outdoor cotations display all cotations when selector empty

* Hide excluded POIs on Outdoor Site and Course detail pages

2.65.0 (2021-09-21)
----------------------
Expand Down
11 changes: 11 additions & 0 deletions geotrek/outdoor/mixins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from geotrek.trekking.models import POI
from geotrek.common.utils import intersecting


class ExcludedPOIsMixin:

@property
def pois(self):
pois = intersecting(POI, self)
pois = pois.exclude(pk__in=self.pois_excluded.values_list('pk', flat=True))
return pois
7 changes: 3 additions & 4 deletions geotrek/outdoor/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from geotrek.outdoor.mixins import ExcludedPOIsMixin
from colorfield.fields import ColorField
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
Expand Down Expand Up @@ -121,7 +122,7 @@ def __str__(self):


class Site(ZoningPropertiesMixin, AddPropertyMixin, PicturesMixin, PublishableMixin, MapEntityMixin, StructureRelated,
AltimetryMixin, TimeStampedModelMixin, MPTTModel):
AltimetryMixin, TimeStampedModelMixin, MPTTModel, ExcludedPOIsMixin):
ORIENTATION_CHOICES = (
('N', _("↑ N")),
('NE', _("↗ NE")),
Expand Down Expand Up @@ -292,7 +293,6 @@ def site_interventions(self):

Site.add_property('sites', lambda self: intersecting(Site, self), _("Sites"))
Site.add_property('treks', lambda self: intersecting(Trek, self), _("Treks"))
Site.add_property('pois', lambda self: intersecting(POI, self), _("POIs"))
Site.add_property('services', lambda self: intersecting(Service, self), _("Services"))
Site.add_property('trails', lambda self: intersecting(Trail, self), _("Trails"))
Site.add_property('infrastructures', lambda self: intersecting(Infrastructure, self), _("Infrastructures"))
Expand Down Expand Up @@ -324,7 +324,7 @@ class Meta:
)


class Course(ZoningPropertiesMixin, AddPropertyMixin, PublishableMixin, MapEntityMixin, StructureRelated, PicturesMixin, AltimetryMixin, TimeStampedModelMixin):
class Course(ZoningPropertiesMixin, AddPropertyMixin, PublishableMixin, MapEntityMixin, StructureRelated, PicturesMixin, AltimetryMixin, TimeStampedModelMixin, ExcludedPOIsMixin):
geom = models.GeometryCollectionField(verbose_name=_("Location"), srid=settings.SRID)
site = models.ForeignKey(Site, related_name="courses", on_delete=models.PROTECT, verbose_name=_("Site"))
description = models.TextField(verbose_name=_("Description"), blank=True,
Expand Down Expand Up @@ -414,7 +414,6 @@ def course_interventions(self):

Course.add_property('sites', lambda self: intersecting(Course, self), _("Sites"))
Course.add_property('treks', lambda self: intersecting(Trek, self), _("Treks"))
Course.add_property('pois', lambda self: intersecting(POI, self), _("POIs"))
Course.add_property('services', lambda self: intersecting(Service, self), _("Services"))
Course.add_property('trails', lambda self: intersecting(Trail, self), _("Trails"))
Course.add_property('infrastructures', lambda self: intersecting(Infrastructure, self), _("Infrastructures"))
Expand Down
23 changes: 23 additions & 0 deletions geotrek/outdoor/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from django.contrib.gis.geos.collections import GeometryCollection
from geotrek.trekking.factories import POIFactory
from django.contrib.gis.geos.point import Point
from django.contrib.gis.geos import Polygon
from geotrek.common.factories import OrganismFactory
from geotrek.outdoor.factories import SiteFactory, RatingScaleFactory, SectorFactory
from django.test import TestCase, override_settings
Expand Down Expand Up @@ -105,3 +109,22 @@ class RatingScaleTest(TestCase):
def test_ratingscale_str(self):
scale = RatingScaleFactory.create(name='Bar', practice__name='Foo')
self.assertEqual(str(scale), 'Bar (Foo)')


class ExcludedPOIsTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.poi1 = POIFactory()
cls.poi1.geom = Point(0.5, 0.5, srid=2154)
cls.poi1.save()
cls.poi2 = POIFactory()
cls.poi2.geom = Point(0.5, 0.5, srid=2154)
cls.poi2.save()
cls.site = SiteFactory(geom=GeometryCollection(Polygon(((0, 0), (0, 1), (1, 0), (1, 1), (0, 0)), srid=2154)))

def test_no_poi_excluded(self):
self.assertEqual(self.site.pois.count(), 2)

def test_one_poi_excluded(self):
self.site.pois_excluded.set([self.poi1])
self.assertEqual(self.site.pois.count(), 1)

0 comments on commit 81b0135

Please sign in to comment.