77 lines
2.1 KiB
Python
77 lines
2.1 KiB
Python
import graphene
|
|
import json
|
|
from graphene import relay
|
|
|
|
from api.utils import get_object
|
|
from books.models import ContentBlock
|
|
from notes.inputs import AddNoteArgument
|
|
from notes.models import ContentBlockBookmark, Note
|
|
from notes.schema import NoteNode
|
|
|
|
class UpdateContentBookmark(relay.ClientIDMutation):
|
|
class Input:
|
|
id = graphene.String(required=True)
|
|
content_block = graphene.ID(required=True)
|
|
bookmarked = graphene.Boolean(required=True)
|
|
|
|
success = graphene.Boolean()
|
|
errors = graphene.String()
|
|
|
|
@classmethod
|
|
def mutate_and_get_payload(cls, root, info, **kwargs):
|
|
id = kwargs.get('id')
|
|
user = info.context.user
|
|
content_block_id = kwargs.get('content_block')
|
|
bookmarked = kwargs.get('bookmarked')
|
|
|
|
content_block = get_object(ContentBlock, content_block_id)
|
|
|
|
if bookmarked:
|
|
ContentBlockBookmark.objects.create(
|
|
content_block=content_block,
|
|
id=id,
|
|
user=user
|
|
)
|
|
else:
|
|
ContentBlockBookmark.objects.get(
|
|
content_block=content_block,
|
|
id=id,
|
|
user=user
|
|
).delete()
|
|
|
|
return cls(success=True)
|
|
|
|
|
|
|
|
class AddNote(relay.ClientIDMutation):
|
|
class Input:
|
|
note = graphene.Argument(AddNoteArgument)
|
|
|
|
note = graphene.Field(NoteNode)
|
|
|
|
@classmethod
|
|
def mutate_and_get_payload(cls, root, info, **kwargs):
|
|
user = info.context.user
|
|
|
|
note = kwargs.get('note')
|
|
content_uuid = note.get('content')
|
|
content_block_id = note.get('content_block')
|
|
content_block = get_object(ContentBlock, content_block_id)
|
|
text = note.get('text')
|
|
|
|
bookmark = ContentBlockBookmark.objects.get(
|
|
content_block=content_block,
|
|
id=content_uuid,
|
|
user=user
|
|
)
|
|
|
|
bookmark.note = Note.objects.create(text=text)
|
|
bookmark.save()
|
|
|
|
return cls(note=bookmark.note)
|
|
|
|
|
|
class NoteMutations:
|
|
add_note = AddNote.Field()
|
|
update_content_bookmark = UpdateContentBookmark.Field()
|