Add mutations for objective group visibility
This commit is contained in:
parent
a437620f13
commit
53f50bdc13
|
|
@ -29,7 +29,7 @@ class ContentElementInput(InputObjectType):
|
||||||
value = ContentElementValueInput()
|
value = ContentElementValueInput()
|
||||||
|
|
||||||
|
|
||||||
class UserGroupContentBlockVisibility(InputObjectType):
|
class UserGroupBlockVisibility(InputObjectType):
|
||||||
school_class_id = graphene.ID(required=True)
|
school_class_id = graphene.ID(required=True)
|
||||||
hidden = graphene.Boolean(required=True)
|
hidden = graphene.Boolean(required=True)
|
||||||
|
|
||||||
|
|
@ -38,4 +38,4 @@ class ContentBlockInput(InputObjectType):
|
||||||
title = graphene.String()
|
title = graphene.String()
|
||||||
type = graphene.String()
|
type = graphene.String()
|
||||||
contents = graphene.List(ContentElementInput)
|
contents = graphene.List(ContentElementInput)
|
||||||
visibility = graphene.List(UserGroupContentBlockVisibility)
|
visibility = graphene.List(UserGroupBlockVisibility)
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ from api.utils import get_object, get_errors
|
||||||
from books.models import ContentBlock, Chapter, SchoolClass
|
from books.models import ContentBlock, Chapter, SchoolClass
|
||||||
from books.schema.inputs import ContentBlockInput
|
from books.schema.inputs import ContentBlockInput
|
||||||
from books.schema.queries import ContentBlockNode
|
from books.schema.queries import ContentBlockNode
|
||||||
|
from core.utils import set_hidden_for, set_visible_for
|
||||||
from .utils import handle_content_block, set_user_defined_block_type
|
from .utils import handle_content_block, set_user_defined_block_type
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -37,9 +38,9 @@ class MutateContentBlock(relay.ClientIDMutation):
|
||||||
|
|
||||||
if visibility_list is not None:
|
if visibility_list is not None:
|
||||||
if content_block.user_created:
|
if content_block.user_created:
|
||||||
cls.set_visible_for(content_block, visibility_list)
|
set_visible_for(content_block, visibility_list)
|
||||||
else:
|
else:
|
||||||
cls.set_hidden_for(content_block, visibility_list)
|
set_hidden_for(content_block, visibility_list)
|
||||||
|
|
||||||
if title is not None:
|
if title is not None:
|
||||||
content_block.title = title
|
content_block.title = title
|
||||||
|
|
@ -59,24 +60,6 @@ class MutateContentBlock(relay.ClientIDMutation):
|
||||||
|
|
||||||
return cls(content_block=None, errors=errors)
|
return cls(content_block=None, errors=errors)
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def set_hidden_for(cls, content_block, visibility_list):
|
|
||||||
for v in visibility_list:
|
|
||||||
school_class = get_object(SchoolClass, v.school_class_id)
|
|
||||||
if v.hidden:
|
|
||||||
content_block.hidden_for.add(school_class)
|
|
||||||
else:
|
|
||||||
content_block.hidden_for.remove(school_class)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def set_visible_for(cls, content_block, visibility_list):
|
|
||||||
for v in visibility_list:
|
|
||||||
school_class = get_object(SchoolClass, v.school_class_id)
|
|
||||||
if v.hidden:
|
|
||||||
content_block.visible_for.remove(school_class)
|
|
||||||
else:
|
|
||||||
content_block.visible_for.add(school_class)
|
|
||||||
|
|
||||||
|
|
||||||
class AddContentBlock(relay.ClientIDMutation):
|
class AddContentBlock(relay.ClientIDMutation):
|
||||||
class Input:
|
class Input:
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
from api.utils import get_object
|
||||||
|
from users.models import SchoolClass
|
||||||
|
|
||||||
|
|
||||||
|
def set_hidden_for(block, visibility_list):
|
||||||
|
for v in visibility_list:
|
||||||
|
school_class = get_object(SchoolClass, v.school_class_id)
|
||||||
|
if v.hidden:
|
||||||
|
block.hidden_for.add(school_class)
|
||||||
|
else:
|
||||||
|
block.hidden_for.remove(school_class)
|
||||||
|
|
||||||
|
|
||||||
|
def set_visible_for(block, visibility_list):
|
||||||
|
for v in visibility_list:
|
||||||
|
school_class = get_object(SchoolClass, v.school_class_id)
|
||||||
|
if v.hidden:
|
||||||
|
block.visible_for.remove(school_class)
|
||||||
|
else:
|
||||||
|
block.visible_for.add(school_class)
|
||||||
|
|
@ -5,8 +5,8 @@ from objectives.models import ObjectiveGroup, Objective, ObjectiveProgressStatus
|
||||||
|
|
||||||
@admin.register(ObjectiveGroup)
|
@admin.register(ObjectiveGroup)
|
||||||
class ObjectiveGroupAdmin(admin.ModelAdmin):
|
class ObjectiveGroupAdmin(admin.ModelAdmin):
|
||||||
list_display = ('title', 'module', 'user')
|
list_display = ('title', 'module', 'owner')
|
||||||
list_filter = ('title', 'module', 'user')
|
list_filter = ('title', 'module', 'owner')
|
||||||
|
|
||||||
|
|
||||||
@admin.register(Objective)
|
@admin.register(Objective)
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
# Generated by Django 2.0.6 on 2018-10-31 13:23
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('users', '0002_auto_20181017_1340'),
|
||||||
|
('objectives', '0005_migrate_titles'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.RenameField(
|
||||||
|
model_name='objectivegroup',
|
||||||
|
old_name='user',
|
||||||
|
new_name='owner',
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='objectivegroup',
|
||||||
|
name='hidden_for',
|
||||||
|
field=models.ManyToManyField(related_name='hidden_objective_groups', to='users.SchoolClass'),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='objectivegroup',
|
||||||
|
name='visible_for',
|
||||||
|
field=models.ManyToManyField(related_name='visible_objective_groups', to='users.SchoolClass'),
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
@ -0,0 +1,23 @@
|
||||||
|
# Generated by Django 2.0.6 on 2018-10-31 13:47
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('objectives', '0006_auto_20181031_1323'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='objectivegroup',
|
||||||
|
name='hidden_for',
|
||||||
|
field=models.ManyToManyField(blank=True, related_name='hidden_objective_groups', to='users.SchoolClass'),
|
||||||
|
),
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='objectivegroup',
|
||||||
|
name='visible_for',
|
||||||
|
field=models.ManyToManyField(blank=True, related_name='visible_objective_groups', to='users.SchoolClass'),
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
@ -2,6 +2,7 @@ from django.contrib.auth import get_user_model
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
from books.models import Module
|
from books.models import Module
|
||||||
|
from users.models import SchoolClass
|
||||||
|
|
||||||
|
|
||||||
class ObjectiveGroup(models.Model):
|
class ObjectiveGroup(models.Model):
|
||||||
|
|
@ -20,7 +21,10 @@ class ObjectiveGroup(models.Model):
|
||||||
title = models.CharField('title', blank=True, null=False, max_length=255, choices=TITLE_CHOICES, default=LANGUAGE_COMMUNICATION)
|
title = models.CharField('title', blank=True, null=False, max_length=255, choices=TITLE_CHOICES, default=LANGUAGE_COMMUNICATION)
|
||||||
module = models.ForeignKey(Module, blank=False, null=False, on_delete=models.CASCADE, related_name='objective_groups')
|
module = models.ForeignKey(Module, blank=False, null=False, on_delete=models.CASCADE, related_name='objective_groups')
|
||||||
# a user can define her own objectives, hence this optional param
|
# a user can define her own objectives, hence this optional param
|
||||||
user = models.ForeignKey(get_user_model(), blank=True, null=True, on_delete=models.CASCADE)
|
owner = models.ForeignKey(get_user_model(), blank=True, null=True, on_delete=models.CASCADE)
|
||||||
|
|
||||||
|
hidden_for = models.ManyToManyField(SchoolClass, related_name='hidden_objective_groups', blank=True)
|
||||||
|
visible_for = models.ManyToManyField(SchoolClass, related_name='visible_objective_groups', blank=True)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return 'ObjectiveGroup {}-{}-{}'.format(self.id, self.module, self.title)
|
return 'ObjectiveGroup {}-{}-{}'.format(self.id, self.module, self.title)
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,10 @@
|
||||||
import graphene
|
import graphene
|
||||||
from graphene import relay
|
from graphene import relay, InputObjectType
|
||||||
from api.utils import get_object
|
from api.utils import get_object
|
||||||
from objectives.models import ObjectiveProgressStatus, Objective
|
from books.schema.inputs import UserGroupBlockVisibility
|
||||||
from objectives.schema import ObjectiveNode
|
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 UpdateObjectiveProgress(relay.ClientIDMutation):
|
||||||
|
|
@ -29,5 +31,31 @@ class UpdateObjectiveProgress(relay.ClientIDMutation):
|
||||||
return cls(objective=objective)
|
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:
|
class ObjectiveMutations:
|
||||||
update_objective_progress = UpdateObjectiveProgress.Field()
|
update_objective_progress = UpdateObjectiveProgress.Field()
|
||||||
|
update_objective_group_visibility = UpdateObjectiveGroupVisibility.Field()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue