0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 11:41:20 +01:00

Set up a LogContext for the user on all admin views

This commit is contained in:
Matt Westcott 2021-06-21 22:17:28 +01:00 committed by Matt Westcott
parent 1a0e53ba09
commit 4126aff021
2 changed files with 27 additions and 26 deletions

View File

@ -14,6 +14,7 @@ from django.utils.translation import gettext as _
from django.utils.translation import override
from wagtail.admin import messages
from wagtail.core.log_actions import LogContext
from wagtail.core.models import GroupPagePermission
@ -167,30 +168,31 @@ def require_admin_access(view_func):
l18n.set_language(preferred_language)
time_zone = user.wagtail_userprofile.get_current_time_zone()
activate_tz(time_zone)
if preferred_language:
with override(preferred_language):
response = view_func(request, *args, **kwargs)
with LogContext(user=user):
if preferred_language:
with override(preferred_language):
response = view_func(request, *args, **kwargs)
if hasattr(response, "render"):
# If the response has a render() method, Django treats it
# like a TemplateResponse, so we should do the same
# In this case, we need to guarantee that when the TemplateResponse
# is rendered, it is done within the override context manager
# or the user preferred_language will not be used
# (this could be replaced with simply rendering the TemplateResponse
# for simplicity but this does remove some of its middleware modification
# potential)
render = response.render
if hasattr(response, "render"):
# If the response has a render() method, Django treats it
# like a TemplateResponse, so we should do the same
# In this case, we need to guarantee that when the TemplateResponse
# is rendered, it is done within the override context manager
# or the user preferred_language will not be used
# (this could be replaced with simply rendering the TemplateResponse
# for simplicity but this does remove some of its middleware modification
# potential)
render = response.render
def overridden_render(response):
with override(preferred_language):
return render()
def overridden_render(response):
with override(preferred_language):
return render()
response.render = types.MethodType(overridden_render, response)
# decorate the response render method with the override context manager
return response
else:
return view_func(request, *args, **kwargs)
response.render = types.MethodType(overridden_render, response)
# decorate the response render method with the override context manager
return response
else:
return view_func(request, *args, **kwargs)
except PermissionDenied:
if request.headers.get('x-requested-with') == 'XMLHttpRequest':

View File

@ -18,7 +18,7 @@ from wagtail.admin import messages
from wagtail.admin.edit_handlers import ObjectList, extract_panel_definitions_from_model_class
from wagtail.admin.forms.search import SearchForm
from wagtail.core import hooks
from wagtail.core.log_actions import LogContext, log
from wagtail.core.log_actions import log
from wagtail.core.models import Locale, TranslatableMixin
from wagtail.search.backends import get_search_backend
from wagtail.search.index import class_is_indexed
@ -212,8 +212,7 @@ def create(request, app_label, model_name):
if form.is_valid():
form.save()
with LogContext(user=request.user):
log(instance=instance, action='wagtail.create')
log(instance=instance, action='wagtail.create')
messages.success(
request,
@ -294,7 +293,7 @@ def edit(request, app_label, model_name, pk):
if form.is_valid():
form.save()
log(instance=instance, action='wagtail.edit', user=request.user)
log(instance=instance, action='wagtail.edit')
messages.success(
request,
@ -371,7 +370,7 @@ def delete(request, app_label, model_name, pk=None):
if request.method == 'POST':
for instance in instances:
log(instance=instance, action='wagtail.delete', user=request.user)
log(instance=instance, action='wagtail.delete')
instance.delete()
if count == 1: