Refactor content block type

This commit is contained in:
Ramon Wenger 2021-05-04 22:52:03 +02:00
parent 4ea598b700
commit 29f3726993
8 changed files with 112 additions and 51 deletions

View File

@ -3,15 +3,8 @@ from graphene import relay
from api.graphene_wagtail import GenericStreamFieldType from api.graphene_wagtail import GenericStreamFieldType
class ContentBlockType(graphene.Enum):
NORMAL = 'normal'
BASE_COMMUNICATION = 'base_communication'
TASK = 'task'
BASE_SOCIETY = 'base_society'
BASE_INTERDISCIPLINARY = 'base_interdisciplinary'
class ContentBlockInterface(relay.Node): class ContentBlockInterface(relay.Node):
title = graphene.String() title = graphene.String()
contents = GenericStreamFieldType() contents = GenericStreamFieldType()
type = ContentBlockType() type = graphene.String()

View File

@ -14,7 +14,7 @@ from notes.schema import ChapterBookmarkNode
class ChapterNode(DjangoObjectType): class ChapterNode(DjangoObjectType):
bookmark = graphene.Field(ChapterBookmarkNode) bookmark = graphene.Field(ChapterBookmarkNode)
content_blocks = DjangoFilterConnectionField('books.schema.nodes.ContentBlockNode') content_blocks = graphene.List('books.schema.nodes.ContentBlockNode')
class Meta: class Meta:
model = Chapter model = Chapter

View File

@ -49,9 +49,14 @@ class ContentBlockNode(DjangoObjectType):
'slug', 'title', 'slug', 'title',
] ]
interfaces = (ContentBlockInterface,) interfaces = (ContentBlockInterface,)
convert_choices_to_enum = False
def resolve_mine(self, info, **kwargs): def resolve_mine(parent, info, **kwargs):
return self.owner is not None and self.owner.pk == info.context.user.pk return parent.owner is not None and parent.owner.pk == info.context.user.pk
@staticmethod
def resolve_type(parent, info, **kwargs):
return parent.type
def resolve_contents(self, info, **kwargs): def resolve_contents(self, info, **kwargs):
updated_stream_data = [] updated_stream_data = []

View File

@ -14,6 +14,8 @@ from ...models import Module, Chapter, ChapterSnapshot, ContentBlock
class SnapshotContentBlock: class SnapshotContentBlock:
def __init__(self, content_block, snapshot): def __init__(self, content_block, snapshot):
self.id = content_block.id
self.pk = content_block.pk
self.title = content_block.title self.title = content_block.title
self.contents = content_block.title self.contents = content_block.title
self.type = content_block.type self.type = content_block.type
@ -22,6 +24,8 @@ class SnapshotContentBlock:
class SnapshotChapter: class SnapshotChapter:
def __init__(self, chapter, snapshot, description_hidden=False, title_hidden=False): def __init__(self, chapter, snapshot, description_hidden=False, title_hidden=False):
self.id = chapter.id
self.pk = chapter.pk
self.title = chapter.title self.title = chapter.title
self.description = chapter.description self.description = chapter.description
self.title_hidden = title_hidden self.title_hidden = title_hidden

View File

@ -0,0 +1,52 @@
from books.factories import ModuleFactory, ChapterFactory
from books.models import ContentBlock
from core.tests.base_test import SkillboxTestCase
CONTENT_BLOCK_QUERY = """
query ContentBlockQuery($slug: String!) {
module(slug: $slug) {
chapters {
edges {
node {
id
contentBlocks {
edges {
node {
id
title
type
}
}
}
}
}
}
}
}
"""
class ContentBlockTestCase(SkillboxTestCase):
def setUp(self) -> None:
self.createDefault()
self.client = self.get_client()
self.slug = 'module'
self.module = ModuleFactory(slug=self.slug)
self.chapter = ChapterFactory(parent=self.module)
self.content_block = ContentBlock(
type=ContentBlock.NORMAL,
title='Title'
)
self.chapter.add_child(instance=self.content_block)
def test_content_block(self):
result = self.client.execute(CONTENT_BLOCK_QUERY, variables={
"slug": self.slug
})
self.assertIsNone(result.get('errors'))
module = result.get('data').get('module')
content_block = module['chapters']['edges'][0]['node']['contentBlocks']['edges'][0]['node']
self.assertEqual(content_block['title'], 'Title')
self.assertIsNotNone(content_block['type'])

View File

@ -5,6 +5,7 @@ from graphql_relay import to_global_id, from_global_id
from api.schema import schema from api.schema import schema
from books.factories import ModuleFactory, ChapterFactory, ContentBlockFactory from books.factories import ModuleFactory, ChapterFactory, ContentBlockFactory
from books.models import Snapshot, ChapterSnapshot from books.models import Snapshot, ChapterSnapshot
from core.tests.base_test import SkillboxTestCase
from users.models import User, SchoolClass from users.models import User, SchoolClass
from users.services import create_users from users.services import create_users
@ -55,23 +56,16 @@ mutation CreateSnapshot($input: CreateSnapshotInput!) {
creator { creator {
username username
} }
snapshotChapters { chapters {
edges { id
node { descriptionHidden
id titleHidden
descriptionHidden title
titleHidden description
title contentBlocks {
description id
contentBlocks { title
edges { hidden
node {
id
title
}
}
}
}
} }
} }
} }
@ -112,12 +106,13 @@ def edges_to_array(entity):
return [edge['node'] for edge in entity.get('edges')] return [edge['node'] for edge in entity.get('edges')]
class CreateSnapshotTestCase(TestCase): class CreateSnapshotTestCase(SkillboxTestCase):
def setUp(self): def setUp(self):
create_users() self.createDefault()
self.client = self.get_client()
# teacher will create snapshot # teacher will create snapshot
self.slug = 'some-module' self.slug = 'some-module'
self.teacher = User.objects.get(username='teacher')
self.module = ModuleFactory(slug=self.slug) self.module = ModuleFactory(slug=self.slug)
self.skillbox_class = SchoolClass.objects.get(name='skillbox') self.skillbox_class = SchoolClass.objects.get(name='skillbox')
@ -144,10 +139,6 @@ class CreateSnapshotTestCase(TestCase):
# chapter description is hidden for school class X # chapter description is hidden for school class X
self.chapter.title_hidden_for.add(self.skillbox_class) self.chapter.title_hidden_for.add(self.skillbox_class)
request = RequestFactory().get('/')
request.user = self.teacher
self.client = Client(schema=schema, context_value=request)
# we make a snapshot S of the module M # we make a snapshot S of the module M
# snapshot S looks like module M for school class X # snapshot S looks like module M for school class X
@ -190,7 +181,7 @@ class CreateSnapshotTestCase(TestCase):
}) })
self.assertIsNone(result.get('errors')) self.assertIsNone(result.get('errors'))
snapshot = result.get('data').get('createSnapshot').get('snapshot') snapshot = result.get('data').get('createSnapshot').get('snapshot')
chapter = snapshot.get('snapshotChapters').get('edges')[0]['node'] chapter = snapshot.get('chapters')[0]
self.assertIsNotNone(snapshot.get('created')) self.assertIsNotNone(snapshot.get('created'))
self.assertEqual(snapshot.get('creator').get('username'), self.teacher.username) self.assertEqual(snapshot.get('creator').get('username'), self.teacher.username)
@ -199,10 +190,15 @@ class CreateSnapshotTestCase(TestCase):
self.assertFalse(chapter['descriptionHidden']) self.assertFalse(chapter['descriptionHidden'])
_, chapter_id = from_global_id(chapter['id']) _, chapter_id = from_global_id(chapter['id'])
self.assertEqual(int(chapter_id), self.chapter.id) self.assertEqual(int(chapter_id), self.chapter.id)
content_blocks = [edge['node'] for edge in chapter['contentBlocks']['edges']] content_blocks = chapter['contentBlocks']
self.assertEqual(len(content_blocks), 2) self.assertEqual(len(content_blocks), 3)
self.assertEqual(content_blocks[0]['title'], self.title_visible) visible, hidden, custom = content_blocks
self.assertEqual(content_blocks[1]['title'], self.title_custom) 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(ChapterSnapshot.objects.count(), 2) self.assertEqual(ChapterSnapshot.objects.count(), 2)
def test_apply_snapshot(self): def test_apply_snapshot(self):

View File

@ -0,0 +1,19 @@
from django.test import TestCase, RequestFactory
from graphene.test import Client
from api.schema import schema
from users.models import User
from users.services import create_users
class SkillboxTestCase(TestCase):
def createDefault(self) -> None:
create_users()
self.teacher = User.objects.get(username='teacher')
def get_client(self, user=None) -> Client:
request = RequestFactory().get('/')
if user is None:
user = self.teacher
request.user = user
return Client(schema=schema, context_value=request)

View File

@ -303,7 +303,7 @@ interface ContentBlockInterface {
id: ID! id: ID!
title: String title: String
contents: GenericStreamFieldType contents: GenericStreamFieldType
type: ContentBlockType type: String
} }
type ContentBlockNode implements ContentBlockInterface { type ContentBlockNode implements ContentBlockInterface {
@ -313,7 +313,7 @@ type ContentBlockNode implements ContentBlockInterface {
visibleFor(offset: Int, before: String, after: String, first: Int, last: Int, name: String): SchoolClassNodeConnection! visibleFor(offset: Int, before: String, after: String, first: Int, last: Int, name: String): SchoolClassNodeConnection!
userCreated: Boolean! userCreated: Boolean!
contents: GenericStreamFieldType contents: GenericStreamFieldType
type: ContentBlockType type: String
id: ID! id: ID!
mine: Boolean mine: Boolean
bookmarks: [ContentBlockBookmarkNode] bookmarks: [ContentBlockBookmarkNode]
@ -329,14 +329,6 @@ type ContentBlockNodeEdge {
cursor: String! cursor: String!
} }
enum ContentBlockType {
NORMAL
BASE_COMMUNICATION
TASK
BASE_SOCIETY
BASE_INTERDISCIPLINARY
}
input ContentElementInput { input ContentElementInput {
id: String id: String
type: InputTypes! type: InputTypes!
@ -924,7 +916,7 @@ type SnapshotContentBlockNode implements ContentBlockInterface {
id: ID! id: ID!
title: String title: String
contents: GenericStreamFieldType contents: GenericStreamFieldType
type: ContentBlockType type: String
hidden: Boolean hidden: Boolean
} }