skillbox/server/books/schema/mutations/chapter.py

44 lines
1.4 KiB
Python

import graphene
from graphene import relay
from api.utils import get_object
from books.models import Chapter
from books.schema.inputs import UserGroupBlockVisibility
from books.schema.queries import ChapterNode
from users.models import SchoolClass
class UpdateChapterVisibility(relay.ClientIDMutation):
class Input:
id = graphene.ID(required=True)
visibility = graphene.List(UserGroupBlockVisibility)
type = graphene.String(required=True)
chapter = graphene.Field(ChapterNode)
@classmethod
def mutate_and_get_payload(cls, root, info, **kwargs):
id_param = kwargs['id']
visibility_list = kwargs.get('visibility', None)
mutation_type = kwargs['type']
chapter = get_object(Chapter, id_param)
if visibility_list is not None:
for v in visibility_list:
school_class = get_object(SchoolClass, v.school_class_id)
if v.hidden:
if mutation_type == 'chapter-title':
chapter.title_hidden_for.add(school_class)
else:
chapter.description_hidden_for.add(school_class)
else:
if mutation_type == 'chapter-title':
chapter.title_hidden_for.remove(school_class)
else:
chapter.description_hidden_for.remove(school_class)
chapter.save()
return cls(chapter=chapter)