Skip to content
admin.py 27.8 KiB
Newer Older
from __future__ import annotations


__copyright__ = "Copyright (C) 2014 Andreas Kloeckner"

__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
Andreas Klöckner's avatar
Andreas Klöckner committed

from django import forms
Andreas Klöckner's avatar
Andreas Klöckner committed
from django.contrib import admin
Andreas Klöckner's avatar
Andreas Klöckner committed
from django.utils.translation import gettext_lazy as _, pgettext
Andreas Klöckner's avatar
Andreas Klöckner committed
from course.constants import exam_ticket_states, participation_permission as pperm
from course.enrollment import approve_enrollment, deny_enrollment
from course.models import (
    AuthenticationToken,
    Course,
    Event,
    Exam,
    ExamTicket,
    FlowPageData,
    FlowPageVisit,
    FlowPageVisitGrade,
    FlowRuleException,
    FlowSession,
    GradeChange,
    GradingOpportunity,
    InstantFlowRequest,
    InstantMessage,
    Participation,
    ParticipationPermission,
    ParticipationPreapproval,
    ParticipationRole,
    ParticipationRolePermission,
    ParticipationTag,
Andreas Klöckner's avatar
Andreas Klöckner committed
)
from relate.utils import string_concat
Dong Zhuang's avatar
Dong Zhuang committed

# {{{ permission helpers

def _filter_courses_for_user(queryset, user):
    if user.is_superuser:
        return queryset
    z = queryset.filter(
            participations__user=user,
            participations__roles__permissions__permission=pperm.use_admin_interface)
    return z


def _filter_course_linked_obj_for_user(queryset, user):
    if user.is_superuser:
        return queryset
    return queryset.filter(
            course__participations__user=user,
Andreas Klöckner's avatar
Andreas Klöckner committed
            course__participations__roles__permissions__permission=pperm.use_admin_interface


def _filter_participation_linked_obj_for_user(queryset, user):
    if user.is_superuser:
        return queryset
    return queryset.filter(
        participation__course__participations__user=user,
Andreas Klöckner's avatar
Andreas Klöckner committed
        participation__course__participations__roles__permissions__permission=pperm.use_admin_interface)
def _filter_related_only(filter_arg: str) -> tuple[str, Any]:
    return (filter_arg, admin.RelatedOnlyFieldListFilter)

# }}}


class UnsafePasswordInput(forms.TextInput):
    # This sends passwords back to the user--not ideal, but OK for the XMPP
    # password.
    input_type = "password"
class CourseAdminForm(forms.ModelForm):
    class Meta:
        model = Course
        widgets = {
                "course_xmpp_password": UnsafePasswordInput
@admin.register(Course)
Andreas Klöckner's avatar
Andreas Klöckner committed
class CourseAdmin(admin.ModelAdmin):
    list_display = (
            "identifier",
            "number",
            "name",
            "time_period",
            "start_date",
            "end_date",
            "hidden",
            "listed",
            "accepts_enrollment")
    list_editable = (
            "number",
            "name",
            "time_period",
            "start_date",
            "end_date",
            "hidden",
            "listed",
            "accepts_enrollment")
    list_filter = (
            "number",
            "time_period",
            "hidden",
            "listed",
            "accepts_enrollment")
    date_hierarchy = "start_date"
Andreas Klöckner's avatar
Andreas Klöckner committed

    search_fields = (
            "identifier",
            "number",
            "name",
            "time_period")

    readonly_fields = ("identifier",)

    save_on_top = True

    # {{{ permissions

    def has_add_permission(self, request):
        # These are created only through the course creation form.
        return False

    def get_queryset(self, request):
        qs = super().get_queryset(request)
        return _filter_courses_for_user(qs, request.user)

Andreas Klöckner's avatar
Andreas Klöckner committed

Andreas Klöckner's avatar
Andreas Klöckner committed
# {{{ events
@admin.register(Event)
Andreas Klöckner's avatar
Andreas Klöckner committed
class EventAdmin(admin.ModelAdmin):
    list_display = (
            "course",
            "kind",
            "ordinal",
            "time",
            "end_time",
            "shown_in_calendar")
    list_filter = (_filter_related_only("course"), "kind", "shown_in_calendar")
    search_fields = (
            "course__identifier",
            "kind",
            )

    def __unicode__(self):  # pragma: no cover  # not used
            self.kind,
            " (%s)" % str(self.ordinal) if self.ordinal is not None else "",
            self.course)
    __str__ = __unicode__
    list_editable = ("ordinal", "time", "end_time", "shown_in_calendar")
    # {{{ permissions
Loading
Loading full blame...