From a39a4fd34b8ddcd360801048edfcb654e811239d Mon Sep 17 00:00:00 2001
From: Augustin-FL
Date: Mon, 6 Jan 2025 00:36:03 +0100
Subject: [PATCH] Display last comment date in incident API response, add
corresponding filters
---
fir_api/filters.py | 17 +++++++++++++++--
fir_api/locale/fr/LC_MESSAGES/django.po | 24 +++++++++++++++++++-----
fir_api/serializers.py | 1 +
fir_api/views.py | 15 +++------------
4 files changed, 38 insertions(+), 19 deletions(-)
diff --git a/fir_api/filters.py b/fir_api/filters.py
index 6f5a164e..1e20d4e0 100644
--- a/fir_api/filters.py
+++ b/fir_api/filters.py
@@ -1,4 +1,5 @@
from django.apps import apps
+from django.utils.translation import gettext_lazy as _
from django_filters.rest_framework import (
FilterSet,
DateTimeFilter,
@@ -70,7 +71,10 @@ class IncidentFilter(FilterSet):
subject = CharFilter(field_name="subject", lookup_expr="icontains")
status = ValueChoiceFilter(field_name="status", choices=STATUS_CHOICES)
status__not = ValueChoiceFilter(
- field_name="status", choices=STATUS_CHOICES, exclude=True
+ field_name="status",
+ choices=STATUS_CHOICES,
+ exclude=True,
+ label=_("Status is not"),
)
confidentiality = ValueChoiceFilter(
field_name="confidentiality", choices=CONFIDENTIALITY_LEVEL
@@ -91,12 +95,21 @@ class IncidentFilter(FilterSet):
)
is_incident = BooleanFilter(field_name="is_incident")
is_major = BooleanFilter(field_name="is_major")
+ last_comment_date_before = DateTimeFilter(
+ field_name="last_comment_date",
+ lookup_expr="lte",
+ label=_("Last comment date is less than or equal to"),
+ )
+ last_comment_date_after = DateTimeFilter(
+ field_name="last_comment_date",
+ lookup_expr="gte",
+ label=_("Last comment date is greater than or equal to"),
+ )
class Meta:
model = Incident
fields = [
"id",
- "date",
"subject",
"status",
"concerned_business_lines",
diff --git a/fir_api/locale/fr/LC_MESSAGES/django.po b/fir_api/locale/fr/LC_MESSAGES/django.po
index f47a3c96..63ed6fd5 100644
--- a/fir_api/locale/fr/LC_MESSAGES/django.po
+++ b/fir_api/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: 2024-12-29 06:34-0500\n"
+"POT-Creation-Date: 2025-01-07 21:46+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME \n"
"Language-Team: LANGUAGE \n"
@@ -18,6 +18,18 @@ msgstr ""
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"
+#: filters.py:77
+msgid "Status is not"
+msgstr "Status n'est pas"
+
+#: filters.py:101
+msgid "Last comment date is less than or equal to"
+msgstr "Date du dernier commentaire est inférieure ou égale à"
+
+#: filters.py:106
+msgid "Last comment date is greater than or equal to"
+msgstr "Date du dernier commentaire est supérieure ou égale à"
+
#: templates/fir_api/plugins/user_profile.html:7
msgid "API Access"
msgstr "Accès à l'API"
@@ -26,15 +38,17 @@ msgstr "Accès à l'API"
msgid ""
"You need to be authenticated in order to use the API. It will accept session "
"or token based authentication."
-msgstr "Vous devez être authentifié pour utiliser l'API, à l'aide d'un jeton "
-"ou d'une session."
+msgstr ""
+"Vous devez être authentifié pour utiliser l'API, à l'aide d'un jeton ou "
+"d'une session."
#: templates/fir_api/plugins/user_profile.html:10
msgid ""
"Tokens can be managed in the administration interface and should be "
"specified as a request header.
"
-msgstr "Les jetons peuvent être gérés dans l'interface d'administration et doivent être "
-"spécifiés dans les en-têtes des requêtes."
+msgstr ""
+"Les jetons peuvent être gérés dans l'interface d'administration et doivent "
+"être spécifiés dans les en-têtes des requêtes."
#: templates/fir_api/plugins/user_profile.html:11
msgid "Example:"
diff --git a/fir_api/serializers.py b/fir_api/serializers.py
index 5523e24f..3e16558d 100644
--- a/fir_api/serializers.py
+++ b/fir_api/serializers.py
@@ -244,6 +244,7 @@ class IncidentSerializer(serializers.ModelSerializer):
description = serializers.CharField(
style={"base_template": "textarea.html"}, required=False
)
+ last_comment_date = serializers.DateTimeField(read_only=True)
if apps.is_installed("fir_todos"):
todoitem_set = TodoSerializer(many=True, read_only=True)
diff --git a/fir_api/views.py b/fir_api/views.py
index 4f93cc4d..eb33e129 100644
--- a/fir_api/views.py
+++ b/fir_api/views.py
@@ -25,16 +25,7 @@
from rest_framework import renderers
from rest_framework.response import Response
from rest_framework.filters import OrderingFilter
-from django_filters.rest_framework import (
- DjangoFilterBackend,
- FilterSet,
- DateTimeFilter,
- CharFilter,
- NumberFilter,
- MultipleChoiceFilter,
- AllValuesFilter,
- BooleanFilter,
-)
+from django_filters.rest_framework import DjangoFilterBackend
from fir_api.serializers import (
UserSerializer,
@@ -94,8 +85,8 @@ class IncidentViewSet(
API endpoints for viewing, creating and editing incidents
"""
- queryset = (
- Incident.objects.all()
+ queryset = (Incident.objects.all()).annotate(
+ last_comment_date=Max("comments__date")
) # Will be overriden by get_queryset(). We still need to define this property as DRF use it to get the basename
serializer_class = IncidentSerializer
permission_classes = (IsAuthenticated, IsIncidentHandler)