From 9b9500566924e7195eca6004ea7927dff45bbd05 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 01:07:52 +0100
Subject: [PATCH] uniweb: Colored content blocks

---
 uniweb/models.py                              | 72 +++++++++++++++++++
 .../uniweb/blocks/advanced_text.html          |  4 ++
 .../uniweb/blocks/advanced_text_columns.html  |  9 +++
 .../uniweb/blocks/advanced_title.html         |  1 +
 .../templates/uniweb/blocks/text_columns.html |  9 +++
 uniweb/templates/uniweb/snippet_sections.html | 31 ++++----
 6 files changed, 113 insertions(+), 13 deletions(-)
 create mode 100644 uniweb/templates/uniweb/blocks/advanced_text.html
 create mode 100644 uniweb/templates/uniweb/blocks/advanced_text_columns.html
 create mode 100644 uniweb/templates/uniweb/blocks/advanced_title.html
 create mode 100644 uniweb/templates/uniweb/blocks/text_columns.html

diff --git a/uniweb/models.py b/uniweb/models.py
index ae201d1d..e9bcf7c3 100644
--- a/uniweb/models.py
+++ b/uniweb/models.py
@@ -37,6 +37,44 @@ RICH_TEXT_FEATURES = [
 ]
 
 
+BLACK_ON_WHITE = "black_on_white"
+WHITE_ON_BLACK = "white_on_black"
+WHITE_ON_BLUE = "white_on_blue"
+WHITE_ON_CYAN = "white_on_cyan"
+WHITE_ON_VIOLET = "white_on_violet"
+
+COLOR_CHOICES = (
+    (BLACK_ON_WHITE, "černá na bílé"),
+    (WHITE_ON_BLACK, "bílá na černé"),
+    (WHITE_ON_BLUE, "bílá na modré"),
+    (WHITE_ON_CYAN, "bílá na tyrkysové"),
+    (WHITE_ON_VIOLET, "bílá na fialové"),
+)
+
+COLOR_CSS = {
+    BLACK_ON_WHITE: "",
+    WHITE_ON_BLACK: "text-white bg-black",
+    WHITE_ON_BLUE: "text-white bg-blue-300",
+    WHITE_ON_CYAN: "text-white bg-cyan-300",
+    WHITE_ON_VIOLET: "text-white bg-violet-300",
+}
+
+
+class ColorBlock(blocks.StructBlock):
+    """
+    Intended as parent class for blocks with color option.
+    """
+
+    color = blocks.ChoiceBlock(
+        label="barva", choices=COLOR_CHOICES, default=BLACK_ON_WHITE
+    )
+
+    def get_context(self, value, parent_context=None):
+        context = super().get_context(value, parent_context=parent_context)
+        context["css_class"] = COLOR_CSS[value["color"]]
+        return context
+
+
 class ColumnsTextBlock(blocks.StructBlock):
     left_text = blocks.RichTextBlock(label="levĂ˝ sloupec", features=RICH_TEXT_FEATURES)
     right_text = blocks.RichTextBlock(
@@ -46,6 +84,37 @@ class ColumnsTextBlock(blocks.StructBlock):
     class Meta:
         label = "text dva sloupce"
         icon = "doc-full"
+        template = "uniweb/blocks/text_columns.html"
+
+
+class AdvancedColumnsTextBlock(ColorBlock):
+    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 (pokroÄŤilĂ˝)"
+        icon = "doc-full"
+        template = "uniweb/blocks/advanced_text_columns.html"
+
+
+class AdvancedTitleBlock(ColorBlock):
+    title = blocks.CharBlock(label="nadpis")
+
+    class Meta:
+        label = "nadpis (pokroÄŤilĂ˝)"
+        icon = "title"
+        template = "uniweb/blocks/advanced_title.html"
+
+
+class AdvancedTextBlock(ColorBlock):
+    text = blocks.RichTextBlock(label="text", features=RICH_TEXT_FEATURES)
+
+    class Meta:
+        label = "text (pokroÄŤilĂ˝)"
+        icon = "doc-full"
+        template = "uniweb/blocks/advanced_text.html"
 
 
 class MenuItemBlock(blocks.StructBlock):
@@ -76,8 +145,11 @@ class UniwebContentMixin(models.Model):
     content = StreamField(
         [
             ("title", blocks.CharBlock(label="nadpis", icon="title")),
+            ("advanced_title", AdvancedTitleBlock()),
             ("text", blocks.RichTextBlock(label="text", features=RICH_TEXT_FEATURES)),
+            ("advanced_text", AdvancedTextBlock()),
             ("text_columns", ColumnsTextBlock()),
+            ("advanced_text_columns", AdvancedColumnsTextBlock()),
             (
                 "gallery",
                 blocks.ListBlock(
diff --git a/uniweb/templates/uniweb/blocks/advanced_text.html b/uniweb/templates/uniweb/blocks/advanced_text.html
new file mode 100644
index 00000000..87f05325
--- /dev/null
+++ b/uniweb/templates/uniweb/blocks/advanced_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 %} {{ css_class }}">
+  {{ block.value.text|richtext }}
+</div>
diff --git a/uniweb/templates/uniweb/blocks/advanced_text_columns.html b/uniweb/templates/uniweb/blocks/advanced_text_columns.html
new file mode 100644
index 00000000..46169330
--- /dev/null
+++ b/uniweb/templates/uniweb/blocks/advanced_text_columns.html
@@ -0,0 +1,9 @@
+{% load wagtailcore_tags %}
+<div class="lg:flex clearfix">
+  <div class="content-block lg:w-1/2 lg:pr-5 px-4 py-2{% if first %} mt-8 lg:mt-12{% endif %} {{ css_class }}">
+    {{ block.value.left_text|richtext }}
+  </div>
+  <div class="content-block lg:w-1/2 lg:pl-5 px-4 py-2{% if first %} mt-8 lg:mt-12{% endif %} {{ css_class }}">
+    {{ block.value.right_text|richtext }}
+  </div>
+</div>
diff --git a/uniweb/templates/uniweb/blocks/advanced_title.html b/uniweb/templates/uniweb/blocks/advanced_title.html
new file mode 100644
index 00000000..395940b0
--- /dev/null
+++ b/uniweb/templates/uniweb/blocks/advanced_title.html
@@ -0,0 +1 @@
+<h1 class="head-alt-md md:head-alt-lg px-4 pt-6 pb-4 lg:mt-16 {{ css_class }}">{{ block.value.title }}</h1>
diff --git a/uniweb/templates/uniweb/blocks/text_columns.html b/uniweb/templates/uniweb/blocks/text_columns.html
new file mode 100644
index 00000000..cbb8962c
--- /dev/null
+++ b/uniweb/templates/uniweb/blocks/text_columns.html
@@ -0,0 +1,9 @@
+{% load wagtailcore_tags %}
+<div class="lg:flex clearfix">
+  <div class="content-block lg:w-1/2 lg:pr-5 px-4 py-2{% if first %} mt-8 lg:mt-12{% endif %}">
+    {{ block.value.left_text|richtext }}
+  </div>
+  <div class="content-block lg:w-1/2 lg:pl-5 px-4 py-2{% if first %} mt-8 lg:mt-12{% endif %}">
+    {{ block.value.right_text|richtext }}
+  </div>
+</div>
diff --git a/uniweb/templates/uniweb/snippet_sections.html b/uniweb/templates/uniweb/snippet_sections.html
index 63fb8dae..74f92f66 100644
--- a/uniweb/templates/uniweb/snippet_sections.html
+++ b/uniweb/templates/uniweb/snippet_sections.html
@@ -3,24 +3,29 @@
 <section class="mb-8 lg:mb-16">
 {% for block in page.content %}
 
-  {% if block.block_type == "title" %}
-    <h1 class="head-alt-md md:head-alt-lg max-w-5xl my-6 lg:mt-16">{{ block.value }}</h1>
+  {% if block.block_type == "advanced_title" %}
+    {% include_block block %}
   {% endif %}
 
-  {% if block.block_type == "text" %}
-  <div class="content-block my-4 clearfix{% if forloop.first %} mt-8 lg:mt-12{% endif %}">
-    {{ block.value|richtext }}
-  </div>
+  {% if block.block_type == "advanced_text" %}
+    {% include_block block with first=forloop.first %}
   {% endif %}
 
   {% if block.block_type == "text_columns" %}
-  <div class="lg:flex clearfix">
-    <div class="content-block lg:w-1/2 lg:pr-5 my-4{% if forloop.first %} mt-8 lg:mt-12{% endif %}">
-      {{ block.value.left_text|richtext }}
-    </div>
-    <div class="content-block lg:w-1/2 lg:pl-5 my-4{% if forloop.first %} mt-8 lg:mt-12{% endif %}">
-      {{ block.value.right_text|richtext }}
-    </div>
+    {% 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-6 pb-4 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 %}
 
-- 
GitLab