skillbox/server/books/models/chapter.py

70 lines
2.3 KiB
Python

import logging
from django.db import models
from wagtail.admin.panels import FieldPanel, TabbedInterface, ObjectList
from core.wagtail_utils import StrictHierarchyPage, get_default_settings
from users.models import SchoolClass
from core.mixins import GraphqlNodeMixin
logger = logging.getLogger(__name__)
class Chapter(StrictHierarchyPage, GraphqlNodeMixin):
class Meta:
verbose_name = "Kapitel"
verbose_name_plural = "Kapitel"
description = models.TextField(blank=True)
content_panels = [
FieldPanel("title", classname="full title"),
FieldPanel("description", classname="full description"),
]
edit_handler = TabbedInterface(
[ObjectList(content_panels, heading="Content"), get_default_settings()]
)
template = "generic_page.html"
parent_page_types = ["books.Module"]
subpage_types = ["books.ContentBlock"]
title_hidden_for = models.ManyToManyField(
SchoolClass, related_name="hidden_chapter_titles"
)
description_hidden_for = models.ManyToManyField(
SchoolClass, related_name="hidden_chapter_descriptions"
)
def sync_title_visibility(self, school_class_template, school_class_to_sync):
if (
self.title_hidden_for.filter(id=school_class_template.id).exists()
and not self.title_hidden_for.filter(id=school_class_to_sync.id).exists()
):
self.title_hidden_for.add(school_class_to_sync)
if (
self.title_hidden_for.filter(id=school_class_to_sync.id).exists()
and not self.title_hidden_for.filter(id=school_class_template.id).exists()
):
self.title_hidden_for.remove(school_class_to_sync)
def sync_description_visibility(self, school_class_template, school_class_to_sync):
if (
self.description_hidden_for.filter(id=school_class_template.id).exists()
and not self.description_hidden_for.filter(
id=school_class_to_sync.id
).exists()
):
self.description_hidden_for.add(school_class_to_sync)
if (
self.description_hidden_for.filter(id=school_class_to_sync.id).exists()
and not self.description_hidden_for.filter(
id=school_class_template.id
).exists()
):
self.description_hidden_for.remove(school_class_to_sync)