skillbox/server/notes/schema.py

188 lines
5.1 KiB
Python

import json
import uuid
import graphene
from basicknowledge.models import BasicKnowledge
from basicknowledge.queries import InstrumentNode
from books.schema.nodes import ContentBlockNode, ModuleNode
from books.schema.nodes.chapter import ChapterNode
from core.logger import get_logger
from graphene import relay
from graphene_django import DjangoObjectType
from notes.models import (
ChapterBookmark,
ContentBlockBookmark,
Highlight,
InstrumentBookmark,
ModuleBookmark,
Note,
)
logger = get_logger(__name__)
content_dict = {
'assignment': 'Auftrag',
'basic_knowledge': 'dsafasdfasdf',
'survey': 'Übung',
'image_block': 'Bild',
'link_block': 'Link',
'solution': 'Lösung',
'video_block': 'Video',
'document_block': 'Dokument',
'infogram_block': 'Infografik',
'genially_block': 'Infografik',
'thinglink_block': 'Infografik',
'subtitle': 'Titel',
'instruction': 'Anweisung',
'cms_document_block': 'Dokument',
}
def find_content(content_list, bookmark):
found = (content for content in content_list if uuid.UUID(content['id']) == bookmark.uuid)
content = next(found, None)
if content is None:
for c in content_list:
if c.get('id') == bookmark.uuid:
return c.get('value').get('text', '')
return c.get('value').get('text', '')
return ''
if content['type'] in ['text_block', 'subtitle', 'solution']:
return content['value'].get('text', '')
if content['type'] in ['basic_knowledge']:
description = content['value'].get('description', '')
if not description:
try:
return BasicKnowledge.objects.get(pk=content['value']['basic_knowledge']).title
except BasicKnowledge.DoesNotExist:
return ''
else:
return description
print(bookmark.uuid)
print(json.dumps(content_dict, indent=4))
return content_dict.get(content['type'], '')
class NoteNode(DjangoObjectType):
pk = graphene.Int()
class Meta:
model = Note
fields = "__all__"
interfaces = (relay.Node,)
def resolve_pk(self, *args, **kwargs):
return self.id
class ContentBlockBookmarkNode(DjangoObjectType):
uuid = graphene.UUID()
note = graphene.Field(NoteNode)
path = graphene.String()
content = graphene.String()
class Meta:
model = ContentBlockBookmark
fields = "__all__"
filter_fields = []
interfaces = (relay.Node,)
@staticmethod
def resolve_path(root: ContentBlockBookmark, info, **kwargs):
return root.content_block.route
@staticmethod
def resolve_content(root: ContentBlockBookmark, info, **kwargs):
contents = root.content_block.contents.raw_data
return find_content(contents, root)
class ModuleBookmarkNode(DjangoObjectType):
note = graphene.Field(NoteNode)
path = graphene.String()
content = graphene.String()
class Meta:
model = ModuleBookmark
fields = "__all__"
@staticmethod
def resolve_path(root: ModuleBookmark, info, **kwargs):
return root.module.route
@staticmethod
def resolve_content(root: ModuleBookmark, info, **kwargs):
return root.module.intro
class ChapterBookmarkNode(DjangoObjectType):
note = graphene.Field(NoteNode)
path = graphene.String()
content = graphene.String()
class Meta:
model = ChapterBookmark
fields = "__all__"
filter_fields = []
interfaces = (relay.Node,)
@staticmethod
def resolve_path(root: ChapterBookmark, info, **kwargs):
return root.chapter.route
@staticmethod
def resolve_content(root: ChapterBookmark, info, **kwargs):
return root.chapter.description
class InstrumentBookmarkNode(DjangoObjectType):
uuid = graphene.UUID()
note = graphene.Field(NoteNode)
path = graphene.String(required=True)
content = graphene.String()
class Meta:
model = InstrumentBookmark
fields = "__all__"
filter_fields = []
interfaces = (relay.Node,)
@staticmethod
def resolve_path(root: InstrumentBookmark, info, **kwargs):
return root.instrument.route
@staticmethod
def resolve_content(root: InstrumentBookmark, info, **kwargs):
contents = root.instrument.contents.raw_data
return find_content(contents, root)
class BookmarkNode(graphene.Union):
class Meta:
types = (
ContentBlockBookmarkNode,
ModuleBookmarkNode,
ChapterBookmarkNode,
InstrumentBookmarkNode,
)
class HighlightableNode(graphene.Union):
class Meta:
types = (ContentBlockNode, InstrumentNode, ModuleNode, ChapterNode)
class HighlightNode(DjangoObjectType):
page = graphene.Field(HighlightableNode, required=True)
class Meta:
model = Highlight
fields = "__all__"
filter_fields = []
interfaces = (relay.Node,)
@staticmethod
def resolve_page(root: Highlight, *args, **kwargs):
return root.page.specific