From dee588056593448e5d95f7bbed721456212fc597 Mon Sep 17 00:00:00 2001 From: Ramon Wenger Date: Tue, 21 Feb 2023 17:09:17 +0100 Subject: [PATCH] Update StreamField usage to include JSON --- .../0026_alter_basicknowledge_contents.py | 22 ++++ server/basicknowledge/models.py | 101 ++++++++++-------- .../0041_alter_contentblock_contents.py | 26 +++++ server/books/models/contentblock.py | 4 +- server/books/models/module.py | 3 +- .../0013_alter_roomentry_contents.py | 20 ++++ server/rooms/models.py | 75 ++++++++----- 7 files changed, 179 insertions(+), 72 deletions(-) create mode 100644 server/basicknowledge/migrations/0026_alter_basicknowledge_contents.py create mode 100644 server/books/migrations/0041_alter_contentblock_contents.py create mode 100644 server/rooms/migrations/0013_alter_roomentry_contents.py diff --git a/server/basicknowledge/migrations/0026_alter_basicknowledge_contents.py b/server/basicknowledge/migrations/0026_alter_basicknowledge_contents.py new file mode 100644 index 00000000..c7f3a747 --- /dev/null +++ b/server/basicknowledge/migrations/0026_alter_basicknowledge_contents.py @@ -0,0 +1,22 @@ +# Generated by Django 3.2.16 on 2023-02-21 16:04 + +import books.blocks +from django.db import migrations +import wagtail.blocks +import wagtail.fields +import wagtail.images.blocks + + +class Migration(migrations.Migration): + + dependencies = [ + ('basicknowledge', '0025_auto_20220914_1338'), + ] + + operations = [ + migrations.AlterField( + model_name='basicknowledge', + name='contents', + field=wagtail.fields.StreamField([('text_block', wagtail.blocks.StructBlock([('text', wagtail.blocks.RichTextBlock(features=['bold', 'ul', 'brand', 'secondary']))])), ('image_block', wagtail.images.blocks.ImageChooserBlock()), ('link_block', wagtail.blocks.StructBlock([('text', wagtail.blocks.TextBlock()), ('url', wagtail.blocks.URLBlock())])), ('video_block', wagtail.blocks.StructBlock([('url', wagtail.blocks.URLBlock())])), ('document_block', wagtail.blocks.StructBlock([('url', wagtail.blocks.URLBlock())])), ('section_title', wagtail.blocks.StructBlock([('text', wagtail.blocks.TextBlock())])), ('infogram_block', wagtail.blocks.StructBlock([('id', wagtail.blocks.TextBlock()), ('title', wagtail.blocks.TextBlock())])), ('genially_block', wagtail.blocks.StructBlock([('id', wagtail.blocks.TextBlock())])), ('thinglink_block', wagtail.blocks.StructBlock([('id', wagtail.blocks.TextBlock())])), ('subtitle', wagtail.blocks.StructBlock([('text', wagtail.blocks.TextBlock())])), ('cms_document_block', books.blocks.CMSDocumentBlock())], blank=True, null=True, use_json_field=True), + ), + ] diff --git a/server/basicknowledge/models.py b/server/basicknowledge/models.py index 85ff9434..febe823d 100644 --- a/server/basicknowledge/models.py +++ b/server/basicknowledge/models.py @@ -1,35 +1,44 @@ from django.db import models from django.utils.text import slugify -from wagtail.admin.panels import FieldPanel, StreamFieldPanel +from wagtail.admin.panels import FieldPanel from wagtail.fields import RichTextField, StreamField from wagtail.images.blocks import ImageChooserBlock -from books.blocks import CMSDocumentBlock, DocumentBlock, GeniallyBlock, InfogramBlock, InstrumentTextBlock, LinkBlock, \ - SectionTitleBlock, \ - SubtitleBlock, ThinglinkBlock, VideoBlock +from books.blocks import ( + CMSDocumentBlock, + DocumentBlock, + GeniallyBlock, + InfogramBlock, + InstrumentTextBlock, + LinkBlock, + SectionTitleBlock, + SubtitleBlock, + ThinglinkBlock, + VideoBlock, +) from core.constants import DEFAULT_RICH_TEXT_FEATURES from core.wagtail_utils import StrictHierarchyPage from django.utils.translation import ugettext_lazy as _ -LANGUAGE_COMMUNICATION = 'language_communication' -SOCIETY = 'society' -INTERDISCIPLINARY = 'interdisciplinary' -LANGUAGE_COMMUNICATION_LABEL = 'Sprache & Kommunikation' -SOCIETY_LABEL = 'Gesellschaft' -INTERDISCIPLINARY_LABEL = 'Überfachliche Instrumente' +LANGUAGE_COMMUNICATION = "language_communication" +SOCIETY = "society" +INTERDISCIPLINARY = "interdisciplinary" +LANGUAGE_COMMUNICATION_LABEL = "Sprache & Kommunikation" +SOCIETY_LABEL = "Gesellschaft" +INTERDISCIPLINARY_LABEL = "Überfachliche Instrumente" class InstrumentCategory(models.Model): name = models.CharField(max_length=255, unique=True) - background = models.CharField('background color', max_length=7) - foreground = models.CharField('foreground color', max_length=7) + background = models.CharField("background color", max_length=7) + foreground = models.CharField("foreground color", max_length=7) def __str__(self): return self.name class Meta: - verbose_name_plural = _('instrument categories') - verbose_name = _('instrument category') + verbose_name_plural = _("instrument categories") + verbose_name = _("instrument category") def default_category(): @@ -38,8 +47,8 @@ def default_category(): class InstrumentType(models.Model): class Meta: - verbose_name = _('instrument type') - verbose_name_plural = _('instrument types') + verbose_name = _("instrument type") + verbose_name_plural = _("instrument types") CATEGORY_CHOICES = ( (LANGUAGE_COMMUNICATION, LANGUAGE_COMMUNICATION_LABEL), @@ -53,7 +62,7 @@ class InstrumentType(models.Model): on_delete=models.PROTECT, null=False, default=default_category, - related_name='instrument_types' + related_name="instrument_types", ) @property @@ -64,42 +73,46 @@ class InstrumentType(models.Model): return self.type - class BasicKnowledge(StrictHierarchyPage): class Meta: - verbose_name = _('instrument') - verbose_name_plural = _('instruments') + verbose_name = _("instrument") + verbose_name_plural = _("instruments") - parent_page_types = ['books.book'] + parent_page_types = ["books.book"] - intro = RichTextField(features=DEFAULT_RICH_TEXT_FEATURES, default='', blank=True) + intro = RichTextField( + features=DEFAULT_RICH_TEXT_FEATURES, default="", blank=True) - contents = StreamField([ - ('text_block', InstrumentTextBlock()), - ('image_block', ImageChooserBlock()), - ('link_block', LinkBlock()), - ('video_block', VideoBlock()), - ('document_block', DocumentBlock()), - ('section_title', SectionTitleBlock()), - ('infogram_block', InfogramBlock()), - ('genially_block', GeniallyBlock()), - ('thinglink_block', ThinglinkBlock()), - ('subtitle', SubtitleBlock()), - ('cms_document_block', CMSDocumentBlock()), - ], null=True, blank=True) + contents = StreamField( + [ + ("text_block", InstrumentTextBlock()), + ("image_block", ImageChooserBlock()), + ("link_block", LinkBlock()), + ("video_block", VideoBlock()), + ("document_block", DocumentBlock()), + ("section_title", SectionTitleBlock()), + ("infogram_block", InfogramBlock()), + ("genially_block", GeniallyBlock()), + ("thinglink_block", ThinglinkBlock()), + ("subtitle", SubtitleBlock()), + ("cms_document_block", CMSDocumentBlock()), + ], + null=True, + blank=True, + use_json_field=True, + ) - new_type = models.ForeignKey(InstrumentType, null=True, on_delete=models.PROTECT, related_name='instruments') + new_type = models.ForeignKey( + InstrumentType, null=True, on_delete=models.PROTECT, related_name="instruments" + ) old_type = models.CharField( - max_length=100, - choices=InstrumentType.CATEGORY_CHOICES, - blank=True + max_length=100, choices=InstrumentType.CATEGORY_CHOICES, blank=True ) content_panels = [ - FieldPanel('title', classname="full title"), - FieldPanel('new_type'), - FieldPanel('intro'), - StreamFieldPanel('contents') + FieldPanel("title", classname="full title"), + FieldPanel("new_type"), + FieldPanel("intro"), + FieldPanel("contents"), ] - diff --git a/server/books/migrations/0041_alter_contentblock_contents.py b/server/books/migrations/0041_alter_contentblock_contents.py new file mode 100644 index 00000000..010789da --- /dev/null +++ b/server/books/migrations/0041_alter_contentblock_contents.py @@ -0,0 +1,26 @@ +# Generated by Django 3.2.16 on 2023-02-21 16:04 + +import assignments.models +import books.blocks +from django.db import migrations +import surveys.models +import wagtail.blocks +import wagtail.documents.blocks +import wagtail.fields +import wagtail.images.blocks +import wagtail.snippets.blocks + + +class Migration(migrations.Migration): + + dependencies = [ + ('books', '0040_module_hero_source'), + ] + + operations = [ + migrations.AlterField( + model_name='contentblock', + name='contents', + field=wagtail.fields.StreamField([('text_block', wagtail.blocks.StructBlock([('text', wagtail.blocks.RichTextBlock(features=['ul', 'bold']))])), ('basic_knowledge', wagtail.blocks.StructBlock([('description', wagtail.blocks.RichTextBlock(required=False)), ('basic_knowledge', wagtail.blocks.PageChooserBlock(page_type=['basicknowledge.BasicKnowledge'], required=True))])), ('assignment', wagtail.blocks.StructBlock([('assignment_id', wagtail.snippets.blocks.SnippetChooserBlock(assignments.models.Assignment))])), ('survey', wagtail.blocks.StructBlock([('survey_id', wagtail.snippets.blocks.SnippetChooserBlock(surveys.models.Survey))])), ('image_block', wagtail.images.blocks.ImageChooserBlock()), ('image_url_block', wagtail.blocks.StructBlock([('title', wagtail.blocks.TextBlock()), ('url', wagtail.blocks.URLBlock())])), ('link_block', wagtail.blocks.StructBlock([('text', wagtail.blocks.TextBlock()), ('url', wagtail.blocks.URLBlock())])), ('solution', wagtail.blocks.StructBlock([('text', wagtail.blocks.RichTextBlock(features=['ul', 'bold'])), ('document', books.blocks.CMSDocumentBlock(required=False))])), ('video_block', wagtail.blocks.StructBlock([('url', wagtail.blocks.URLBlock())])), ('document_block', wagtail.blocks.StructBlock([('url', wagtail.blocks.URLBlock())])), ('infogram_block', wagtail.blocks.StructBlock([('id', wagtail.blocks.TextBlock()), ('title', wagtail.blocks.TextBlock())])), ('genially_block', wagtail.blocks.StructBlock([('id', wagtail.blocks.TextBlock())])), ('thinglink_block', wagtail.blocks.StructBlock([('id', wagtail.blocks.TextBlock())])), ('subtitle', wagtail.blocks.StructBlock([('text', wagtail.blocks.TextBlock())])), ('instruction', wagtail.blocks.StructBlock([('url', wagtail.blocks.URLBlock(required=False)), ('text', wagtail.blocks.TextBlock(required=False)), ('document', wagtail.documents.blocks.DocumentChooserBlock(required=False))])), ('module_room_slug', wagtail.blocks.StructBlock([('title', wagtail.blocks.TextBlock())])), ('cms_document_block', books.blocks.CMSDocumentBlock()), ('content_list_item', wagtail.blocks.StreamBlock([('text_block', wagtail.blocks.StructBlock([('text', wagtail.blocks.RichTextBlock(features=['ul', 'bold']))])), ('basic_knowledge', wagtail.blocks.StructBlock([('description', wagtail.blocks.RichTextBlock(required=False)), ('basic_knowledge', wagtail.blocks.PageChooserBlock(page_type=['basicknowledge.BasicKnowledge'], required=True))])), ('assignment', wagtail.blocks.StructBlock([('assignment_id', wagtail.snippets.blocks.SnippetChooserBlock(assignments.models.Assignment))])), ('survey', wagtail.blocks.StructBlock([('survey_id', wagtail.snippets.blocks.SnippetChooserBlock(surveys.models.Survey))])), ('image_block', wagtail.images.blocks.ImageChooserBlock()), ('image_url_block', wagtail.blocks.StructBlock([('title', wagtail.blocks.TextBlock()), ('url', wagtail.blocks.URLBlock())])), ('link_block', wagtail.blocks.StructBlock([('text', wagtail.blocks.TextBlock()), ('url', wagtail.blocks.URLBlock())])), ('solution', wagtail.blocks.StructBlock([('text', wagtail.blocks.RichTextBlock(features=['ul', 'bold'])), ('document', books.blocks.CMSDocumentBlock(required=False))])), ('video_block', wagtail.blocks.StructBlock([('url', wagtail.blocks.URLBlock())])), ('document_block', wagtail.blocks.StructBlock([('url', wagtail.blocks.URLBlock())])), ('infogram_block', wagtail.blocks.StructBlock([('id', wagtail.blocks.TextBlock()), ('title', wagtail.blocks.TextBlock())])), ('genially_block', wagtail.blocks.StructBlock([('id', wagtail.blocks.TextBlock())])), ('thinglink_block', wagtail.blocks.StructBlock([('id', wagtail.blocks.TextBlock())])), ('subtitle', wagtail.blocks.StructBlock([('text', wagtail.blocks.TextBlock())])), ('instruction', wagtail.blocks.StructBlock([('url', wagtail.blocks.URLBlock(required=False)), ('text', wagtail.blocks.TextBlock(required=False)), ('document', wagtail.documents.blocks.DocumentChooserBlock(required=False))])), ('module_room_slug', wagtail.blocks.StructBlock([('title', wagtail.blocks.TextBlock())])), ('cms_document_block', books.blocks.CMSDocumentBlock())]))], blank=True, null=True, use_json_field=True), + ), + ] diff --git a/server/books/models/contentblock.py b/server/books/models/contentblock.py index c8b7b022..f2f6a43c 100644 --- a/server/books/models/contentblock.py +++ b/server/books/models/contentblock.py @@ -5,7 +5,6 @@ from wagtail.admin.panels import ( FieldPanel, TabbedInterface, ObjectList, - StreamFieldPanel, ) from wagtail.blocks import StreamBlock from wagtail.fields import StreamField @@ -100,6 +99,7 @@ class ContentBlock(StrictHierarchyPage, GraphqlNodeMixin): content_blocks + [("content_list_item", content_list_item)], null=True, blank=True, + use_json_field=True, ) type = models.CharField( @@ -108,7 +108,7 @@ class ContentBlock(StrictHierarchyPage, GraphqlNodeMixin): content_panels = [ FieldPanel("title", classname="full title"), FieldPanel("type"), - StreamFieldPanel("contents"), + FieldPanel("contents"), ] # diff --git a/server/books/models/module.py b/server/books/models/module.py index c06ac61c..abcddfe1 100644 --- a/server/books/models/module.py +++ b/server/books/models/module.py @@ -2,7 +2,6 @@ from django.db import models from django.utils import timezone from wagtail.admin.panels import FieldPanel, TabbedInterface, ObjectList from wagtail.fields import RichTextField -from wagtail.images.edit_handlers import ImageChooserPanel from core.constants import DEFAULT_RICH_TEXT_FEATURES from core.wagtail_utils import StrictHierarchyPage, get_default_settings @@ -35,7 +34,7 @@ class Module(StrictHierarchyPage): content_panels = [ FieldPanel("title", classname="full title"), FieldPanel("meta_title", classname="full title"), - ImageChooserPanel("hero_image"), + FieldPanel("hero_image"), FieldPanel("hero_source"), FieldPanel("teaser"), FieldPanel("intro"), diff --git a/server/rooms/migrations/0013_alter_roomentry_contents.py b/server/rooms/migrations/0013_alter_roomentry_contents.py new file mode 100644 index 00000000..fdc6a4d9 --- /dev/null +++ b/server/rooms/migrations/0013_alter_roomentry_contents.py @@ -0,0 +1,20 @@ +# Generated by Django 3.2.16 on 2023-02-21 16:04 + +from django.db import migrations +import wagtail.blocks +import wagtail.fields + + +class Migration(migrations.Migration): + + dependencies = [ + ('rooms', '0012_auto_20220712_1109'), + ] + + operations = [ + migrations.AlterField( + model_name='roomentry', + name='contents', + field=wagtail.fields.StreamField([('text_block', wagtail.blocks.StructBlock([('text', wagtail.blocks.RichTextBlock(features=['ul', 'bold']))])), ('image_url_block', wagtail.blocks.StructBlock([('title', wagtail.blocks.TextBlock()), ('url', wagtail.blocks.URLBlock())])), ('link_block', wagtail.blocks.StructBlock([('text', wagtail.blocks.TextBlock()), ('url', wagtail.blocks.URLBlock())])), ('document_block', wagtail.blocks.StructBlock([('url', wagtail.blocks.URLBlock())])), ('subtitle', wagtail.blocks.StructBlock([('text', wagtail.blocks.TextBlock())])), ('video_block', wagtail.blocks.StructBlock([('url', wagtail.blocks.URLBlock())]))], blank=True, null=True, use_json_field=True), + ), + ] diff --git a/server/rooms/models.py b/server/rooms/models.py index 2a21c9e0..4f1ab6ad 100644 --- a/server/rooms/models.py +++ b/server/rooms/models.py @@ -3,7 +3,13 @@ from django.db import models from django_extensions.db.models import TitleSlugDescriptionModel from wagtail.fields import StreamField -from books.blocks import DocumentBlock, ImageUrlBlock, LinkBlock, SubtitleBlock, VideoBlock +from books.blocks import ( + DocumentBlock, + ImageUrlBlock, + LinkBlock, + SubtitleBlock, + VideoBlock, +) from books.models import TextBlock from core.mixins import GraphqlNodeMixin from users.models import SchoolClass @@ -11,38 +17,55 @@ from users.models import SchoolClass class Room(TitleSlugDescriptionModel, GraphqlNodeMixin): class Meta: - verbose_name = 'Raum' - verbose_name_plural = 'Räume' + verbose_name = "Raum" + verbose_name_plural = "Räume" - school_class = models.ForeignKey(SchoolClass, blank=False, null=False, on_delete=models.CASCADE, - related_name='rooms') + school_class = models.ForeignKey( + SchoolClass, + blank=False, + null=False, + on_delete=models.CASCADE, + related_name="rooms", + ) appearance = models.CharField(blank=True, null=False, max_length=255) user_created = models.BooleanField(blank=False, null=False, default=True) restricted = models.BooleanField(default=False) def __str__(self): - return f'{self.title} - {self.school_class}' + return f"{self.title} - {self.school_class}" class RoomEntry(TitleSlugDescriptionModel): class Meta: - verbose_name = 'Raumeintrag' - verbose_name_plural = 'Raumeinträge' + verbose_name = "Raumeintrag" + verbose_name_plural = "Raumeinträge" - room = models.ForeignKey(Room, blank=False, null=False, on_delete=models.CASCADE, related_name='room_entries') - author = models.ForeignKey(get_user_model(), null=True, on_delete=models.CASCADE) + room = models.ForeignKey( + Room, + blank=False, + null=False, + on_delete=models.CASCADE, + related_name="room_entries", + ) + author = models.ForeignKey( + get_user_model(), null=True, on_delete=models.CASCADE) - contents = StreamField([ - ('text_block', TextBlock()), - ('image_url_block', ImageUrlBlock()), - ('link_block', LinkBlock()), - ('document_block', DocumentBlock()), - ('subtitle', SubtitleBlock()), - ('video_block', VideoBlock()) - ], null=True, blank=True) + contents = StreamField( + [ + ("text_block", TextBlock()), + ("image_url_block", ImageUrlBlock()), + ("link_block", LinkBlock()), + ("document_block", DocumentBlock()), + ("subtitle", SubtitleBlock()), + ("video_block", VideoBlock()), + ], + null=True, + blank=True, + use_json_field=True, + ) def __str__(self): - return f'RoomEntry {self.id}-{self.title}-{self.author}' + return f"RoomEntry {self.id}-{self.title}-{self.author}" def can_user_see_entry(self, user): return user.is_superuser or self.room.school_class.is_user_in_schoolclass(user) @@ -50,17 +73,21 @@ class RoomEntry(TitleSlugDescriptionModel): class ModuleRoomSlug(TitleSlugDescriptionModel): class Meta: - verbose_name = 'Slug für Modul-Raum' - verbose_name_plural = 'Slugs für Modul-Raum' + verbose_name = "Slug für Modul-Raum" + verbose_name_plural = "Slugs für Modul-Raum" def __str__(self): - return f'ModuleRoomSlug {self.id}-{self.title}' + return f"ModuleRoomSlug {self.id}-{self.title}" class Comment(models.Model): text = models.TextField() - room_entry = models.ForeignKey(RoomEntry, related_name='comments', on_delete=models.CASCADE) - owner = models.ForeignKey(get_user_model(), related_name='comments', on_delete=models.PROTECT) + room_entry = models.ForeignKey( + RoomEntry, related_name="comments", on_delete=models.CASCADE + ) + owner = models.ForeignKey( + get_user_model(), related_name="comments", on_delete=models.PROTECT + ) created = models.DateTimeField(auto_now_add=True) def __str__(self):