Change content block mutation

This commit is contained in:
Ramon Wenger 2018-09-20 10:29:05 +02:00
parent 4884c62c3b
commit c8937243f5
7 changed files with 57 additions and 42 deletions

View File

@ -46,12 +46,14 @@
variables: { variables: {
input: { input: {
id: this.contentBlock.id, id: this.contentBlock.id,
visibility: this.userGroupsWithVisibilityInfo.map(g => { contentBlock: {
return { visibility: this.userGroupsWithVisibilityInfo.map(g => {
userGroupId: g.id, return {
hidden: g.hidden || false userGroupId: g.id,
} hidden: g.hidden || false
}) }
})
}
} }
} }
// refetchQueries: [{ // refetchQueries: [{

View File

@ -71,7 +71,11 @@
data() { data() {
return { return {
error: false, error: false,
localContentBlock: JSON.parse(JSON.stringify(this.contentBlock)) localContentBlock: Object.assign({}, {
title: this.contentBlock.title,
contents: [...this.contentBlock.contents],
id: this.contentBlock.id || undefined
})
} }
}, },
@ -107,7 +111,6 @@
this._updateProperty(value, index, 'url') this._updateProperty(value, index, 'url')
}, },
changeLinkText(value, index) { changeLinkText(value, index) {
// debugger;
this._updateProperty(value, index, 'text') this._updateProperty(value, index, 'text')
}, },
changeVideoUrl(value, index) { changeVideoUrl(value, index) {

View File

@ -11,7 +11,7 @@
import store from '@/store/index'; import store from '@/store/index';
import NEW_CONTENT_BLOCK_MUTATION from '@/graphql/gql/mutations/addContentBlock.gql'; import EDIT_CONTENT_BLOCK_MUTATION from '@/graphql/gql/mutations/mutateContentBlock.gql';
import MODULE_DETAILS_QUERY from '@/graphql/gql/moduleDetailsQuery.gql'; import MODULE_DETAILS_QUERY from '@/graphql/gql/moduleDetailsQuery.gql';
import CONTENT_BLOCK_QUERY from '@/graphql/gql/contentBlockQuery.gql'; import CONTENT_BLOCK_QUERY from '@/graphql/gql/contentBlockQuery.gql';
@ -27,15 +27,14 @@
}, },
saveContentBlock(contentBlock) { saveContentBlock(contentBlock) {
this.$apollo.mutate({ this.$apollo.mutate({
mutation: NEW_CONTENT_BLOCK_MUTATION, mutation: EDIT_CONTENT_BLOCK_MUTATION,
variables: { variables: {
input: { input: {
contentBlock: { contentBlock: {
title: contentBlock.title, title: contentBlock.title,
contents: contentBlock.contents.filter(value => Object.keys(value).length > 0) contents: contentBlock.contents.filter(value => Object.keys(value).length > 0)
}, },
after: this.$store.state.contentBlockPosition.after, id: contentBlock.id
parent: this.$store.state.contentBlockPosition.parent
} }
}, },
refetchQueries: [{ refetchQueries: [{

View File

@ -37,7 +37,7 @@
function openImagePanel(panelElement) { function openImagePanel(panelElement) {
return uploadcare.openPanel(panelElement, null, { return uploadcare.openPanel(panelElement, null, {
tabs: ['file'], tabs: ['file'],
publicKey: '78212ff39934a59775ac', publicKey: '78212ff39934a59775ac'
}); });
} }
}, },
@ -49,7 +49,7 @@
} }
return null; return null;
} }
}, }
} }
</script> </script>

View File

@ -13,7 +13,7 @@
computed: { computed: {
text() { text() {
return this.value.text.replace(/(<([^>]+)>)/ig, '') return this.value.text.replace(/<br(\/)?>/, '\n').replace(/(<([^>]+)>)/ig, '')
} }
} }
} }

View File

@ -25,16 +25,18 @@ class ContentElementValueInput(InputObjectType):
class ContentElementInput(InputObjectType): class ContentElementInput(InputObjectType):
id = graphene.String()
type = InputTypes(required=True) type = InputTypes(required=True)
value = ContentElementValueInput() value = ContentElementValueInput()
class ContentBlockInput(InputObjectType):
title = graphene.String(required=True)
type = graphene.String()
contents = graphene.List(ContentElementInput)
class UserGroupContentBlockVisibility(InputObjectType): class UserGroupContentBlockVisibility(InputObjectType):
user_group_id = graphene.ID(required=True) user_group_id = graphene.ID(required=True)
hidden = graphene.Boolean(required=True) hidden = graphene.Boolean(required=True)
class ContentBlockInput(InputObjectType):
title = graphene.String()
type = graphene.String()
contents = graphene.List(ContentElementInput)
visibility = graphene.List(UserGroupContentBlockVisibility)

View File

@ -2,15 +2,22 @@ import json
import bleach import bleach
import graphene import graphene
import re
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from graphene import relay from graphene import relay
from api.utils import get_object, get_errors from api.utils import get_object, get_errors
from book.models import ContentBlock, Chapter, UserGroup from book.models import ContentBlock, Chapter, UserGroup
from book.schema.inputs import ContentBlockInput, UserGroupContentBlockVisibility from book.schema.inputs import ContentBlockInput
from book.schema.queries import ContentBlockNode from book.schema.queries import ContentBlockNode
def newlines_to_paragraphs(text):
parts = re.split(r'[\r\n]+', text)
paragraphs = ['<p>{}</p>'.format(p.strip()) for p in parts]
return '\n'.join(paragraphs)
def handle_content_blocks(content_data): def handle_content_blocks(content_data):
new_contents = [] new_contents = []
@ -21,7 +28,7 @@ def handle_content_blocks(content_data):
new_contents.append({ new_contents.append({
'type': 'text_block', 'type': 'text_block',
'value': { 'value': {
'text': '<p>{}</p>'.format(bleach.clean(content['value']['text'])) 'text': newlines_to_paragraphs(bleach.clean(content['value']['text'], strip=True))
}}) }})
elif content['type'] == 'student_entry': elif content['type'] == 'student_entry':
pass pass
@ -60,10 +67,7 @@ def handle_content_blocks(content_data):
class MutateContentBlock(relay.ClientIDMutation): class MutateContentBlock(relay.ClientIDMutation):
class Input: class Input:
id = graphene.ID(required=True) id = graphene.ID(required=True)
type = graphene.String() content_block = graphene.Argument(ContentBlockInput)
title = graphene.String()
contents = graphene.String()
visibility = graphene.List(UserGroupContentBlockVisibility)
errors = graphene.List(graphene.String) errors = graphene.List(graphene.String)
content_block = graphene.Field(ContentBlockNode) content_block = graphene.Field(ContentBlockNode)
@ -72,31 +76,36 @@ class MutateContentBlock(relay.ClientIDMutation):
def mutate_and_get_payload(cls, *args, **kwargs): def mutate_and_get_payload(cls, *args, **kwargs):
try: try:
id_param = kwargs['id'] id_param = kwargs['id']
type_param = kwargs.get('type') content_block_data = kwargs.get('content_block')
title = kwargs.get('title') # type_param = content_block_data.get('type')
contents_data = kwargs.get('contents') title = content_block_data.get('title', None)
visibility_list = kwargs.get('visibility', []) contents = content_block_data.get('contents', None)
visibility_list = content_block_data.get('visibility', None)
#new_content_block = ContentBlock(type=type_param, title=title, contents=contents_data)
content_block = get_object(ContentBlock, id_param) content_block = get_object(ContentBlock, id_param)
for v in visibility_list: if visibility_list is not None:
user_group = get_object(UserGroup, v.user_group_id) for v in visibility_list:
if v.hidden: user_group = get_object(UserGroup, v.user_group_id)
content_block.hidden_for.add(user_group) if v.hidden:
else: content_block.hidden_for.add(user_group)
content_block.hidden_for.remove(user_group) else:
content_block.hidden_for.remove(user_group)
if title is not None:
content_block.title = title
if contents is not None:
content_block.contents = json.dumps(handle_content_blocks(contents))
content_block.save() content_block.save()
# content_block.add_sibling(instance=new_content_block, pos='right')
#content_block.add_sibling(instance=new_content_block, pos='right')
# ContentBlock.objects.get() # ContentBlock.objects.get()
# cb.add_sibling() # cb.add_sibling()
#new_content_block.saveI() # new_content_block.saveI()
# image_instance = get_object(Image, kwargs['id']) # image_instance = get_object(Image, kwargs['id'])
# if image_instance: # if image_instance: