Hide hidden custom entities in snapshot

This commit is contained in:
Ramon Wenger 2021-05-18 22:38:47 +02:00
parent ef15a655b8
commit e1e3b62807
5 changed files with 53 additions and 26 deletions

View File

@ -97,6 +97,13 @@ class ContentBlock(StrictHierarchyPage):
def module(self):
return self.get_parent().get_parent().specific
def is_hidden_for_class(self, school_class):
return (
not self.user_created and self.hidden_for.filter(id=school_class.id).exists()
) or (
self.user_created and not self.visible_for.filter(id=school_class.id).exists()
)
def save(self, *args, **kwargs):
for data in self.contents.stream_data:
block_type, value = get_type_and_value(data)

View File

@ -55,7 +55,7 @@ class SnapshotManager(models.Manager):
snapshot.hidden_content_blocks.add(content_block)
for content_block in base_qs.filter(Q(user_created=True) & Q(owner=user)):
new_content_block = ContentBlockSnapshot(
hidden=False, # todo
hidden=content_block.is_hidden_for_class(school_class),
snapshot=snapshot,
contents=content_block.contents,
type=content_block.type,
@ -78,7 +78,7 @@ class SnapshotManager(models.Manager):
snapshot.hidden_objectives.add(objective)
for objective in base_qs.filter(owner=user):
ObjectiveSnapshot.objects.create(
hidden=False, # todo
hidden=objective.is_hidden_for_class(school_class=school_class),
snapshot=snapshot,
text=objective.text,
group=objective_group,

View File

@ -16,7 +16,9 @@ class SnapshotContentBlock:
self.title = content_block.title
self.contents = content_block.contents
self.type = content_block.type
self.hidden = snapshot.hidden_content_blocks.filter(id=content_block.id).exists()
self.hidden = snapshot.hidden_content_blocks.filter(id=content_block.id).exists() or (
hasattr(content_block, 'contentblocksnapshot') and content_block.contentblocksnapshot.hidden
)
class SnapshotChapter:
@ -46,7 +48,9 @@ class SnapshotObjective:
def __init__(self, objective, snapshot):
self.id = objective.id
self.text = objective.text
self.hidden = snapshot.hidden_objectives.filter(id=objective.id).exists()
self.hidden = snapshot.hidden_objectives.filter(id=objective.id).exists() or (
hasattr(objective, 'objectivesnapshot') and objective.objectivesnapshot.hidden
)
class SnapshotObjectiveGroup:

View File

@ -143,6 +143,10 @@ class CreateSnapshotTestCase(SkillboxTestCase):
self.assertEqual(objective['text'], text)
self.assertEqual(objective['hidden'], hidden)
def _test_content_block(self, content_block, title, hidden):
self.assertEqual(content_block['title'], title)
self.assertEqual(content_block['hidden'], hidden)
def setUp(self):
self.createDefault()
self.client = self.get_client()
@ -160,6 +164,7 @@ class CreateSnapshotTestCase(SkillboxTestCase):
self.title_visible = 'visible'
self.title_hidden = 'hidden'
self.title_custom = 'custom'
self.title_custom_hidden = 'custom-hidden'
self.visible_content_block = ContentBlockFactory(parent=self.chapter, module=self.module,
title=self.title_visible, slug='cb-a')
self.hidden_content_block = ContentBlockFactory(parent=self.chapter, module=self.module,
@ -168,6 +173,10 @@ class CreateSnapshotTestCase(SkillboxTestCase):
self.custom_content_block = ContentBlockFactory(parent=self.chapter, owner=self.teacher, user_created=True,
module=self.module, title=self.title_custom,
slug='cb-c')
# content block d is user created, but hidden
self.custom_hidden_content_block = ContentBlockFactory(parent=self.chapter, owner=self.teacher, user_created=True,
module=self.module, title=self.title_custom_hidden,
slug='cb-d')
# content block a and c are visible to school class X
self.hidden_content_block.hidden_for.add(self.skillbox_class)
self.custom_content_block.visible_for.add(self.skillbox_class)
@ -184,6 +193,8 @@ class CreateSnapshotTestCase(SkillboxTestCase):
self.visible_objective = ObjectiveFactory(text='visible-objective', group=objective_group)
self.hidden_objective = ObjectiveFactory(text='hidden-objective', group=objective_group)
self.custom_objective = ObjectiveFactory(text='custom-objective', group=objective_group, owner=self.teacher)
self.custom_hidden_objective = ObjectiveFactory(text='custom-hidden-objective', group=objective_group,
owner=self.teacher)
self.hidden_objective.hidden_for.add(self.skillbox_class)
self.custom_objective.visible_for.add(self.skillbox_class)
@ -217,7 +228,7 @@ class CreateSnapshotTestCase(SkillboxTestCase):
objectives = module['objectiveGroups'][0]['objectives']
self.assertEqual(len(objectives), 3)
self.assertEqual(len(objectives), 4)
hidden_objective = [objective for objective in objectives if
objective['text'] == self.hidden_objective.text][0]
@ -257,32 +268,30 @@ class CreateSnapshotTestCase(SkillboxTestCase):
self.assertEqual(int(chapter_id), self.chapter.id)
content_blocks = chapter['contentBlocks']
self.assertEqual(len(content_blocks), 3)
visible, hidden, custom = content_blocks
self.assertEqual(visible['title'], self.title_visible)
self.assertEqual(visible['hidden'], False)
self.assertEqual(hidden['title'], self.title_hidden)
self.assertEqual(hidden['hidden'], True)
self.assertEqual(custom['title'], self.title_custom)
self.assertEqual(custom['hidden'], False)
self.assertEqual(len(content_blocks), 4)
visible, hidden, custom, custom_hidden = content_blocks
self._test_content_block(visible, self.title_visible, False)
self._test_content_block(hidden, self.title_hidden, True)
self._test_content_block(custom, self.title_custom, False)
self._test_content_block(custom_hidden, self.title_custom_hidden, True)
self.assertEqual(ChapterSnapshot.objects.count(), 2)
visible, hidden, custom = snapshot['objectiveGroups'][0]['objectives']
visible, hidden, custom, custom_hidden = snapshot['objectiveGroups'][0]['objectives']
self._test_objective(objective=visible, text=self.visible_objective.text, hidden=False)
self._test_objective(objective=hidden, text=self.hidden_objective.text, hidden=True)
self._test_objective(objective=custom, text=self.custom_objective.text, hidden=False)
self._test_objective(objective=custom_hidden, text=self.custom_hidden_objective.text, hidden=True)
id = snapshot['id']
snapshot = get_object(Snapshot, id)
self.assertEqual(snapshot.objective_groups.count(), 2)
def test_apply_snapshot(self):
self.snapshot = Snapshot.objects.create_snapshot(module=self.module, school_class=self.skillbox_class,
user=self.teacher)
self.assertEqual(Snapshot.objects.count(), 1)
self.assertEqual(self.snapshot.custom_objectives.count(), 1)
self.assertEqual(self.snapshot.custom_objectives.count(), 2)
school_class_name = 'second_class'
second_class = SchoolClass.objects.get(name=school_class_name)
request = RequestFactory().get('/')
@ -311,25 +320,25 @@ class CreateSnapshotTestCase(SkillboxTestCase):
self.assertEqual(len(chapters), 2)
chapter = chapters[0]
content_blocks = chapter.get('contentBlocks')
self.assertEqual(len(content_blocks), 3)
first, second, third = content_blocks
self.assertEqual(first['title'], 'visible')
self.assertEqual(second['title'], 'hidden')
self.assertEqual(second['hidden'], True)
self.assertEqual(third['title'], 'custom')
self.assertEqual(len(content_blocks), 4)
first, second, third, fourth = content_blocks
self._test_content_block(first, self.title_visible, False)
self._test_content_block(second, self.title_hidden, True)
self._test_content_block(third, self.title_custom, False)
self._test_content_block(fourth, self.title_custom_hidden, True)
objective_groups = snapshot['objectiveGroups']
self.assertEqual(len(objective_groups), 2)
objective_group1, objective_group2 = objective_groups
objective1, objective2, objective3 = objective_group1['objectives']
objective1, objective2, objective3, objective4 = objective_group1['objectives']
self._test_objective(objective1, self.visible_objective.text, False)
self._test_objective(objective2, self.hidden_objective.text, True)
self._test_objective(objective3, self.custom_objective.text, False)
self._test_objective(objective4, self.custom_hidden_objective.text, True)
self.assertEqual(objective_group2['hidden'], True)
def test_apply_initial_snapshot(self):
teacher2 = User.objects.get(username='teacher2')
teacher2_client = self.get_client(user=teacher2)
@ -362,10 +371,11 @@ class CreateSnapshotTestCase(SkillboxTestCase):
self.assertIsNone(result.get('errors'))
module = result['data']['module']
chapter1, chapter2 = module['chapters']
cb1, cb2, cb3 = chapter1['contentBlocks']
cb1, cb2, cb3, cb4 = chapter1['contentBlocks']
self.assertTrue(self.skillbox_class.name not in [sc['name'] for sc in cb1['hiddenFor']])
self.assertTrue(self.skillbox_class.name not in [sc['name'] for sc in cb2['hiddenFor']])
self.assertTrue(self.skillbox_class.name not in [sc['name'] for sc in cb3['visibleFor']])
self.assertTrue(self.skillbox_class.name not in [sc['name'] for sc in cb4['visibleFor']])
class SnapshotTestCase(SkillboxTestCase):

View File

@ -1,6 +1,5 @@
from django.contrib.auth import get_user_model
from django.db import models
from django.db.models import F
from books.models import Module
from core.utils import sync_visible_for, sync_hidden_for
@ -66,6 +65,13 @@ class Objective(models.Model):
sync_hidden_for(self, school_class_template, school_class_to_sync)
sync_visible_for(self, school_class_template, school_class_to_sync)
def is_hidden_for_class(self, school_class):
return (
self.owner is None and self.hidden_for.filter(id=school_class.id).exists()
) or (
self.owner is not None and not self.visible_for.filter(id=school_class.id).exists()
)
class ObjectiveSnapshot(Objective):
hidden = models.BooleanField(default=False)