From f2ba7414993f3eba2b0613bbe5a367bda255b641 Mon Sep 17 00:00:00 2001 From: ErikKalkoken Date: Tue, 18 Feb 2020 01:35:17 +0100 Subject: [PATCH] Add sorting to user for group, state for character, corporation, alliance and group for group user leaders, group group leaders --- allianceauth/authentication/admin.py | 54 +++++++++++++++---- .../static/authentication/css/admin.css | 9 +++- allianceauth/groupmanagement/admin.py | 19 +++++-- 3 files changed, 66 insertions(+), 16 deletions(-) diff --git a/allianceauth/authentication/admin.py b/allianceauth/authentication/admin.py index dac057d8..0393168e 100644 --- a/allianceauth/authentication/admin.py +++ b/allianceauth/authentication/admin.py @@ -20,7 +20,8 @@ from allianceauth.authentication.models import State, get_guest_state,\ from allianceauth.hooks import get_hooks from allianceauth.eveonline.models import EveCharacter, EveCorporationInfo from allianceauth.eveonline.tasks import update_character -from .app_settings import * +from .app_settings import AUTHENTICATION_ADMIN_USERS_MAX_GROUPS, \ + AUTHENTICATION_ADMIN_USERS_MAX_CHARS if 'allianceauth.eveonline.autogroups' in settings.INSTALLED_APPS: _has_auto_groups = True @@ -365,7 +366,7 @@ class UserAdmin(BaseUserAdmin): 'username', 'character_ownerships__character__character_name' ) - + def _characters(self, obj): my_characters = [ x.character.character_name @@ -426,19 +427,54 @@ class UserAdmin(BaseUserAdmin): def has_delete_permission(self, request, obj=None): return request.user.has_perm('auth.delete_user') + def formfield_for_manytomany(self, db_field, request, **kwargs): + """overriding this formfield to have sorted lists in the form""" + if db_field.name == "groups": + kwargs["queryset"] = Group.objects.all().order_by(Lower('name')) + return super().formfield_for_manytomany(db_field, request, **kwargs) + @admin.register(State) -class StateAdmin(admin.ModelAdmin): +class StateAdmin(admin.ModelAdmin): + list_select_related = True + list_display = ('name', 'priority', '_user_count') + + def _user_count(self, obj): + return obj.userprofile_set.all().count() + _user_count.short_description = 'Users' + fieldsets = ( (None, { 'fields': ('name', 'permissions', 'priority'), }), ('Membership', { - 'fields': ('public', 'member_characters', 'member_corporations', 'member_alliances'), + 'fields': ( + 'public', + 'member_characters', + 'member_corporations', + 'member_alliances' + ), }) ) - filter_horizontal = ['member_characters', 'member_corporations', 'member_alliances', 'permissions'] - list_display = ('name', 'priority', 'user_count') + filter_horizontal = [ + 'member_characters', + 'member_corporations', + 'member_alliances', + 'permissions' + ] + + def formfield_for_manytomany(self, db_field, request, **kwargs): + """overriding this formfield to have sorted lists in the form""" + if db_field.name == "member_characters": + kwargs["queryset"] = EveCharacter.objects.all()\ + .order_by(Lower('character_name')) + elif db_field.name == "member_corporations": + kwargs["queryset"] = EveCorporationInfo.objects.all()\ + .order_by(Lower('corporation_name')) + elif db_field.name == "member_alliances": + kwargs["queryset"] = EveAllianceInfo.objects.all()\ + .order_by(Lower('alliance_name')) + return super().formfield_for_manytomany(db_field, request, **kwargs) def has_delete_permission(self, request, obj=None): if obj == get_guest_state(): @@ -453,11 +489,7 @@ class StateAdmin(admin.ModelAdmin): }), ) return super(StateAdmin, self).get_fieldsets(request, obj=obj) - - @staticmethod - def user_count(obj): - return obj.userprofile_set.all().count() - + class BaseOwnershipAdmin(admin.ModelAdmin): class Media: diff --git a/allianceauth/authentication/static/authentication/css/admin.css b/allianceauth/authentication/static/authentication/css/admin.css index 5fe8a4d8..489edd2e 100644 --- a/allianceauth/authentication/static/authentication/css/admin.css +++ b/allianceauth/authentication/static/authentication/css/admin.css @@ -3,8 +3,13 @@ CSS for allianceauth admin site */ /* styling for profile pic */ -.img-circle { border-radius: 50%; } -.column-user_profile_pic { width: 50px; } +.img-circle { + border-radius: 50%; +} +.column-user_profile_pic { + width: 1px; + white-space: nowrap; +} /* tooltip */ .tooltip { diff --git a/allianceauth/groupmanagement/admin.py b/allianceauth/groupmanagement/admin.py index 6a5cf71c..eaf9060d 100644 --- a/allianceauth/groupmanagement/admin.py +++ b/allianceauth/groupmanagement/admin.py @@ -3,6 +3,7 @@ from django.conf import settings from django.contrib import admin from django.contrib.auth.models import Group as BaseGroup from django.db.models import Count +from django.db.models.functions import Lower from django.db.models.signals import pre_save, post_save, pre_delete, \ post_delete, m2m_changed from django.dispatch import receiver @@ -25,6 +26,17 @@ class AuthGroupInlineAdmin(admin.StackedInline): verbose_name_plural = 'Auth Settings' verbose_name = '' + def formfield_for_manytomany(self, db_field, request, **kwargs): + """overriding this formfield to have sorted lists in the form""" + if db_field.name == "group_leaders": + kwargs["queryset"] = User.objects\ + .filter(profile__state__name='Member')\ + .order_by(Lower('username')) + elif db_field.name == "group_leader_groups": + kwargs["queryset"] = Group.objects\ + .order_by(Lower('name')) + return super().formfield_for_manytomany(db_field, request, **kwargs) + def has_add_permission(self, request): return False @@ -100,9 +112,7 @@ class GroupAdmin(admin.ModelAdmin): HasLeaderFilter ) search_fields = ('name', 'authgroup__description') - filter_horizontal = ('permissions',) - inlines = (AuthGroupInlineAdmin,) - + def get_queryset(self, request): qs = super().get_queryset(request) qs = qs.annotate( @@ -147,6 +157,9 @@ class GroupAdmin(admin.ModelAdmin): _properties.short_description = "properties" + filter_horizontal = ('permissions',) + inlines = (AuthGroupInlineAdmin,) + class Group(BaseGroup): class Meta: