181 lines
5.1 KiB
Python
181 lines
5.1 KiB
Python
from builtins import PermissionError
|
|
|
|
import graphene
|
|
import json
|
|
from graphene import relay
|
|
from graphql_relay import from_global_id
|
|
|
|
from api.utils import get_object
|
|
from books.models import ContentBlock, Chapter, Module
|
|
from notes.inputs import AddNoteArgument, UpdateNoteArgument
|
|
from notes.models import ContentBlockBookmark, Note, ChapterBookmark, ModuleBookmark
|
|
from notes.schema import NoteNode
|
|
|
|
|
|
class UpdateContentBookmark(relay.ClientIDMutation):
|
|
class Input:
|
|
uuid = graphene.UUID(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):
|
|
uuid = kwargs.get('uuid')
|
|
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,
|
|
uuid=uuid,
|
|
user=user
|
|
)
|
|
else:
|
|
ContentBlockBookmark.objects.get(
|
|
content_block=content_block,
|
|
uuid=uuid,
|
|
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', '')
|
|
parent = note.get('parent')
|
|
text = note.get('text')
|
|
|
|
if content_uuid != '':
|
|
content_block = get_object(ContentBlock, content_block_id)
|
|
|
|
bookmark = ContentBlockBookmark.objects.get(
|
|
content_block=content_block,
|
|
uuid=content_uuid,
|
|
user=user
|
|
)
|
|
else:
|
|
type, id = from_global_id(parent)
|
|
if type == 'ModuleNode':
|
|
bookmark = ModuleBookmark.objects.get(
|
|
module__id=id,
|
|
user=user
|
|
)
|
|
else:
|
|
bookmark = ChapterBookmark.objects.get(
|
|
chapter__id=id,
|
|
user=user
|
|
)
|
|
|
|
bookmark.note = Note.objects.create(text=text)
|
|
bookmark.save()
|
|
|
|
return cls(note=bookmark.note)
|
|
|
|
|
|
class UpdateNote(relay.ClientIDMutation):
|
|
class Input:
|
|
note = graphene.Argument(UpdateNoteArgument)
|
|
|
|
note = graphene.Field(NoteNode)
|
|
|
|
@classmethod
|
|
def mutate_and_get_payload(cls, root, info, **kwargs):
|
|
user = info.context.user
|
|
|
|
note = kwargs.get('note')
|
|
id = note.get('id')
|
|
text = note.get('text')
|
|
note = get_object(Note, id)
|
|
|
|
if hasattr(note, 'contentblockbookmark') and note.contentblockbookmark.user != user \
|
|
or hasattr(note, 'chapterbookmark') and note.chapterbookmark.user != user \
|
|
or hasattr(note, 'modulebookmark') and note.modulebookmark.user != user:
|
|
raise PermissionError
|
|
|
|
note.text = text
|
|
note.save()
|
|
return cls(note=note)
|
|
|
|
|
|
class UpdateChapterBookmark(relay.ClientIDMutation):
|
|
class Input:
|
|
chapter = graphene.ID(required=True)
|
|
bookmarked = graphene.Boolean(required=True)
|
|
|
|
success = graphene.Boolean()
|
|
|
|
@classmethod
|
|
def mutate_and_get_payload(cls, root, info, **kwargs):
|
|
user = info.context.user
|
|
chapter_id = kwargs.get('chapter')
|
|
bookmarked = kwargs.get('bookmarked')
|
|
|
|
chapter = get_object(Chapter, chapter_id)
|
|
|
|
if bookmarked:
|
|
ChapterBookmark.objects.create(
|
|
chapter=chapter,
|
|
user=user
|
|
)
|
|
else:
|
|
ChapterBookmark.objects.get(
|
|
chapter=chapter,
|
|
user=user
|
|
).delete()
|
|
|
|
return cls(success=True)
|
|
|
|
|
|
class UpdateModuleBookmark(relay.ClientIDMutation):
|
|
class Input:
|
|
module = graphene.ID(required=True)
|
|
bookmarked = graphene.Boolean(required=True)
|
|
|
|
success = graphene.Boolean()
|
|
|
|
@classmethod
|
|
def mutate_and_get_payload(cls, root, info, **kwargs):
|
|
user = info.context.user
|
|
module_id = kwargs.get('module')
|
|
bookmarked = kwargs.get('bookmarked')
|
|
|
|
module = get_object(Module, module_id)
|
|
|
|
if bookmarked:
|
|
ModuleBookmark.objects.create(
|
|
module=module,
|
|
user=user
|
|
)
|
|
else:
|
|
ModuleBookmark.objects.get(
|
|
module=module,
|
|
user=user
|
|
).delete()
|
|
|
|
return cls(success=True)
|
|
|
|
|
|
class NoteMutations:
|
|
add_note = AddNote.Field()
|
|
update_note = UpdateNote.Field()
|
|
update_content_bookmark = UpdateContentBookmark.Field()
|
|
update_chapter_bookmark = UpdateChapterBookmark.Field()
|
|
update_module_bookmark = UpdateModuleBookmark.Field()
|