Handle contents not editable by users on the server

Relates to MS-589
This commit is contained in:
Ramon Wenger 2022-10-04 17:11:03 +02:00
parent 638efac6e3
commit 4489a8dcf8
3 changed files with 14 additions and 6 deletions

View File

@ -13,6 +13,7 @@ class InputTypes(graphene.Enum):
document_block = 'document_block'
content_list_item = 'content_list_item'
subtitle = 'subtitle'
readonly = 'readonly'
class ContentElementValueInput(InputObjectType):

View File

@ -48,7 +48,7 @@ class MutateContentBlock(relay.ClientIDMutation):
content_block.title = title
if contents is not None:
content_block.contents = json.dumps([handle_content_block(c, info.context, module) for c in contents])
content_block.contents = json.dumps([handle_content_block(c, info.context, module, previous_contents=content_block.contents) for c in contents if c is not None])
content_block.save()

View File

@ -41,10 +41,11 @@ ALLOWED_BLOCKS = (
'document_block',
'content_list_item',
'subtitle',
'readonly'
)
def handle_content_block(content, context=None, module=None, allowed_blocks=ALLOWED_BLOCKS):
def handle_content_block(content, context=None, module=None, allowed_blocks=ALLOWED_BLOCKS, previous_contents=None):
# todo: add all the content blocks
# todo: sanitize user inputs!
if content['type'] not in allowed_blocks:
@ -64,6 +65,7 @@ def handle_content_block(content, context=None, module=None, allowed_blocks=ALLO
value = content['value']
if value.get('id') is not None:
assignment = get_object(Assignment, value.get('id'))
if assignment.user_created and assignment.owner == context.user:
assignment.title = value.get('title')
assignment.assignment = value.get('assignment')
assignment.save()
@ -119,7 +121,12 @@ def handle_content_block(content, context=None, module=None, allowed_blocks=ALLO
'type': 'content_list_item',
'value': [handle_content_block(c, context, module) for c in content['contents']]
}
elif content['type'] == 'readonly' and previous_contents is not None:
# get first item that matches the id
# users can re-order readonly items, but we won't let them change them otherwise, so we just take the
# item from before and ignore anything else
previous_content = next((c for c in previous_contents.raw_data if c['id'] == content['id']), None)
return previous_content
return None