Refactor some code

Implement suggestions from pull request
This commit is contained in:
Ramon Wenger 2023-03-16 15:30:56 +01:00
parent cb0e23a5ba
commit 9ce6f9d48e
2 changed files with 33 additions and 28 deletions

View File

@ -6,16 +6,29 @@ from assignments.models import Assignment
from core.constants import DEFAULT_RICH_TEXT_FEATURES, INSTRUMENTS_RICH_TEXT_FEATURES from core.constants import DEFAULT_RICH_TEXT_FEATURES, INSTRUMENTS_RICH_TEXT_FEATURES
from surveys.models import Survey from surveys.models import Survey
"""
Using a StructBlock inside a StreamField (e.g. inside a ContentBlock):
as an illustration
data = {'text': 'This is me'}
self.contents.append(('solution', data))
by itself:
block = SolutionBlock()
data = {'text': 'This is me'}
value = block.to_python(data)
cleaned_value = block.clean(value)
"""
class CMSDocumentBlock(DocumentChooserBlock): class CMSDocumentBlock(DocumentChooserBlock):
class Meta: class Meta:
label = 'CMS Document' label = "CMS Document"
# link_block # link_block
class LinkBlock(blocks.StructBlock): class LinkBlock(blocks.StructBlock):
class Meta: class Meta:
icon = 'link' icon = "link"
text = blocks.TextBlock() text = blocks.TextBlock()
url = blocks.URLBlock() url = blocks.URLBlock()
@ -24,14 +37,14 @@ class LinkBlock(blocks.StructBlock):
# 'text_block' 'solution' # 'text_block' 'solution'
class TextBlock(blocks.StructBlock): class TextBlock(blocks.StructBlock):
class Meta: class Meta:
icon = 'doc-full' icon = "doc-full"
text = blocks.RichTextBlock(features=DEFAULT_RICH_TEXT_FEATURES) text = blocks.RichTextBlock(features=DEFAULT_RICH_TEXT_FEATURES)
class SolutionBlock(blocks.StructBlock): class SolutionBlock(blocks.StructBlock):
class Meta: class Meta:
icon = 'tick' icon = "tick"
text = blocks.RichTextBlock(features=DEFAULT_RICH_TEXT_FEATURES) text = blocks.RichTextBlock(features=DEFAULT_RICH_TEXT_FEATURES)
document = CMSDocumentBlock(required=False) document = CMSDocumentBlock(required=False)
@ -40,17 +53,19 @@ class SolutionBlock(blocks.StructBlock):
# 'basic_knowledge' # 'basic_knowledge'
class BasicKnowledgeBlock(blocks.StructBlock): class BasicKnowledgeBlock(blocks.StructBlock):
class Meta: class Meta:
icon = 'placeholder' icon = "placeholder"
label = 'Instrument' label = "Instrument"
description = blocks.RichTextBlock(required=False) description = blocks.RichTextBlock(required=False)
basic_knowledge = blocks.PageChooserBlock(required=True, page_type='basicknowledge.BasicKnowledge') basic_knowledge = blocks.PageChooserBlock(
required=True, page_type="basicknowledge.BasicKnowledge"
)
# 'image_url' # 'image_url'
class ImageUrlBlock(blocks.StructBlock): class ImageUrlBlock(blocks.StructBlock):
class Meta: class Meta:
icon = 'image' icon = "image"
title = blocks.TextBlock() title = blocks.TextBlock()
url = blocks.URLBlock() url = blocks.URLBlock()
@ -59,7 +74,7 @@ class ImageUrlBlock(blocks.StructBlock):
# 'assignment' # 'assignment'
class AssignmentBlock(blocks.StructBlock): class AssignmentBlock(blocks.StructBlock):
class Meta: class Meta:
icon = 'download' icon = "download"
assignment_id = SnippetChooserBlock(Assignment) assignment_id = SnippetChooserBlock(Assignment)
@ -67,7 +82,7 @@ class AssignmentBlock(blocks.StructBlock):
# 'survey' # 'survey'
class SurveyBlock(blocks.StructBlock): class SurveyBlock(blocks.StructBlock):
class Meta: class Meta:
icon = 'form' icon = "form"
survey_id = SnippetChooserBlock(Survey) survey_id = SnippetChooserBlock(Survey)
@ -75,7 +90,7 @@ class SurveyBlock(blocks.StructBlock):
# 'video_block' # 'video_block'
class VideoBlock(blocks.StructBlock): class VideoBlock(blocks.StructBlock):
class Meta: class Meta:
icon = 'media' icon = "media"
url = blocks.URLBlock() url = blocks.URLBlock()
@ -83,7 +98,7 @@ class VideoBlock(blocks.StructBlock):
# 'document_block' # 'document_block'
class DocumentBlock(blocks.StructBlock): class DocumentBlock(blocks.StructBlock):
class Meta: class Meta:
icon = 'doc-full' icon = "doc-full"
url = blocks.URLBlock() url = blocks.URLBlock()
@ -111,21 +126,21 @@ class SubtitleBlock(blocks.StructBlock):
class InstrumentTextBlock(blocks.StructBlock): class InstrumentTextBlock(blocks.StructBlock):
class Meta: class Meta:
icon = 'doc-full' icon = "doc-full"
text = blocks.RichTextBlock(features=INSTRUMENTS_RICH_TEXT_FEATURES) text = blocks.RichTextBlock(features=INSTRUMENTS_RICH_TEXT_FEATURES)
class ModuleRoomSlugBlock(blocks.StructBlock): class ModuleRoomSlugBlock(blocks.StructBlock):
class Meta: class Meta:
icon = 'link' icon = "link"
title = blocks.TextBlock() title = blocks.TextBlock()
class InstructionBlock(blocks.StructBlock): class InstructionBlock(blocks.StructBlock):
class Meta: class Meta:
icon = 'help' icon = "help"
url = blocks.URLBlock(required=False) url = blocks.URLBlock(required=False)
text = blocks.TextBlock(required=False) text = blocks.TextBlock(required=False)

View File

@ -163,14 +163,9 @@ class ContentBlock(StrictHierarchyPage, GraphqlNodeMixin):
# duplicate all attached Surveys and Assignments # duplicate all attached Surveys and Assignments
def duplicate_attached_entities(self): def duplicate_attached_entities(self):
# logger.debug("starting to duplicate inside the content block") duplicate_entities_func = duplicate_entities_generator(self.module)
duplicate_entities = duplicate_entities_generator(self.module) new_contents = [duplicate_entities_func(content) for content in self.contents]
# logger.debug(f"new module: {self.module}")
iterator = map(duplicate_entities, self.contents)
# logger.debug("here is the iterator")
new_contents = list(iterator)
cleaned_contents = [item for item in new_contents if item is not None] cleaned_contents = [item for item in new_contents if item is not None]
# logger.debug(new_contents)
# we can't just insert a list here, we need a StreamValue data type # we can't just insert a list here, we need a StreamValue data type
# so we need to clear the list, then add each element in turn # so we need to clear the list, then add each element in turn
self.contents.clear() self.contents.clear()
@ -179,13 +174,8 @@ class ContentBlock(StrictHierarchyPage, GraphqlNodeMixin):
self.contents.append(content) self.contents.append(content)
# as an illustration # as an illustration
# block = SolutionBlock()
# data = {'text': 'This is me'} # data = {'text': 'This is me'}
# value = block.to_python(data) # self.contents.append(('solution', data))
# clean_value = block.clean(value)
# self.contents.append(('solution', clean_value))
# logger.debug("self.contents")
# logger.debug(self.contents)
self.save() self.save()
def is_hidden_for_class(self, school_class): def is_hidden_for_class(self, school_class):