Refactor content block mutation

This commit is contained in:
Ramon Wenger 2018-09-10 10:18:09 +02:00
parent 5725c3d4e4
commit d6b3fc09cb
1 changed files with 25 additions and 16 deletions

View File

@ -102,30 +102,39 @@ class AddContentBlock(relay.ClientIDMutation):
errors = graphene.List(graphene.String) errors = graphene.List(graphene.String)
@classmethod @classmethod
def create_content_block(cls, content_block_data): def create_content_block(cls, content_block_data, parent=None, after=None):
if after is None and parent is None:
raise Exception('Define either a parent or a sibling id')
title = content_block_data.get('title') title = content_block_data.get('title')
contents = content_block_data.get('contents') contents = content_block_data.get('contents')
new_contents = handle_content_blocks(contents)
return ContentBlock(title=title, contents=new_contents) new_content_block = ContentBlock(title=title)
if parent is not None:
parent_chapter = get_object(Chapter, parent).specific
parent_chapter.add_child(instance=new_content_block)
if after is not None:
sibling = get_object(ContentBlock, after).specific
sibling.add_sibling(instance=new_content_block, pos='right')
revision = new_content_block.save_revision()
revision.publish()
new_content_block.save()
new_contents = handle_content_blocks(contents) # can only do this after the content block has been saved
new_content_block.contents = json.dumps(new_contents)
return new_content_block
@classmethod @classmethod
def mutate_and_get_payload(cls, root, info, **args): def mutate_and_get_payload(cls, root, info, **args):
try: try:
parent = args.get('parent', None) parent = args.get('parent', None)
after = args.get('after', None) after = args.get('after', None)
if after is not None: new_content_block = cls.create_content_block(content_block_data=args.get('content_block'), parent=parent,
new_content_block = cls.create_content_block(content_block_data=args.get('content_block')) after=after)
sibling = get_object(ContentBlock, after)
sibling.add_sibling(instance=new_content_block, pos='right')
elif parent is not None:
new_content_block = cls.create_content_block(content_block_data=args.get('content_block'))
parent = get_object(Chapter, parent)
parent.add_child(instance=new_content_block)
else:
raise Exception('Define either a parent or a sibling id')
revision = new_content_block.save_revision()
revision.publish()
new_content_block.save()
return cls(new_content_block=new_content_block) return cls(new_content_block=new_content_block)
except ValidationError as e: except ValidationError as e: