Add sorting to user for group, state for character, corporation, alliance and group for group user leaders, group group leaders

This commit is contained in:
ErikKalkoken 2020-02-18 01:35:17 +01:00
parent 3bd8107fcf
commit f2ba741499
3 changed files with 66 additions and 16 deletions

View File

@ -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
@ -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):
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():
@ -454,10 +490,6 @@ 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:

View File

@ -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 {

View File

@ -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,8 +112,6 @@ 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)
@ -147,6 +157,9 @@ class GroupAdmin(admin.ModelAdmin):
_properties.short_description = "properties"
filter_horizontal = ('permissions',)
inlines = (AuthGroupInlineAdmin,)
class Group(BaseGroup):
class Meta: