Skip to content

Commit

Permalink
fixup!: be better about anonymous users
Browse files Browse the repository at this point in the history
  • Loading branch information
rgraber committed Jan 13, 2025
1 parent bcba30b commit 4cf6cba
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 24 deletions.
15 changes: 6 additions & 9 deletions kobo/apps/audit_log/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from kpi.fields.kpi_uid import UUID_LENGTH
from kpi.models import Asset, ImportTask
from kpi.utils.log import logging
from kpi.utils.object_permission import get_database_user

ANONYMOUS_USER_PERMISSION_ACTIONS = {
# key: (permission, granting?), value: ph log action
Expand Down Expand Up @@ -81,7 +82,7 @@ class AuditLog(models.Model):
db_index=True,
)
user_uid = models.CharField(
db_index=True, max_length=UUID_LENGTH + 1, null=True
db_index=True, max_length=UUID_LENGTH + 1
) # 1 is prefix length
log_type = models.CharField(choices=AuditType.choices, db_index=True)

Expand Down Expand Up @@ -610,7 +611,7 @@ def _create_from_submission_request(cls, request):
instances: dict[int:SubmissionUpdate] = getattr(request, 'instances', {})
logs = []
url_name = request.resolver_match.url_name

user = get_database_user(request.user)
for instance in instances.values():
if instance.action == 'add':
action = AuditAction.ADD_SUBMISSION
Expand All @@ -627,17 +628,13 @@ def _create_from_submission_request(cls, request):
}
if 'validation-status' in url_name:
metadata['submission']['status'] = instance.status
user_uid = (
request.user.extra_details.uid
if not request.user.is_anonymous
else None
)

logs.append(
ProjectHistoryLog(
user=request.user if not request.user.is_anonymous else None,
user=user,
object_id=request.asset.id,
action=action,
user_uid=user_uid,
user_uid=user.extra_details.uid,
metadata=metadata,
)
)
Expand Down
17 changes: 2 additions & 15 deletions kobo/apps/audit_log/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 getattr(audit_log.user, 'username', None)
return audit_log.user.username


class AccessLogSerializer(serializers.Serializer):
Expand All @@ -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
Expand All @@ -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 getattr(audit_log.user, 'username', None)

0 comments on commit 4cf6cba

Please sign in to comment.