skillbox/server/objectives/mutations.py

62 lines
2.2 KiB
Python

import graphene
from graphene import relay, InputObjectType
from api.utils import get_object
from books.schema.inputs import UserGroupBlockVisibility
from core.utils import set_visible_for, set_hidden_for
from objectives.models import ObjectiveProgressStatus, Objective, ObjectiveGroup
from objectives.schema import ObjectiveNode, ObjectiveGroupNode
class UpdateObjectiveProgress(relay.ClientIDMutation):
class Input:
id = graphene.ID(required=True, description="The ID of the objective")
done = graphene.Boolean(required=True)
errors = graphene.List(graphene.String)
objective = graphene.Field(ObjectiveNode)
@classmethod
def mutate_and_get_payload(cls, root, info, **kwargs):
objective_id = kwargs.get('id')
done = kwargs.get('done')
objective = get_object(Objective, objective_id) # info.context.user = django user
objective_progress, created = ObjectiveProgressStatus.objects.get_or_create(
objective=objective,
user=info.context.user
)
objective_progress.done = done
objective_progress.save()
return cls(objective=objective)
class UpdateObjectiveGroupVisibility(relay.ClientIDMutation):
class Input:
id = graphene.ID(required=True, description='The ID of the objective group')
visibility = graphene.List(UserGroupBlockVisibility)
objective_group = graphene.Field(ObjectiveGroupNode)
@classmethod
def mutate_and_get_payload(cls, root, info, **kwargs):
objective_group_id = kwargs.get('id')
visibility_list = kwargs.get('visibility')
objective_group = get_object(ObjectiveGroup, objective_group_id) # info.context.user = django user
if visibility_list is not None:
if objective_group.owner is not None:
set_visible_for(objective_group, visibility_list)
else:
set_hidden_for(objective_group, visibility_list)
objective_group.save()
return cls(objective_group=objective_group)
class ObjectiveMutations:
update_objective_progress = UpdateObjectiveProgress.Field()
update_objective_group_visibility = UpdateObjectiveGroupVisibility.Field()