skillbox/server/book/schema/mutations/utils.py

73 lines
1.9 KiB
Python

# -*- coding: utf-8 -*-
#
# Iterativ GmbH
# http://www.iterativ.ch/
#
# Copyright (c) 2018 Iterativ GmbH. All rights reserved.
#
# Created on 25.09.18
# @author: Ramon Wenger <ramon.wenger@iterativ.ch>
import bleach
import re
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)
ALLOWED_BLOCKS = (
'text_block',
'student_entry',
'image_url_block',
'link_block',
'video_block',
'document_block',
)
def handle_content_block(content, allowed_blocks=ALLOWED_BLOCKS):
# todo: add all the content blocks
# todo: sanitize user inputs!
if content['type'] not in allowed_blocks:
return
if content['type'] == 'text_block':
return {
'type': 'text_block',
'value': {
'text': newlines_to_paragraphs(bleach.clean(content['value']['text'], strip=True))
}}
elif content['type'] == 'student_entry':
return
elif content['type'] == 'image_url_block':
return {
'type': 'image_url_block',
'value': {
'url': bleach.clean(content['value']['url'])
}}
elif content['type'] == 'link_block':
return {
'type': 'link_block',
'value': {
'text': bleach.clean(content['value']['text']),
'url': bleach.clean(content['value']['url'])
}
}
elif content['type'] == 'video_block':
return {
'type': 'video_block',
'value': {
'url': bleach.clean(content['value']['url'])
}}
elif content['type'] == 'document_block':
return {
'type': 'document_block',
'value': {
'url': bleach.clean(content['value']['url'])
}}
return None