From a1fd28b48e6ebfe45211e1511cd98b3782993a9d Mon Sep 17 00:00:00 2001 From: Ramon Wenger Date: Tue, 20 Nov 2018 17:28:19 +0100 Subject: [PATCH] Add objective group mutation --- .../gql/mutations/addObjectiveGroup.gql | 16 ++++++++++++ server/objectives/inputs.py | 16 ++++++++++++ server/objectives/mutations.py | 26 ++++++++++++++++++- 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 client/src/graphql/gql/mutations/addObjectiveGroup.gql create mode 100644 server/objectives/inputs.py diff --git a/client/src/graphql/gql/mutations/addObjectiveGroup.gql b/client/src/graphql/gql/mutations/addObjectiveGroup.gql new file mode 100644 index 00000000..69bf95dd --- /dev/null +++ b/client/src/graphql/gql/mutations/addObjectiveGroup.gql @@ -0,0 +1,16 @@ +#import "../fragments/objectiveGroupParts.gql" +mutation AddObjectiveGroup($input: AddObjectiveGroupInput!) { + addObjectiveGroup(input: $input) { + objectiveGroup { + ...ObjectiveGroupParts + objectives { + edges { + node { + id + text + } + } + } + } + } +} diff --git a/server/objectives/inputs.py b/server/objectives/inputs.py new file mode 100644 index 00000000..cd4a96cf --- /dev/null +++ b/server/objectives/inputs.py @@ -0,0 +1,16 @@ +import graphene +from graphene import InputObjectType + + +class ObjectiveInput(InputObjectType): + text = graphene.String(required=True) + + +class ObjectiveGroupInput(InputObjectType): + title = graphene.String(required=True) + module = graphene.ID(required=True) + objectives = graphene.List(ObjectiveInput) + + +class AddObjectiveGroupArgument(ObjectiveGroupInput): + pass diff --git a/server/objectives/mutations.py b/server/objectives/mutations.py index f88c6c10..ee27bcd6 100644 --- a/server/objectives/mutations.py +++ b/server/objectives/mutations.py @@ -1,8 +1,10 @@ import graphene from graphene import relay, InputObjectType from api.utils import get_object +from books.models import Module from books.schema.inputs import UserGroupBlockVisibility from core.utils import set_visible_for, set_hidden_for +from objectives.inputs import AddObjectiveGroupArgument from objectives.models import ObjectiveProgressStatus, Objective, ObjectiveGroup from objectives.schema import ObjectiveNode, ObjectiveGroupNode @@ -52,10 +54,32 @@ class UpdateObjectiveGroupVisibility(relay.ClientIDMutation): objective_group.save() - return cls(objective_group=objective_group) +class AddObjectiveGroup(relay.ClientIDMutation): + class Input: + objective_group = graphene.Argument(AddObjectiveGroupArgument) + + objective_group = graphene.Field(ObjectiveGroupNode) + + @classmethod + def mutate_and_get_payload(cls, root, info, **kwargs): + objective_group_data = kwargs.get('objective_group') + title = objective_group_data.get('title') + if title != 'society': + title = 'language_communication' + module_id = objective_group_data.get('module') + module = get_object(Module, module_id) + owner = info.context.user + new_objective_group = ObjectiveGroup.objects.create(title=title, module=module, owner=owner) + objectives = objective_group_data.get('objectives') + for objective in objectives: + Objective.objects.create(text=objective.get('text'), group=new_objective_group) + return cls(objective_group=new_objective_group) + + class ObjectiveMutations: update_objective_progress = UpdateObjectiveProgress.Field() update_objective_group_visibility = UpdateObjectiveGroupVisibility.Field() + add_objective_group = AddObjectiveGroup.Field()