Skip to content

Commit

Permalink
feat(admin): tweaks for sections
Browse files Browse the repository at this point in the history
  • Loading branch information
thejoeejoee committed Nov 19, 2023
1 parent 8d48275 commit b132d6d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
14 changes: 12 additions & 2 deletions fiesta/apps/accounts/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,18 @@ def get_queryset(self, request):

@admin.register(UserProfile)
class UserProfileAdmin(admin.ModelAdmin):
list_display = ("user", "nationality", "gender", "picture")
list_display = (
"user_name",
"user",
"nationality",
"gender",
"faculty",
"picture",
)
list_filter = (
"user__memberships__section",
"gender",
("nationality", admin.AllValuesFieldListFilter),
"nationality",
)
autocomplete_fields = ("user",)
search_fields = [
Expand All @@ -66,6 +73,9 @@ class UserProfileAdmin(admin.ModelAdmin):
"user__last_name",
]

def user_name(self, obj):
return obj.user.get_full_name()

def get_queryset(self, request):
return (
super()
Expand Down
2 changes: 2 additions & 0 deletions fiesta/apps/plugins/models/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class Meta:

unique_together = (("app_label", "section"),)

ordering = ("section", "app_label")

def clean_fields(self, exclude=None) -> None:
if exclude and "configuration" in exclude:
return super().clean_fields(exclude=exclude)
Expand Down
52 changes: 49 additions & 3 deletions fiesta/apps/sections/admin.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from __future__ import annotations

from django.contrib import admin
from django.db.models import Prefetch
from django.urls import reverse
from django.utils.html import format_html, format_html_join
from django.utils.translation import gettext_lazy as _

from ..plugins.admin import BaseChildConfigurationAdmin
from ..plugins.models import Plugin
from .models import Section, SectionMembership, SectionsConfiguration, SectionUniversity


Expand All @@ -21,13 +25,48 @@ class SectionsConfigurationAdmin(BaseChildConfigurationAdmin):

@admin.register(Section)
class SectionAdmin(admin.ModelAdmin):
list_display = ("name", "country", "space_slug", "all_universities", "memberships_count", "system_state")
list_display = (
"name",
"country",
"space_slug",
"all_universities",
"all_plugins",
"memberships_count",
"system_state",
)
list_filter = (("country", admin.AllValuesFieldListFilter), "system_state")

list_editable = ("system_state",)

def get_queryset(self, request):
return super().get_queryset(request).prefetch_related("memberships", "universities")
return (
super()
.get_queryset(request)
.prefetch_related(
"memberships",
"universities",
Prefetch("plugins", queryset=Plugin.objects.exclude(state=Plugin.State.DISABLED)),
)
)

@admin.display(
description=_("Plugins"),
)
def all_plugins(self, obj: Section):
return format_html(
"<ul>{}</ul>",
format_html_join(
"\n",
"<li><a href='{}'>{}</a></li>",
(
(
reverse("admin:plugins_plugin_change", args=(p.pk,)),
p,
)
for p in obj.plugins.all()
),
),
)

@admin.display(
description=_("Memberships"),
Expand All @@ -39,7 +78,14 @@ def memberships_count(self, obj: Section):
description=_("Universities"),
)
def all_universities(self, obj: Section):
return ", ".join(map(str, obj.universities.all()))
return format_html(
"<ul>{}</ul>",
format_html_join(
"\n",
"<li>{}</li>",
((p,) for p in obj.universities.all()),
),
)

class SectionUniversityInline(admin.StackedInline):
model = SectionUniversity
Expand Down

0 comments on commit b132d6d

Please sign in to comment.