Skip to content

Commit

Permalink
Fix: Always annotate deterministic email on queryset in user admin
Browse files Browse the repository at this point in the history
So admins of User subclasses have access to search field
  • Loading branch information
stefanw committed Apr 9, 2024
1 parent 4a35932 commit c540f46
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
15 changes: 14 additions & 1 deletion froide/account/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@
from . import account_email_changed
from .auth import MFAAndRecentAuthRequiredAdminMixin, RecentAuthRequiredAdminMixin
from .forms import UserChangeForm, UserCreationForm
from .models import AccountBlocklist, TaggedUser, User, UserPreference, UserTag
from .models import (
AccountBlocklist,
TaggedUser,
User,
UserPreference,
UserTag,
annotate_deterministic_email,
)
from .services import AccountService
from .tasks import merge_accounts_task, send_bulk_mail, start_export_task
from .utils import (
Expand Down Expand Up @@ -135,6 +142,12 @@ class UserAdmin(RecentAuthRequiredAdminMixin, DjangoUserAdmin):

def get_queryset(self, request):
qs = super().get_queryset(request)
# Anotate deterministic email on queryset
# as the user admin may also be used on
# subclasses of User with other default managers
# where deterministic email would otherwise not be available
qs = annotate_deterministic_email(qs)

if has_foirequests:
qs = qs.annotate(request_count=Count("foirequest"))

Expand Down
11 changes: 7 additions & 4 deletions froide/account/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,19 @@ class Meta:
verbose_name_plural = _("tagged users")


def annotate_deterministic_email(queryset):
return queryset.annotate(
email_deterministic=Collate("email", "und-x-icu"),
)


class UserManager(BaseUserManager):
def get_public_profiles(self) -> QuerySet:
return super().get_queryset().filter(is_active=True, private=False)

def get_queryset(self) -> QuerySet:
qs = super().get_queryset()
qs = qs.annotate(
email_deterministic=Collate("email", "und-x-icu"),
)
return qs
return annotate_deterministic_email(qs)

def _create_user(
self,
Expand Down

0 comments on commit c540f46

Please sign in to comment.