Exclude bookmarks when copying a page

This commit is contained in:
Ramon Wenger 2023-01-23 12:32:34 +01:00
parent e4dded714c
commit a1ac88dbef
1 changed files with 89 additions and 58 deletions

View File

@ -1,17 +1,36 @@
import logging import logging
from django.db import models from django.db import models
from wagtail.admin.edit_handlers import FieldPanel, TabbedInterface, ObjectList, StreamFieldPanel from wagtail.admin.edit_handlers import (
FieldPanel,
TabbedInterface,
ObjectList,
StreamFieldPanel,
)
from wagtail.core.blocks import StreamBlock from wagtail.core.blocks import StreamBlock
from wagtail.core.fields import StreamField from wagtail.core.fields import StreamField
from wagtail.images.blocks import ImageChooserBlock from wagtail.images.blocks import ImageChooserBlock
from books.managers import ContentBlockManager from books.managers import ContentBlockManager
from core.wagtail_utils import get_default_settings from core.wagtail_utils import get_default_settings
from books.blocks import CMSDocumentBlock, SolutionBlock, TextBlock, BasicKnowledgeBlock, LinkBlock, VideoBlock, \ from books.blocks import (
DocumentBlock, \ CMSDocumentBlock,
ImageUrlBlock, AssignmentBlock, InfogramBlock, GeniallyBlock, SubtitleBlock, SurveyBlock, ModuleRoomSlugBlock, \ SolutionBlock,
ThinglinkBlock, InstructionBlock TextBlock,
BasicKnowledgeBlock,
LinkBlock,
VideoBlock,
DocumentBlock,
ImageUrlBlock,
AssignmentBlock,
InfogramBlock,
GeniallyBlock,
SubtitleBlock,
SurveyBlock,
ModuleRoomSlugBlock,
ThinglinkBlock,
InstructionBlock,
)
from books.utils import get_type_and_value from books.utils import get_type_and_value
from core.wagtail_utils import StrictHierarchyPage from core.wagtail_utils import StrictHierarchyPage
from notes.models import ContentBlockBookmark from notes.models import ContentBlockBookmark
@ -23,73 +42,83 @@ logger = logging.getLogger(__name__)
class ContentBlock(StrictHierarchyPage): class ContentBlock(StrictHierarchyPage):
class Meta: class Meta:
verbose_name = 'Inhaltsblock' verbose_name = "Inhaltsblock"
verbose_name_plural = 'Inhaltsblöcke' verbose_name_plural = "Inhaltsblöcke"
NORMAL = 'normal' NORMAL = "normal"
TASK = 'task' TASK = "task"
INSTRUMENT = 'instrument' INSTRUMENT = "instrument"
TYPE_CHOICES = ( TYPE_CHOICES = (
(NORMAL, 'Normal'), (NORMAL, "Normal"),
(TASK, 'Auftrag'), (TASK, "Auftrag"),
(INSTRUMENT, 'Instrument'), (INSTRUMENT, "Instrument"),
) )
# blocks without owner are visible by default, need to be hidden for each class # blocks without owner are visible by default, need to be hidden for each class
hidden_for = models.ManyToManyField(SchoolClass, related_name='hidden_content_blocks') hidden_for = models.ManyToManyField(
SchoolClass, related_name="hidden_content_blocks"
)
# blocks with owner are hidden by default, need to be shown for each class # blocks with owner are hidden by default, need to be shown for each class
visible_for = models.ManyToManyField(SchoolClass, related_name='visible_content_blocks') visible_for = models.ManyToManyField(
SchoolClass, related_name="visible_content_blocks"
)
user_created = models.BooleanField(default=False) user_created = models.BooleanField(default=False)
original_creator = models.ForeignKey(User, null=True, blank=True, default=None, on_delete=models.SET_NULL) original_creator = models.ForeignKey(
User, null=True, blank=True, default=None, on_delete=models.SET_NULL
)
bookmarks = models.ManyToManyField(User, through=ContentBlockBookmark, related_name='bookmarked_content_blocks') bookmarks = models.ManyToManyField(
User, through=ContentBlockBookmark, related_name="bookmarked_content_blocks"
)
content_blocks = [ content_blocks = [
('text_block', TextBlock()), ("text_block", TextBlock()),
('basic_knowledge', BasicKnowledgeBlock()), ("basic_knowledge", BasicKnowledgeBlock()),
('assignment', AssignmentBlock()), ("assignment", AssignmentBlock()),
('survey', SurveyBlock()), ("survey", SurveyBlock()),
('image_block', ImageChooserBlock()), ("image_block", ImageChooserBlock()),
('image_url_block', ImageUrlBlock()), ("image_url_block", ImageUrlBlock()),
('link_block', LinkBlock()), ("link_block", LinkBlock()),
# ('solution', TextBlock(icon='tick')), # ('solution', TextBlock(icon='tick')),
('solution', SolutionBlock()), ("solution", SolutionBlock()),
('video_block', VideoBlock()), ("video_block", VideoBlock()),
('document_block', DocumentBlock()), ("document_block", DocumentBlock()),
('infogram_block', InfogramBlock()), ("infogram_block", InfogramBlock()),
('genially_block', GeniallyBlock()), ("genially_block", GeniallyBlock()),
('thinglink_block', ThinglinkBlock()), ("thinglink_block", ThinglinkBlock()),
('subtitle', SubtitleBlock()), ("subtitle", SubtitleBlock()),
('instruction', InstructionBlock()), ("instruction", InstructionBlock()),
('module_room_slug', ModuleRoomSlugBlock()), ("module_room_slug", ModuleRoomSlugBlock()),
# ('cms_document_block', DocumentChooserBlock(label='CMS Document')) # ('cms_document_block', DocumentChooserBlock(label='CMS Document'))
('cms_document_block', CMSDocumentBlock()) ("cms_document_block", CMSDocumentBlock()),
] ]
content_list_item = StreamBlock(content_blocks) content_list_item = StreamBlock(content_blocks)
contents = StreamField(content_blocks + [('content_list_item', content_list_item)], null=True, blank=True) contents = StreamField(
content_blocks + [("content_list_item", content_list_item)],
type = models.CharField( null=True,
max_length=100, blank=True,
choices=TYPE_CHOICES,
default=NORMAL
) )
type = models.CharField(
max_length=100, choices=TYPE_CHOICES, default=NORMAL)
content_panels = [ content_panels = [
FieldPanel('title', classname="full title"), FieldPanel("title", classname="full title"),
FieldPanel('type'), FieldPanel("type"),
StreamFieldPanel('contents') StreamFieldPanel("contents"),
] ]
# #
edit_handler = TabbedInterface([ edit_handler = TabbedInterface(
ObjectList(content_panels, heading='Content'), [ObjectList(content_panels, heading="Content"), get_default_settings()]
get_default_settings() )
])
parent_page_types = ['books.Chapter'] parent_page_types = ["books.Chapter"]
subpage_types = [] subpage_types = []
# prevent these fields from being copied
exclude_fields_in_copy = ["bookmarks"]
objects = ContentBlockManager() objects = ContentBlockManager()
@ -99,18 +128,20 @@ class ContentBlock(StrictHierarchyPage):
def is_hidden_for_class(self, school_class): def is_hidden_for_class(self, school_class):
return ( return (
not self.user_created and self.hidden_for.filter(id=school_class.id).exists() not self.user_created
and self.hidden_for.filter(id=school_class.id).exists()
) or ( ) or (
self.user_created and not self.visible_for.filter(id=school_class.id).exists() self.user_created
and not self.visible_for.filter(id=school_class.id).exists()
) )
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
for data in self.contents.raw_data: for data in self.contents.raw_data:
block_type, value = get_type_and_value(data) block_type, value = get_type_and_value(data)
if block_type == 'survey': if block_type == "survey":
module = self.module module = self.module
survey = value['survey_id'] survey = value["survey_id"]
if isinstance(survey, int): if isinstance(survey, int):
survey = Survey.objects.get(pk=survey) survey = Survey.objects.get(pk=survey)
if survey.module != module: if survey.module != module:
@ -122,10 +153,10 @@ class ContentBlock(StrictHierarchyPage):
class ContentBlockSnapshot(ContentBlock): class ContentBlockSnapshot(ContentBlock):
hidden = models.BooleanField(default=False) hidden = models.BooleanField(default=False)
snapshot = models.ForeignKey( snapshot = models.ForeignKey(
'books.snapshot', "books.snapshot",
on_delete=models.SET_NULL, on_delete=models.SET_NULL,
null=True, null=True,
related_name='custom_content_blocks' related_name="custom_content_blocks",
) )
def to_regular_content_block(self, owner, school_class): def to_regular_content_block(self, owner, school_class):
@ -135,9 +166,9 @@ class ContentBlockSnapshot(ContentBlock):
title=self.title, title=self.title,
owner=owner, owner=owner,
original_creator=self.original_creator, original_creator=self.original_creator,
user_created=True user_created=True,
) )
self.add_sibling(instance=cb, pos='right') self.add_sibling(instance=cb, pos="right")
# some wagtail magic # some wagtail magic
revision = cb.save_revision() revision = cb.save_revision()
revision.publish() revision.publish()