Replace category string in instrument type with proper model

This commit is contained in:
Ramon Wenger 2022-09-13 11:22:46 +02:00
parent d172e9a005
commit d1c4d63179
10 changed files with 119 additions and 24 deletions

View File

@ -0,0 +1,31 @@
# Generated by Django 3.2.13 on 2022-09-08 15:17
from django.db import migrations
LANGUAGE_COMMUNICATION = 'language_communication'
SOCIETY = 'society'
INTERDISCIPLINARY = 'interdisciplinary'
CATEGORY_CHOICES = (
(LANGUAGE_COMMUNICATION, '#DAA009', '#FFF5D9', 'Sprache & Kommunikation'),
(SOCIETY, '#0F7CAC', '#DBEEF6', 'Gesellschaft'),
(INTERDISCIPLINARY, '#99B53E', '#F3F9E3', 'Überfachliches Instrument'),
)
def create_categories(apps, schema_editor):
InstrumentCategory = apps.get_model('basicknowledge', 'InstrumentCategory')
InstrumentType = apps.get_model('basicknowledge', 'InstrumentType')
for code, foreground, background, category in CATEGORY_CHOICES:
instrument_category = InstrumentCategory.objects.create(name=category, background=background, foreground=foreground)
InstrumentType.objects.filter(category=code).update(new_category=instrument_category)
class Migration(migrations.Migration):
dependencies = [
('basicknowledge', '0019_auto_20220913_0820'),
]
operations = [
migrations.RunPython(create_categories, migrations.RunPython.noop)
]

View File

@ -0,0 +1,17 @@
# Generated by Django 3.2.13 on 2022-09-13 08:52
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('basicknowledge', '0020_auto_20220908_1517'),
]
operations = [
migrations.RemoveField(
model_name='instrumenttype',
name='category',
),
]

View File

@ -0,0 +1,18 @@
# Generated by Django 3.2.13 on 2022-09-13 08:52
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('basicknowledge', '0021_remove_instrumenttype_category'),
]
operations = [
migrations.RenameField(
model_name='instrumenttype',
old_name='new_category',
new_name='category',
),
]

View File

@ -0,0 +1,20 @@
# Generated by Django 3.2.13 on 2022-09-13 09:01
import basicknowledge.models
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('basicknowledge', '0022_rename_new_category_instrumenttype_category'),
]
operations = [
migrations.AlterField(
model_name='instrumenttype',
name='category',
field=models.ForeignKey(default=basicknowledge.models.default_category, on_delete=django.db.models.deletion.PROTECT, to='basicknowledge.instrumentcategory'),
),
]

View File

@ -13,6 +13,9 @@ from core.wagtail_utils import StrictHierarchyPage
LANGUAGE_COMMUNICATION = 'language_communication'
SOCIETY = 'society'
INTERDISCIPLINARY = 'interdisciplinary'
LANGUAGE_COMMUNICATION_LABEL = 'Sprache & Kommunikation'
SOCIETY_LABEL = 'Gesellschaft'
INTERDISCIPLINARY_LABEL = 'Überfachliche Instrumente'
class InstrumentCategory(models.Model):
@ -27,23 +30,22 @@ class InstrumentCategory(models.Model):
verbose_name_plural = 'instrument categories'
def default_category():
return InstrumentCategory.objects.first().pk
class InstrumentType(models.Model):
CATEGORY_CHOICES = (
(LANGUAGE_COMMUNICATION, 'Sprache & Kommunikation'),
(SOCIETY, 'Gesellschaft'),
(INTERDISCIPLINARY, 'Überfachliches Instrument'),
(LANGUAGE_COMMUNICATION, LANGUAGE_COMMUNICATION_LABEL),
(SOCIETY, SOCIETY_LABEL),
(INTERDISCIPLINARY, INTERDISCIPLINARY_LABEL),
)
name = models.CharField(max_length=255, unique=True)
category = models.CharField(
max_length=100,
choices=CATEGORY_CHOICES
)
new_category = models.ForeignKey(
category = models.ForeignKey(
InstrumentCategory,
on_delete=models.PROTECT,
null=True,
default=None
null=False,
default=default_category
)
@property

View File

@ -6,11 +6,18 @@ from api.graphene_wagtail import GenericStreamFieldType
from api.utils import get_object
from notes.models import InstrumentBookmark
from notes.schema import InstrumentBookmarkNode
from .models import BasicKnowledge, InstrumentType
from .models import BasicKnowledge, InstrumentCategory, InstrumentType
class InstrumentCategoryNode(DjangoObjectType):
class Meta:
model = InstrumentCategory
only_fields = [
'name', 'foreground', 'background', 'id'
]
class InstrumentTypeNode(DjangoObjectType):
type = graphene.String(required=True)
category = graphene.Field(InstrumentCategoryNode)
class Meta:
model = InstrumentType

View File

@ -6,7 +6,12 @@ query InstrumentTypesQuery {
instrumentTypes {
name
type
category
category {
id
name
foreground
background
}
}
}
"""

View File

@ -1,5 +1,5 @@
from wagtail.contrib.modeladmin.options import ModelAdmin, modeladmin_register
from .models import BasicKnowledge
from .models import BasicKnowledge, InstrumentCategory, InstrumentType
class InstrumentAdmin(ModelAdmin):

View File

@ -9,8 +9,9 @@ from wagtail.core.models import Page, Site
from wagtail.core.rich_text import RichText
from assignments.models import Assignment
from basicknowledge.models import BasicKnowledge, INTERDISCIPLINARY, InstrumentCategory, InstrumentType, \
LANGUAGE_COMMUNICATION, SOCIETY
from basicknowledge.models import BasicKnowledge, INTERDISCIPLINARY, INTERDISCIPLINARY_LABEL, InstrumentCategory, \
InstrumentType, \
LANGUAGE_COMMUNICATION, LANGUAGE_COMMUNICATION_LABEL, SOCIETY, SOCIETY_LABEL
from books.blocks import AssignmentBlock, BasicKnowledgeBlock, ImageUrlBlock, LinkBlock, VideoBlock
from books.models import Book, Chapter, ContentBlock, Module, TextBlock, Topic
from core.factories import BasePageFactory, DummyImageFactory, fake, fake_paragraph, fake_title
@ -76,7 +77,7 @@ class InstrumentCategoryFactory(factory.DjangoModelFactory):
model = InstrumentCategory
django_get_or_create = ('name',)
name = factory.Iterator([LANGUAGE_COMMUNICATION, SOCIETY, INTERDISCIPLINARY])
name = factory.Iterator([LANGUAGE_COMMUNICATION_LABEL, SOCIETY_LABEL, INTERDISCIPLINARY_LABEL])
foreground = factory.Iterator(['FF0000', 'FFFFFF', '000000'])
background = factory.Iterator(['FF0000', 'FFFFFF', '000000'])
@ -84,7 +85,7 @@ class InstrumentTypeFactory(factory.DjangoModelFactory):
class Meta:
model = InstrumentType
category = factory.Iterator([LANGUAGE_COMMUNICATION, SOCIETY, INTERDISCIPLINARY])
category = factory.SubFactory(InstrumentCategoryFactory)
name = factory.LazyAttribute(lambda x: fake.text(max_nb_chars=20))

View File

@ -538,16 +538,10 @@ type InstrumentNodeEdge {
cursor: String!
}
enum InstrumentTypeCategory {
LANGUAGE_COMMUNICATION
SOCIETY
INTERDISCIPLINARY
}
type InstrumentTypeNode {
id: ID!
name: String!
category: InstrumentTypeCategory!
category: InstrumentCategoryNode
type: String!
}