From 5202ecdc8e10cef54a66ae716cbf5149b55c7372 Mon Sep 17 00:00:00 2001 From: Ramon Wenger Date: Tue, 29 Jun 2021 16:43:07 +0200 Subject: [PATCH] Fix chapter and content block querysets for snapshots --- server/books/models/snapshot.py | 4 ++-- server/books/schema/nodes/snapshot.py | 4 ++-- server/books/tests/test_snapshots.py | 19 ++++++++++--------- server/users/services.py | 3 +++ 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/server/books/models/snapshot.py b/server/books/models/snapshot.py index c0a1c72e..27404be4 100644 --- a/server/books/models/snapshot.py +++ b/server/books/models/snapshot.py @@ -127,14 +127,14 @@ class Snapshot(models.Model): def reset(self, user, selected_class): for chapter in Chapter.get_by_parent(self.module): qs = ContentBlock.get_by_parent(chapter) - without_owner = Q(owner__isnull=True) + default_content = Q(user_created=False) no_snapshot = Q(contentblocksnapshot__isnull=True) owner_user = Q(owner=user) logger.info(f'resetting hidden properties for chapter {chapter.id}') chapter.title_hidden_for.remove(selected_class) chapter.description_hidden_for.remove(selected_class) - for content_block in qs.filter(without_owner & no_snapshot): + for content_block in qs.filter(default_content & no_snapshot): content_block.hidden_for.remove(selected_class) for content_block in qs.filter(owner_user): content_block.visible_for.remove(selected_class) diff --git a/server/books/schema/nodes/snapshot.py b/server/books/schema/nodes/snapshot.py index dce9abfe..3aac2ecf 100644 --- a/server/books/schema/nodes/snapshot.py +++ b/server/books/schema/nodes/snapshot.py @@ -31,14 +31,14 @@ class SnapshotChapter: self.description_hidden = description_hidden self.content_blocks = [] base_qs = ContentBlock.get_by_parent(chapter) - without_owner = Q(Q(owner__isnull=True) & Q(contentblocksnapshot__snapshot__isnull=True)) + default_content = Q(Q(user_created=False) & Q(contentblocksnapshot__snapshot__isnull=True)) this_snapshot = Q(contentblocksnapshot__snapshot=snapshot) self.content_blocks = [ SnapshotContentBlock( content_block=content_block, snapshot=snapshot ) for content_block in - base_qs.filter(without_owner | this_snapshot) + base_qs.filter(default_content | this_snapshot) ] # all from module without owner # all with snapshotcontentblock with this snapshot diff --git a/server/books/tests/test_snapshots.py b/server/books/tests/test_snapshots.py index f30f2b0a..0813448b 100644 --- a/server/books/tests/test_snapshots.py +++ b/server/books/tests/test_snapshots.py @@ -38,10 +38,11 @@ class CreateSnapshotTestCase(SkillboxTestCase): self.teacher2 = User.objects.get(username='teacher2') self.second_class_name = 'second_class' self.second_class = SchoolClass.objects.get(name=self.second_class_name) + self.admin = User.objects.get(username='admin') # module M has a chapter - self.chapter = ChapterFactory(parent=self.module, slug='some-chapter') - ChapterFactory(parent=self.module, slug='some-other-chapter') + self.chapter = ChapterFactory(parent=self.module, slug='some-chapter', owner=self.admin) + ChapterFactory(parent=self.module, slug='some-other-chapter', owner=self.admin) # chapter has some content blocks a, b, c self.title_visible = 'visible' @@ -49,9 +50,9 @@ class CreateSnapshotTestCase(SkillboxTestCase): 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') + title=self.title_visible, slug='cb-a', owner=self.admin) self.hidden_content_block = ContentBlockFactory(parent=self.chapter, module=self.module, - title=self.title_hidden, slug='cb-b') + title=self.title_hidden, slug='cb-b', owner=self.admin) # content block c is user created self.custom_content_block = ContentBlockFactory(parent=self.chapter, owner=self.teacher, user_created=True, module=self.module, title=self.title_custom, @@ -275,11 +276,11 @@ class CreateSnapshotTestCase(SkillboxTestCase): self.assertIsNone(result.get('errors')) module = result['data']['module'] chapter1, chapter2 = module['chapters'] - 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']]) + visible, hidden, custom, custom_hidden = chapter1['contentBlocks'] + self.assertTrue(self.skillbox_class.name not in [sc['name'] for sc in visible['hiddenFor']]) + self.assertTrue(self.skillbox_class.name not in [sc['name'] for sc in hidden['hiddenFor']]) + self.assertTrue(self.skillbox_class.name not in [sc['name'] for sc in custom['visibleFor']]) + self.assertTrue(self.skillbox_class.name not in [sc['name'] for sc in custom_hidden['visibleFor']]) def test_create_apply_view_snapshot(self): result = self.graphene_client.execute(MODULE_QUERY, variables={ diff --git a/server/users/services.py b/server/users/services.py index 7a64a65f..56a0b257 100644 --- a/server/users/services.py +++ b/server/users/services.py @@ -22,6 +22,9 @@ def create_users(data=None): teacher_role = Role.objects.get_default_teacher_role() if data is None: + #make an admin account + UserFactory(username='admin', is_superuser=True, is_staff=True) + teacher = UserFactory(username='teacher') UserRole.objects.create(user=teacher, role=teacher_role)