diff --git a/kobo/apps/audit_log/serializers.py b/kobo/apps/audit_log/serializers.py index 5307018584..4130e47279 100644 --- a/kobo/apps/audit_log/serializers.py +++ b/kobo/apps/audit_log/serializers.py @@ -45,7 +45,7 @@ def get_date_created(self, audit_log): return audit_log.date_created.strftime('%Y-%m-%dT%H:%M:%SZ') def get_username(self, audit_log): - return audit_log.user.username + return getattr(audit_log.user, 'username', None) class AccessLogSerializer(serializers.Serializer): @@ -66,14 +66,7 @@ def get_date_created(self, audit_log): return audit_log['date_created'].strftime('%Y-%m-%dT%H:%M:%SZ') -class ProjectHistoryLogSerializer(serializers.ModelSerializer): - user = serializers.HyperlinkedRelatedField( - queryset=get_user_model().objects.all(), - lookup_field='username', - view_name='user-kpi-detail', - ) - date_created = serializers.SerializerMethodField() - username = serializers.SerializerMethodField() +class ProjectHistoryLogSerializer(AuditLogSerializer): class Meta: model = ProjectHistoryLog @@ -94,9 +87,3 @@ class Meta: 'metadata', 'date_created', ) - - def get_date_created(self, audit_log): - return audit_log.date_created.strftime('%Y-%m-%dT%H:%M:%SZ') - - def get_username(self, audit_log): - return audit_log.user.username diff --git a/kobo/apps/audit_log/tests/api/v2/test_api_audit_log.py b/kobo/apps/audit_log/tests/api/v2/test_api_audit_log.py index ff73c25cd1..b0a0d9f9d0 100644 --- a/kobo/apps/audit_log/tests/api/v2/test_api_audit_log.py +++ b/kobo/apps/audit_log/tests/api/v2/test_api_audit_log.py @@ -263,6 +263,26 @@ def test_filter_list(self): assert response.data['count'] == 1 assert response.data['results'] == expected + def test_view_log_from_deleted_user(self): + someuser = get_user_model().objects.get(username='someuser') + date_created = timezone.now().strftime('%Y-%m-%dT%H:%M:%SZ') + AuditLog.objects.create( + user=someuser, + app_label='foo', + model_name='bar', + object_id=1, + date_created=date_created, + action=AuditAction.UPDATE, + log_type=AuditType.DATA_EDITING, + ) + someuser.delete() + self.login_user(username='adminuser', password='pass') + response = self.client.get(self.url) + assert response.status_code == status.HTTP_200_OK + assert response.data['count'] == 1 + assert response.data['results'][0]['username'] is None + assert response.data['results'][0]['user'] is None + class ApiAccessLogTestCase(BaseAuditLogTestCase):