Handle contents not editable by users on the server
Relates to MS-589
This commit is contained in:
parent
638efac6e3
commit
4489a8dcf8
|
|
@ -13,6 +13,7 @@ class InputTypes(graphene.Enum):
|
||||||
document_block = 'document_block'
|
document_block = 'document_block'
|
||||||
content_list_item = 'content_list_item'
|
content_list_item = 'content_list_item'
|
||||||
subtitle = 'subtitle'
|
subtitle = 'subtitle'
|
||||||
|
readonly = 'readonly'
|
||||||
|
|
||||||
|
|
||||||
class ContentElementValueInput(InputObjectType):
|
class ContentElementValueInput(InputObjectType):
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,7 @@ class MutateContentBlock(relay.ClientIDMutation):
|
||||||
content_block.title = title
|
content_block.title = title
|
||||||
|
|
||||||
if contents is not None:
|
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()
|
content_block.save()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -41,10 +41,11 @@ ALLOWED_BLOCKS = (
|
||||||
'document_block',
|
'document_block',
|
||||||
'content_list_item',
|
'content_list_item',
|
||||||
'subtitle',
|
'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: add all the content blocks
|
||||||
# todo: sanitize user inputs!
|
# todo: sanitize user inputs!
|
||||||
if content['type'] not in allowed_blocks:
|
if content['type'] not in allowed_blocks:
|
||||||
|
|
@ -64,9 +65,10 @@ def handle_content_block(content, context=None, module=None, allowed_blocks=ALLO
|
||||||
value = content['value']
|
value = content['value']
|
||||||
if value.get('id') is not None:
|
if value.get('id') is not None:
|
||||||
assignment = get_object(Assignment, value.get('id'))
|
assignment = get_object(Assignment, value.get('id'))
|
||||||
assignment.title = value.get('title')
|
if assignment.user_created and assignment.owner == context.user:
|
||||||
assignment.assignment = value.get('assignment')
|
assignment.title = value.get('title')
|
||||||
assignment.save()
|
assignment.assignment = value.get('assignment')
|
||||||
|
assignment.save()
|
||||||
else:
|
else:
|
||||||
assignment = Assignment.objects.create(
|
assignment = Assignment.objects.create(
|
||||||
title=value.get('title'),
|
title=value.get('title'),
|
||||||
|
|
@ -119,7 +121,12 @@ def handle_content_block(content, context=None, module=None, allowed_blocks=ALLO
|
||||||
'type': 'content_list_item',
|
'type': 'content_list_item',
|
||||||
'value': [handle_content_block(c, context, module) for c in content['contents']]
|
'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
|
return None
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue