Forked from
TO / Maják
1853 commits behind the upstream repository.
models.py 4.99 KiB
from django.db import models
from django.utils.translation import gettext_lazy
from wagtail.admin.edit_handlers import (
FieldPanel,
HelpPanel,
MultiFieldPanel,
StreamFieldPanel,
)
from wagtail.contrib.table_block.blocks import TableBlock
from wagtail.core import blocks
from wagtail.core.fields import StreamField
from wagtail.core.models import Page
from wagtail.documents.blocks import DocumentChooserBlock
from wagtail.images.blocks import ImageChooserBlock
from wagtail.images.edit_handlers import ImageChooserPanel
from wagtailmetadata.models import MetadataPageMixin
from tuning import help
RICH_TEXT_FEATURES = [
"h2",
"h3",
"h4",
"h5",
"bold",
"italic",
"ol",
"ul",
"hr",
"link",
"document-link",
"image",
"superscript",
"subscript",
"strikethrough",
"blockquote",
]
class ColumnsTextBlock(blocks.StructBlock):
left_text = blocks.RichTextBlock(label="levý sloupec", features=RICH_TEXT_FEATURES)
right_text = blocks.RichTextBlock(
label="pravý sloupec", features=RICH_TEXT_FEATURES
)
class Meta:
label = "text dva sloupce"
icon = "doc-full"
class MenuItemBlock(blocks.StructBlock):
name = blocks.CharBlock(label="název")
page = blocks.PageChooserBlock(
label="stránka",
page_type=["uniweb.UniwebHomePage", "uniweb.UniwebFlexiblePage"],
)
class Meta:
label = "stránka"
class SubpageMixin:
"""Must be used in class definition before MetadataPageMixin!"""
@property
def root_page(self):
if not hasattr(self, "_root_page"):
self._root_page = self.get_ancestors().type(UniwebHomePage).specific().get()
return self._root_page
def get_meta_image(self):
return self.search_image or self.root_page.get_meta_image()
class UniwebHomePage(MetadataPageMixin, Page):
### FIELDS
content = StreamField(
[
("title", blocks.CharBlock(label="nadpis", icon="title")),
("text", blocks.RichTextBlock(label="text", features=RICH_TEXT_FEATURES)),
("text_columns", ColumnsTextBlock()),
(
"gallery",
blocks.ListBlock(
ImageChooserBlock(label="obrázek"), label="galerie", icon="image"
),
),
(
"table",
TableBlock(label="tabulka", template="uniweb/snippet_table.html",),
),
],
verbose_name="obsah stránky",
blank=True,
)
# settings
matomo_id = models.IntegerField(
"Matomo ID pro sledování návštěvnosti", blank=True, null=True
)
top_menu = StreamField(
[("item", MenuItemBlock())], verbose_name="horní menu", blank=True,
)
### PANELS
content_panels = Page.content_panels + [
StreamFieldPanel("content"),
]
promote_panels = [
MultiFieldPanel(
[
FieldPanel("seo_title"),
FieldPanel("search_description"),
ImageChooserPanel("search_image"),
HelpPanel(help.build(help.IMPORTANT_TITLE)),
],
gettext_lazy("Common page configuration"),
),
]
settings_panels = [
FieldPanel("matomo_id"),
StreamFieldPanel("top_menu"),
]
### RELATIONS
subpage_types = [
"uniweb.UniwebFlexiblePage",
]
### OTHERS
class Meta:
verbose_name = "Univerzální web"
@property
def root_page(self):
return self
class UniwebFlexiblePage(Page, SubpageMixin, MetadataPageMixin):
### FIELDS
content = StreamField(
[
("title", blocks.CharBlock(label="nadpis", icon="title")),
("text", blocks.RichTextBlock(label="text", features=RICH_TEXT_FEATURES)),
("text_columns", ColumnsTextBlock()),
(
"gallery",
blocks.ListBlock(
ImageChooserBlock(label="obrázek"), label="galerie", icon="image"
),
),
(
"table",
TableBlock(label="tabulka", template="uniweb/snippet_table.html",),
),
("jupyter", DocumentChooserBlock(label="Jupyter notebook")),
],
verbose_name="obsah stránky",
blank=True,
)
### PANELS
promote_panels = [
MultiFieldPanel(
[
FieldPanel("slug"),
FieldPanel("seo_title"),
FieldPanel("search_description"),
ImageChooserPanel("search_image"),
HelpPanel(help.build(help.NO_SEO_TITLE, help.NO_SEARCH_IMAGE)),
],
gettext_lazy("Common page configuration"),
),
]
content_panels = Page.content_panels + [
StreamFieldPanel("content"),
]
settings_panels = []
### RELATIONS
parent_page_types = ["uniweb.UniwebHomePage"]
subpage_types = []
### OTHERS
class Meta:
verbose_name = "Flexibilní stránka"