Skip to content

Commit

Permalink
[#3889] Add a log entry when a log entry is viewed
Browse files Browse the repository at this point in the history
  • Loading branch information
Viicos committed Apr 3, 2024
1 parent 06aec02 commit 96645a8
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
20 changes: 20 additions & 0 deletions src/openforms/logging/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
from typing import Any

from django.contrib import admin
from django.http import Http404
from django.http.request import HttpRequest
from django.http.response import HttpResponse

from import_export import resources
from import_export.admin import ExportActionModelAdmin
Expand All @@ -7,6 +12,8 @@

from openforms.logging.models import AVGTimelineLogProxy, TimelineLogProxy

from .logevent import timelinelog_details_view_admin


class TimelineLogProxyResource(resources.ModelResource):
user = Field(attribute="user")
Expand Down Expand Up @@ -68,6 +75,19 @@ def has_change_permission(self, request, obj=None):
def has_delete_permission(self, request, obj=None):
return False

def change_view(
self,
request: HttpRequest,
object_id: str,
form_url: str = "",
extra_context: dict[str, Any] | None = None,
) -> HttpResponse:
timelinelog = self.get_object(request, object_id)
if timelinelog is None:
raise Http404(f"No {self.model._meta.object_name} matches the given query.")

Check warning on line 87 in src/openforms/logging/admin.py

View check run for this annotation

Codecov / codecov/patch

src/openforms/logging/admin.py#L87

Added line #L87 was not covered by tests
timelinelog_details_view_admin(timelinelog, request.user)
return super().change_view(request, object_id, form_url, extra_context)


@admin.register(AVGTimelineLogProxy)
class AVGTimelineLogProxyAdmin(TimelineLogProxyAdmin):
Expand Down
12 changes: 12 additions & 0 deletions src/openforms/logging/logevent.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ def _create_log(
return log_entry


def timelinelog_details_view_admin(timelinelog_proxy: TimelineLogProxy, user: User):
_create_log(
timelinelog_proxy,
"timelinelog_details_view_admin",
tags=[TimelineLogTags.AVG],
user=user,
)


# - - -


def enabling_analytics_tool(
analytics_tools_configuration: AnalyticsToolsConfiguration, analytics_tool: str
):
Expand Down
2 changes: 1 addition & 1 deletion src/openforms/logging/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def fmt_url(self) -> str:
def content_admin_url(self) -> str:
if not (self.object_id and self.content_type_id):
return ""

breakpoint()

Check warning on line 129 in src/openforms/logging/models.py

View check run for this annotation

Codecov / codecov/patch

src/openforms/logging/models.py#L129

Added line #L129 was not covered by tests
ct = self.content_type
return reverse(
f"admin:{ct.app_label}_{ct.model}_change", args=(self.object_id,)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% load i18n %}
{% blocktrans trimmed with lead=log.fmt_lead user=log.fmt_user timelinelog=log.pk %}
{{ lead }}: User {{ user }} viewed timelinelog {{ timelinelog }} in the admin
{% endblocktrans %}
28 changes: 27 additions & 1 deletion src/openforms/logging/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
from django_webtest import WebTest
from maykin_2fa.test import disable_admin_mfa

from openforms.accounts.tests.factories import StaffUserFactory, SuperUserFactory
from openforms.accounts.tests.factories import (
StaffUserFactory,
SuperUserFactory,
UserFactory,
)
from openforms.logging import logevent
from openforms.logging.models import TimelineLogProxy
from openforms.logging.tests.factories import TimelineLogProxyFactory
Expand Down Expand Up @@ -98,6 +102,28 @@ def test_deleted_submission_doesnt_crash_logs(self):
self.assertEqual(200, response.status_code)


@disable_admin_mfa()
class TimelineLogAdminTests(WebTest):
def test_viewing_timelinelog_details_in_admin_creates_log(self):
user = UserFactory.create(is_superuser=True, is_staff=True)
timeline_log = TimelineLogProxyFactory.create(user=user)

self.app.get(
reverse(
"admin:logging_timelinelogproxy_change",
kwargs={"object_id": timeline_log.id},
),
user=user,
)

self.assertEqual(
TimelineLogProxy.objects.filter(
template="logging/events/timelinelog_details_view_admin.txt"
).count(),
1,
)


class TimelineLogExportsTest(TestCase):
def test_bare_timelinelog_export(self):
user = StaffUserFactory.create()
Expand Down

0 comments on commit 96645a8

Please sign in to comment.