Skip to content

Commit

Permalink
Merge pull request #3919 from GeotrekCE/all_intervention_display_on_p…
Browse files Browse the repository at this point in the history
…roject_map_issue_3878

🐛 [Fix] intervention date filter and add intervention years filter
  • Loading branch information
juggler31 authored Feb 16, 2024
2 parents 22461b7 + 953b89a commit 566db8c
Show file tree
Hide file tree
Showing 12 changed files with 120 additions and 10 deletions.
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ CHANGELOG
**Bug fixes**

- Signage & Blade conditions translations + admin BladeCondition entry (#3847)
- Add intervention date filter and add intervention years filter (#3825)

**Documentation**

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', 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
3 changes: 3 additions & 0 deletions geotrek/maintenance/locale/de/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ msgstr ""
msgid "end date"
msgstr ""

msgid "Year"
msgstr ""

msgid "On"
msgstr ""

Expand Down
3 changes: 3 additions & 0 deletions geotrek/maintenance/locale/en/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ msgstr ""
msgid "end date"
msgstr ""

msgid "Year"
msgstr ""

msgid "On"
msgstr ""

Expand Down
3 changes: 3 additions & 0 deletions geotrek/maintenance/locale/es/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ msgstr ""
msgid "end date"
msgstr ""

msgid "Year"
msgstr ""

msgid "On"
msgstr ""

Expand Down
7 changes: 5 additions & 2 deletions geotrek/maintenance/locale/fr/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
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
3 changes: 3 additions & 0 deletions geotrek/maintenance/locale/it/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ msgstr ""
msgid "end date"
msgstr ""

msgid "Year"
msgstr ""

msgid "On"
msgstr ""

Expand Down
3 changes: 3 additions & 0 deletions geotrek/maintenance/locale/nl/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
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');
};
});
23 changes: 23 additions & 0 deletions geotrek/maintenance/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,29 @@ 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
25 changes: 25 additions & 0 deletions geotrek/maintenance/tests/test_managers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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(), [(2022, 2022), (2020, 2020)])

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(), [(2024, 2024), (2023, 2023), (2022, 2022), (2020, 2020), (2019, 2019), (2018, 2018)])

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(), [(2024, 2024), (2023, 2023), (2022, 2022), (2020, 2020)])

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(), [(2024, 2024), (2023, 2023), (2022, 2022), (2021, 2021), (2020, 2020), (2019, 2019), (2018, 2018)])

0 comments on commit 566db8c

Please sign in to comment.