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

messages refactor

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