From abe102716f018832aaa37bffbc7ac2013cb03ac6 Mon Sep 17 00:00:00 2001 From: OndraRehounek <ondra.rehounek@seznam.cz> Date: Mon, 28 Mar 2022 17:38:01 +0200 Subject: [PATCH] Different zip names handled, different article URLs handled in redirects --- district/jekyll_import.py | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/district/jekyll_import.py b/district/jekyll_import.py index bf5ef470..697709c6 100644 --- a/district/jekyll_import.py +++ b/district/jekyll_import.py @@ -78,7 +78,20 @@ def download_repo_as_zip(url: str) -> (str, str): with zipfile.ZipFile(zip_path, "r") as zip_ref: zip_ref.extractall(path) - return os.path.join(path, "{}-gh-pages".format(repo_name)), repo_name + # zdá se, že někdy je -gh-pages, někdy -master... + gh_pages_path = os.path.join(path, "{}-gh-pages".format(repo_name)) + gh_pages_path_exists = os.path.exists(gh_pages_path) + master_path = os.path.join(path, "{}-master".format(repo_name)) + master_path_exists = os.path.exists(master_path) + + if gh_pages_path_exists: + return gh_pages_path, repo_name + + if master_path_exists: + return master_path, repo_name + + else: + raise NotImplementedError("Tento zip nedokážeme zpracovat.") def get_or_create_image( @@ -257,7 +270,8 @@ class JekyllArticleImporter: self.site = article_parent_page.get_site() self.site_config = get_site_config(self.path) - self.article_path = self.site_config.get("articlepath", "aktuality") + self.article_path = self.site_config.get("articlepath", None) + self.permalink = self.site_config.get("permalink", None) self.title_suffix = get_title_from_site_config(self.site_config) # Counters @@ -482,7 +496,10 @@ class JekyllArticleImporter: if ext == "md": article = self.import_post(file_path) - if article: + 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" @@ -490,13 +507,13 @@ class JekyllArticleImporter: defaults={"is_permanent": True, "redirect_page": article}, ) - # TODO handle redirects! PRAGUE X CB - # Redirect.objects.get_or_create( - # site=site, - # old_path="/%s/%s.html" - # % (article_path, 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}, + ) + else: msg = "Nepodporovaná přípona souboru: %s" % ext logger.warning(msg) -- GitLab