diff --git a/district/forms.py b/district/forms.py index 34be9b263bc68c5886eb11667ed2d242c0c31e90..690cda87f601967b70b8fb52c816902faa5543f4 100644 --- a/district/forms.py +++ b/district/forms.py @@ -51,11 +51,16 @@ class JekyllImportForm(WagtailAdminPageForm): if not cleaned_data.get("do_import"): return cleaned_data - else: - if not cleaned_data.get("collection"): - self.add_error("collection", "Pro import je toto pole povinné") - if not cleaned_data.get("jekyll_repo_url"): - self.add_error("jekyll_repo_url", "Pro import je toto pole povinné") + + if cleaned_data.get("do_import") and not self.instance.id: + self.add_error( + "do_import", "Import proveďte prosím až po vytvoření stránky" + ) + + if not cleaned_data.get("collection"): + self.add_error("collection", "Pro import je toto pole povinné") + if not cleaned_data.get("jekyll_repo_url"): + self.add_error("jekyll_repo_url", "Pro import je toto pole povinné") if cleaned_data.get("use_git"): if cleaned_data.get("jekyll_repo_url", "").endswith(".zip"): diff --git a/region/forms.py b/region/forms.py index 34be9b263bc68c5886eb11667ed2d242c0c31e90..690cda87f601967b70b8fb52c816902faa5543f4 100644 --- a/region/forms.py +++ b/region/forms.py @@ -51,11 +51,16 @@ class JekyllImportForm(WagtailAdminPageForm): if not cleaned_data.get("do_import"): return cleaned_data - else: - if not cleaned_data.get("collection"): - self.add_error("collection", "Pro import je toto pole povinné") - if not cleaned_data.get("jekyll_repo_url"): - self.add_error("jekyll_repo_url", "Pro import je toto pole povinné") + + if cleaned_data.get("do_import") and not self.instance.id: + self.add_error( + "do_import", "Import proveďte prosím až po vytvoření stránky" + ) + + if not cleaned_data.get("collection"): + self.add_error("collection", "Pro import je toto pole povinné") + if not cleaned_data.get("jekyll_repo_url"): + self.add_error("jekyll_repo_url", "Pro import je toto pole povinné") if cleaned_data.get("use_git"): if cleaned_data.get("jekyll_repo_url", "").endswith(".zip"): diff --git a/shared/jekyll_import.py b/shared/jekyll_import.py index 2871f3783ae4968dc5b068bbefbe8f0729afc34d..4427af8c711be2c66ae5a0ca5a86fb8bc2cd6001 100644 --- a/shared/jekyll_import.py +++ b/shared/jekyll_import.py @@ -100,7 +100,7 @@ def get_or_create_image( ) -> Image or None: """ Funkce, která se snaží najít a vrátit Wagtail Image. - Nejdříve hledá v existujících podle cesty... + Nejdříve hledá v existujících podle cesty, resp. title... Pak zkusí najít soubor fyzicky na disku... Pak zkusí ještě assets/img adresář... Pak zkusí stáhnout image z https://a.pirati.cz... @@ -115,68 +115,79 @@ def get_or_create_image( if Image.objects.filter(title=file_path).exists(): return Image.objects.filter(title=file_path).first(), "" - else: + + try: + file = ImageFile(open(os.path.join(path, file_path), "rb"), name=file_path) + image = Image(title=file_path, file=file, collection=collection) + image.save() + return image, "" + except FileNotFoundError: + pass # cesta pomocí file_path neexisuje, jdeme dál + + try: + file = ImageFile( + open(os.path.join(path, "assets/img", file_path), "rb"), + name=file_path, + ) + image = Image(title=file_path, file=file, collection=collection) + image.save() + return image, "" + except FileNotFoundError: + pass # cesta s vložením "assets/img" před file_path neexisuje, jdeme dál + + try: + file = ImageFile( + open(os.path.join(path, "assets/img/posts", file_path), "rb"), + name=file_path, + ) + image = Image(title=file_path, file=file, collection=collection) + image.save() + return image, "" + except FileNotFoundError: + pass + + # ani cesta "assets/img/posts" nefunguje, jdeme zkusit assets server a.pirati.cz + fallback_name = ( + "".join(random.choice(string.ascii_lowercase) for _ in range(10)) + ".jpg" + ) # někdy je název obrzau spojený s poznámkou apod., připravíme si fallback name + img_name = file_path.split("/")[-1] or fallback_name + img_assets_folder = repo_name.split(".")[0] # např. "praha" z praha.pirati.cz + img_url = "https://a.pirati.cz/{}/img/{}".format( + img_assets_folder, file_path.split("#")[0] # cistime nazev od poznamek apod + ) + img_path = os.path.join(path, img_name) + try: + urllib.request.urlretrieve(img_url, img_path) + except (HTTPError, UnicodeEncodeError, InvalidURL, IsADirectoryError): + try: + # druhý pokus s "posts" přidáno do URL (obvykle je ve file_path) + img_url = "https://a.pirati.cz/{}/img/posts/{}".format( + img_assets_folder, img_name.split()[0] + ) + urllib.request.urlretrieve(img_url, img_path) + except (HTTPError, UnicodeEncodeError, InvalidURL, IsADirectoryError): + msg = "Nedohledán obrázek při importu článků - ani na disku, ani na URL" + log_message = "{}: cesta {}, URL {}\n".format(msg, file_path, img_url) + logger.warning( + msg, + extra={ + "file_path": file_path, + "img_name": img_name, + "img_url": img_url, + }, + ) + return None, log_message + + file = ImageFile(open(img_path, "rb"), name=img_path) + image = Image(title=file_path, file=file, collection=collection) + try: - file = ImageFile(open(os.path.join(path, file_path), "rb"), name=file_path) - image = Image(title=file_path, file=file, collection=collection) image.save() - return image, "" - except FileNotFoundError: - try: - file = ImageFile( - open(os.path.join(path, "assets/img", file_path), "rb"), - name=file_path, - ) - image = Image(title=file_path, file=file, collection=collection) - image.save() - return image, "" - except FileNotFoundError: - 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] - ) - img_path = os.path.join(path, img_name) - try: - urllib.request.urlretrieve(img_url, img_path) - except (HTTPError, UnicodeEncodeError, InvalidURL, IsADirectoryError): - try: - # druhý pokus s "posts" přidáno do URL (obvykle je ve file_path) - img_url = "https://a.pirati.cz/{}/img/posts/{}".format( - img_assets_folder, img_name.split()[0] - ) - urllib.request.urlretrieve(img_url, img_path) - except ( - HTTPError, - UnicodeEncodeError, - InvalidURL, - IsADirectoryError, - ): - msg = "Nedohledán obrázek při importu článků" - log_message = "{} - {}\n".format(msg, img_url) - logger.warning( - msg, - extra={ - "file_path": file_path, - "img_name": img_name, - "img_url": img_url, - }, - ) - return None, log_message - - file = ImageFile(open(img_path, "rb"), name=img_path) - image = Image(title=file_path, file=file, collection=collection) - try: - image.save() - except Exception as e: - msg = "Nelze uložit obrázek" - logger.warning(msg, extra={"exc": e}) - return None, msg - return image, "" + except Exception as e: + msg = "Nelze uložit obrázek" + logger.warning(msg, extra={"exc": e}) + return None, msg + return image, "" def get_path_and_repo_name(url: str, use_git: bool) -> (str, str):