From 47392b29108f049c9310ca711d7843170217c3d5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Bedna=C5=99=C3=ADk?= <jan.bednarik@gmail.com>
Date: Thu, 26 Nov 2020 12:46:51 +0100
Subject: [PATCH] uniweb: Refactor templates

---
 uniweb/models.py                              | 50 +++++++++++++++++--
 uniweb/templates/uniweb/base.html             |  2 +-
 .../uniweb/blocks/advanced_title.html         |  2 +-
 uniweb/templates/uniweb/blocks/gallery.html   | 10 ++++
 .../{snippet_table.html => blocks/table.html} |  6 ++-
 uniweb/templates/uniweb/blocks/text.html      |  4 ++
 uniweb/templates/uniweb/blocks/title.html     |  1 +
 uniweb/templates/uniweb/snippet_sections.html | 46 +----------------
 8 files changed, 68 insertions(+), 53 deletions(-)
 create mode 100644 uniweb/templates/uniweb/blocks/gallery.html
 rename uniweb/templates/uniweb/{snippet_table.html => blocks/table.html} (93%)
 create mode 100644 uniweb/templates/uniweb/blocks/text.html
 create mode 100644 uniweb/templates/uniweb/blocks/title.html

diff --git a/uniweb/models.py b/uniweb/models.py
index 89e9992d..ecc8812a 100644
--- a/uniweb/models.py
+++ b/uniweb/models.py
@@ -70,6 +70,7 @@ class ColumnsTextBlock(blocks.StructBlock):
     class Meta:
         label = "text dva sloupce"
         icon = "doc-full"
+        group = "texty"
         template = "uniweb/blocks/text_columns.html"
 
 
@@ -82,6 +83,7 @@ class AdvancedColumnsTextBlock(ColorBlock, AlignBlock):
     class Meta:
         label = "text dva sloupce (pokroÄŤilĂ˝)"
         icon = "doc-full"
+        group = "texty"
         template = "uniweb/blocks/advanced_text_columns.html"
 
 
@@ -91,15 +93,28 @@ class AdvancedTitleBlock(ColorBlock, AlignBlock):
     class Meta:
         label = "nadpis (pokroÄŤilĂ˝)"
         icon = "title"
+        group = "nadpisy"
         template = "uniweb/blocks/advanced_title.html"
 
 
+class PictureTitleBlock(ColorBlock):
+    title = blocks.CharBlock(label="nadpis")
+    image = ImageChooserBlock(label="obrázek")
+
+    class Meta:
+        label = "nadpis (s obrázkem)"
+        icon = "title"
+        group = "nadpisy"
+        template = "uniweb/blocks/picture_title.html"
+
+
 class AdvancedTextBlock(ColorBlock, AlignBlock):
     text = blocks.RichTextBlock(label="text", features=RICH_TEXT_FEATURES)
 
     class Meta:
         label = "text (pokroÄŤilĂ˝)"
         icon = "doc-full"
+        group = "texty"
         template = "uniweb/blocks/advanced_text.html"
 
 
@@ -130,26 +145,51 @@ class SubpageMixin:
 class UniwebContentMixin(models.Model):
     content = StreamField(
         [
-            ("title", blocks.CharBlock(label="nadpis", icon="title")),
+            (
+                "title",
+                blocks.CharBlock(
+                    label="nadpis",
+                    icon="title",
+                    group="nadpisy",
+                    template="uniweb/blocks/title.html",
+                ),
+            ),
             ("advanced_title", AdvancedTitleBlock()),
-            ("text", blocks.RichTextBlock(label="text", features=RICH_TEXT_FEATURES)),
+            ("picture_title", PictureTitleBlock()),
+            (
+                "text",
+                blocks.RichTextBlock(
+                    label="text",
+                    features=RICH_TEXT_FEATURES,
+                    group="texty",
+                    template="uniweb/blocks/text.html",
+                ),
+            ),
             ("advanced_text", AdvancedTextBlock()),
             ("text_columns", ColumnsTextBlock()),
             ("advanced_text_columns", AdvancedColumnsTextBlock()),
             (
                 "gallery",
                 blocks.ListBlock(
-                    ImageChooserBlock(label="obrázek"), label="galerie", icon="image"
+                    ImageChooserBlock(label="obrázek"),
+                    label="galerie",
+                    icon="image",
+                    group="ostatnĂ­",
+                    template="uniweb/blocks/gallery.html",
                 ),
             ),
             (
                 "table",
                 TableBlock(
                     label="tabulka",
-                    template="uniweb/snippet_table.html",
+                    group="ostatnĂ­",
+                    template="uniweb/blocks/table.html",
                 ),
             ),
-            ("jupyter", DocumentChooserBlock(label="Jupyter notebook")),
+            (
+                "jupyter",
+                DocumentChooserBlock(label="Jupyter notebook", group="ostatnĂ­"),
+            ),
         ],
         verbose_name="obsah stránky",
         blank=True,
diff --git a/uniweb/templates/uniweb/base.html b/uniweb/templates/uniweb/base.html
index f27f5fc0..cdb254e7 100644
--- a/uniweb/templates/uniweb/base.html
+++ b/uniweb/templates/uniweb/base.html
@@ -22,7 +22,7 @@
     .inline-block {
       display: inline-block;
     }
-    .leading-tight {
+    .head-alt-md, .head-alt-lg {
       line-height: 1.25;
     }
   </style>
diff --git a/uniweb/templates/uniweb/blocks/advanced_title.html b/uniweb/templates/uniweb/blocks/advanced_title.html
index 844e96f3..1a5fb116 100644
--- a/uniweb/templates/uniweb/blocks/advanced_title.html
+++ b/uniweb/templates/uniweb/blocks/advanced_title.html
@@ -1 +1 @@
-<h1 class="head-alt-md md:head-alt-lg px-4 pt-4 pb-2 lg:mt-16 leading-tight {{ css_class|join:" " }}">{{ block.value.title }}</h1>
+<h1 class="head-alt-md md:head-alt-lg px-4 pt-4 pb-2 lg:mt-16 {{ css_class|join:" " }}">{{ block.value.title }}</h1>
diff --git a/uniweb/templates/uniweb/blocks/gallery.html b/uniweb/templates/uniweb/blocks/gallery.html
new file mode 100644
index 00000000..3cf2f1ac
--- /dev/null
+++ b/uniweb/templates/uniweb/blocks/gallery.html
@@ -0,0 +1,10 @@
+{% load wagtailimages_tags %}
+<div class="content-block w-full my-6 grid grid-cols-4 gap-4{% if first %} mt-8 lg:mt-12{% endif %}">
+  {% for picture in block.value %}
+    {% image picture width-2000 as img %}
+    {% image picture fill-300x200 as thumb %}
+    <div>
+      <a data-fancybox="gallery" href="{{ img.url }}"><img data-src="{{ thumb.url }}" class="lazyload img-fluid" alt="{{ thumb.alt }}"></a>
+    </div>
+  {% endfor %}
+</div>
diff --git a/uniweb/templates/uniweb/snippet_table.html b/uniweb/templates/uniweb/blocks/table.html
similarity index 93%
rename from uniweb/templates/uniweb/snippet_table.html
rename to uniweb/templates/uniweb/blocks/table.html
index eb2061b7..e2ccf4a6 100644
--- a/uniweb/templates/uniweb/snippet_table.html
+++ b/uniweb/templates/uniweb/blocks/table.html
@@ -1,6 +1,7 @@
 {% load table_block_tags %}
 
-<table class="table table--bordered table--condensed mx-auto">
+<div class="my-6{% if first %} mt-8 lg:mt-12{% endif %}">
+  <table class="table table--bordered table--condensed mx-auto">
     {% if table_caption %}
        <caption class="head-heavy-sm my-4">{{ table_caption }}</caption>
     {% endif %}
@@ -56,4 +57,5 @@
     {% endwith %}
     {% endfor %}
     </tbody>
-</table>
+  </table>
+</div>
diff --git a/uniweb/templates/uniweb/blocks/text.html b/uniweb/templates/uniweb/blocks/text.html
new file mode 100644
index 00000000..fe369c3a
--- /dev/null
+++ b/uniweb/templates/uniweb/blocks/text.html
@@ -0,0 +1,4 @@
+{% load wagtailcore_tags %}
+<div class="content-block px-4 py-2 clearfix{% if first %} mt-8 lg:mt-12{% endif %}">
+  {{ block.value|richtext }}
+</div>
diff --git a/uniweb/templates/uniweb/blocks/title.html b/uniweb/templates/uniweb/blocks/title.html
new file mode 100644
index 00000000..a09530cd
--- /dev/null
+++ b/uniweb/templates/uniweb/blocks/title.html
@@ -0,0 +1 @@
+<h1 class="head-alt-md md:head-alt-lg px-4 pt-4 pb-2 lg:mt-16">{{ block.value }}</h1>
diff --git a/uniweb/templates/uniweb/snippet_sections.html b/uniweb/templates/uniweb/snippet_sections.html
index 5e8f817a..20fe13e6 100644
--- a/uniweb/templates/uniweb/snippet_sections.html
+++ b/uniweb/templates/uniweb/snippet_sections.html
@@ -3,54 +3,12 @@
 <section class="mb-8 lg:mb-16">
 {% for block in page.content %}
 
-  {% if block.block_type == "advanced_title" %}
-    {% include_block block %}
-  {% endif %}
-
-  {% if block.block_type == "advanced_text" %}
-    {% include_block block with first=forloop.first %}
-  {% endif %}
-
-  {% if block.block_type == "text_columns" %}
-    {% include_block block with first=forloop.first %}
-  {% endif %}
-
-  {% if block.block_type == "advanced_text_columns" %}
-    {% include_block block with first=forloop.first %}
-  {% endif %}
-
-  {% if block.block_type == "title" %}
-    <h1 class="head-alt-md md:head-alt-lg px-4 pt-4 pb-2 leading-tight lg:mt-16">{{ block.value }}</h1>
-  {% endif %}
-
-  {% if block.block_type == "text" %}
-  <div class="content-block px-4 py-2 clearfix{% if forloop.first %} mt-8 lg:mt-12{% endif %}">
-    {{ block.value|richtext }}
-  </div>
-  {% endif %}
-
-  {% if block.block_type == "gallery" %}
-  <div class="content-block w-full my-6 grid grid-cols-4 gap-4{% if forloop.first %} mt-8 lg:mt-12{% endif %}">
-    {% for picture in block.value %}
-      {% image picture width-2000 as img %}
-      {% image picture fill-300x200 as thumb %}
-      <div>
-        <a data-fancybox="gallery" href="{{ img.url }}"><img data-src="{{ thumb.url }}" class="lazyload img-fluid" alt="{{ thumb.alt }}"></a>
-      </div>
-    {% endfor %}
-  </div>
-  {% endif %}
-
-  {% if block.block_type == "table" %}
-  <div class="my-6{% if forloop.first %} mt-8 lg:mt-12{% endif %}">
-    {% include_block block %}
-  </div>
-  {% endif %}
-
   {% if block.block_type == "jupyter" %}
   <div class="content-block my-4 clearfix{% if forloop.first %} mt-8 lg:mt-12{% endif %}">
     {{ block|jupyterize }}
   </div>
+  {% else %}
+    {% include_block block with first=forloop.first %}
   {% endif %}
 
 {% endfor %}
-- 
GitLab