Skip to content

Commit

Permalink
Refactor the implementation to fit with the same pattern as other log…
Browse files Browse the repository at this point in the history
…s mechanisms, fix tests
  • Loading branch information
Guitlle committed Nov 19, 2024
1 parent 333bdaa commit 1b4759c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 15 deletions.
1 change: 1 addition & 0 deletions kobo/apps/audit_log/base_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class AuditLoggedViewSet(viewsets.GenericViewSet):
def initialize_request(self, request, *args, **kwargs):
request = super().initialize_request(request, *args, **kwargs)
request._request.log_type = self.log_type
request._request._data = request.data.copy()
return request

def get_object(self):
Expand Down
7 changes: 6 additions & 1 deletion kobo/apps/audit_log/middleware.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from rest_framework import status

from kobo.apps.audit_log.models import AuditType, ProjectHistoryLog


Expand All @@ -7,7 +9,10 @@ def create_audit_logs(request):
if request.method in ['GET', 'HEAD']:
return response
log_type = getattr(request, 'log_type', None)
if log_type == AuditType.PROJECT_HISTORY:
if (
status.is_success(response.status_code) and
log_type == AuditType.PROJECT_HISTORY
):
ProjectHistoryLog.create_from_request(request)
return response

Expand Down
20 changes: 15 additions & 5 deletions kobo/apps/audit_log/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ def create_from_request(cls, request):
'asset-file-list': cls.create_from_file_request,
'asset-export-list': cls.create_from_export_request,
'exporttask-list': cls.create_from_v1_export,
'asset-bulk': cls.create_from_bulk_request,
}
url_name = request.resolver_match.url_name
method = url_name_to_action.get(url_name, None)
Expand All @@ -343,22 +344,31 @@ def create_from_request(cls, request):
method(request)

@staticmethod
def create_from_bulk_action(request, payload):
def create_from_bulk_request(request):
try:
payload = request._data['payload']
action = payload['action']
asset_uids = payload['asset_uids']
except KeyError:
return # Incorrect payload

if type(asset_uids) is not list or len(asset_uids) == 0: # Nothing to do
return

bulk_action_to_audit_action = {
'archive': AuditAction.ARCHIVE,
'unarchive': AuditAction.UNARCHIVE,
'delete': AuditAction.DELETE,
'undelete': AuditAction.UNDELETE,
}
audit_action = bulk_action_to_audit_action[payload.get('action')]
audit_action = bulk_action_to_audit_action[action]
if audit_action is None:
# Unsupported action
return
return # Unsupported action

source = get_human_readable_client_user_agent(request)
client_ip = get_client_ip(request)

for asset_uid in payload.get('asset_uids', []):
for asset_uid in asset_uids:
asset = Asset.all_objects.get(uid=asset_uid)
object_id = asset.pk
metadata = {
Expand Down
8 changes: 5 additions & 3 deletions kobo/apps/audit_log/tests/test_project_history_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,6 @@ def test_export_v1_creates_log(self):
self.assertEqual(log.object_id, self.asset.id)

@data(
# File or url, change asset name?, use v2?
('archive', AuditAction.ARCHIVE),
('unarchive', AuditAction.UNARCHIVE),
('undelete', AuditAction.UNDELETE),
Expand All @@ -909,8 +908,11 @@ def test_bulk_actions(self, bulk_action, audit_action):

uids = [asset.uid for asset in assets]

if bulk_action == 'undelete':
self._make_bulk_request(uids, 'delete')

self._make_bulk_request(uids, bulk_action)
archived_logs = ProjectHistoryLog.objects.filter(
project_hist_logs = ProjectHistoryLog.objects.filter(
object_id__in=[asset.id for asset in assets], action=audit_action
)
self.assertEqual(archived_logs.count(), 2)
self.assertEqual(project_hist_logs.count(), 2)
6 changes: 0 additions & 6 deletions kpi/serializers/v2/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from rest_framework.reverse import reverse
from rest_framework.utils.serializer_helpers import ReturnList

from kobo.apps.audit_log.models import ProjectHistoryLog
from kobo.apps.organizations.constants import ORG_ADMIN_ROLE
from kobo.apps.reports.constants import FUZZY_VERSION_PATTERN
from kobo.apps.reports.report_data import build_formpack
Expand Down Expand Up @@ -89,11 +88,6 @@ def __init__(
super().__init__(instance=instance, data=data, **kwargs)

def create(self, validated_data):
ProjectHistoryLog.create_from_bulk_action(
self.context['request'],
validated_data['payload'],
)

delete_request, put_back_ = self._get_action_type_and_direction(
validated_data['payload']
)
Expand Down

0 comments on commit 1b4759c

Please sign in to comment.