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 ...@@ -2,6 +2,7 @@ from django.contrib import admin
from shared.admin import MarkdownxGuardedModelAdmin from shared.admin import MarkdownxGuardedModelAdmin
from .forms import LectureGroupTypeFormset
from .models import ( from .models import (
Lecture, Lecture,
LectureGroup, LectureGroup,
...@@ -39,6 +40,7 @@ class LectureMaterialInline(admin.StackedInline): ...@@ -39,6 +40,7 @@ class LectureMaterialInline(admin.StackedInline):
class LectureGroupTypeInline(admin.StackedInline): class LectureGroupTypeInline(admin.StackedInline):
model = LectureGroupType model = LectureGroupType
autocomplete_fields = ("group",) autocomplete_fields = ("group",)
formset = LectureGroupTypeFormset
extra = 1 extra = 1
...@@ -56,8 +58,23 @@ class LectureAdmin(MarkdownxGuardedModelAdmin): ...@@ -56,8 +58,23 @@ class LectureAdmin(MarkdownxGuardedModelAdmin):
list_display = ( list_display = (
"name", "name",
"timestamp", "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): class LectureLectorAdmin(MarkdownxGuardedModelAdmin):
search_fields = ("name", "username") 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): ...@@ -106,6 +106,9 @@ class LectureGroupType(models.Model):
verbose_name="Požadovanost", verbose_name="Požadovanost",
) )
def __str__(self) -> str:
return f"{self.group} - {self.lecture}"
class Meta: class Meta:
verbose_name = "Úroveň požadovanosti pro skupinu" verbose_name = "Úroveň požadovanosti pro skupinu"
verbose_name_plural = "Úroveň požadovanosti pro skupiny" verbose_name_plural = "Úroveň požadovanosti pro skupiny"
...@@ -261,7 +264,7 @@ class LectureRecording(NameStrMixin, models.Model): ...@@ -261,7 +264,7 @@ class LectureRecording(NameStrMixin, models.Model):
def get_lecture_material_file_location(instance, filename): 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): class LectureMaterialFileProxy(FieldFile):
......
...@@ -25,23 +25,23 @@ class LectureMaterialFileDownloadView(ObjectDownloadView): ...@@ -25,23 +25,23 @@ class LectureMaterialFileDownloadView(ObjectDownloadView):
attachment = False attachment = False
def get_queryset(self, *args, **kwargs): def get_queryset(self, *args, **kwargs):
queryset = ( queryset = super().get_queryset(*args, **kwargs)
super() material = queryset.first()
.get_queryset(*args, **kwargs)
.filter( if material is None:
lecture__groups__in=( raise HTTPExceptions.NOT_FOUND
get_objects_for_user(
self.current_user, "lectures.view_lecturegroup" success, auth_redirect = get_lectures(
).filter(models.Q(user_groups__in=self.current_user.groups.all())) self.request, filter=models.Q(id=material.lecture_id), get_exceptions=True
)
)
) )
if not success:
raise HTTPExceptions.NOT_FOUND
return queryset return queryset
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
self.current_user = request.user self.request = request
return super().get(request, *args, **kwargs) return super().get(request, *args, **kwargs)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment