diff --git a/district/jekyll_import.py b/district/jekyll_import.py index 7d9fbaa11b2337d9e6b7c431b43d72d667e7e170..504c315ebb0e20100e4064525ee9cfd255a1f276 100644 --- a/district/jekyll_import.py +++ b/district/jekyll_import.py @@ -6,7 +6,6 @@ import zipfile from datetime import date from http.client import InvalidURL from io import StringIO -from shutil import rmtree from sys import stdout from typing import List from urllib.error import HTTPError @@ -15,6 +14,7 @@ import markdown.serializers import yaml from django.contrib.messages import ERROR, INFO, SUCCESS, WARNING from django.core.files.images import ImageFile +from django.utils import timezone from markdown import Markdown from markdown.extensions import Extension from markdown.inlinepatterns import InlineProcessor @@ -59,8 +59,9 @@ class ImgProcessor(InlineProcessor): el.attrib["format"] = "left" collection = get_collection() # TODO FIX REPO_NAME + parsed_image_path = get_parsed_file_path(m.group(2)) image_obj = get_or_create_image( - params["path"], m.group(2), collection=collection, repo_name="" + params["path"], parsed_image_path, collection=collection, repo_name="" ) if not image_obj: message_list.append( @@ -75,6 +76,17 @@ class ImgProcessor(InlineProcessor): return el, m.start(0), m.end(0) +def get_parsed_file_path(path: str): + if "{{" in path: + try: + parsed_path = path.split("{{")[1].split("|")[0].split("'")[1] + except IndexError: + parsed_path = path.split("{{")[1].split("|")[0].split('"')[1] + return parsed_path + else: + return path + + class ImgExtension(Extension): def extendMarkdown(self, md): IMG_PATTERN = r"!\[(.*?)\]\((.*?)\)" @@ -133,11 +145,14 @@ def import_post(path, file_path, parent, title_suffix, dry_run, repo_name): article.content = [("text", RichText(html))] article.perex = get_perex(md) - meta_date = meta["date"] - if isinstance(meta_date, date): - article.date = meta_date + if meta.get("date", None): + meta_date = meta["date"] + if isinstance(meta_date, date): + article.date = meta_date + else: + article.date = meta["date"].split()[0] else: - article.date = meta["date"].split()[0] + article.date = timezone.now() article.title = meta["title"] article.author = meta.get("author", "Česká pirátská strana") @@ -197,27 +212,36 @@ def get_or_create_image(path, file_path, collection, repo_name) -> Image or None image.save() return image except FileNotFoundError: - img_name = file_path.split("/")[-1] - img_assets_folder = repo_name.split(".")[0] # TODO make as form field - img_url = "https://a.pirati.cz/{}/img/{}".format( - img_assets_folder, file_path - ) - img_path = os.path.join(path, img_name) try: - urllib.request.urlretrieve(img_url, img_path) - except (HTTPError, UnicodeEncodeError, InvalidURL): - message_list.append( - { - "level": WARNING, - "text": "Nelze stáhout obrázek {}".format(img_url), - } + file = ImageFile( + open(os.path.join(path, "assets/img", file_path), "rb"), + name=file_path, ) - return None - - file = ImageFile(open(img_path, "rb"), name=img_path) - image = Image(title=file_path, file=file, collection=collection) - image.save() - return image + image = Image(title=file_path, file=file, collection=collection) + image.save() + return image + except FileNotFoundError: + img_name = file_path.split("/")[-1] + img_assets_folder = repo_name.split(".")[0] # TODO make as form field + img_url = "https://a.pirati.cz/{}/img/{}".format( + img_assets_folder, file_path + ) + img_path = os.path.join(path, img_name) + try: + urllib.request.urlretrieve(img_url, img_path) + except (HTTPError, UnicodeEncodeError, InvalidURL): + message_list.append( + { + "level": WARNING, + "text": "Nelze stáhout obrázek {}".format(img_url), + } + ) + return None + + file = ImageFile(open(img_path, "rb"), name=img_path) + image = Image(title=file_path, file=file, collection=collection) + image.save() + return image def get_title_from_site_config(site_config: dict) -> str: @@ -239,10 +263,12 @@ def clone_repo(url: str) -> (str, str): repo_name = url.split("/")[-1] repo_path = os.path.join(path, repo_name) + os.chdir(path) if os.path.exists(repo_path): - rmtree(repo_path) + os.chdir(repo_path) + os.system("git pull --depth 1") + return repo_path, repo_name - os.chdir(path) os.system("git clone --depth 1 {}".format(url)) return repo_path, repo_name @@ -369,6 +395,14 @@ def article_parser( 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" + # % (articlepath, slug), + # defaults={"is_permanent": True, "redirect_page": article}, + # ) + success_counter += 1 else: msg = "ERROR: Nepodporovaná přípona souboru: %s" % ext