Skip to content

Commit

Permalink
♻️ [#499] Refactor auditlog endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
SilviaAmAm committed Nov 26, 2024
1 parent 8e2983e commit 5152d26
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 43 deletions.
2 changes: 2 additions & 0 deletions backend/src/openarchiefbeheer/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
DestructionListViewSet,
ReviewResponseViewSet,
)
from openarchiefbeheer.logging.api.viewsets import LogsViewset
from openarchiefbeheer.selection.api.views import (
SelectionCountView,
SelectionSelectAllView,
Expand Down Expand Up @@ -71,6 +72,7 @@
basename="review-responses",
)
router.register(r"zaken", ZakenViewSet, basename="zaken")
router.register(r"logs", LogsViewset, basename="logs")

destruction_list_router = BulkNestedRouter(
router, r"destruction-lists", lookup="destruction_list"
Expand Down
16 changes: 0 additions & 16 deletions backend/src/openarchiefbeheer/destruction/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from rest_framework import serializers
from rest_framework.exceptions import ValidationError
from rest_framework.relations import SlugRelatedField
from timeline_logger.models import TimelineLog

from openarchiefbeheer.accounts.api.serializers import UserSerializer
from openarchiefbeheer.accounts.models import User
Expand Down Expand Up @@ -858,21 +857,6 @@ def create(self, validated_data: dict) -> ReviewResponse:
return review_response


class AuditTrailItemSerializer(serializers.ModelSerializer):
message = serializers.CharField(source="get_message")
user = UserSerializer()

class Meta:
model = TimelineLog
fields = (
"pk",
"timestamp",
"user",
"message",
"extra_data",
)


class AbortDestructionSerializer(serializers.Serializer):
comment = serializers.CharField(required=True, allow_blank=False)

Expand Down
21 changes: 0 additions & 21 deletions backend/src/openarchiefbeheer/destruction/api/viewsets.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from datetime import date, timedelta

from django.contrib.contenttypes.models import ContentType
from django.db import transaction
from django.db.models import Case, OuterRef, Prefetch, QuerySet, Subquery, Value, When
from django.shortcuts import get_object_or_404
Expand All @@ -13,7 +12,6 @@
from rest_framework.exceptions import ValidationError
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from timeline_logger.models import TimelineLog

from openarchiefbeheer.logging import logevent
from openarchiefbeheer.utils.paginators import PageNumberPagination
Expand Down Expand Up @@ -60,7 +58,6 @@
)
from .serializers import (
AbortDestructionSerializer,
AuditTrailItemSerializer,
CoReviewerAssignementSerializer,
DestructionListAssigneeReadSerializer,
DestructionListCoReviewSerializer,
Expand Down Expand Up @@ -197,12 +194,6 @@
request=ReassignementSerializer,
responses={200: None},
),
auditlog=extend_schema(
tags=["Destruction list"],
summary=_("Retrieve audit log."),
description=_("Retrieve the audit log for this destruction list."),
responses={200: AuditTrailItemSerializer(many=True)},
),
mark_ready_review=extend_schema(
tags=["Destruction list"],
summary=_("Mark as ready to review."),
Expand Down Expand Up @@ -258,8 +249,6 @@ def get_permissions(self):
permission_classes = [IsAuthenticated & CanMarkListAsFinal]
elif self.action == "reassign":
permission_classes = [IsAuthenticated & CanReassignDestructionList]
elif self.action == "auditlog":
permission_classes = [IsAuthenticated]
elif self.action == "mark_ready_review":
permission_classes = [IsAuthenticated & CanMarkAsReadyToReview]
elif self.action == "abort_destruction":
Expand Down Expand Up @@ -359,16 +348,6 @@ def reassign(self, request, *args, **kwargs):
)
return Response()

@action(detail=True, methods=["get"], name="audit_log")
def auditlog(self, request, *args, **kwargs):
destruction_list = self.get_object()
items = TimelineLog.objects.filter(
content_type=ContentType.objects.get_for_model(DestructionList),
object_id=destruction_list.pk,
)
serializer = AuditTrailItemSerializer(instance=items, many=True)
return Response(serializer.data)

@action(detail=True, methods=["post"], name="mark-ready-review")
def mark_ready_review(self, request, *args, **kwargs):
destruction_list = self.get_object()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,9 @@ def test_audit_log(self):
)
self.assertEqual(response.status_code, status.HTTP_200_OK)

endpoint_audittrail = reverse(
"api:destructionlist-auditlog", kwargs={"uuid": destruction_list.uuid}
)
response_audittrail = self.client.get(endpoint_audittrail)
endpoint_audittrail = furl(reverse("api:logs-list"))
endpoint_audittrail.args["destruction_list"] = destruction_list.uuid
response_audittrail = self.client.get(endpoint_audittrail.url)

self.assertEqual(response_audittrail.status_code, status.HTTP_200_OK)

Expand Down
Empty file.
34 changes: 34 additions & 0 deletions backend/src/openarchiefbeheer/logging/api/filtersets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from django.contrib.contenttypes.models import ContentType
from django.db.models import QuerySet
from django.utils.translation import gettext_lazy as _

from django_filters import FilterSet, UUIDFilter
from timeline_logger.models import TimelineLog

from openarchiefbeheer.destruction.models import DestructionList


class LogsFilterset(FilterSet):
destruction_list = UUIDFilter(
field_name="destruction_list",
method="filter_destruction_list",
help_text=_("Retrieve the logs of a particular destruction list."),
)

class Meta:
model = TimelineLog
fields = ("destruction_list",)

def filter_destruction_list(
self, queryset: QuerySet[TimelineLog], name: str, value: str
) -> QuerySet[TimelineLog]:
destruction_list = DestructionList.objects.filter(uuid=value).first()
if not destruction_list:
return queryset.none()

content_type = ContentType.objects.get_for_model(destruction_list)

return queryset.filter(
content_type=content_type,
object_id=destruction_list.pk,
)
19 changes: 19 additions & 0 deletions backend/src/openarchiefbeheer/logging/api/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from rest_framework import serializers
from timeline_logger.models import TimelineLog

from openarchiefbeheer.accounts.api.serializers import UserSerializer


class AuditTrailItemSerializer(serializers.ModelSerializer):
message = serializers.CharField(source="get_message")
user = UserSerializer()

class Meta:
model = TimelineLog
fields = (
"pk",
"timestamp",
"user",
"message",
"extra_data",
)
23 changes: 23 additions & 0 deletions backend/src/openarchiefbeheer/logging/api/viewsets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from django.utils.translation import gettext_lazy as _

from django_filters.rest_framework import DjangoFilterBackend
from drf_spectacular.utils import extend_schema, extend_schema_view
from rest_framework import mixins
from rest_framework.viewsets import GenericViewSet
from timeline_logger.models import TimelineLog

from .filtersets import LogsFilterset
from .serializers import AuditTrailItemSerializer


@extend_schema_view(
list=extend_schema(
tags=["Logs"],
summary=_("List logs"),
),
)
class LogsViewset(mixins.ListModelMixin, GenericViewSet):
queryset = TimelineLog.objects.all()
serializer_class = AuditTrailItemSerializer
filter_backends = (DjangoFilterBackend,)
filterset_class = LogsFilterset
2 changes: 1 addition & 1 deletion frontend/.storybook/mockData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { zaaktypeChoicesFactory } from "../src/fixtures/zaaktypeChoices";

export const MOCKS = {
AUDIT_LOG: {
url: "http://localhost:8000/api/v1/destruction-lists/00000000-0000-0000-0000-000000000000/auditlog/",
url: "http://localhost:8000/api/v1/logs?destruction-list=00000000-0000-0000-0000-000000000000",
method: "GET",
status: 200,
response: auditLogFactory(),
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/lib/api/auditLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export type AuditLogItem = {
* @param uuid
*/
export async function listAuditLog(uuid: string) {
const response = await request("GET", `/destruction-lists/${uuid}/auditlog/`);
const response = await request("GET", "/logs/", { destruction_list: uuid });
const promise: AuditLogItem[] = await response.json();
return promise;
}

0 comments on commit 5152d26

Please sign in to comment.