Skip to content

Commit

Permalink
🐛 [Fix] intervention date filter and add intervention years filter
Browse files Browse the repository at this point in the history
  • Loading branch information
juggler31 committed Feb 8, 2024
1 parent 942174f commit 1dd46fe
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 16 deletions.
4 changes: 4 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ CHANGELOG

- Allow Apidae Trek parser to handle traces not in utf-8

**Bug fixes**

- Intervention date filter and add intervention years filter (#3825)

**Documentation**

- Improve performance in spatial intersection (zoning district and zoning city) for sql views (#3600)
Expand Down
16 changes: 12 additions & 4 deletions geotrek/maintenance/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,10 @@ class InterventionFilterSet(AltimetryInterventionFilterSet, ZoningFilterSet, Str
ON_CHOICES += (('course', _("Outdoor Course")), ('site', _("Outdoor Site")),)

bbox = PolygonTopologyFilter(lookup_expr='intersects')
begin_date = CustomDateFromToRangeFilter(lookup_expr='icontains', widget=OneLineRangeWidget(attrs={'type': 'date', 'class': 'minmax-field', 'title': _('Filter by begin date range')},), label=_('begin date'))
end_date = CustomDateFromToRangeFilter(lookup_expr='icontains', widget=OneLineRangeWidget(attrs={'type': 'date', 'class': 'minmax-field', 'title': _('Filter by end date range')},), label=_('end date'))
begin_date = CustomDateFromToRangeFilter(widget=OneLineRangeWidget(attrs={'type': 'text', 'class': 'minmax-field', 'title': _('Filter by begin date range')},), label=_('begin date'))
end_date = CustomDateFromToRangeFilter(widget=OneLineRangeWidget(attrs={'type': 'text', 'class': 'minmax-field', 'title': _('Filter by end date range')},), label=_('end date'))
year = MultipleChoiceFilter(choices=lambda: Intervention.objects.year_choices(),
method='filter_year', lookup_expr='year', label=_("Year"))
on = ChoiceFilter(field_name='target_type__model', choices=ON_CHOICES, label=_("On"), empty_label=_("On"))
area_type = InterventionIntersectionFilterRestrictedAreaType(label=_('Restricted area type'), required=False,
lookup_expr='intersects')
Expand All @@ -154,8 +156,14 @@ class Meta(StructureRelatedFilterSet.Meta):
'status', 'type', 'stake', 'subcontracting', 'project', 'on',
]

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def filter_year(self, qs, name, values):
conditions = Q()
for value in values:
# Filter only with precise begin year
conditions |= Q(begin_date__year=value, end_date__isnull=True)
# Filter year between begin and end date
conditions |= Q(begin_date__year__lte=value, end_date__year__gte=value)
return qs.filter(conditions)


class ProjectFilterSet(StructureRelatedFilterSet):
Expand Down
5 changes: 4 additions & 1 deletion geotrek/maintenance/locale/de/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2023-12-11 15:38+0000\n"
"POT-Creation-Date: 2024-02-05 08:27+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -83,6 +83,9 @@ msgstr ""
msgid "end date"
msgstr ""

msgid "Year"
msgstr ""

msgid "On"
msgstr ""

Expand Down
5 changes: 4 additions & 1 deletion geotrek/maintenance/locale/en/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2023-12-11 15:38+0000\n"
"POT-Creation-Date: 2024-02-05 08:27+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -83,6 +83,9 @@ msgstr ""
msgid "end date"
msgstr ""

msgid "Year"
msgstr ""

msgid "On"
msgstr ""

Expand Down
5 changes: 4 additions & 1 deletion geotrek/maintenance/locale/es/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2023-12-11 15:38+0000\n"
"POT-Creation-Date: 2024-02-05 08:27+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -83,6 +83,9 @@ msgstr ""
msgid "end date"
msgstr ""

msgid "Year"
msgstr ""

msgid "On"
msgstr ""

Expand Down
9 changes: 6 additions & 3 deletions geotrek/maintenance/locale/fr/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2023-12-11 15:38+0000\n"
"POT-Creation-Date: 2024-02-05 08:27+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -74,13 +74,16 @@ msgid "Filter by begin date range"
msgstr "Filtrer par intervalle de date de début"

msgid "begin date"
msgstr "date de début"
msgstr "date début"

msgid "Filter by end date range"
msgstr "Filtrer par intervalle de date de fin"

msgid "end date"
msgstr "date de fin"
msgstr "date fin"

msgid "Year"
msgstr "Année"

msgid "On"
msgstr "Sur"
Expand Down
5 changes: 4 additions & 1 deletion geotrek/maintenance/locale/it/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2023-12-11 15:38+0000\n"
"POT-Creation-Date: 2024-02-05 08:27+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -83,6 +83,9 @@ msgstr ""
msgid "end date"
msgstr ""

msgid "Year"
msgstr ""

msgid "On"
msgstr ""

Expand Down
5 changes: 4 additions & 1 deletion geotrek/maintenance/locale/nl/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2023-12-11 15:38+0000\n"
"POT-Creation-Date: 2024-02-05 08:27+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -83,6 +83,9 @@ msgstr ""
msgid "end date"
msgstr ""

msgid "Year"
msgstr ""

msgid "On"
msgstr ""

Expand Down
16 changes: 12 additions & 4 deletions geotrek/maintenance/managers.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
from django.db.models import Min, Max
from django.db.models.functions import ExtractYear
from django.db.models import Min, Max, Func, Case, When, IntegerField
from django.db.models.functions import ExtractYear, Cast

from geotrek.common.mixins.managers import NoDeleteManager


class InterventionManager(NoDeleteManager):
def year_choices(self):
values = self.existing().filter(begin_date__isnull=False).annotate(year=ExtractYear('begin_date')) \
.order_by('-year').distinct().values_list('year', flat=True)
""" Get all range years between begin_date and end_date and concatenates distinct years """
qs = (self.existing().all().annotate(
years=Func(
Cast(ExtractYear("begin_date"), output_field=IntegerField()),
Cast(Case(When(end_date__isnull=False, then=ExtractYear("end_date")),
default=ExtractYear("begin_date")),
output_field=IntegerField()),
function="generate_series"),
))
values = qs.distinct('years').order_by('years').values_list('years', flat=True)
return [(year, year) for year in values]


Expand Down
27 changes: 27 additions & 0 deletions geotrek/maintenance/static/maintenance/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,30 @@ $(window).on('entity:map', function (e, data) {
}
});
});


// Date picker with placeholder on input
function setDatePickerConfig(idList) {
$(idList).datepicker({
autoclose: true,
language: window.SETTINGS.languages.default,
format: window.SETTINGS.date_format
});
}
$(window).on('entity:view:add', function (e, data) {
if (data.modelname === "intervention"){
setDatePickerConfig('#id_begin_date, #id_end_date');
};
});

$(window).on('entity:view:list', function (e, data) {
if (data.modelname === "intervention"){
setDatePickerConfig('#id_begin_date_0, #id_begin_date_1, #id_end_date_0, #id_end_date_1');
};
});

$(window).on('entity:view:update', function (e, data) {
if (data.modelname === "intervention"){
setDatePickerConfig('#id_begin_date, #id_end_date');
};
});
24 changes: 24 additions & 0 deletions geotrek/maintenance/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,30 @@ def test_filter_zoning_area_type(self):
self.assertEqual(len(filter.qs), 0)


class InterventionDateFilterTest(TestCase):

def test_filter_year_without_end_date(self):
InterventionFactory(name="intervention1", begin_date="2020-07-30")
InterventionFactory(name="intervention1", begin_date="2021-07-30")
InterventionFactory(name="intervention1", begin_date="2022-07-30")
intervention_filter = InterventionFilterSet({'year': [2021]})
self.assertEqual(intervention_filter.qs.count(), 1)

def test_filter_year_with_range(self):
InterventionFactory(name="intervention1", begin_date="2020-07-30", end_date="2024-07-30")
InterventionFactory(name="intervention1", begin_date="2021-07-30")
InterventionFactory(name="intervention1", begin_date="2022-07-30", end_date="2024-07-30")
intervention_filter = InterventionFilterSet({'year': [2022]})
self.assertEqual(intervention_filter.qs.count(), 2)

def test_filter_year_with_end_date(self):
InterventionFactory(name="intervention1", begin_date="2020-07-30", end_date="2023-07-30")
InterventionFactory(name="intervention1", begin_date="2021-07-30")
InterventionFactory(name="intervention1", begin_date="2022-07-30", end_date="2024-07-30")
intervention_filter = InterventionFilterSet({'year': [2023, 2024]})
self.assertEqual(intervention_filter.qs.count(), 2)


@skipIf(not settings.TREKKING_TOPOLOGY_ENABLED, 'Test with dynamic segmentation only')
class InterventionFilteringByLandTest(TestCase):
@classmethod
Expand Down
26 changes: 26 additions & 0 deletions geotrek/maintenance/tests/test_managers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from django.test import TestCase
from geotrek.maintenance.models import Intervention
from geotrek.maintenance.tests.factories import InterventionFactory


class InterventionManagerTest(TestCase):

def test_intervention_with_not_end_date(self):
InterventionFactory(name="test1", begin_date="2020-06-03")
InterventionFactory(name="test2", begin_date="2022-06-03")
self.assertEqual(Intervention.objects.year_choices(), [(2020, 2020), (2022, 2022)])

def test_intervention_with_end_date(self):
InterventionFactory(name="test1", begin_date="2018-06-03", end_date="2020-06-03")
InterventionFactory(name="test2", begin_date="2022-06-03", end_date="2024-06-03")
self.assertEqual(Intervention.objects.year_choices(), [(2018, 2018), (2019, 2019), (2020, 2020), (2022, 2022), (2023, 2023), (2024, 2024)])

def test_intervention_with_one_have_end_date(self):
InterventionFactory(name="test1", begin_date="2020-06-03")
InterventionFactory(name="test2", begin_date="2022-06-03", end_date="2024-06-03")
self.assertEqual(Intervention.objects.year_choices(), [(2020, 2020), (2022, 2022), (2023, 2023), (2024, 2024)])

def test_intervention_return_distinct_year(self):
InterventionFactory(name="test1", begin_date="2020-06-03")
InterventionFactory(name="test2", begin_date="2018-06-03", end_date="2024-06-03")
self.assertEqual(Intervention.objects.year_choices(), [(2018, 2018), (2019, 2019), (2020, 2020), (2021, 2021), (2022, 2022), (2023, 2023), (2024, 2024)])

0 comments on commit 1dd46fe

Please sign in to comment.