From a7bad9aa3be193219e2055c14ce5f46a200ff3b5 Mon Sep 17 00:00:00 2001
From: "Edgar P. Burkhart" <git@edgarpierre.fr>
Date: Wed, 12 Jun 2024 18:47:41 +0200
Subject: [PATCH] Add blog

---
 lps/blog/__init__.py                         |  0
 lps/blog/apps.py                             |  6 +++
 lps/blog/migrations/0001_initial.py          | 57 ++++++++++++++++++++
 lps/blog/migrations/0002_blogindexpage.py    | 50 +++++++++++++++++
 lps/blog/migrations/__init__.py              |  0
 lps/blog/models.py                           | 54 +++++++++++++++++++
 lps/blog/templates/blog/blog_index_page.html | 34 ++++++++++++
 lps/blog/templates/blog/blog_page.html       |  6 +++
 lps/lps/settings/base.py                     |  1 +
 lps/lps/static/css/lps.css                   |  5 ++
 lps/lps/templates/base.html                  |  8 +--
 11 files changed, 217 insertions(+), 4 deletions(-)
 create mode 100644 lps/blog/__init__.py
 create mode 100644 lps/blog/apps.py
 create mode 100644 lps/blog/migrations/0001_initial.py
 create mode 100644 lps/blog/migrations/0002_blogindexpage.py
 create mode 100644 lps/blog/migrations/__init__.py
 create mode 100644 lps/blog/models.py
 create mode 100644 lps/blog/templates/blog/blog_index_page.html
 create mode 100644 lps/blog/templates/blog/blog_page.html

diff --git a/lps/blog/__init__.py b/lps/blog/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/lps/blog/apps.py b/lps/blog/apps.py
new file mode 100644
index 0000000..6be26c7
--- /dev/null
+++ b/lps/blog/apps.py
@@ -0,0 +1,6 @@
+from django.apps import AppConfig
+
+
+class BlogConfig(AppConfig):
+    default_auto_field = "django.db.models.BigAutoField"
+    name = "blog"
diff --git a/lps/blog/migrations/0001_initial.py b/lps/blog/migrations/0001_initial.py
new file mode 100644
index 0000000..5a49410
--- /dev/null
+++ b/lps/blog/migrations/0001_initial.py
@@ -0,0 +1,57 @@
+# Generated by Django 5.0.6 on 2024-06-12 16:15
+
+import django.db.models.deletion
+import wagtail.fields
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+    initial = True
+
+    dependencies = [
+        ("wagtailcore", "0093_uploadedfile"),
+        ("wagtailimages", "0026_delete_uploadedimage"),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name="BlogPage",
+            fields=[
+                (
+                    "page_ptr",
+                    models.OneToOneField(
+                        auto_created=True,
+                        on_delete=django.db.models.deletion.CASCADE,
+                        parent_link=True,
+                        primary_key=True,
+                        serialize=False,
+                        to="wagtailcore.page",
+                    ),
+                ),
+                (
+                    "introduction",
+                    models.TextField(blank=True, help_text="Introduction de l'article"),
+                ),
+                ("body", wagtail.fields.RichTextField(blank=True)),
+                (
+                    "date_published",
+                    models.DateField(verbose_name="Date de publication de l'article"),
+                ),
+                (
+                    "image",
+                    models.ForeignKey(
+                        blank=True,
+                        help_text="Image de l'article",
+                        null=True,
+                        on_delete=django.db.models.deletion.SET_NULL,
+                        related_name="+",
+                        to="wagtailimages.image",
+                    ),
+                ),
+            ],
+            options={
+                "abstract": False,
+            },
+            bases=("wagtailcore.page",),
+        ),
+    ]
diff --git a/lps/blog/migrations/0002_blogindexpage.py b/lps/blog/migrations/0002_blogindexpage.py
new file mode 100644
index 0000000..e64f691
--- /dev/null
+++ b/lps/blog/migrations/0002_blogindexpage.py
@@ -0,0 +1,50 @@
+# Generated by Django 5.0.6 on 2024-06-12 16:25
+
+import django.db.models.deletion
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+    dependencies = [
+        ("blog", "0001_initial"),
+        ("wagtailcore", "0093_uploadedfile"),
+        ("wagtailimages", "0026_delete_uploadedimage"),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name="BlogIndexPage",
+            fields=[
+                (
+                    "page_ptr",
+                    models.OneToOneField(
+                        auto_created=True,
+                        on_delete=django.db.models.deletion.CASCADE,
+                        parent_link=True,
+                        primary_key=True,
+                        serialize=False,
+                        to="wagtailcore.page",
+                    ),
+                ),
+                (
+                    "introduction",
+                    models.TextField(blank=True, help_text="Description de la page"),
+                ),
+                (
+                    "image",
+                    models.ForeignKey(
+                        blank=True,
+                        help_text="Image du blog",
+                        null=True,
+                        on_delete=django.db.models.deletion.SET_NULL,
+                        related_name="+",
+                        to="wagtailimages.image",
+                    ),
+                ),
+            ],
+            options={
+                "abstract": False,
+            },
+            bases=("wagtailcore.page",),
+        ),
+    ]
diff --git a/lps/blog/migrations/__init__.py b/lps/blog/migrations/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/lps/blog/models.py b/lps/blog/models.py
new file mode 100644
index 0000000..6637542
--- /dev/null
+++ b/lps/blog/models.py
@@ -0,0 +1,54 @@
+from django.db import models
+from wagtail.admin.panels import FieldPanel
+from wagtail.fields import RichTextField
+from wagtail.models import Page
+
+
+class BlogPage(Page):
+    introduction = models.TextField(help_text="Introduction de l'article", blank=True)
+    image = models.ForeignKey(
+        "wagtailimages.Image",
+        null=True,
+        blank=True,
+        on_delete=models.SET_NULL,
+        related_name="+",
+        help_text="Image de l'article",
+    )
+    body = RichTextField(blank=True)
+    date_published = models.DateField("Date de publication de l'article")
+
+    content_panels = Page.content_panels + [
+        FieldPanel("introduction"),
+        FieldPanel("image"),
+        FieldPanel("body"),
+        FieldPanel("date_published"),
+    ]
+
+
+class BlogIndexPage(Page):
+    introduction = models.TextField(help_text="Description de la page", blank=True)
+    image = models.ForeignKey(
+        "wagtailimages.Image",
+        null=True,
+        blank=True,
+        on_delete=models.SET_NULL,
+        related_name="+",
+        help_text="Image du blog",
+    )
+
+    content_panels = Page.content_panels + [
+        FieldPanel("introduction"),
+        FieldPanel("image"),
+    ]
+
+    subpage_types = ["BlogPage"]
+
+    def children(self):
+        return self.get_children().specific().live()
+
+    def get_context(self, request):
+        context = super().get_context(request)
+        context["posts"] = (
+            BlogPage.objects.descendant_of(self).live().order_by("-date_published")
+        )
+        return context
diff --git a/lps/blog/templates/blog/blog_index_page.html b/lps/blog/templates/blog/blog_index_page.html
new file mode 100644
index 0000000..033ed49
--- /dev/null
+++ b/lps/blog/templates/blog/blog_index_page.html
@@ -0,0 +1,34 @@
+{% extends "base.html" %}
+{% load wagtailcore_tags navigation_tags wagtailimages_tags %}
+{% block content %}
+    <h1>{{ page.title }}</h1>
+    {% if page.introduction or page.image %}
+        <section>
+            {% if page.introduction %}<p>{{ page.introduction }}</p>{% endif %}
+            {% if page.image %}
+                {% srcset_image page.image width-{510,700,950,1200,1450} %}
+            {% endif %}
+        </section>
+    {% endif %}
+    <section class="grid blog-grid">
+        {% for blog in posts %}
+            <div>
+                <article>
+                    <header>
+                        <strong><a href="{% pageurl blog %}">{{ blog.title }}</a></strong>
+                    </header>
+                    {% if blog.image %}
+                        {% srcset_image blog.image width-{510,700,950,1200,1450} %}
+                    {% else %}
+                        {{ blog.introduction }}
+                    {% endif %}
+                    <footer>
+                        {{ blog.date_published }}
+                    </footer>
+                </article>
+            </div>
+        {% empty %}
+            Aucun article n'a été publié ici pour le moment.
+        {% endfor %}
+    </section>
+{% endblock %}
diff --git a/lps/blog/templates/blog/blog_page.html b/lps/blog/templates/blog/blog_page.html
new file mode 100644
index 0000000..06247fd
--- /dev/null
+++ b/lps/blog/templates/blog/blog_page.html
@@ -0,0 +1,6 @@
+{% extends "base.html" %}
+{% load wagtailcore_tags wagtailimages_tags %}
+{% block content %}
+    <h1>{{ page.title }}</h1>
+    {{ page.body|richtext }}
+{% endblock %}
diff --git a/lps/lps/settings/base.py b/lps/lps/settings/base.py
index dc6a92f..1252ed5 100644
--- a/lps/lps/settings/base.py
+++ b/lps/lps/settings/base.py
@@ -26,6 +26,7 @@ BASE_DIR = os.path.dirname(PROJECT_DIR)
 INSTALLED_APPS = [
     "base",
     "home",
+    "blog",
     "search",
     "wagtail.contrib.forms",
     "wagtail.contrib.redirects",
diff --git a/lps/lps/static/css/lps.css b/lps/lps/static/css/lps.css
index f49fa54..fb0ec77 100644
--- a/lps/lps/static/css/lps.css
+++ b/lps/lps/static/css/lps.css
@@ -9,3 +9,8 @@ h1, h2, h3, h4, h5, h6 {
 img {
 	width: 100%;
 }
+
+.blog-grid {
+	display: grid;
+	grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
+}
diff --git a/lps/lps/templates/base.html b/lps/lps/templates/base.html
index 6cbc952..de4d820 100644
--- a/lps/lps/templates/base.html
+++ b/lps/lps/templates/base.html
@@ -25,7 +25,7 @@
         <link rel="stylesheet" href="https://rsms.me/inter/inter.css">
         <link rel="preconnect" href="https://cdn.jsdelivr.net/">
         <link rel="stylesheet"
-              href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.classless.min.css" />
+              href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.jade.min.css" />
         <link href="https://cdn.jsdelivr.net/npm/remixicon@4.2.0/fonts/remixicon.css"
               rel="stylesheet" />
         <link rel="stylesheet" type="text/css" href="{% static 'css/lps.css' %}">
@@ -33,13 +33,13 @@
     </head>
     <body>
         {% wagtailuserbar %}
-        <header>
+        <header class="container">
             {% include "nav.html" %}
         </header>
-        <main>
+        <main class="container">
             {% block content %}{% endblock %}
         </main>
-        <footer>
+        <footer class="container">
             {% include "footer.html" %}
         </footer>
         {# Global javascript #}
