Skip to content

Commit

Permalink
Merge branch 'master' into 934-bug-sortering-af-foreningsmedlemskaber…
Browse files Browse the repository at this point in the history
…-er-efter-afdeling-istedet-for-forening
  • Loading branch information
rasmusselsmark authored Aug 20, 2023
2 parents 6dae7b7 + 7aa4815 commit 57a5856
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
10 changes: 9 additions & 1 deletion members/admin/union_admin.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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:
Expand Down
34 changes: 26 additions & 8 deletions members/admin/user_admin.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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 += (
(
Expand All @@ -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
Expand All @@ -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 += (
(
Expand All @@ -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


Expand All @@ -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 += (
(
Expand All @@ -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


Expand Down
2 changes: 1 addition & 1 deletion members/tests/factories/union_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 57a5856

Please sign in to comment.