From 847006494a0c8ac9179e214b2165761a4468fbe1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Bedna=C5=99=C3=ADk?= <jan.bednarik@gmail.com>
Date: Wed, 17 Feb 2021 15:59:03 +0100
Subject: [PATCH] uniweb: Article tags

---
 uniweb/migrations/0014_auto_20210217_1316.py  | 60 +++++++++++++++++++
 uniweb/models.py                              | 42 +++++++++++--
 .../templates/uniweb/uniweb_article_page.html | 20 +++----
 .../uniweb/uniweb_articles_index_page.html    | 29 +++++----
 4 files changed, 119 insertions(+), 32 deletions(-)
 create mode 100644 uniweb/migrations/0014_auto_20210217_1316.py

diff --git a/uniweb/migrations/0014_auto_20210217_1316.py b/uniweb/migrations/0014_auto_20210217_1316.py
new file mode 100644
index 00000000..234941cb
--- /dev/null
+++ b/uniweb/migrations/0014_auto_20210217_1316.py
@@ -0,0 +1,60 @@
+# Generated by Django 3.1.6 on 2021-02-17 12:16
+
+import django.db.models.deletion
+import modelcluster.contrib.taggit
+import modelcluster.fields
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ("taggit", "0003_taggeditem_add_unique_index"),
+        ("uniweb", "0013_auto_20210217_1130"),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name="UniwebArticleTag",
+            fields=[
+                (
+                    "id",
+                    models.AutoField(
+                        auto_created=True,
+                        primary_key=True,
+                        serialize=False,
+                        verbose_name="ID",
+                    ),
+                ),
+                (
+                    "content_object",
+                    modelcluster.fields.ParentalKey(
+                        on_delete=django.db.models.deletion.CASCADE,
+                        to="uniweb.uniwebarticlepage",
+                    ),
+                ),
+                (
+                    "tag",
+                    models.ForeignKey(
+                        on_delete=django.db.models.deletion.CASCADE,
+                        related_name="uniweb_uniwebarticletag_items",
+                        to="taggit.tag",
+                    ),
+                ),
+            ],
+            options={
+                "abstract": False,
+            },
+        ),
+        migrations.AddField(
+            model_name="uniwebarticlepage",
+            name="tags",
+            field=modelcluster.contrib.taggit.ClusterTaggableManager(
+                blank=True,
+                help_text="A comma-separated list of tags.",
+                through="uniweb.UniwebArticleTag",
+                to="taggit.Tag",
+                verbose_name="Tags",
+            ),
+        ),
+    ]
diff --git a/uniweb/models.py b/uniweb/models.py
index e0601fc8..d63994b6 100644
--- a/uniweb/models.py
+++ b/uniweb/models.py
@@ -2,12 +2,17 @@ from django import forms
 from django.core.paginator import Paginator
 from django.db import models
 from django.utils.translation import gettext_lazy
+from modelcluster.contrib.taggit import ClusterTaggableManager
+from modelcluster.fields import ParentalKey
+from taggit.models import TaggedItemBase
 from wagtail.admin.edit_handlers import (
     FieldPanel,
     HelpPanel,
     MultiFieldPanel,
+    ObjectList,
     PublishingPanel,
     StreamFieldPanel,
+    TabbedInterface,
 )
 from wagtail.contrib.table_block.blocks import TableBlock
 from wagtail.core import blocks
@@ -242,6 +247,10 @@ class UniwebContentMixin(models.Model):
         abstract = True
 
 
+class UniwebArticleTag(TaggedItemBase):
+    content_object = ParentalKey("uniweb.UniwebArticlePage", on_delete=models.CASCADE)
+
+
 class UniwebHomePage(Page, UniwebContentMixin, MetadataPageMixin):
     ### FIELDS
 
@@ -285,10 +294,22 @@ class UniwebHomePage(Page, UniwebContentMixin, MetadataPageMixin):
                 FieldPanel("narrow_layout"),
             ],
             "nastavenĂ­ webu",
-        ),
-        StreamFieldPanel("top_menu"),
+        )
     ]
 
+    menu_panels = [StreamFieldPanel("top_menu")]
+
+    edit_handler = TabbedInterface(
+        [
+            ObjectList(content_panels, heading=gettext_lazy("Content")),
+            ObjectList(promote_panels, heading=gettext_lazy("Promote")),
+            ObjectList(
+                settings_panels, heading=gettext_lazy("Settings"), classname="settings"
+            ),
+            ObjectList(menu_panels, heading="Menu"),
+        ]
+    )
+
     ### RELATIONS
 
     subpage_types = [
@@ -332,8 +353,8 @@ class UniwebFlexiblePage(Page, UniwebContentMixin, SubpageMixin, MetadataPageMix
 
     ### RELATIONS
 
-    parent_page_types = ["uniweb.UniwebHomePage"]
-    subpage_types = []
+    parent_page_types = ["uniweb.UniwebHomePage", "uniweb.UniwebFlexiblePage"]
+    subpage_types = ["uniweb.UniwebFlexiblePage"]
 
     ### OTHERS
 
@@ -373,20 +394,29 @@ class UniwebArticlesIndexPage(Page, SubpageMixin, MetadataPageMixin):
 
     def get_context(self, request):
         context = super().get_context(request)
+        num = request.GET.get("page")
+        tag = request.GET.get("tag")
+
         articles = (
             self.get_children().live().specific().order_by("-uniwebarticlepage__date")
         )
-        num = request.GET.get("page")
+        if tag is not None:
+            articles = articles.filter(uniwebarticlepage__tags__name=tag)
+
         context["articles"] = Paginator(articles, ARTICLES_PER_PAGE).get_page(num)
+        context["tags"] = UniwebArticleTag.tags_for(UniwebArticlePage)
+        context["active_tag"] = tag
         return context
 
 
 class UniwebArticlePage(Page, ArticleMixin, SubpageMixin, MetadataPageMixin):
     ### FIELDS
 
+    tags = ClusterTaggableManager(through=UniwebArticleTag, blank=True)
+
     ### PANELS
 
-    content_panels = ArticleMixin.content_panels
+    content_panels = ArticleMixin.content_panels + [FieldPanel("tags")]
 
     promote_panels = [
         MultiFieldPanel(
diff --git a/uniweb/templates/uniweb/uniweb_article_page.html b/uniweb/templates/uniweb/uniweb_article_page.html
index 8f779824..b96dc2ed 100644
--- a/uniweb/templates/uniweb/uniweb_article_page.html
+++ b/uniweb/templates/uniweb/uniweb_article_page.html
@@ -21,17 +21,15 @@
         {% endif %}
       </div>
 
-      {# TODO tags #}
-      {% comment %}
-      <div class="my-4">
-        <button class="btn btn--grey-125 btn--condensed">
-          <div class="btn__body ">Kategorie 1</div>
-        </button>
-        <button class="btn btn--grey-125 btn--condensed">
-          <div class="btn__body ">Kategorie 2</div>
-        </button>
-      </div>
-      {% endcomment %}
+      {% if page.has_tags %}
+        <div class="my-4">
+          {% for tag in page.tags.all %}
+            <a href="{% pageurl page.tag_filter_page %}?tag={{ tag }}" class="btn btn--grey-125 btn--condensed">
+              <div class="btn__body ">{{ tag }}</div>
+            </a>
+          {% endfor %}
+        </div>
+      {% endif %}
     </div>
 
     <figure class="figure">
diff --git a/uniweb/templates/uniweb/uniweb_articles_index_page.html b/uniweb/templates/uniweb/uniweb_articles_index_page.html
index 6ec837fc..cf06fcc1 100644
--- a/uniweb/templates/uniweb/uniweb_articles_index_page.html
+++ b/uniweb/templates/uniweb/uniweb_articles_index_page.html
@@ -1,27 +1,26 @@
 {% extends "uniweb/base.html" %}
+{% load wagtailcore_tags %}
 
 {% block content %}
 
 <section>
   <h1 class="head-alt-md md:head-alt-lg max-w-5xl mb-4">{{ page.title }}</h1>
 
-  {# TODO tags #}
-  {% comment %}
+  {% if tags %}
   <nav>
-    <button class="btn btn--grey-125 btn--condensed">
-      <div class="btn__body ">Zobrazit vše</div>
-    </button>
-    <button class="btn btn--grey-125 btn--condensed">
-      <div class="btn__body ">Kategorie 1</div>
-    </button>
-    <button class="btn btn--grey-125 btn--condensed">
-      <div class="btn__body ">Kategorie 2</div>
-    </button>
-    <button class="btn btn--grey-125 btn--condensed">
-      <div class="btn__body ">Kategorie 3</div>
-    </button>
+    {% if active_tag %}
+      <a href="{% pageurl page %}" class="btn btn--grey-125 btn--condensed">
+        <div class="btn__body ">zobrazit vše</div>
+      </a>
+    {% endif %}
+    {% for tag in tags %}
+      <a href="{% pageurl page %}?tag={{ tag }}"
+         class="btn {% if tag.name == active_tag %}btn--grey-500{% else %}btn--grey-125{% endif %} btn--condensed">
+        <div class="btn__body ">{{ tag }}</div>
+      </a>
+    {% endfor %}
   </nav>
-  {% endcomment %}
+  {% endif %}
 
   <hr>
 
-- 
GitLab