125 lines
3.3 KiB
Python
125 lines
3.3 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 ContentBlockNodeType(DjangoObjectType):
|
|
class Meta:
|
|
model = ContentBlock
|
|
only_fields = [
|
|
'slug', 'title', 'type', 'contents'
|
|
]
|
|
filter_fields = [
|
|
'slug', 'title',
|
|
]
|
|
interfaces = (relay.Node,)
|
|
|
|
|
|
class ChapterType(DjangoObjectType):
|
|
content_blocks = DjangoFilterConnectionField(ContentBlockNodeType)
|
|
|
|
class Meta:
|
|
model = Chapter
|
|
only_fields = [
|
|
'slug', 'title',
|
|
]
|
|
filter_fields = [
|
|
'slug', 'title',
|
|
]
|
|
interfaces = (relay.Node,)
|
|
|
|
def resolve_content_blocks(self, *args, **kwargs):
|
|
return ContentBlock.get_by_parent(self)
|
|
|
|
|
|
class ModuleType(DjangoObjectType):
|
|
pk = graphene.Int()
|
|
chapters = DjangoFilterConnectionField(ChapterType)
|
|
hero_image = graphene.String()
|
|
|
|
class Meta:
|
|
model = Module
|
|
only_fields = [
|
|
'slug', 'title', 'meta_title', 'teaser', 'intro', 'objective_groups'
|
|
]
|
|
filter_fields = {
|
|
'slug': ['exact', 'icontains', 'in'],
|
|
'title': ['exact', 'icontains', 'in'],
|
|
}
|
|
interfaces = (relay.Node,)
|
|
|
|
def resolve_pk(self, *args, **kwargs):
|
|
return self.id
|
|
|
|
def resolve_hero_image(self, *args, **kwargs):
|
|
if self.hero_image:
|
|
return self.hero_image.file.url
|
|
|
|
def resolve_chapters(self, *args, **kwargs):
|
|
return Chapter.get_by_parent(self)
|
|
|
|
|
|
class TopicType(DjangoObjectType):
|
|
pk = graphene.Int()
|
|
modules = DjangoFilterConnectionField(ModuleType)
|
|
|
|
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 BookType(DjangoObjectType):
|
|
pk = graphene.Int()
|
|
topics = DjangoFilterConnectionField(TopicType)
|
|
|
|
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)
|
|
|
|
|
|
class BookQuery(object):
|
|
module = relay.Node.Field(ModuleType)
|
|
|
|
books = DjangoFilterConnectionField(BookType)
|
|
topics = DjangoFilterConnectionField(TopicType)
|
|
modules = DjangoFilterConnectionField(ModuleType)
|
|
|
|
def resolve_books(self, *args, **kwargs):
|
|
return Book.objects.filter(**kwargs).live()
|
|
|
|
def resolve_topics(self, *args, **kwargs):
|
|
return Topic.objects.filter(**kwargs).live()
|
|
|
|
def resolve_modules(self, *args, **kwargs):
|
|
return Module.objects.filter(**kwargs).live()
|
|
|