From 51848982a9747d0fc3c6a67910c699dfcef5acfb Mon Sep 17 00:00:00 2001
From: Ondrej Rehounek <ondra.rehounek@seznam.cz>
Date: Sun, 3 Apr 2022 09:52:29 +0200
Subject: [PATCH] district: Misc fixes

---
 district/forms.py         |  3 +--
 district/jekyll_import.py | 52 +++++++++++++++++++++++----------------
 district/models.py        |  3 ---
 3 files changed, 32 insertions(+), 26 deletions(-)

diff --git a/district/forms.py b/district/forms.py
index 13c0c657..d4e7108c 100644
--- a/district/forms.py
+++ b/district/forms.py
@@ -79,8 +79,7 @@ class JekyllImportForm(WagtailAdminPageForm):
         return cleaned_data
 
     def handle_import(self):
-        # import_jekyll_articles.delay(  # TODO
-        import_jekyll_articles(
+        import_jekyll_articles.delay(
             article_parent_page_id=self.instance.id,
             collection_id=self.cleaned_data["collection"].id,
             url=self.cleaned_data["jekyll_repo_url"],
diff --git a/district/jekyll_import.py b/district/jekyll_import.py
index 3e742fbf..1f083b29 100644
--- a/district/jekyll_import.py
+++ b/district/jekyll_import.py
@@ -1,6 +1,8 @@
 import logging
 import os
+import random
 import re
+import string
 import urllib
 import xml.etree.ElementTree as ET
 import zipfile
@@ -129,7 +131,11 @@ def get_or_create_image(
                 image.save()
                 return image, ""
             except FileNotFoundError:
-                img_name = file_path.split("/")[-1]
+                fallback_name = (
+                    "".join(random.choice(string.ascii_lowercase) for _ in range(10))
+                    + ".jpg"
+                )  # i PNG...?
+                img_name = file_path.split("/")[-1] or fallback_name
                 img_assets_folder = repo_name.split(".")[0]
                 img_url = "https://a.pirati.cz/{}/img/{}".format(
                     img_assets_folder, file_path.split("#")[0]
@@ -298,6 +304,27 @@ class JekyllArticleImporter:
         image_params["collection"] = self.collection
         image_params["repo_name"] = self.repo_name
 
+    def create_redirects(self, article, match):
+        y = match.group(1)
+        m = match.group(2)
+        d = match.group(3)
+        slug = match.group(4)
+
+        if article and self.article_path:  # asi jenom Ceske Budejovice
+            Redirect.objects.get_or_create(
+                site=self.site,
+                old_path="/%s/%s/%s/%s/%s"
+                % (self.article_path, y, m.zfill(2), d.zfill(2), slug),
+                defaults={"is_permanent": True, "redirect_page": article},
+            )
+
+        elif article and self.permalink:
+            Redirect.objects.get_or_create(
+                site=self.site,
+                old_path=self.permalink.replace(":title", slug),
+                defaults={"is_permanent": True, "redirect_page": article},
+            )
+
     def create_summary_log(self):
         """
         Podle (aktuálních) hodnot counterů přidá do self.page_log
@@ -408,7 +435,7 @@ class JekyllArticleImporter:
 
         # article.text = html
         article.content = [("text", RichText(html))]
-        article.perex = self.get_perex(md)
+        article.perex = self.get_perex(md) or "..."
 
         if meta.get("date", None):
             meta_date = meta["date"]
@@ -485,10 +512,6 @@ class JekyllArticleImporter:
     def process_article(self, file_name: str, file_path: str):
         match = re.match(r"(\d*)-(\d*)-(\d*)-(.*)\.(.*)", file_name)
         if match:
-            y = match.group(1)
-            m = match.group(2)
-            d = match.group(3)
-            slug = match.group(4)
             ext = match.group(5)
 
             if ext == "md":
@@ -497,21 +520,8 @@ class JekyllArticleImporter:
                 if self.dry_run:
                     return
 
-                if article and self.article_path:  # asi hlavne Ceske Budejovice
-                    Redirect.objects.get_or_create(
-                        site=self.site,
-                        old_path="/%s/%s/%s/%s/%s"
-                        % (self.article_path, y, m.zfill(2), d.zfill(2), slug),
-                        defaults={"is_permanent": True, "redirect_page": article},
-                    )
-
-                elif article and self.permalink:
-                    Redirect.objects.get_or_create(
-                        site=self.site,
-                        old_path=self.permalink.replace(":title", slug),
-                        defaults={"is_permanent": True, "redirect_page": article},
-                    )
-
+                article.save()  # ujistím se, že mám "redirect_page" pro Redirect uloženou
+                self.create_redirects(article, match)
             else:
                 msg = "Nepodporovaná přípona souboru: %s" % ext
                 logger.warning(msg)
diff --git a/district/models.py b/district/models.py
index 0ecb4028..a98927ec 100644
--- a/district/models.py
+++ b/district/models.py
@@ -433,9 +433,6 @@ class DistrictArticlesPage(SubpageMixin, MetadataPageMixin, Page):
         ).get_page(request.GET.get("page"))
         return context
 
-    def save(self, clean=True, user=None, log_action=False, **kwargs):
-        super(DistrictArticlesPage, self).save()
-
 
 class DistrictContactPage(SubpageMixin, MetadataPageMixin, Page):
     ### FIELDS
-- 
GitLab