<commit_before>from cms.views import details as cms_view_details
from django.core.exceptions import PermissionDenied
from django.db.models import F, Q
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, redirect
from django.urls import reverse_lazy as reverse
from django.utils.timezone import now
from django.utils.translation import gettext_lazy as _

from ..forms.subjects import (
    CourseRegistrationForm,
    EventRegistrationForm,
    OrderableRegistrationForm,
    SubjectFilterForm,
    SubjectForm,
)
from ..models.courses import Course
from ..models.events import Event
from ..models.orderables import Orderable
from ..models.subjects import (
    Subject,
    SubjectPayment,
    SubjectReceivedPayment,
    SubjectRegistration,
    SubjectReturnedPayment,
    SubjectType,
)
from ..utils import reverse_with_back
from .generic import ConfirmUpdateView, CreateView, DetailView, FilteredListView, ListView, UpdateView


class SubjectTypeMixin:
    _models = {
        SubjectType.COURSE: Course,
        SubjectType.EVENT: Event,
        SubjectType.ORDERABLE: Orderable,
    }

    def dispatch(self, request, subject_type, *args, **kwargs):
        self.subject_type = get_object_or_404(SubjectType, slug=subject_type)
        self.model = self._models[self.subject_type.subject_type]
        return super().dispatch(request, *args, **kwargs)

    def get_placeholder(self):
        return super().get_placeholder() + ":" + self.subject_type.slug

    def get_queryset(self):
        return super().get_queryset().filter(subject_type=self.subject_type)

    def get_template_names(self):
        return [
            "leprikon/{}{}.html".format(self.subject_type.slug, self.template_name_suffix),
            "leprikon/{}{}.html".format(self.subject_type.subject_type, self.template_name_suffix),
            "leprikon/subject{}.html".format(self.template_name_suffix),
        ]


class CMSSubjectTypeMixin(SubjectTypeMixin):
    def dispatch(self, request, *args, **kwargs):
        # Get current CMS Page
        cms_page = request.current_page.get_public_object()

        # Check whether it really is a Leprikon subject type application.
        # It might also be one of its sub-pages.
        if cms_page.application_urls != "LeprikonSubjectTypeApp":
            # In such case show regular CMS Page
            return cms_view_details(request, *args, **kwargs)
        return super().dispatch(request, cms_page.application_namespace, **kwargs)


class SubjectListBaseView(FilteredListView):
    form_class = SubjectFilterForm
    preview_template = "leprikon/subject_preview.html"
    paginate_by = 10

    def get_title(self):
        if self.subject_type.subject_type == SubjectType.EVENT:
            if self.request.GET.get("past"):
                return _("Past {subject_type} in school year {school_year}").format(
                    subject_type=self.subject_type.plural,
                    school_year=self.request.school_year,
                )
            return _("Oncoming {subject_type}").format(
                subject_type=self.subject_type.plural,
            )
        return _("{subject_type} in school year {school_year}").format(
            subject_type=self.subject_type.plural,
            school_year=self.request.school_year,
        )

    def get_message_empty(self):
        return _("No {subject_type} matching given search parameters found.").format(
            subject_type=self.subject_type.plural,
        )

    def get_form(self):
        return self.form_class(
            subject_type_type=self.subject_type.subject_type,
            subject_types=[self.subject_type],
            school_year=self.request.school_year,
            is_staff=self.request.user.is_staff,
            data=self.request.GET,
        )


class SubjectListView(CMSSubjectTypeMixin, SubjectListBaseView):
    pass


class SubjectListMineView(SubjectTypeMixin, SubjectListBaseView):
    def get_template_names(self):
        return [
            "leprikon/{}_list_mine.html".format(self.subject_type.slug),
            "leprikon/{}_list_mine.html".format(self.subject_type.subject_type),
            "leprikon/subject_list_mine.html",
        ] + super().get_template_names()

    def get_title(self):
        return _("My {subject_type} in school year {school_year}").format(
            subject_type=self.subject_type.plural,
            school_year=self.request.school_year,
        )

    def get_queryset(self):
        return super().get_queryset().filter(leaders=self.request.leader)


class SubjectDetailView(CMSSubjectTypeMixin, DetailView):
    def get_queryset(self):
        qs = super().get_queryset()
        if not self.request.user.is_staff:
            qs = qs.filter(public=True)
        return qs


class SubjectJournalsView(SubjectTypeMixin, DetailView):
    template_name_suffix = "_journals"

    def get_queryset(self):
        qs = super().get_queryset()
        if not self.request.user.is_staff:
            qs = qs.filter(leaders=self.request.leader)
        return qs

    def get_context_data(self, **kwargs):
        return super().get_context_data(
            add_label=_("Create journal"),
            add_url=reverse_with_back(self.request, "leprikon:journal_create", kwargs={"subject": self.object.id}),
            preview_template="leprikon/journal_preview.html",
            message_empty=_("No journals matching given search parameters found."),
            **kwargs,
        )

    def get_title(self):
        return _("Journals of {subject_type} {subject}").format(
            subject_type=self.subject_type.name_genitiv,
            subject=self.object,
        )


class SubjectRegistrationsView(SubjectTypeMixin, DetailView):
    template_name_suffix = "_registrations"

    def get_queryset(self):
        qs = super().get_queryset()
        if not self.request.user.is_staff:<commit_msg>Fix journals list title<commit_after>from cms.views import details as cms_view_details
from django.core.exceptions import PermissionDenied
from django.db.models import F, Q
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, redirect
from django.urls import reverse_lazy as reverse
from django.utils.timezone import now
from django.utils.translation import gettext_lazy as _

from ..forms.subjects import (
    CourseRegistrationForm,
    EventRegistrationForm,
    OrderableRegistrationForm,
    SubjectFilterForm,
    SubjectForm,
)
from ..models.courses import Course
from ..models.events import Event
from ..models.orderables import Orderable
from ..models.subjects import (
    Subject,
    SubjectPayment,
    SubjectReceivedPayment,
    SubjectRegistration,
    SubjectReturnedPayment,
    SubjectType,
)
from ..utils import reverse_with_back
from .generic import ConfirmUpdateView, CreateView, DetailView, FilteredListView, ListView, UpdateView


class SubjectTypeMixin:
    _models = {
        SubjectType.COURSE: Course,
        SubjectType.EVENT: Event,
        SubjectType.ORDERABLE: Orderable,
    }

    def dispatch(self, request, subject_type, *args, **kwargs):
        self.subject_type = get_object_or_404(SubjectType, slug=subject_type)
        self.model = self._models[self.subject_type.subject_type]
        return super().dispatch(request, *args, **kwargs)

    def get_placeholder(self):
        return super().get_placeholder() + ":" + self.subject_type.slug

    def get_queryset(self):
        return super().get_queryset().filter(subject_type=self.subject_type)

    def get_template_names(self):
        return [
            "leprikon/{}{}.html".format(self.subject_type.slug, self.template_name_suffix),
            "leprikon/{}{}.html".format(self.subject_type.subject_type, self.template_name_suffix),
            "leprikon/subject{}.html".format(self.template_name_suffix),
        ]


class CMSSubjectTypeMixin(SubjectTypeMixin):
    def dispatch(self, request, *args, **kwargs):
        # Get current CMS Page
        cms_page = request.current_page.get_public_object()

        # Check whether it really is a Leprikon subject type application.
        # It might also be one of its sub-pages.
        if cms_page.application_urls != "LeprikonSubjectTypeApp":
            # In such case show regular CMS Page
            return cms_view_details(request, *args, **kwargs)
        return super().dispatch(request, cms_page.application_namespace, **kwargs)


class SubjectListBaseView(FilteredListView):
    form_class = SubjectFilterForm
    preview_template = "leprikon/subject_preview.html"
    paginate_by = 10

    def get_title(self):
        if self.subject_type.subject_type == SubjectType.EVENT:
            if self.request.GET.get("past"):
                return _("Past {subject_type} in school year {school_year}").format(
                    subject_type=self.subject_type.plural,
                    school_year=self.request.school_year,
                )
            return _("Oncoming {subject_type}").format(
                subject_type=self.subject_type.plural,
            )
        return _("{subject_type} in school year {school_year}").format(
            subject_type=self.subject_type.plural,
            school_year=self.request.school_year,
        )

    def get_message_empty(self):
        return _("No {subject_type} matching given search parameters found.").format(
            subject_type=self.subject_type.plural,
        )

    def get_form(self):
        return self.form_class(
            subject_type_type=self.subject_type.subject_type,
            subject_types=[self.subject_type],
            school_year=self.request.school_year,
            is_staff=self.request.user.is_staff,
            data=self.request.GET,
        )


class SubjectListView(CMSSubjectTypeMixin, SubjectListBaseView):
    pass


class SubjectListMineView(SubjectTypeMixin, SubjectListBaseView):
    def get_template_names(self):
        return [
            "leprikon/{}_list_mine.html".format(self.subject_type.slug),
            "leprikon/{}_list_mine.html".format(self.subject_type.subject_type),
            "leprikon/subject_list_mine.html",
        ] + super().get_template_names()

    def get_title(self):
        return _("My {subject_type} in school year {school_year}").format(
            subject_type=self.subject_type.plural,
            school_year=self.request.school_year,
        )

    def get_queryset(self):
        return super().get_queryset().filter(leaders=self.request.leader)


class SubjectDetailView(CMSSubjectTypeMixin, DetailView):
    def get_queryset(self):
        qs = super().get_queryset()
        if not self.request.user.is_staff:
            qs = qs.filter(public=True)
        return qs


class SubjectJournalsView(SubjectTypeMixin, DetailView):
    template_name_suffix = "_journals"

    def get_queryset(self):
        qs = super().get_queryset()
        if not self.request.user.is_staff:
            qs = qs.filter(leaders=self.request.leader)
        return qs

    def get_context_data(self, **kwargs):
        return super().get_context_data(
            add_label=_("Create journal"),
            add_url=reverse_with_back(self.request, "leprikon:journal_create", kwargs={"subject": self.object.id}),
            preview_template="leprikon/journal_preview.html",
            message_empty=_("No journals matching given search parameters found."),
            **kwargs,
        )

    def get_title(self):
        return _("Journals of {subject_type} {subject}").format(
            subject_type=self.subject_type.name_genitiv,
            subject=self.object.display_name,
        )


class SubjectRegistrationsView(SubjectTypeMixin, DetailView):
    template_name_suffix = "_registrations"

    def get_queryset(self):
        qs = super().get_queryset()
        if not self.request.user.is_staff:
{'fraction_non_alphanumeric': 0.09671424951431709, 'fraction_numerical': 0.0013514654953965706, 'mean_word_length': 3.0423352680095594, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5739005', 'n_tokens_mistral': 3503, 'n_tokens_neox': 3383, 'n_words': 656}