Update StreamField usage to include JSON

This commit is contained in:
Ramon Wenger 2023-02-21 17:09:17 +01:00
parent 5033668b79
commit dee5880565
7 changed files with 179 additions and 72 deletions

View File

@ -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),
),
]

View File

@ -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"),
]

View File

@ -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),
),
]

View File

@ -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"),
]
#

View File

@ -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"),

View File

@ -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),
),
]

View File

@ -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):