diff --git a/district/jekyll_import.py b/district/jekyll_import.py index 275ff54e64c4926f0fcbc2ee7bcf8f2524c119ff..f7247682ebe5931e7eabd0b5eb3593445a564a4d 100644 --- a/district/jekyll_import.py +++ b/district/jekyll_import.py @@ -57,6 +57,15 @@ class ImgProcessor(InlineProcessor): image_obj = get_or_create_image( params["path"], m.group(2), collection=collection, repo_name="" ) + if not image_obj: + message_list.append( + { + "level": WARNING, + "text": "Nenalezen obrázek {}".format(params["path"]), + } + ) + return None, m.start(0), m.end(0) + el.attrib["id"] = str(image_obj.pk) return el, m.start(0), m.end(0) @@ -90,18 +99,28 @@ def import_post(path, file_path, parent, title_suffix, dry_run, repo_name): with open(os.path.join(path, file_path), "rt") as f: r = re.split(r"^---\s*$", f.read(), maxsplit=3, flags=re.MULTILINE) - meta = yaml.safe_load(r[1]) + try: + meta = yaml.safe_load(r[1]) + except ScannerError: + message_list.append( + {"level": ERROR, "text": "Nevalidní yaml pro {}".format(path)} + ) + return None, False + md = r[2] html = html_md.convert(md) + # meta_date = meta["date"] + # article_date = meta_date if isinstance(meta_date, date) else meta["date"].split()[0] + if DistrictArticlePage.objects.filter(title=meta["title"]).exists(): for article in DistrictArticlePage.objects.filter(title=meta["title"]): - if article.date == parse_date(meta["date"].split()[0]): - msg = "Článek již existuje: %s" % article - stdout.write(msg) - message_list.append({"level": INFO, "text": msg}) + # if article.date == parse_date(meta["date"].split()[0]): + msg = "Článek již existuje: %s" % article + stdout.write(msg) + message_list.append({"level": INFO, "text": msg}) - return article, False + return article, False article = DistrictArticlePage() @@ -162,7 +181,7 @@ def get_path_and_repo_name(url: str, use_git: bool) -> (str, str): return download_repo_as_zip(url) -def get_or_create_image(path, file_path, collection, repo_name): +def get_or_create_image(path, file_path, collection, repo_name) -> Image or None: file_path = file_path.lstrip("/") if Image.objects.filter(title=file_path).exists(): return Image.objects.filter(title=file_path).first() @@ -179,7 +198,16 @@ def get_or_create_image(path, file_path, collection, repo_name): img_assets_folder, img_name ) img_path = os.path.join(path, img_name) - urllib.request.urlretrieve(img_url, img_path) + 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)