Skip to content
Snippets Groups Projects
Commit 055f8892 authored by Tomáš Valenta's avatar Tomáš Valenta
Browse files

finish material queryset

parent aa61bd5e
No related branches found
No related tags found
No related merge requests found
Pipeline #14049 passed
......@@ -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")
......
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
......@@ -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):
......
......@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment