Add category information to instrument queries

This commit is contained in:
Ramon Wenger 2022-09-15 16:04:37 +02:00
parent bd87999487
commit 37e4461a22
7 changed files with 75 additions and 10 deletions

View File

@ -81,9 +81,10 @@ def augment_fields(raw_data):
logger.error('Survey {} does not exist'.format(survey_id)) logger.error('Survey {} does not exist'.format(survey_id))
if _type == 'basic_knowledge' or _type == 'instrument': if _type == 'basic_knowledge' or _type == 'instrument':
_value = data['value'] _value = data['value']
basic_knowledge = BasicKnowledge.objects.get(pk=_value['basic_knowledge']) instrument = BasicKnowledge.objects.get(pk=_value['basic_knowledge'])
_value.update({ _value.update({
'slug': basic_knowledge.slug 'slug': instrument.slug,
'foreground': instrument.new_type.category.foreground
}) })
data['value'] = _value data['value'] = _value

View File

@ -0,0 +1,18 @@
# Generated by Django 3.2.13 on 2022-09-15 13:39
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('books', '0036_alter_contentblock_contents'),
]
operations = [
migrations.AlterField(
model_name='contentblock',
name='type',
field=models.CharField(choices=[('normal', 'Normal'), ('base_communication', 'Instrument Sprache & Kommunikation'), ('task', 'Auftrag'), ('instrument', 'Instrument'), ('base_society', 'Instrument Gesellschaft'), ('base_interdisciplinary', 'Überfachliches Instrument')], default='normal', max_length=100),
),
]

View File

@ -0,0 +1,17 @@
# Generated by Django 3.2.13 on 2022-09-15 13:40
from django.db import migrations
def migrate_instruments(apps, schema_editor):
ContentBlock = apps.get_model('books', 'ContentBlock')
ContentBlock.objects.filter(type__startswith='base_').update(type='instrument')
class Migration(migrations.Migration):
dependencies = [
('books', '0037_alter_contentblock_type'),
]
operations = [
migrations.RunPython(migrate_instruments, migrations.RunPython.noop)
]

View File

@ -0,0 +1,18 @@
# Generated by Django 3.2.13 on 2022-09-15 14:02
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('books', '0038_auto_20220915_1340'),
]
operations = [
migrations.AlterField(
model_name='contentblock',
name='type',
field=models.CharField(choices=[('normal', 'Normal'), ('task', 'Auftrag'), ('instrument', 'Instrument')], default='normal', max_length=100),
),
]

View File

@ -26,17 +26,13 @@ class ContentBlock(StrictHierarchyPage):
verbose_name_plural = 'Inhaltsblöcke' verbose_name_plural = 'Inhaltsblöcke'
NORMAL = 'normal' NORMAL = 'normal'
BASE_COMMUNICATION = 'base_communication'
TASK = 'task' TASK = 'task'
BASE_SOCIETY = 'base_society' INSTRUMENT = 'instrument'
BASE_INTERDISCIPLINARY = 'base_interdisciplinary'
TYPE_CHOICES = ( TYPE_CHOICES = (
(NORMAL, 'Normal'), (NORMAL, 'Normal'),
(BASE_COMMUNICATION, 'Instrument Sprache & Kommunikation'),
(TASK, 'Auftrag'), (TASK, 'Auftrag'),
(BASE_SOCIETY, 'Instrument Gesellschaft'), (INSTRUMENT, 'Instrument'),
(BASE_INTERDISCIPLINARY, 'Überfachliches Instrument'),
) )
# blocks without owner are visible by default, need to be hidden for each class # blocks without owner are visible by default, need to be hidden for each class

View File

@ -2,6 +2,8 @@ import graphene
from graphene import relay from graphene import relay
from graphene_django import DjangoObjectType from graphene_django import DjangoObjectType
from basicknowledge.models import BasicKnowledge
from basicknowledge.queries import InstrumentCategoryNode
from books.models import ContentBlock from books.models import ContentBlock
from books.schema.interfaces.contentblock import ContentBlockInterface from books.schema.interfaces.contentblock import ContentBlockInterface
from books.utils import are_solutions_enabled_for from books.utils import are_solutions_enabled_for
@ -40,6 +42,7 @@ class ContentBlockNode(DjangoObjectType, HiddenAndVisibleForMixin):
mine = graphene.Boolean() mine = graphene.Boolean()
bookmarks = graphene.List(ContentBlockBookmarkNode) bookmarks = graphene.List(ContentBlockBookmarkNode)
original_creator = graphene.Field('users.schema.PublicUserNode') original_creator = graphene.Field('users.schema.PublicUserNode')
instrument_category = graphene.Field(InstrumentCategoryNode)
class Meta: class Meta:
model = ContentBlock model = ContentBlock
@ -80,6 +83,17 @@ class ContentBlockNode(DjangoObjectType, HiddenAndVisibleForMixin):
content_block=self content_block=self
) )
@staticmethod
def resolve_instrument_category(root: ContentBlock, info, **kwargs):
if root.type == ContentBlock.INSTRUMENT:
for content in root.contents.raw_data:
if content['type'] == 'instrument' or content['type'] == 'basic_knowledge':
_id = content['value']['basic_knowledge']
instrument = BasicKnowledge.objects.get(id=_id)
category = instrument.new_type.category
return category
return None
def process_module_room_slug_block(content): def process_module_room_slug_block(content):
if content['type'] == 'module_room_slug': if content['type'] == 'module_room_slug':

View File

@ -299,6 +299,7 @@ type ContentBlockNode implements Node & ContentBlockInterface {
mine: Boolean mine: Boolean
bookmarks: [ContentBlockBookmarkNode] bookmarks: [ContentBlockBookmarkNode]
originalCreator: PublicUserNode originalCreator: PublicUserNode
instrumentCategory: InstrumentCategoryNode
} }
type ContentBlockNodeConnection { type ContentBlockNodeConnection {
@ -511,7 +512,7 @@ type InstrumentBookmarkNode implements Node {
instrument: InstrumentNode! instrument: InstrumentNode!
} }
type InstrumentCategoryNode { type InstrumentCategoryNode implements Node {
id: ID! id: ID!
name: String! name: String!
background: String! background: String!
@ -539,7 +540,7 @@ type InstrumentNodeEdge {
cursor: String! cursor: String!
} }
type InstrumentTypeNode { type InstrumentTypeNode implements Node {
id: ID! id: ID!
name: String! name: String!
category: InstrumentCategoryNode category: InstrumentCategoryNode