Fix chapter and content block querysets for snapshots

This commit is contained in:
Ramon Wenger 2021-06-29 16:43:07 +02:00
parent f5b1c29085
commit 5202ecdc8e
4 changed files with 17 additions and 13 deletions

View File

@ -127,14 +127,14 @@ class Snapshot(models.Model):
def reset(self, user, selected_class): def reset(self, user, selected_class):
for chapter in Chapter.get_by_parent(self.module): for chapter in Chapter.get_by_parent(self.module):
qs = ContentBlock.get_by_parent(chapter) qs = ContentBlock.get_by_parent(chapter)
without_owner = Q(owner__isnull=True) default_content = Q(user_created=False)
no_snapshot = Q(contentblocksnapshot__isnull=True) no_snapshot = Q(contentblocksnapshot__isnull=True)
owner_user = Q(owner=user) owner_user = Q(owner=user)
logger.info(f'resetting hidden properties for chapter {chapter.id}') logger.info(f'resetting hidden properties for chapter {chapter.id}')
chapter.title_hidden_for.remove(selected_class) chapter.title_hidden_for.remove(selected_class)
chapter.description_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) content_block.hidden_for.remove(selected_class)
for content_block in qs.filter(owner_user): for content_block in qs.filter(owner_user):
content_block.visible_for.remove(selected_class) content_block.visible_for.remove(selected_class)

View File

@ -31,14 +31,14 @@ class SnapshotChapter:
self.description_hidden = description_hidden self.description_hidden = description_hidden
self.content_blocks = [] self.content_blocks = []
base_qs = ContentBlock.get_by_parent(chapter) 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) this_snapshot = Q(contentblocksnapshot__snapshot=snapshot)
self.content_blocks = [ self.content_blocks = [
SnapshotContentBlock( SnapshotContentBlock(
content_block=content_block, content_block=content_block,
snapshot=snapshot snapshot=snapshot
) for content_block in ) for content_block in
base_qs.filter(without_owner | this_snapshot) base_qs.filter(default_content | this_snapshot)
] ]
# all from module without owner # all from module without owner
# all with snapshotcontentblock with this snapshot # all with snapshotcontentblock with this snapshot

View File

@ -38,10 +38,11 @@ class CreateSnapshotTestCase(SkillboxTestCase):
self.teacher2 = User.objects.get(username='teacher2') self.teacher2 = User.objects.get(username='teacher2')
self.second_class_name = 'second_class' self.second_class_name = 'second_class'
self.second_class = SchoolClass.objects.get(name=self.second_class_name) self.second_class = SchoolClass.objects.get(name=self.second_class_name)
self.admin = User.objects.get(username='admin')
# module M has a chapter # module M has a chapter
self.chapter = ChapterFactory(parent=self.module, slug='some-chapter') self.chapter = ChapterFactory(parent=self.module, slug='some-chapter', owner=self.admin)
ChapterFactory(parent=self.module, slug='some-other-chapter') ChapterFactory(parent=self.module, slug='some-other-chapter', owner=self.admin)
# chapter has some content blocks a, b, c # chapter has some content blocks a, b, c
self.title_visible = 'visible' self.title_visible = 'visible'
@ -49,9 +50,9 @@ class CreateSnapshotTestCase(SkillboxTestCase):
self.title_custom = 'custom' self.title_custom = 'custom'
self.title_custom_hidden = 'custom-hidden' self.title_custom_hidden = 'custom-hidden'
self.visible_content_block = ContentBlockFactory(parent=self.chapter, module=self.module, 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, 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 # content block c is user created
self.custom_content_block = ContentBlockFactory(parent=self.chapter, owner=self.teacher, user_created=True, self.custom_content_block = ContentBlockFactory(parent=self.chapter, owner=self.teacher, user_created=True,
module=self.module, title=self.title_custom, module=self.module, title=self.title_custom,
@ -275,11 +276,11 @@ class CreateSnapshotTestCase(SkillboxTestCase):
self.assertIsNone(result.get('errors')) self.assertIsNone(result.get('errors'))
module = result['data']['module'] module = result['data']['module']
chapter1, chapter2 = module['chapters'] chapter1, chapter2 = module['chapters']
cb1, cb2, cb3, cb4 = chapter1['contentBlocks'] visible, hidden, custom, custom_hidden = 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 visible['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 hidden['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 custom['visibleFor']])
self.assertTrue(self.skillbox_class.name not in [sc['name'] for sc in cb4['visibleFor']]) self.assertTrue(self.skillbox_class.name not in [sc['name'] for sc in custom_hidden['visibleFor']])
def test_create_apply_view_snapshot(self): def test_create_apply_view_snapshot(self):
result = self.graphene_client.execute(MODULE_QUERY, variables={ result = self.graphene_client.execute(MODULE_QUERY, variables={

View File

@ -22,6 +22,9 @@ def create_users(data=None):
teacher_role = Role.objects.get_default_teacher_role() teacher_role = Role.objects.get_default_teacher_role()
if data is None: if data is None:
#make an admin account
UserFactory(username='admin', is_superuser=True, is_staff=True)
teacher = UserFactory(username='teacher') teacher = UserFactory(username='teacher')
UserRole.objects.create(user=teacher, role=teacher_role) UserRole.objects.create(user=teacher, role=teacher_role)