Skip to content
Snippets Groups Projects
Commit 513385cc authored by OndraRehounek's avatar OndraRehounek Committed by jan.bednarik
Browse files

working import for vysocina - handling several possible exceptions

parent d50aa43b
No related branches found
No related tags found
2 merge requests!442Release,!432Feature/majak imports
......@@ -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,13 +99,23 @@ 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)
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]):
# 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})
......@@ -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)
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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment