Update content block mutation

This commit is contained in:
Ramon Wenger 2018-09-10 09:52:38 +02:00
parent 5dec3d06c9
commit 5725c3d4e4
1 changed files with 34 additions and 15 deletions

View File

@ -95,26 +95,45 @@ class AddContentBlock(relay.ClientIDMutation):
class Input: class Input:
content_block = graphene.Argument(ContentBlockInput) content_block = graphene.Argument(ContentBlockInput)
# todo: handle both of these differently, one for a new chapter maybe # todo: handle both of these differently, one for a new chapter maybe
parent = graphene.ID() parent = graphene.ID() # ID of chapter node; new content block will be inserted at the start of it
after = graphene.ID() after = graphene.ID() # ID of content block node; new content block will be inserted after this content block node
new_content_block = graphene.Field(ContentBlockNode) new_content_block = graphene.Field(ContentBlockNode)
errors = graphene.List(graphene.String)
@classmethod
def create_content_block(cls, content_block_data):
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)
@classmethod @classmethod
def mutate_and_get_payload(cls, root, info, **args): def mutate_and_get_payload(cls, root, info, **args):
content_block_data = args.get('content_block') try:
title = content_block_data.get('title') parent = args.get('parent', None)
new_content_block = ContentBlock(title=title) after = args.get('after', None)
parent = Chapter.objects.get( if after is not None:
pk=18).specific # atm just "1.1 Lehrbeginn" / todo: dynamic, refactor in above class new_content_block = cls.create_content_block(content_block_data=args.get('content_block'))
parent.add_child(instance=new_content_block) sibling = get_object(ContentBlock, after)
revision = new_content_block.save_revision() sibling.add_sibling(instance=new_content_block, pos='right')
revision.publish() elif parent is not None:
contents = content_block_data.get('contents') new_content_block = cls.create_content_block(content_block_data=args.get('content_block'))
new_contents = handle_content_blocks(contents) parent = get_object(Chapter, parent)
new_content_block.contents = json.dumps(new_contents) parent.add_child(instance=new_content_block)
new_content_block.save() else:
return cls(new_content_block=new_content_block) 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)
except ValidationError as e:
errors = get_errors(e)
except Exception as e:
errors = ['Error: {}'.format(e)]
return cls(new_content_block=None, errors=errors)
class BookMutations(object): class BookMutations(object):