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

messages refactor

parent 00154e84
No related branches found
No related tags found
2 merge requests!442Release,!432Feature/majak imports
from django import forms
from django.contrib.messages import ERROR, WARNING
from wagtail.admin.forms import WagtailAdminPageForm
from wagtail.core.models.collections import Collection
......@@ -63,29 +64,26 @@ class JekyllImportForm(WagtailAdminPageForm):
return cleaned_data
if cleaned_data.get("dry_run"):
error_list = perform_import(
article_parent_page=self.instance,
collection=self.cleaned_data["collection"],
url=self.cleaned_data["jekyll_repo_url"],
dry_run=True,
use_git=self.cleaned_data["use_git"],
)
# self.instance.import_error_list = error_list
for error in error_list:
self.add_error("jekyll_repo_url", error)
import_message_list = self.handle_import()
for message in import_message_list:
if message["level"] in (WARNING, ERROR):
self.add_error("jekyll_repo_url", message["text"])
return cleaned_data
def save(self, commit=True):
if self.cleaned_data.get("do_import") and not self.cleaned_data["dry_run"]:
error_list = perform_import(
def handle_import(self):
import_message_list = perform_import(
article_parent_page=self.instance,
collection=self.cleaned_data["collection"],
url=self.cleaned_data["jekyll_repo_url"],
dry_run=False,
dry_run=self.cleaned_data["dry_run"],
use_git=self.cleaned_data["use_git"],
)
self.instance.import_error_list = error_list
self.instance.import_message_list = import_message_list
return import_message_list
def save(self, commit=True):
if self.cleaned_data.get("do_import") and not self.cleaned_data["dry_run"]:
self.handle_import()
return super().save(commit=commit)
......@@ -11,6 +11,7 @@ from typing import List
import markdown.serializers
import yaml
from django.contrib.messages import ERROR, INFO, SUCCESS, WARNING
from django.core.files.images import ImageFile
from django.utils.dateparse import parse_date
from markdown import Markdown
......@@ -22,6 +23,8 @@ from wagtail.images.models import Image
# Wagtail to portrebuje https://docs.wagtail.io/en/stable/extending/rich_text_internals.html#data-format
markdown.serializers.HTML_EMPTY.add("embed")
message_list = []
# Plain format pro perex
def unmark_element(element, stream=None):
......@@ -93,13 +96,11 @@ def import_post(path, file_path, parent, title_suffix, dry_run):
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]):
warning = "Article already imported: %s" % article
stdout.write(warning)
if dry_run:
return article, warning
msg = "Článek již existuje: %s" % article
stdout.write(msg)
message_list.append({"level": INFO, "text": msg})
return article, ""
return article, False
article = DistrictArticlePage()
......@@ -125,8 +126,8 @@ def import_post(path, file_path, parent, title_suffix, dry_run):
article.image = get_or_create_image(path, meta["image"], collection=collection)
if dry_run:
return article, ""
else:
return article, True
try:
parent.add_child(instance=article)
stdout.write("Creating article: %s" % article)
......@@ -134,9 +135,15 @@ def import_post(path, file_path, parent, title_suffix, dry_run):
if meta["published"]:
rev.publish()
except Exception as e:
return article, "Nelze uložit článek {}: {}".format(article.title, str(e))
message_list.append(
{
"level": WARNING if dry_run else ERROR,
"text": "Nelze uložit článek {}: {}".format(article.title, str(e)),
}
)
return article, False
return article, ""
return article, True
def get_collection():
......@@ -213,9 +220,13 @@ def download_repo_as_zip(url: str) -> str:
def perform_import(
article_parent_page, collection, url: str, dry_run: bool, use_git: bool
) -> "List[str]":
error_list = []
) -> "List[dict]":
"""
Přijímá parent page pro články, kolekci pro obrázky, url pro stažení (zip nebo git
repo, boolean jestli jde o testovací běh a boolean, zda použít git (anebo zip)).
Vrací list dict pro requests messages (klíče level, text).
"""
success_counter = 0
params["kolekce"] = collection
site = article_parent_page.get_site()
......@@ -237,15 +248,15 @@ def perform_import(
ext = match.group(5)
if ext == "md":
article, error = import_post(
article, success = import_post(
path, fname, article_parent_page, title_suffix, dry_run
)
if error:
error_list.append(error)
if not success:
continue
if dry_run:
success_counter += 1
continue
Redirect.objects.get_or_create(
......@@ -254,15 +265,23 @@ def perform_import(
% (articlepath, y, m.zfill(2), d.zfill(2), slug),
defaults={"is_permanent": True, "redirect_page": article},
)
success_counter += 1
else:
error = "ERROR: This extension is not implemented: %s" % ext
error_list.append(error)
stdout.write(error)
msg = "ERROR: Nepodporovaná přípona souboru: %s" % ext
message_list.append({"level": ERROR, "text": msg})
stdout.write(msg)
else:
warning = "WARNING: Skipping: %s" % fn
stdout.write(warning)
msg = "Skipped: %s" % fn
stdout.write(msg)
if dry_run:
error_list.append(warning)
message_list.append({"level": WARNING, "text": msg})
if success_counter:
base_msg = "Lze importovat" if dry_run else "Úspěšně naimportováno"
message_list.append(
{"level": SUCCESS, "text": "{} {} článků".format(base_msg, success_counter)}
)
return error_list
return message_list
from django.contrib.messages import ERROR, SUCCESS, WARNING, add_message
from wagtail.admin import messages
from wagtail.core import hooks
from .models import DistrictHomePage
from .models import DistrictArticlesPage
@hooks.register("after_edit_page")
......@@ -9,9 +10,9 @@ from .models import DistrictHomePage
def handle_page_import(request, page): # def after_create_page(request, page):
"""Block awesome page deletion and show a message."""
if request.method == "POST" and page.specific_class in [DistrictHomePage]:
if getattr(page, "import_messages", None):
messages.warning(request, str(page.import_error_list))
if request.method == "POST" and page.specific_class in [DistrictArticlesPage]:
for message in getattr(page, "import_message_list", []):
add_message(request, message["level"], message["text"])
# import re
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment