diff --git a/district/forms.py b/district/forms.py index 13c0c657e035bc16270f711260e578c2fc3c546a..d4e7108c72d82cabe6f99761962e686b32b6d690 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 3e742fbf02bb61ea403ffe38a7d81d86e8ae4809..1f083b296a581f4c5739f562a8fd74feaac1876b 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 0ecb40280ea83038f5d4121a2650d76509748d09..a98927ece6618edb10b7076be391848a8caffcd7 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