skillbox/server/books/models/contentblock.py

82 lines
2.4 KiB
Python

import logging
from django.db import models
from wagtail.admin.edit_handlers import FieldPanel, TabbedInterface, ObjectList, StreamFieldPanel
from wagtail.core.fields import StreamField
from wagtail.images.blocks import ImageChooserBlock
from wagtail.core import hooks
from books.blocks import TextBlock, BasicKnowledgeBlock, LinkBlock, VideoBlock, DocumentBlock, \
ImageUrlBlock, AssignmentBlock
from core.wagtail_utils import StrictHierarchyPage
from users.models import SchoolClass
logger = logging.getLogger(__name__)
class ContentBlock(StrictHierarchyPage):
class Meta:
verbose_name = 'Inhaltsblock'
verbose_name_plural = 'Inhaltsblöcke'
PLAIN = 'plain'
YELLOW = 'yellow'
GREEN = 'green'
BLUE = 'blue'
TYPE_CHOICES = (
(PLAIN, 'Normal'),
(YELLOW, 'Gelb'),
(GREEN, 'Grün'),
(BLUE, 'Blau'),
)
hidden_for = models.ManyToManyField(SchoolClass, related_name='hidden_content_blocks')
visible_for = models.ManyToManyField(SchoolClass, related_name='visible_content_blocks')
user_created = models.BooleanField(default=False)
contents = StreamField([
('text_block', TextBlock()),
('basic_knowledge', BasicKnowledgeBlock()),
('assignment', AssignmentBlock()),
('image_block', ImageChooserBlock()),
('image_url_block', ImageUrlBlock()),
('link_block', LinkBlock()),
('task', TextBlock(icon='tick')),
('video_block', VideoBlock()),
('document_block', DocumentBlock()),
], null=True, blank=True)
type = models.CharField(
max_length=100,
choices=TYPE_CHOICES,
default=PLAIN
)
content_panels = [
FieldPanel('title', classname="full title"),
FieldPanel('type'),
StreamFieldPanel('contents')
]
settings_panels = [
FieldPanel('slug')
]
edit_handler = TabbedInterface([
ObjectList(content_panels, heading='Content'),
ObjectList(settings_panels, heading='Settings'),
])
template = 'generic_page.html'
parent_page_types = ['books.Chapter']
subpage_types = []
@hooks.register('construct_explorer_page_queryset')
def remove_user_created_content_blocks_from_menu(parent_page, pages, request):
if parent_page.content_type.model == 'chapter':
return ContentBlock.get_by_parent(parent_page.specific).exclude(user_created=True)
return pages