Skip to content

Commit

Permalink
temporary commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mhewel committed Dec 31, 2024
1 parent 5a191bd commit 0979456
Show file tree
Hide file tree
Showing 23 changed files with 1,161 additions and 417 deletions.
3 changes: 3 additions & 0 deletions members/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
VolunteerRequestDepartment,
WaitingList,
EmailItem,
Volunteer,
)

from .activity_admin import ActivityAdmin
Expand All @@ -37,6 +38,7 @@
from .volunteerrequestdepartment_admin import VolunteerRequestDepartmentAdmin
from .waitinglist_admin import WaitingListAdmin
from .emailitem_admin import EmailItemAdmin
from .volunteer_admin import VolunteerAdmin

admin.site.site_header = "Coding Pirates Medlemsdatabase"
admin.site.index_title = "Afdelings admin"
Expand All @@ -57,6 +59,7 @@
admin.site.register(VolunteerRequest, VolunteerRequestAdmin)
admin.site.register(VolunteerRequestDepartment, VolunteerRequestDepartmentAdmin)
admin.site.register(EmailItem, EmailItemAdmin)
admin.site.register(Volunteer, VolunteerAdmin)
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
# admin.site.register(AdminUserInformation, AdminUserInformationAdmin)
264 changes: 264 additions & 0 deletions members/admin/volunteer_admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
import codecs
from django.contrib import admin
from django.http import HttpResponse
from django import forms
from django.db.models import Q

from members.models import (
Volunteer,
Department,
AdminUserInformation,
Activity,
)


class VolunteerDepartmentListFilter(admin.SimpleListFilter):
title = "Afdelinger"
parameter_name = "department"

def lookups(self, request, model_admin):
departments = []
for d in (
Department.objects.filter(
volunteer__department__in=AdminUserInformation.get_departments_admin(
request.user
)
)
.order_by("name")
.distinct()
):
departments.append((str(d.pk), str(d)))
return departments

def queryset(self, request, queryset):
if self.value() is None:
return queryset
else:
return queryset.filter(department__pk=self.value())


class VolunteerActivityListFilter(admin.SimpleListFilter):
title = "Aktiviteter"
parameter_name = "activity"

def lookups(self, request, model_admin):
activities = []
for a in (
Activity.objects.filter(
volunteer__activity__department__in=AdminUserInformation.get_departments_admin(
request.user
)
)
.order_by("name")
.distinct()
):
activities.append((str(a.id), str(a)))
return activities

def queryset(self, request, queryset):
if self.value() is None:
return queryset
else:
return queryset.filter(activity__pk=self.value())


class VolunteerAdminForm(forms.ModelForm):
class Meta:
model = Volunteer
fields = "__all__"

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if "department" in self.data:
print(f"*1")
try:
department_id = int(self.data.get("department"))
self.fields["activity"].queryset = Activity.objects.filter(
department_id=department_id,
activitytype__id__in=["FORLØB", "ARRANGEMENT"],
).order_by("name")
# self.fields["activity"].label_from_instance = self.label_from_instance
except (ValueError, TypeError):
pass # invalid input from the client; ignore and fallback to empty Activity queryset
elif self.instance.pk:
print(f"*2")
self.fields["activity"].queryset = (
self.instance.department.activity_set.filter(
activitytype__id__in=["FORLØB", "ARRANGEMENT"]
).order_by("name")
)
print(f' activity: {self.fields["activity"].queryset}')
print(f" initial: {self.instance.activity}")
# self.fields["activity"].label_from_instance = self.label_from_instance
self.fields["activity"].initial = self.instance.activity
else:
print(f"*3")
self.fields["activity"].queryset = (
Activity.objects.none()
) # Set to empty queryset initially

# def label_from_instance(self, obj):
# return f"[{obj.start_date} - {obj.end_date}] {obj.name}"


class VolunteerAdmin(admin.ModelAdmin):
form = VolunteerAdminForm

list_display = (
"get_person_name",
"get_person_email",
"department",
"activity",
"start_date",
"end_date",
)
list_filter = (
VolunteerDepartmentListFilter,
VolunteerActivityListFilter,
)

readonly_fields = (
"get_person_name",
"get_person_email",
"added_at",
"confirmed",
"removed",
)

autocomplete_fields = [
"person", "department", "activity", ]

actions = [
"export_volunteer_info_csv",
]

class Media:
js = ("members/js/volunteer_admin.js",)

def get_fieldsets(self, request, obj=None):
# if not obj:
# return self.add_fieldsets

# Check for super user (or special perm)

info_fields = ("confirmed",)

if request.user.is_superuser:
info_fields = (
"added_at",
"confirmed",
"removed",
)

return [
(
"Person data",
{
"fields": (
"person",
"get_person_name",
"get_person_email",
)
},
),
(
"Hvad og hvornår",
{
"fields": (
"department",
"activity",
"start_date",
"end_date",
)
},
),
("Ekstra info", {"fields": (info_fields,)}),
]

def get_queryset(self, request):
qs = super().get_queryset(request)
if request.user.is_superuser or request.user.has_perm(
"members.view_all_departments"
):
return qs
else:
departments = AdminUserInformation.get_departments_admin(request.user)
return qs.filter(department__in=departments)

def get_search_results(self, request, queryset, search_term):
queryset, use_distinct = super().get_search_results(request, queryset, search_term)
queryset = queryset.filter(activity__activitytype_id__in=["FORLØB", "ARRANGEMENT"])
return queryset, use_distinct


def formfield_for_foreignkey(self, db_field, request, **kwargs):
'''
# Only show departments that user can access
if db_field.name == "department":
if request.user.is_superuser or request.user.has_perm(
"members.view_all_departments"
):
kwargs["queryset"] = Department.objects.all()
else:
kwargs["queryset"] = Department.objects.filter(
adminuserinformation__user=request.user
)
return super().formfield_for_foreignkey(db_field, request, **kwargs)
'''
if db_field.name == "activity":
if "department" in request.GET:
department_id = request.GET.get("department")
kwargs["queryset"] = Activity.objects.filter(
department_id=department_id,
activity__activitytype_id__in=["FORLØB", "ARRANGEMENT"]
).order_by("name")
else:
kwargs["queryset"] = Activity.objects.none()
return super().formfield_for_foreignkey(db_field, request, **kwargs)

def get_person_name(self, obj):
return obj.person.name

get_person_name.short_description = "Navn"
get_person_name.admin_order_field = "person__name"

def get_person_email(self, obj):
return obj.person.email

get_person_email.short_description = "Email"
get_person_email.admin_order_field = "person__email"

def get_activity_name(self, obj):
return obj.activity.name

get_activity_name.short_description = "Aktivitet"
get_activity_name.admin_order_field = "activity__name"

def export_volunteer_info_csv(self, request, queryset):
result = """"Forening";"Afdeling";"Aktivitet";"Navn";"Email";"Startdato";"Slutdato"\n"""
for v in queryset.order_by("start_date"):
if v.department is not None:
result += v.department.union.name + ";"
result += v.department.name + ";"
else:
result += ";"
if v.activity is not None:
result += v.activity.name
result += ";"
result += v.person.name + ";"
result += v.person.email + ";"
if v.start_date is not None:
result += v.start_date.strftime("%Y-%m-%d")
result += ";"
if v.end_date is not None:
result += v.end_date.strftime("%Y-%m-%d")
result += "\n"

response = HttpResponse(
f'{codecs.BOM_UTF8.decode("utf-8")}{result}',
content_type="text/csv; charset=utf-8",
)
response["Content-Disposition"] = 'attachment; filename="FrivilligInfo.csv"'
return response

export_volunteer_info_csv.short_description = "Exporter frivilliginfo (CSV)"
76 changes: 70 additions & 6 deletions members/admin/volunteerrequest_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,78 @@

class VolunteerRequestAdmin(admin.ModelAdmin):
list_display = (
"person",
"name",
"age",
"email",
"phone",
"get_name",
"get_email",
"get_phone",
"get_age",
"get_new",
"created",
"finished",
"token",
)

date_hierarchy = "created"

readonly_fields = (
"get_name",
"get_email",
"get_phone",
"get_age",
"get_new",
"created",
"finished",
"info_whishes",
"info_reference",
)

fieldsets = [
(
"Person data",
{
"fields": ("get_name", "get_email", "get_phone", "get_age", "get_new"),
},
),
(
"Ønsker og referencer",
{
"fields": ("info_whishes", "info_reference"),
},
),
]

def get_name(self, obj):
if obj.person is None:
return obj.name
else:
return obj.person.name

get_name.short_description = "Navn"

def get_email(self, obj):
if obj.person is None:
return obj.email
else:
return obj.person.email

get_email.short_description = "Email"

def get_phone(self, obj):
if obj.person is None:
return obj.phone
else:
return obj.person.phone

get_phone.short_description = "Telefon"

def get_age(self, obj):
if obj.person is None:
return obj.age
else:
return obj.person.age_years()

get_age.short_description = "Alder"

def get_new(self, obj):
return obj.person is None

get_new.short_description = "Ny ?"
get_new.boolean = True
Loading

0 comments on commit 0979456

Please sign in to comment.