107 lines
2.8 KiB
Python
107 lines
2.8 KiB
Python
import graphene
|
|
from graphene import relay
|
|
from graphene_django import DjangoObjectType
|
|
from graphene_django.filter import DjangoFilterConnectionField
|
|
|
|
from ..models import Book, Topic, Module, Chapter, ContentBlock
|
|
|
|
|
|
class ContentBlockNode(DjangoObjectType):
|
|
class Meta:
|
|
model = ContentBlock
|
|
only_fields = [
|
|
'slug', 'title', 'type', 'contents', 'hidden_for'
|
|
]
|
|
filter_fields = [
|
|
'slug', 'title',
|
|
]
|
|
interfaces = (relay.Node,)
|
|
|
|
|
|
class ChapterNode(DjangoObjectType):
|
|
content_blocks = DjangoFilterConnectionField(ContentBlockNode)
|
|
|
|
class Meta:
|
|
model = Chapter
|
|
only_fields = [
|
|
'slug', 'title', 'description',
|
|
]
|
|
filter_fields = [
|
|
'slug', 'title',
|
|
]
|
|
interfaces = (relay.Node,)
|
|
|
|
def resolve_content_blocks(self, *args, **kwargs):
|
|
return ContentBlock.get_by_parent(self)
|
|
|
|
|
|
class ModuleNode(DjangoObjectType):
|
|
pk = graphene.Int()
|
|
chapters = DjangoFilterConnectionField(ChapterNode)
|
|
hero_image = graphene.String()
|
|
|
|
class Meta:
|
|
model = Module
|
|
only_fields = [
|
|
'slug', 'title', 'meta_title', 'teaser', 'intro', 'objective_groups', 'assignments'
|
|
]
|
|
filter_fields = {
|
|
'slug': ['exact', 'icontains', 'in'],
|
|
'title': ['exact', 'icontains', 'in'],
|
|
}
|
|
interfaces = (relay.Node,)
|
|
|
|
def resolve_pk(self, info, **kwargs):
|
|
return self.id
|
|
|
|
def resolve_hero_image(self, info, **kwargs):
|
|
if self.hero_image:
|
|
return self.hero_image.file.url
|
|
|
|
def resolve_chapters(self, info, **kwargs):
|
|
return Chapter.get_by_parent(self)
|
|
|
|
|
|
class TopicNode(DjangoObjectType):
|
|
pk = graphene.Int()
|
|
modules = DjangoFilterConnectionField(ModuleNode)
|
|
|
|
class Meta:
|
|
model = Topic
|
|
only_fields = [
|
|
'slug', 'title', 'meta_title', 'teaser', 'description',
|
|
]
|
|
filter_fields = {
|
|
'slug': ['exact', 'icontains', 'in'],
|
|
'title': ['exact', 'icontains', 'in'],
|
|
}
|
|
interfaces = (relay.Node,)
|
|
|
|
def resolve_pk(self, *args, **kwargs):
|
|
return self.id
|
|
|
|
def resolve_modules(self, *args, **kwargs):
|
|
return Module.get_by_parent(self)
|
|
|
|
|
|
class BookNode(DjangoObjectType):
|
|
pk = graphene.Int()
|
|
topics = DjangoFilterConnectionField(TopicNode)
|
|
|
|
class Meta:
|
|
model = Book
|
|
only_fields = [
|
|
'slug', 'title',
|
|
]
|
|
filter_fields = {
|
|
'slug': ['exact', 'icontains', 'in'],
|
|
'title': ['exact', 'icontains', 'in'],
|
|
}
|
|
interfaces = (relay.Node,)
|
|
|
|
def resolve_pk(self, *args, **kwargs):
|
|
return self.id
|
|
|
|
def resolve_topics(self, *args, **kwargs):
|
|
return Topic.get_by_parent(self)
|