diff --git a/members/admin/union_admin.py b/members/admin/union_admin.py index bd60b6fa..286a38f8 100644 --- a/members/admin/union_admin.py +++ b/members/admin/union_admin.py @@ -1,10 +1,11 @@ import codecs from django.contrib import admin +from django.db.models.functions import Upper from django.http import HttpResponse from django.urls import reverse from django.utils.safestring import mark_safe -from members.models import Address +from members.models import Address, Person class UnionAdmin(admin.ModelAdmin): @@ -33,6 +34,13 @@ def formfield_for_foreignkey(self, db_field, request, **kwargs): kwargs["queryset"] = Address.get_user_addresses(request.user) return super().formfield_for_foreignkey(db_field, request, **kwargs) + def formfield_for_manytomany(self, db_field, request, **kwargs): + if db_field.name == "board_members": + kwargs["queryset"] = Person.objects.filter(user__is_staff=True).order_by( + Upper("name") + ) + return super().formfield_for_manytomany(db_field, request, **kwargs) + def get_queryset(self, request): qs = super(UnionAdmin, self).get_queryset(request) if request.user.is_superuser: diff --git a/members/admin/user_admin.py b/members/admin/user_admin.py index b329aaae..e4c331ef 100644 --- a/members/admin/user_admin.py +++ b/members/admin/user_admin.py @@ -1,4 +1,5 @@ from django.contrib import admin +from django.db.models.functions import Upper from django.contrib.auth.admin import UserAdmin from django.contrib.auth.models import Group from members.models import AdminUserInformation, Person, Union, Department @@ -9,6 +10,13 @@ class AdminUserInformationInline(admin.StackedInline): filter_horizontal = ("departments", "unions") can_delete = False + def formfield_for_manytomany(self, db_field, request, **kwargs): + if db_field.name == "departments": + kwargs["queryset"] = Department.objects.all().order_by(Upper("name").asc()) + if db_field.name == "unions": + kwargs["queryset"] = Union.objects.all().order_by(Upper("name").asc()) + return super().formfield_for_manytomany(db_field, request, **kwargs) + class PersonInline(admin.StackedInline): model = Person @@ -21,7 +29,7 @@ class AdminUserGroupListFilter(admin.SimpleListFilter): parameter_name = "group" def lookups(self, request, model_admin): - groupList = () + groupList = [("none", "(ingen gruppe)")] for aGroup in Group.objects.all().order_by("name"): groupList += ( ( @@ -33,6 +41,8 @@ def lookups(self, request, model_admin): def queryset(self, request, queryset): group_id = request.GET.get(self.parameter_name, None) + if group_id == "none": + return queryset.filter(groups__isnull=True) if group_id: return queryset.filter(groups=group_id) return queryset @@ -43,7 +53,7 @@ class AdminUserUnionListFilter(admin.SimpleListFilter): parameter_name = "union" def lookups(self, request, model_admin): - unionList = () + unionList = [("none", "(ingen forening)"), ("any", "(mindst en forening)")] for aUnion in Union.objects.all().order_by("name"): unionList += ( ( @@ -55,8 +65,12 @@ def lookups(self, request, model_admin): def queryset(self, request, queryset): union_id = request.GET.get(self.parameter_name, None) - if union_id: - return queryset.filter(groups=union_id) + if union_id == "none": + return queryset.filter(adminuserinformation__unions__isnull=True) + elif union_id == "any": + return queryset.exclude(adminuserinformation__unions__isnull=True) + elif union_id: + return queryset.filter(adminuserinformation__unions=union_id) return queryset @@ -65,7 +79,7 @@ class AdminUserDepartmentListFilter(admin.SimpleListFilter): parameter_name = "department" def lookups(self, request, model_admin): - departmentList = () + departmentList = [("none", "(ingen afdeling)"), ("any", "(mindst en afdeling)")] for aDepartment in Department.objects.all().order_by("name"): departmentList += ( ( @@ -76,9 +90,13 @@ def lookups(self, request, model_admin): return departmentList def queryset(self, request, queryset): - union_id = request.GET.get(self.parameter_name, None) - if union_id: - return queryset.filter(groups=union_id) + department_id = request.GET.get(self.parameter_name, None) + if department_id == "none": + return queryset.filter(adminuserinformation__departments__isnull=True) + elif department_id == "any": + return queryset.exclude(adminuserinformation__departments__isnull=True) + if department_id: + return queryset.filter(adminuserinformation__departments=department_id) return queryset diff --git a/members/tests/factories/union_factory.py b/members/tests/factories/union_factory.py index 1a8c7940..fd84104e 100644 --- a/members/tests/factories/union_factory.py +++ b/members/tests/factories/union_factory.py @@ -10,7 +10,7 @@ class UnionFactory(DjangoModelFactory): class Meta: model = Union - name = factory.LazyAttribute(lambda u: "Coding Pirates {}".format(u.address.city)) + name = factory.LazyAttribute(lambda u: "{}".format(u.address.city)) chairman_old = Faker("name") chairman_email_old = Faker("email") second_chair_old = Faker("name")