NDOJ/judge/admin/organization.py

100 lines
3.1 KiB
Python
Raw Permalink Normal View History

2020-01-21 06:35:58 +00:00
from django.contrib import admin
from django.forms import ModelForm
from django.urls import reverse_lazy
from django.utils.html import format_html
from django.utils.translation import gettext, gettext_lazy as _
from reversion.admin import VersionAdmin
from judge.models import Organization
2022-05-14 17:57:27 +00:00
from judge.widgets import (
AdminHeavySelect2MultipleWidget,
AdminHeavySelect2Widget,
HeavyPreviewAdminPageDownWidget,
)
2020-01-21 06:35:58 +00:00
class OrganizationForm(ModelForm):
class Meta:
widgets = {
2022-05-14 17:57:27 +00:00
"admins": AdminHeavySelect2MultipleWidget(data_view="profile_select2"),
"registrant": AdminHeavySelect2Widget(data_view="profile_select2"),
2020-01-21 06:35:58 +00:00
}
if HeavyPreviewAdminPageDownWidget is not None:
2022-05-14 17:57:27 +00:00
widgets["about"] = HeavyPreviewAdminPageDownWidget(
preview=reverse_lazy("organization_preview")
)
2020-01-21 06:35:58 +00:00
class OrganizationAdmin(VersionAdmin):
2022-05-14 17:57:27 +00:00
readonly_fields = ("creation_date",)
fields = (
"name",
"slug",
"short_name",
"is_open",
"about",
"slots",
"registrant",
"creation_date",
"admins",
)
list_display = (
"name",
"short_name",
"is_open",
2022-09-15 16:21:04 +00:00
"creation_date",
2022-05-14 17:57:27 +00:00
"registrant",
"show_public",
)
search_fields = ("name", "short_name", "registrant__user__username")
2022-05-14 17:57:27 +00:00
prepopulated_fields = {"slug": ("name",)}
2020-01-21 06:35:58 +00:00
actions_on_top = True
actions_on_bottom = True
form = OrganizationForm
2022-09-15 16:21:04 +00:00
ordering = ["-creation_date"]
2020-01-21 06:35:58 +00:00
def show_public(self, obj):
2022-05-14 17:57:27 +00:00
return format_html(
'<a href="{0}" style="white-space:nowrap;">{1}</a>',
obj.get_absolute_url(),
gettext("View on site"),
)
2020-01-21 06:35:58 +00:00
2022-05-14 17:57:27 +00:00
show_public.short_description = ""
2020-01-21 06:35:58 +00:00
def get_readonly_fields(self, request, obj=None):
fields = self.readonly_fields
2022-05-14 17:57:27 +00:00
if not request.user.has_perm("judge.organization_admin"):
return fields + ("registrant", "admins", "is_open", "slots")
2020-01-21 06:35:58 +00:00
return fields
def get_queryset(self, request):
queryset = Organization.objects.all()
2022-05-14 17:57:27 +00:00
if request.user.has_perm("judge.edit_all_organization"):
2020-01-21 06:35:58 +00:00
return queryset
else:
return queryset.filter(admins=request.profile.id)
def has_change_permission(self, request, obj=None):
2022-05-14 17:57:27 +00:00
if not request.user.has_perm("judge.change_organization"):
2020-01-21 06:35:58 +00:00
return False
2022-05-14 17:57:27 +00:00
if request.user.has_perm("judge.edit_all_organization") or obj is None:
2020-01-21 06:35:58 +00:00
return True
return obj.admins.filter(id=request.profile.id).exists()
2022-05-28 08:54:12 +00:00
def save_related(self, request, form, formsets, change):
super().save_related(request, form, formsets, change)
obj = form.instance
obj.members.add(*obj.admins.all())
2020-01-21 06:35:58 +00:00
class OrganizationRequestAdmin(admin.ModelAdmin):
2022-05-14 17:57:27 +00:00
list_display = ("username", "organization", "state", "time")
readonly_fields = ("user", "organization")
2020-01-21 06:35:58 +00:00
def username(self, obj):
return obj.user.user.username
2022-05-14 17:57:27 +00:00
username.short_description = _("username")
username.admin_order_field = "user__user__username"