Add first take on adding a new content block via interface

This commit is contained in:
Pawel Kowalski 2018-09-05 15:09:16 +02:00
parent 356329f266
commit c7711989b7
6 changed files with 20 additions and 19 deletions

View File

@ -4,6 +4,7 @@
<div class="content-block" :class="specialClass">
<h4>{{contentBlock.title}}</h4>
<h4>{{contentBlock.id}}</h4>
<component v-for="component in contentBlock.contents" :key="component.id" :is="component.type"
v-bind="component"></component>

View File

@ -4,9 +4,9 @@ from graphene_django.filter import DjangoFilterConnectionField
from graphql_relay.node.node import from_global_id
def get_object(object_name, relayId, otherwise=None):
def get_object(object_name, relay_id, otherwise=None):
try:
return object_name.objects.get(pk=from_global_id(relayId)[1])
return object_name.objects.get(pk=from_global_id(relay_id)[1])
except:
return otherwise

View File

@ -7,6 +7,7 @@ class LinkBlock(blocks.StructBlock):
text = blocks.TextBlock()
url = blocks.URLBlock()
# 'text_block' 'task'
class TextBlock(blocks.StructBlock):
text = blocks.RichTextBlock()

View File

@ -53,4 +53,4 @@ class ContentBlock(StrictHierarchyPage):
template = 'generic_page.html'
parent_page_types = ['book.Chapter']
subpage_types = []

View File

@ -1,7 +1,7 @@
import graphene
from django.core.exceptions import ValidationError
from api.utils import get_errors
from api.utils import get_object, get_errors
from book.models import ContentBlock
from book.schema.queries import ContentBlockNode
@ -23,11 +23,19 @@ class MutateContentBlock(graphene.relay.ClientIDMutation):
@classmethod
def mutate_and_get_payload(cls, *args, **kwargs):
try:
id_param = kwargs['id']
type_param = kwargs['type']
title = kwargs['title']
cb = ContentBlock(type=type_param, title=title)
new_content_block = ContentBlock(type=type_param, title=title)
content_block = get_object(ContentBlock, id_param)
content_block.add_sibling(instance=new_content_block, pos='right')
# ContentBlock.objects.get()
# cb.add_sibling()
new_content_block.save()
# image_instance = get_object(Image, kwargs['id'])
# if image_instance:
@ -40,25 +48,14 @@ class MutateContentBlock(graphene.relay.ClientIDMutation):
# tag_slugs = [t for t in tag_slugs if t not in [e.slug for e in tags]]
# updated_image.tags.set(*(tags + tag_slugs))
return cls(content_block=cb)
return cls(content_block=new_content_block)
except ValidationError as e:
errors = get_errors(e)
except Exception as e:
errors = ['Error: {}'.format(e)]
return cls(updated_image=None, tags=None, errors=errors)
# user_module_progress = get_object_or_404(
# UserModuleProgress,
# user=get_current_user(),
# module__slug=kwargs.pop('module_slug')
# )
#
# for k, v in kwargs.items():
# setattr(user_module_progress, k, v)
#
# user_module_progress.save()
return cls(content_block=None, errors=errors)
class BookMutations(object):

View File

@ -107,7 +107,9 @@ class BookNode(DjangoObjectType):
class BookQuery(object):
topic = relay.Node.Field(TopicNode)
module = relay.Node.Field(ModuleNode)
chapter = relay.Node.Field(ChapterNode)
books = DjangoFilterConnectionField(BookNode)
topics = DjangoFilterConnectionField(TopicNode)