diff --git a/server/book/schema/mutations.py b/server/book/schema/mutations.py index 015a9c93..b53e0849 100644 --- a/server/book/schema/mutations.py +++ b/server/book/schema/mutations.py @@ -102,30 +102,39 @@ class AddContentBlock(relay.ClientIDMutation): errors = graphene.List(graphene.String) @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') 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 def mutate_and_get_payload(cls, root, info, **args): try: parent = args.get('parent', 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')) - 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() + new_content_block = cls.create_content_block(content_block_data=args.get('content_block'), parent=parent, + after=after) + return cls(new_content_block=new_content_block) except ValidationError as e: