0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-30 01:46:24 +01:00

Add .get_users() method to log entry query sets

This commit is contained in:
Karl Hobley 2021-05-04 11:54:35 +01:00 committed by Karl Hobley
parent 074f673629
commit 09e0126583
3 changed files with 19 additions and 18 deletions

View File

@ -2,7 +2,6 @@ from datetime import timedelta
import django_filters
from django.contrib.auth import get_user_model
from django.core.exceptions import PermissionDenied
from django.core.paginator import Paginator
from django.shortcuts import get_object_or_404
@ -18,17 +17,10 @@ from wagtail.core.models import (
Page, PageLogEntry, PageRevision, TaskState, UserPagePermissionsProxy, WorkflowState)
def get_audit_log_users_queryset(request):
User = get_user_model()
return User.objects.filter(
pk__in=set(PageLogEntry.objects.values_list('user__pk', flat=True))
).order_by(User.USERNAME_FIELD)
class PageHistoryReportFilterSet(WagtailFilterSet):
action = django_filters.ChoiceFilter(choices=page_log_action_registry.get_choices)
user = django_filters.ModelChoiceFilter(
field_name='user', queryset=get_audit_log_users_queryset
field_name='user', queryset=lambda request: PageLogEntry.objects.all().get_users()
)
timestamp = django_filters.DateFromToRangeFilter(label=_('Date'), widget=DateRangePickerWidget)

View File

@ -2,7 +2,6 @@ import datetime
import django_filters
from django.contrib.auth import get_user_model
from django.db.models import Q, Subquery
from django.utils.encoding import force_str
from django.utils.translation import gettext_lazy as _
@ -14,19 +13,12 @@ from wagtail.core.models import Page, PageLogEntry, UserPagePermissionsProxy
from .base import ReportView
def get_audit_log_users_queryset(request):
User = get_user_model()
return User.objects.filter(
pk__in=set(PageLogEntry.objects.values_list('user__pk', flat=True))
).order_by(User.USERNAME_FIELD)
class SiteHistoryReportFilterSet(WagtailFilterSet):
action = django_filters.ChoiceFilter(choices=page_log_action_registry.get_choices)
timestamp = django_filters.DateFromToRangeFilter(label=_('Date'), widget=DateRangePickerWidget)
label = django_filters.CharFilter(label=_('Title'), lookup_expr='icontains')
user = django_filters.ModelChoiceFilter(
field_name='user', queryset=get_audit_log_users_queryset
field_name='user', queryset=lambda request: PageLogEntry.objects.all().get_users()
)
class Meta:

View File

@ -10,6 +10,7 @@ from urllib.parse import urlparse
from django import forms
from django.apps import apps
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group, Permission
from django.contrib.contenttypes.models import ContentType
from django.core import checks
@ -4693,7 +4694,23 @@ class TaskState(models.Model):
verbose_name_plural = _('Task states')
class LogEntryQuerySet(models.QuerySet):
def get_users(self):
"""
Returns a QuerySet of Users who have created at least one log entry in this QuerySet.
The returned queryset is ordered by the username.
"""
User = get_user_model()
return User.objects.filter(
pk__in=set(self.values_list('user__pk', flat=True))
).order_by(User.USERNAME_FIELD)
class BaseLogEntryManager(models.Manager):
def get_queryset(self):
return LogEntryQuerySet(self.model, using=self._db)
def log_action(self, instance, action, **kwargs):
"""
:param instance: The model instance we are logging an action for