Skip to content
admin.py 29.6 KiB
Newer Older
# -*- coding: utf-8 -*-

__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.
"""
from django.utils.translation import (
        ugettext_lazy as _, pgettext)
Andreas Klöckner's avatar
Andreas Klöckner committed
from django.contrib import admin
from course.models import (
Andreas Klöckner's avatar
Andreas Klöckner committed
        Course, Event,
        ParticipationTag,
        Participation, ParticipationPermission,
        ParticipationRole, ParticipationRolePermission,
        ParticipationPreapproval,
        AuthenticationToken,
        InstantFlowRequest,
        FlowSession, FlowPageData,
        FlowPageVisit, FlowPageVisitGrade,
        GradingOpportunity, GradeChange, InstantMessage,
        Exam, ExamTicket)
from django import forms
from relate.utils import string_concat
from course.enrollment import (approve_enrollment, deny_enrollment)
from course.constants import (
        participation_permission as pperm,
        exam_ticket_states
        )
from typing import Any, Text, Tuple  # noqa
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,
            course__participations__roles__permissions__permission  # noqa
            =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,
        participation__course__participations__roles__permissions__permission  # noqa
# {{{ list filter helper

def _filter_related_only(filter_arg):
    # type: (Text) -> Tuple[Text, 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
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")

    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(CourseAdmin, self).get_queryset(request)
        return _filter_courses_for_user(qs, request.user)

Andreas Klöckner's avatar
Andreas Klöckner committed
admin.site.register(Course, CourseAdmin)

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

Andreas Klöckner's avatar
Andreas Klöckner committed
# {{{ events
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
        return u"%s%s in %s" % (
            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

    def get_queryset(self, request):
        qs = super(EventAdmin, self).get_queryset(request)
        return _filter_course_linked_obj_for_user(qs, request.user)
Loading
Loading full blame...