diff --git a/georiviere/contribution/filters.py b/georiviere/contribution/filters.py index 9fb68671..affd7b5f 100644 --- a/georiviere/contribution/filters.py +++ b/georiviere/contribution/filters.py @@ -1,18 +1,74 @@ -from django_filters import CharFilter +import functools +import operator + +from django.db.models import Q +from django_filters import MultipleChoiceFilter from django.utils.translation import gettext_lazy as _ from mapentity.filters import MapEntityFilterSet, PythonPolygonFilter from geotrek.zoning.filters import ZoningFilterSet -from georiviere.contribution.models import Contribution +from georiviere.contribution.models import ( + Contribution, ContributionQuantity, ContributionQuality, ContributionFaunaFlora, ContributionLandscapeElements, + ContributionPotentialDamage +) + from georiviere.watershed.filters import WatershedFilterSet +class ContributionCategoryFilter(MultipleChoiceFilter): + def __init__(self, *args, **kwargs): + self.choices = [ + ('potential_damage', _('Potential damage')), + ('fauna_flora', _('Fauna flora')), + ('quality', _('Quality')), + ('quantity', _('Quantity')), + ('landscape_element', _('Landscape elements')), + ] + super().__init__(choices=self.choices, *args, **kwargs) + + def filter(self, qs, values): + if not values: + return qs + qs = qs.filter(functools.reduce(operator.or_, [Q(**{f'{value}__isnull': False}) for value in values])) + return qs + + +class ContributionTypeFilter(MultipleChoiceFilter): + choices_category = {} + + def __init__(self, *args, **kwargs): + self.choices_category = { + ContributionQuantity: {value: key for key, value in ContributionQuantity.TypeChoice.choices}, + ContributionQuality: {value: key for key, value in ContributionQuality.TypeChoice.choices}, + ContributionFaunaFlora: {value: key for key, value in ContributionFaunaFlora.TypeChoice.choices}, + ContributionLandscapeElements: {value: key for key, value in ContributionLandscapeElements.TypeChoice.choices}, + ContributionPotentialDamage: {value: key for key, value in ContributionPotentialDamage.TypeChoice.choices}, + } + choices = ContributionQuantity.TypeChoice.labels + \ + ContributionQuality.TypeChoice.labels + \ + ContributionFaunaFlora.TypeChoice.labels + \ + ContributionLandscapeElements.TypeChoice.labels + \ + ContributionPotentialDamage.TypeChoice.labels + self.choices = [(value, value) for value in choices] + super().__init__(choices=self.choices, *args, **kwargs) + + def filter(self, qs, values): + if not values: + return qs + contributions = [] + for key_model, dict_value in self.choices_category.items(): + contributions.extend(list(key_model.objects.filter(type__in=[dict_value.get(i) for i in values if dict_value.get(i) is not None]).values_list('contribution_id', flat=True))) + qs = qs.filter(id__in=contributions) + return qs + + class ContributionFilterSet(WatershedFilterSet, ZoningFilterSet, MapEntityFilterSet): bbox = PythonPolygonFilter(field_name='geom') - name_author = CharFilter(label=_('Name author'), lookup_expr='icontains') + category_contribution = ContributionCategoryFilter(label=_('Category')) + type_contribution = ContributionTypeFilter(label=_('Type')) class Meta(MapEntityFilterSet.Meta): model = Contribution fields = MapEntityFilterSet.Meta.fields + [ - 'name_author', + "category_contribution", "type_contribution" ] diff --git a/georiviere/contribution/locale/en/LC_MESSAGES/django.po b/georiviere/contribution/locale/en/LC_MESSAGES/django.po index 343e58ca..f009e90e 100644 --- a/georiviere/contribution/locale/en/LC_MESSAGES/django.po +++ b/georiviere/contribution/locale/en/LC_MESSAGES/django.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2023-06-01 09:11+0000\n" +"POT-Creation-Date: 2023-07-24 12:00+0000\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -21,7 +21,25 @@ msgstr "" msgid "Contribution" msgstr "" -msgid "Name author" +msgid "Potential damage" +msgstr "" + +msgid "Fauna flora" +msgstr "" + +msgid "Quality" +msgstr "" + +msgid "Quantity" +msgstr "" + +msgid "Landscape elements" +msgstr "" + +msgid "Category" +msgstr "" + +msgid "Type" msgstr "" msgid "Label" @@ -33,6 +51,9 @@ msgstr "" msgid "Severity types" msgstr "" +msgid "Name author" +msgstr "" + msgid "First name author" msgstr "" @@ -117,9 +138,6 @@ msgstr "" msgid "Trampling by livestock (impacting)" msgstr "" -msgid "Type" -msgstr "" - msgid "Landing type" msgstr "" @@ -284,8 +302,5 @@ msgstr "" msgid "Knowledge" msgstr "" -msgid "Category" -msgstr "" - msgid "None" msgstr "" diff --git a/georiviere/contribution/locale/fr/LC_MESSAGES/django.po b/georiviere/contribution/locale/fr/LC_MESSAGES/django.po index 3f2b11c2..a7d87450 100644 --- a/georiviere/contribution/locale/fr/LC_MESSAGES/django.po +++ b/georiviere/contribution/locale/fr/LC_MESSAGES/django.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2023-06-01 09:11+0000\n" +"POT-Creation-Date: 2023-07-24 12:00+0000\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -21,8 +21,26 @@ msgstr "" msgid "Contribution" msgstr "Contribution" -msgid "Name author" -msgstr "Nom de l'auteur" +msgid "Potential damage" +msgstr "Dégâts potentiels" + +msgid "Fauna flora" +msgstr "Faune flore" + +msgid "Quality" +msgstr "Qualité" + +msgid "Quantity" +msgstr "Quantité" + +msgid "Landscape elements" +msgstr "Éléments paysagers" + +msgid "Category" +msgstr "Catégorie" + +msgid "Type" +msgstr "Type" msgid "Label" msgstr "Label" @@ -33,6 +51,9 @@ msgstr "Type de gravité" msgid "Severity types" msgstr "Types de gravité" +msgid "Name author" +msgstr "Nom de l'auteur" + msgid "First name author" msgstr "Prénom auteur" @@ -117,9 +138,6 @@ msgstr "Mortalité piscicole" msgid "Trampling by livestock (impacting)" msgstr "Piétinement par le bétail (impactant)" -msgid "Type" -msgstr "Type" - msgid "Landing type" msgstr "Type d'atterrissement" @@ -284,8 +302,5 @@ msgstr "Contentieux" msgid "Knowledge" msgstr "Connaissance" -msgid "Category" -msgstr "Catégorie" - msgid "None" msgstr "Aucun" diff --git a/georiviere/contribution/templates/contribution/contribution_list.html b/georiviere/contribution/templates/contribution/contribution_list.html index cf594b2c..3d0de1f5 100644 --- a/georiviere/contribution/templates/contribution/contribution_list.html +++ b/georiviere/contribution/templates/contribution/contribution_list.html @@ -1,3 +1,116 @@ {% extends "mapentity/mapentity_list.html" %} +{% load i18n %} {% block mainactions %} {% endblock %} + +{% block extrabody %} + {{ block.super }} + + +{% endblock extrabody %} diff --git a/georiviere/contribution/tests/test_filters.py b/georiviere/contribution/tests/test_filters.py new file mode 100644 index 00000000..1989d4ea --- /dev/null +++ b/georiviere/contribution/tests/test_filters.py @@ -0,0 +1,28 @@ +from django.test import TestCase + +from georiviere.contribution.filters import ContributionFilterSet +from georiviere.contribution.tests.factories import ( + ContributionQualityFactory, ContributionQuantityFactory, ContributionPotentialDamageFactory +) + + +class ContributionFilterTest(TestCase): + @classmethod + def setUpTestData(cls): + cls.contribution_quality = ContributionQualityFactory.create() + cls.contribution_quantity = ContributionQuantityFactory.create() + cls.contribution_potential_damage = ContributionPotentialDamageFactory.create() + + def test_filter_category(self): + filter = ContributionFilterSet(data={'category_contribution': ['quality', 'potential_damage']}) + + self.assertIn(self.contribution_quality.contribution, filter.qs) + self.assertIn(self.contribution_potential_damage.contribution, filter.qs) + self.assertEqual(len(filter.qs), 2) + + def test_filter_type(self): + filter = ContributionFilterSet(data={'type_contribution': ['Dry', 'In the process of drying out']}) + + self.assertIn(self.contribution_quantity.contribution, filter.qs) + + self.assertEqual(len(filter.qs), 1) diff --git a/georiviere/contribution/views.py b/georiviere/contribution/views.py index 9b4b0cc0..88ce2af2 100644 --- a/georiviere/contribution/views.py +++ b/georiviere/contribution/views.py @@ -10,8 +10,7 @@ class ContributionList(MapEntityList): queryset = Contribution.objects.all() filterform = ContributionFilterSet - columns = ['id', 'portal', 'category', 'name_author', 'email_author', - 'date_observation', 'severity', 'published'] + columns = ['id', 'category', 'date_observation', 'severity', 'published'] class ContributionLayer(MapEntityLayer):