Skip to content

Commit

Permalink
✨ [FEAT] Add filter to Cirkwi trek exports (refs #3947)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chatewgne committed Feb 23, 2024
1 parent c46c2e5 commit 6807709
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
5 changes: 5 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ CHANGELOG
2.102.1+dev (XXXX-XX-XX)
------------------------

**New features**

- Add `include_externals` filter to Cirkwi trek exports, to allow excluding treks with an external id (eid) (#3947)


2.102.1 (2024-02-20)
--------------------

Expand Down
9 changes: 9 additions & 0 deletions geotrek/cirkwi/filters.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from django.db.models.query_utils import Q
from django_filters import FilterSet
from django_filters.filters import BooleanFilter

from geotrek.authent.models import Structure
from geotrek.common.filters import ComaSeparatedMultipleModelChoiceFilter
from geotrek.common.models import TargetPortal
Expand All @@ -19,6 +22,12 @@ class CirkwiTrekFilterSet(FilterSet):
queryset=Structure.objects.all())
portals = ComaSeparatedMultipleModelChoiceFilter(field_name='portal', required=False,
queryset=TargetPortal.objects.all())
include_externals = BooleanFilter(field_name='eid', method='filter_include_externals', required=False)

def filter_include_externals(self, queryset, name, value):
if not value:
return queryset.filter(Q(eid__isnull=True) | Q(eid__exact=''))
return queryset

class Meta:
model = Trek
Expand Down
19 changes: 15 additions & 4 deletions geotrek/cirkwi/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
from django.contrib.gis.geos import LineString, Point
from django.test import TestCase

from geotrek.common.tests.factories import TargetPortalFactory

from geotrek.authent.tests.factories import StructureFactory
from geotrek.cirkwi.filters import CirkwiPOIFilterSet, CirkwiTrekFilterSet
from geotrek.common.tests.factories import TargetPortalFactory
from geotrek.core.tests.factories import PathFactory

from geotrek.trekking.tests.factories import POIFactory, TrekFactory
from geotrek.cirkwi.filters import CirkwiPOIFilterSet, CirkwiTrekFilterSet


class CirkwiFilterTest(TestCase):
Expand Down Expand Up @@ -46,6 +44,19 @@ def test_trek_filters_portals(self):
qs = CirkwiTrekFilterSet(data={"portals": 'a'}).qs
self.assertEqual(qs.count(), 1)

def test_treks_include_externals(self):
self.trek.eid = "test_eid"
self.trek.save()
# Treks with eid are not excluded by default
qs = CirkwiTrekFilterSet().qs
self.assertEqual(qs.count(), 1)
# Treks with eid are not excluded
qs = CirkwiTrekFilterSet(data={"include_externals": "true"}).qs
self.assertEqual(qs.count(), 1)
# Treks with eid are excluded
qs = CirkwiTrekFilterSet(data={"include_externals": "false"}).qs
self.assertEqual(qs.count(), 0)

def test_poi_filters_structures(self):
other_structure = StructureFactory.create()
qs = CirkwiPOIFilterSet(data={"structures": f'{self.poi.structure.pk}'}).qs
Expand Down
7 changes: 4 additions & 3 deletions geotrek/cirkwi/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from django.http import HttpResponse

from django.views.generic import ListView
from geotrek.trekking.models import Trek, POI

from geotrek.cirkwi.filters import CirkwiPOIFilterSet, CirkwiTrekFilterSet
from geotrek.cirkwi.serializers import CirkwiTrekSerializer, CirkwiPOISerializer
from geotrek.cirkwi.serializers import (CirkwiPOISerializer,
CirkwiTrekSerializer)
from geotrek.trekking.models import POI, Trek


class CirkwiTrekView(ListView):
Expand Down

0 comments on commit 6807709

Please sign in to comment.