From 055f8892caaee86fa64773476331dd974295973c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Valenta?= <git@imaniti.org> Date: Mon, 7 Aug 2023 18:21:14 +0200 Subject: [PATCH] finish material queryset --- lectures/admin.py | 17 +++++++++++++++++ lectures/forms.py | 24 ++++++++++++++++++++++++ lectures/models.py | 5 ++++- lectures/views.py | 24 ++++++++++++------------ 4 files changed, 57 insertions(+), 13 deletions(-) create mode 100644 lectures/forms.py diff --git a/lectures/admin.py b/lectures/admin.py index faa8401..dbc9608 100644 --- a/lectures/admin.py +++ b/lectures/admin.py @@ -2,6 +2,7 @@ from django.contrib import admin from shared.admin import MarkdownxGuardedModelAdmin +from .forms import LectureGroupTypeFormset from .models import ( Lecture, LectureGroup, @@ -39,6 +40,7 @@ class LectureMaterialInline(admin.StackedInline): class LectureGroupTypeInline(admin.StackedInline): model = LectureGroupType autocomplete_fields = ("group",) + formset = LectureGroupTypeFormset extra = 1 @@ -56,8 +58,23 @@ class LectureAdmin(MarkdownxGuardedModelAdmin): list_display = ( "name", "timestamp", + "display_lecture_group_types", ) + @admin.display(description="Výukové skupiny") + def display_lecture_group_types(self, obj) -> str: + group_names = [] + + for lecture_group_type in obj.lecture_group_types.all(): + group_names.append(str(lecture_group_type.group)) + + display_string = ", ".join(group_names) + + if len(display_string) > 64: + display_string = display_string[:64] + "..." + + return display_string + class LectureLectorAdmin(MarkdownxGuardedModelAdmin): search_fields = ("name", "username") diff --git a/lectures/forms.py b/lectures/forms.py new file mode 100644 index 0000000..0a04762 --- /dev/null +++ b/lectures/forms.py @@ -0,0 +1,24 @@ +from django import forms + + +class LectureGroupTypeFormset(forms.models.BaseInlineFormSet): + # https://stackoverflow.com/a/877920 + + def clean(self): + groups = [] + + for form in self.forms: + try: + if form.cleaned_data: + if form.cleaned_data[ + "group" + ] in groups and not form.cleaned_data.get("DELETE", False): + raise forms.ValidationError( + "Školení nemůžeš přiřadit jednotlivé skupině více než jednou." + ) + + groups.append(form.cleaned_data["group"]) + except AttributeError as exception: + # annoyingly, if a subform is invalid Django explicity raises + # an AttributeError for cleaned_data + pass diff --git a/lectures/models.py b/lectures/models.py index 72b0cf7..9f719cc 100644 --- a/lectures/models.py +++ b/lectures/models.py @@ -106,6 +106,9 @@ class LectureGroupType(models.Model): verbose_name="Požadovanost", ) + def __str__(self) -> str: + return f"{self.group} - {self.lecture}" + class Meta: verbose_name = "Úroveň požadovanosti pro skupinu" verbose_name_plural = "Úroveň požadovanosti pro skupiny" @@ -261,7 +264,7 @@ class LectureRecording(NameStrMixin, models.Model): def get_lecture_material_file_location(instance, filename): - return get_file_location(instane, filename, path_prefix="_private") + return get_file_location(instance, filename, path_prefix="_private") class LectureMaterialFileProxy(FieldFile): diff --git a/lectures/views.py b/lectures/views.py index 1b579d4..0f05d7f 100644 --- a/lectures/views.py +++ b/lectures/views.py @@ -25,23 +25,23 @@ class LectureMaterialFileDownloadView(ObjectDownloadView): attachment = False def get_queryset(self, *args, **kwargs): - queryset = ( - super() - .get_queryset(*args, **kwargs) - .filter( - lecture__groups__in=( - get_objects_for_user( - self.current_user, "lectures.view_lecturegroup" - ).filter(models.Q(user_groups__in=self.current_user.groups.all())) - ) - ) + queryset = super().get_queryset(*args, **kwargs) + material = queryset.first() + + if material is None: + raise HTTPExceptions.NOT_FOUND + + success, auth_redirect = get_lectures( + self.request, filter=models.Q(id=material.lecture_id), get_exceptions=True ) + if not success: + raise HTTPExceptions.NOT_FOUND + return queryset def get(self, request, *args, **kwargs): - self.current_user = request.user - + self.request = request return super().get(request, *args, **kwargs) -- GitLab