Update mutation to pass test

This commit is contained in:
Ramon Wenger 2022-02-24 16:24:42 +01:00
parent 3034f77640
commit b5ffb3ee29
2 changed files with 11 additions and 6 deletions

View File

@ -10,7 +10,7 @@ from books.models import ContentBlock, Chapter, SchoolClass
from books.schema.inputs import ContentBlockInput
from ..nodes import ContentBlockNode
from core.utils import set_hidden_for, set_visible_for
from .utils import handle_content_block, set_user_defined_block_type
from .utils import handle_content_block, handle_text, set_user_defined_block_type
class MutateContentBlock(relay.ClientIDMutation):

View File

@ -20,10 +20,15 @@ class AssignmentParameterException(Exception):
pass
def newlines_to_paragraphs(text):
parts = re.split(r'[\r\n]+', text)
paragraphs = ['<p>{}</p>'.format(p.strip()) for p in parts]
return '\n'.join(paragraphs)
def handle_text(text):
is_list = bool(re.search(r'<ul>', text))
if is_list:
# let's assume this is formatted correctly already, otherwise bleach or the browser should strip / ignore it
return text
else:
parts = re.split(r'[\r\n]+', text)
paragraphs = ['<p>{}</p>'.format(p.strip()) for p in parts]
return '\n'.join(paragraphs)
ALLOWED_BLOCKS = (
@ -49,7 +54,7 @@ def handle_content_block(content, context=None, module=None, allowed_blocks=ALLO
return {
'type': 'text_block',
'value': {
'text': newlines_to_paragraphs(bleach.clean(content['value']['text'], strip=True))
'text': handle_text(bleach.clean(content['value']['text'], strip=True))
}}
elif content['type'] == 'assignment':
if module is None: