Change content block mutation
This commit is contained in:
parent
4884c62c3b
commit
c8937243f5
|
|
@ -46,12 +46,14 @@
|
|||
variables: {
|
||||
input: {
|
||||
id: this.contentBlock.id,
|
||||
visibility: this.userGroupsWithVisibilityInfo.map(g => {
|
||||
return {
|
||||
userGroupId: g.id,
|
||||
hidden: g.hidden || false
|
||||
}
|
||||
})
|
||||
contentBlock: {
|
||||
visibility: this.userGroupsWithVisibilityInfo.map(g => {
|
||||
return {
|
||||
userGroupId: g.id,
|
||||
hidden: g.hidden || false
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
// refetchQueries: [{
|
||||
|
|
|
|||
|
|
@ -71,7 +71,11 @@
|
|||
data() {
|
||||
return {
|
||||
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')
|
||||
},
|
||||
changeLinkText(value, index) {
|
||||
// debugger;
|
||||
this._updateProperty(value, index, 'text')
|
||||
},
|
||||
changeVideoUrl(value, index) {
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@
|
|||
|
||||
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 CONTENT_BLOCK_QUERY from '@/graphql/gql/contentBlockQuery.gql';
|
||||
|
||||
|
|
@ -27,15 +27,14 @@
|
|||
},
|
||||
saveContentBlock(contentBlock) {
|
||||
this.$apollo.mutate({
|
||||
mutation: NEW_CONTENT_BLOCK_MUTATION,
|
||||
mutation: EDIT_CONTENT_BLOCK_MUTATION,
|
||||
variables: {
|
||||
input: {
|
||||
contentBlock: {
|
||||
title: contentBlock.title,
|
||||
contents: contentBlock.contents.filter(value => Object.keys(value).length > 0)
|
||||
},
|
||||
after: this.$store.state.contentBlockPosition.after,
|
||||
parent: this.$store.state.contentBlockPosition.parent
|
||||
id: contentBlock.id
|
||||
}
|
||||
},
|
||||
refetchQueries: [{
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@
|
|||
function openImagePanel(panelElement) {
|
||||
return uploadcare.openPanel(panelElement, null, {
|
||||
tabs: ['file'],
|
||||
publicKey: '78212ff39934a59775ac',
|
||||
publicKey: '78212ff39934a59775ac'
|
||||
});
|
||||
}
|
||||
},
|
||||
|
|
@ -49,7 +49,7 @@
|
|||
}
|
||||
return null;
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@
|
|||
|
||||
computed: {
|
||||
text() {
|
||||
return this.value.text.replace(/(<([^>]+)>)/ig, '')
|
||||
return this.value.text.replace(/<br(\/)?>/, '\n').replace(/(<([^>]+)>)/ig, '')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,16 +25,18 @@ class ContentElementValueInput(InputObjectType):
|
|||
|
||||
|
||||
class ContentElementInput(InputObjectType):
|
||||
id = graphene.String()
|
||||
type = InputTypes(required=True)
|
||||
value = ContentElementValueInput()
|
||||
|
||||
|
||||
class ContentBlockInput(InputObjectType):
|
||||
title = graphene.String(required=True)
|
||||
type = graphene.String()
|
||||
contents = graphene.List(ContentElementInput)
|
||||
|
||||
|
||||
class UserGroupContentBlockVisibility(InputObjectType):
|
||||
user_group_id = graphene.ID(required=True)
|
||||
hidden = graphene.Boolean(required=True)
|
||||
|
||||
|
||||
class ContentBlockInput(InputObjectType):
|
||||
title = graphene.String()
|
||||
type = graphene.String()
|
||||
contents = graphene.List(ContentElementInput)
|
||||
visibility = graphene.List(UserGroupContentBlockVisibility)
|
||||
|
|
@ -2,15 +2,22 @@ import json
|
|||
|
||||
import bleach
|
||||
import graphene
|
||||
import re
|
||||
from django.core.exceptions import ValidationError
|
||||
from graphene import relay
|
||||
|
||||
from api.utils import get_object, get_errors
|
||||
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
|
||||
|
||||
|
||||
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):
|
||||
new_contents = []
|
||||
|
||||
|
|
@ -21,7 +28,7 @@ def handle_content_blocks(content_data):
|
|||
new_contents.append({
|
||||
'type': 'text_block',
|
||||
'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':
|
||||
pass
|
||||
|
|
@ -60,10 +67,7 @@ def handle_content_blocks(content_data):
|
|||
class MutateContentBlock(relay.ClientIDMutation):
|
||||
class Input:
|
||||
id = graphene.ID(required=True)
|
||||
type = graphene.String()
|
||||
title = graphene.String()
|
||||
contents = graphene.String()
|
||||
visibility = graphene.List(UserGroupContentBlockVisibility)
|
||||
content_block = graphene.Argument(ContentBlockInput)
|
||||
|
||||
errors = graphene.List(graphene.String)
|
||||
content_block = graphene.Field(ContentBlockNode)
|
||||
|
|
@ -72,31 +76,36 @@ class MutateContentBlock(relay.ClientIDMutation):
|
|||
def mutate_and_get_payload(cls, *args, **kwargs):
|
||||
try:
|
||||
id_param = kwargs['id']
|
||||
type_param = kwargs.get('type')
|
||||
title = kwargs.get('title')
|
||||
contents_data = kwargs.get('contents')
|
||||
visibility_list = kwargs.get('visibility', [])
|
||||
|
||||
#new_content_block = ContentBlock(type=type_param, title=title, contents=contents_data)
|
||||
content_block_data = kwargs.get('content_block')
|
||||
# type_param = content_block_data.get('type')
|
||||
title = content_block_data.get('title', None)
|
||||
contents = content_block_data.get('contents', None)
|
||||
visibility_list = content_block_data.get('visibility', None)
|
||||
|
||||
content_block = get_object(ContentBlock, id_param)
|
||||
|
||||
for v in visibility_list:
|
||||
user_group = get_object(UserGroup, v.user_group_id)
|
||||
if v.hidden:
|
||||
content_block.hidden_for.add(user_group)
|
||||
else:
|
||||
content_block.hidden_for.remove(user_group)
|
||||
if visibility_list is not None:
|
||||
for v in visibility_list:
|
||||
user_group = get_object(UserGroup, v.user_group_id)
|
||||
if v.hidden:
|
||||
content_block.hidden_for.add(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.add_sibling(instance=new_content_block, pos='right')
|
||||
# content_block.add_sibling(instance=new_content_block, pos='right')
|
||||
|
||||
# ContentBlock.objects.get()
|
||||
# cb.add_sibling()
|
||||
|
||||
#new_content_block.saveI()
|
||||
# new_content_block.saveI()
|
||||
|
||||
# image_instance = get_object(Image, kwargs['id'])
|
||||
# if image_instance:
|
||||
|
|
|
|||
Loading…
Reference in New Issue