Skip to content

Commit

Permalink
Hot fix provider_choices
Browse files Browse the repository at this point in the history
  • Loading branch information
juggler31 committed Oct 26, 2023
1 parent ed76b17 commit 16ca567
Show file tree
Hide file tree
Showing 22 changed files with 394 additions and 81 deletions.
10 changes: 10 additions & 0 deletions geotrek/common/mixins/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,13 @@ def get_queryset(self):
# Filter out deleted objects
def existing(self):
return self.get_queryset().filter(deleted=False)


class ProviderChoicesMixin:
def provider_choices(self):
qs = self.get_queryset()
if hasattr(qs, "existing"):
qs = qs.existing()
values = qs.exclude(provider__exact='') \
.distinct('provider').order_by("provider").values_list('provider', flat=True)
return tuple((value, value) for value in values)
12 changes: 10 additions & 2 deletions geotrek/core/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class PathFilterSet(AltimetryAllGeometriesFilterSet, ZoningFilterSet, StructureR
field_name='provider',
empty_label=_("Provider"),
label=_("Provider"),
choices=Path.objects.provider_choices()
choices=(('', '---------'),)
)
networks = ModelMultipleChoiceFilter(queryset=Network.objects.all().select_related("structure"))
usages = ModelMultipleChoiceFilter(queryset=Usage.objects.all().select_related("structure"))
Expand All @@ -114,6 +114,10 @@ class Meta(StructureRelatedFilterSet.Meta):
fields = StructureRelatedFilterSet.Meta.fields + \
['valid', 'networks', 'usages', 'comfort', 'stake', 'draft', 'provider']

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.form.fields['provider'].choices = Path.objects.provider_choices()


class TrailFilterSet(AltimetryAllGeometriesFilterSet, ValidTopologyFilterSet, ZoningFilterSet, StructureRelatedFilterSet):
"""Trail filter set"""
Expand All @@ -130,14 +134,18 @@ class TrailFilterSet(AltimetryAllGeometriesFilterSet, ValidTopologyFilterSet, Zo
field_name='provider',
empty_label=_("Provider"),
label=_("Provider"),
choices=Trail.objects.provider_choices()
choices=(('', '---------'),)
)

class Meta(StructureRelatedFilterSet.Meta):
model = Trail
fields = StructureRelatedFilterSet.Meta.fields + \
['name', 'category', 'departure', 'arrival', 'certification_labels', 'comments', 'provider']

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.form.fields['provider'].choices = Trail.objects.provider_choices()


class TopologyFilterTrail(TopologyFilter):
queryset = Trail.objects.existing()
Expand Down
16 changes: 4 additions & 12 deletions geotrek/core/managers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from django.contrib.gis.db import models

from geotrek.common.functions import Length
from geotrek.common.mixins.managers import NoDeleteManager
from geotrek.common.mixins.managers import NoDeleteManager, ProviderChoicesMixin


class PathManager(models.Manager):
class PathManager(models.Manager, ProviderChoicesMixin):
# Use this manager when walking through FK/M2M relationships
use_for_related_fields = True

Expand All @@ -13,11 +13,6 @@ def get_queryset(self):
"""
return super().get_queryset().filter(visible=True).annotate(length_2d=Length('geom'))

def provider_choices(self):
providers = self.get_queryset().exclude(provider__exact='') \
.distinct('provider').values_list('provider', 'provider')
return providers


class PathInvisibleManager(models.Manager):
use_for_related_fields = True
Expand All @@ -39,8 +34,5 @@ def get_queryset(self):
return super().get_queryset().order_by('order')


class TrailManager(TopologyManager):
def provider_choices(self):
providers = self.get_queryset().existing().exclude(provider__exact='').order_by('provider') \
.distinct('provider').values_list('provider', 'provider')
return providers
class TrailManager(TopologyManager, ProviderChoicesMixin):
pass
51 changes: 51 additions & 0 deletions geotrek/core/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from geotrek.authent.tests.base import AuthentFixturesTest

from geotrek.core.models import Path, Trail, PathSource
from geotrek.core.filters import PathFilterSet, TrailFilterSet

from geotrek.trekking.tests.factories import POIFactory, TrekFactory, ServiceFactory
from geotrek.infrastructure.tests.factories import InfrastructureFactory
Expand Down Expand Up @@ -873,3 +874,53 @@ def test_remove_poi(self):
self.assertEqual(poi.deleted, False)

self.assertAlmostEqual(1.5, poi.offset)


class PathFilterTest(CommonTest, AuthentFixturesTest):
factory = PathFactory
filterset = PathFilterSet

def test_provider_filter_without_provider(self):
filter_set = PathFilterSet(data={})
filter_form = filter_set.form

self.assertTrue(filter_form.is_valid())
self.assertEqual(0, filter_set.qs.count())

def test_provider_filter_with_providers(self):
path1 = PathFactory.create(provider='my_provider1')
path2 = PathFactory.create(provider='my_provider2')

filter_set = PathFilterSet()
filter_form = filter_set.form

self.assertIn('<option value="my_provider1">my_provider1</option>', filter_form.as_p())
self.assertIn('<option value="my_provider2">my_provider2</option>', filter_form.as_p())

self.assertIn(path1, filter_set.qs)
self.assertIn(path2, filter_set.qs)


class TrailFilterTest(CommonTest, AuthentFixturesTest):
factory = TrailFactory
filterset = TrailFilterSet

def test_provider_filter_without_provider(self):
filter_set = TrailFilterSet(data={})
filter_form = filter_set.form

self.assertTrue(filter_form.is_valid())
self.assertEqual(0, filter_set.qs.count())

def test_provider_filter_with_providers(self):
trail1 = TrailFactory.create(provider='my_provider1')
trail2 = TrailFactory.create(provider='my_provider2')

filter_set = TrailFilterSet()
filter_form = filter_set.form

self.assertIn('<option value="my_provider1">my_provider1</option>', filter_form.as_p())
self.assertIn('<option value="my_provider2">my_provider2</option>', filter_form.as_p())

self.assertIn(trail1, filter_set.qs)
self.assertIn(trail2, filter_set.qs)
3 changes: 2 additions & 1 deletion geotrek/infrastructure/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class InfrastructureFilterSet(AltimetryAllGeometriesFilterSet, ValidTopologyFilt
field_name='provider',
empty_label=_("Provider"),
label=_("Provider"),
choices=Infrastructure.objects.provider_choices()
choices=(('', '---------'),)
)

class Meta(StructureRelatedFilterSet.Meta):
Expand All @@ -43,3 +43,4 @@ def filter_intervention_year(self, qs, name, value):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.form.fields['implantation_year'].choices = Infrastructure.objects.implantation_year_choices()
self.form.fields['provider'].choices = Infrastructure.objects.provider_choices()
9 changes: 2 additions & 7 deletions geotrek/infrastructure/managers.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
from geotrek.common.mixins.managers import NoDeleteManager
from geotrek.common.mixins.managers import NoDeleteManager, ProviderChoicesMixin


class InfrastructureGISManager(NoDeleteManager):
class InfrastructureGISManager(NoDeleteManager, ProviderChoicesMixin):
""" Override default typology mixin manager"""
def implantation_year_choices(self):
values = self.get_queryset().existing().filter(implantation_year__isnull=False)\
.order_by('-implantation_year').distinct('implantation_year') \
.values_list('implantation_year', flat=True)
return tuple((value, value) for value in values)

def provider_choices(self):
providers = self.get_queryset().existing().exclude(provider__exact='') \
.distinct('provider').values_list('provider', 'provider')
return providers
26 changes: 26 additions & 0 deletions geotrek/infrastructure/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from geotrek.common.tests import CommonTest, GeotrekAPITestCase
from geotrek.authent.tests.factories import PathManagerFactory
from geotrek.infrastructure.models import (Infrastructure, INFRASTRUCTURE_TYPES)
from geotrek.infrastructure.filters import InfrastructureFilterSet
from geotrek.core.tests.factories import PathFactory
from geotrek.infrastructure.tests.factories import (InfrastructureFactory, InfrastructureNoPictogramFactory,
InfrastructureTypeFactory, InfrastructureConditionFactory,
Expand Down Expand Up @@ -116,3 +117,28 @@ def get_good_data(self):
else:
good_data['geom'] = 'POINT(0.42 0.666)'
return good_data


class InfrastructureFilterTest(CommonTest):
factory = InfrastructureFactory
filterset = InfrastructureFilterSet

def test_provider_filter_without_provider(self):
filter_set = InfrastructureFilterSet(data={})
filter_form = filter_set.form

self.assertTrue(filter_form.is_valid())
self.assertEqual(0, filter_set.qs.count())

def test_provider_filter_with_providers(self):
infrastructure1 = InfrastructureFactory.create(provider='my_provider1')
infrastructure2 = InfrastructureFactory.create(provider='my_provider2')

filter_set = InfrastructureFilterSet()
filter_form = filter_set.form

self.assertIn('<option value="my_provider1">my_provider1</option>', filter_form.as_p())
self.assertIn('<option value="my_provider2">my_provider2</option>', filter_form.as_p())

self.assertIn(infrastructure1, filter_set.qs)
self.assertIn(infrastructure2, filter_set.qs)
12 changes: 10 additions & 2 deletions geotrek/outdoor/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class SiteFilterSet(ZoningFilterSet, StructureRelatedFilterSet):
field_name='provider',
empty_label=_("Provider"),
label=_("Provider"),
choices=Site.objects.provider_choices()
choices=(('', '---------'),)
)

class Meta(StructureRelatedFilterSet.Meta):
Expand All @@ -27,6 +27,10 @@ class Meta(StructureRelatedFilterSet.Meta):
'web_links', 'type', 'orientation', 'wind', 'provider'
]

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.form.fields['provider'].choices = Site.objects.provider_choices()

def filter_orientation(self, qs, name, values):
q = Q()
for value in values:
Expand Down Expand Up @@ -58,7 +62,7 @@ class CourseFilterSet(ZoningFilterSet, StructureRelatedFilterSet):
field_name='provider',
empty_label=_("Provider"),
label=_("Provider"),
choices=Course.objects.provider_choices()
choices=(('', '---------'),)
)

class Meta(StructureRelatedFilterSet.Meta):
Expand All @@ -69,6 +73,10 @@ class Meta(StructureRelatedFilterSet.Meta):
'height', 'provider'
]

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.form.fields['provider'].choices = Course.objects.provider_choices()

def filter_orientation(self, qs, name, values):
q = Q()
for value in values:
Expand Down
16 changes: 6 additions & 10 deletions geotrek/outdoor/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
from django.db.models import Manager
from mptt.managers import TreeManager

from geotrek.common.mixins.managers import ProviderChoicesMixin

class SiteManager(TreeManager):
def provider_choices(self):
providers = self.get_queryset().exclude(provider__exact='').order_by('provider') \
.distinct('provider').values_list('provider', 'provider')
return providers

class SiteManager(TreeManager, ProviderChoicesMixin):
pass


class CourseOrderedChildManager(models.Manager):
Expand All @@ -18,8 +17,5 @@ def get_queryset(self):
return super(CourseOrderedChildManager, self).get_queryset().select_related('parent', 'child')


class CourseManager(Manager):
def provider_choices(self):
providers = self.get_queryset().exclude(provider__exact='').order_by('provider') \
.distinct('provider').values_list('provider', 'provider')
return providers
class CourseManager(Manager, ProviderChoicesMixin):
pass
51 changes: 51 additions & 0 deletions geotrek/outdoor/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
TargetPortalFactory)
from geotrek.outdoor import views as course_views
from geotrek.outdoor.models import Site
from geotrek.outdoor.filters import SiteFilterSet, CourseFilterSet
from geotrek.outdoor.tests.factories import CourseFactory, SiteFactory
from geotrek.tourism.tests.test_views import PNG_BLACK_PIXEL
from geotrek.trekking.tests.factories import POIFactory
Expand Down Expand Up @@ -161,3 +162,53 @@ def test_delete_site(self):
self.assertEqual(response.status_code, 302)
self.assertEqual(Site.objects.count(), 1)
self.assertEqual(Site.objects.filter(pk=site_1.pk).exists(), True)


class SiteFilterTest(TestCase):
factory = SiteFactory
filterset = SiteFilterSet

def test_provider_filter_without_provider(self):
filter_set = SiteFilterSet(data={})
filter_form = filter_set.form

self.assertTrue(filter_form.is_valid())
self.assertEqual(0, filter_set.qs.count())

def test_provider_filter_with_providers(self):
site1 = SiteFactory.create(provider='my_provider1')
site2 = SiteFactory.create(provider='my_provider2')

filter_set = SiteFilterSet()
filter_form = filter_set.form

self.assertIn('<option value="my_provider1">my_provider1</option>', filter_form.as_p())
self.assertIn('<option value="my_provider2">my_provider2</option>', filter_form.as_p())

self.assertIn(site1, filter_set.qs)
self.assertIn(site2, filter_set.qs)


class CourseFilterTest(TestCase):
factory = CourseFactory
filterset = CourseFilterSet

def test_provider_filter_without_provider(self):
filter_set = CourseFilterSet(data={})
filter_form = filter_set.form

self.assertTrue(filter_form.is_valid())
self.assertEqual(0, filter_set.qs.count())

def test_provider_filter_with_providers(self):
course1 = CourseFactory.create(provider='my_provider1')
course2 = CourseFactory.create(provider='my_provider2')

filter_set = CourseFilterSet()
filter_form = filter_set.form

self.assertIn('<option value="my_provider1">my_provider1</option>', filter_form.as_p())
self.assertIn('<option value="my_provider2">my_provider2</option>', filter_form.as_p())

self.assertIn(course1, filter_set.qs)
self.assertIn(course2, filter_set.qs)
6 changes: 5 additions & 1 deletion geotrek/sensitivity/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ class SensitiveAreaFilterSet(StructureRelatedFilterSet):
field_name='provider',
empty_label=_("Provider"),
label=_("Provider"),
choices=SensitiveArea.objects.provider_choices()
choices=(('', '---------'),)
)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.form.fields['provider'].choices = SensitiveArea.objects.provider_choices()

class Meta(StructureRelatedFilterSet.Meta):
model = SensitiveArea
fields = StructureRelatedFilterSet.Meta.fields + [
Expand Down
9 changes: 3 additions & 6 deletions geotrek/sensitivity/managers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
from geotrek.common.mixins.managers import NoDeleteManager
from geotrek.common.mixins.managers import NoDeleteManager, ProviderChoicesMixin


class SensitiveAreaManager(NoDeleteManager):
def provider_choices(self):
providers = self.get_queryset().existing().exclude(provider__exact='') \
.distinct('provider').values_list('provider', 'provider')
return providers
class SensitiveAreaManager(NoDeleteManager, ProviderChoicesMixin):
pass
Loading

0 comments on commit 16ca567

Please sign in to comment.