Skip to content

Commit

Permalink
add context manager option for model logging
Browse files Browse the repository at this point in the history
  • Loading branch information
felixrindt committed Jun 27, 2023
1 parent 50ebdc8 commit 601c466
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
9 changes: 7 additions & 2 deletions ephios/modellogging/log.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import contextlib
import itertools
import threading
from typing import Dict
Expand All @@ -15,6 +16,8 @@


class BaseLogConfig:
log_entry_context_manager = contextlib.nullcontext

def initial_log_recorders(self, instance):
"""
Initial log recorders are always added after model __init__.
Expand Down Expand Up @@ -129,6 +132,7 @@ def update_log(instance, action_type: InstanceActionType):
if not log_data:
return

config = LOGGED_MODELS[type(instance)]
if logentry:
logentry.data.update(log_data)
else:
Expand All @@ -140,7 +144,6 @@ def update_log(instance, action_type: InstanceActionType):
except AttributeError:
user = None
request_id = None
config = LOGGED_MODELS[type(instance)]
attach_to_model, attached_to_object_id = config.object_to_attach_logentries_to(instance)
attached_to_object_type = ContentType.objects.get_for_model(attach_to_model)
logentry = LogEntry(
Expand All @@ -152,7 +155,9 @@ def update_log(instance, action_type: InstanceActionType):
action_type=action_type,
data=log_data,
)
logentry.save()

with config.log_entry_context_manager():
logentry.save()
instance._current_logentry = logentry


Expand Down
2 changes: 1 addition & 1 deletion tests/api/test_user_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def test_manager_can_revoke_password_and_user_token(django_app, user_token, grou
assert not AccessToken.objects.get().is_valid()


def test_inactive_accounts_tokens_dont_work(django_app, user_token):
def test_inactive_accounts_tokens_dont_work(django_app, user_token: AccessToken):
user_token.user.is_active = False
user_token.user.save()
django_app.get(
Expand Down

0 comments on commit 601c466

Please sign in to comment.