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

WIP working prototype

parent 291495f5
Branches
No related tags found
2 merge requests!442Release,!432Feature/majak imports
......@@ -7,23 +7,33 @@ from .jekyll_import import perform_import
class JekyllImportForm(WagtailAdminPageForm):
article_root_page_id = (
forms.IntegerField()
) # TODO resolve circular import and make ModelChoiceField
# TODO resolve circular import and make ModelChoiceField
article_root_page_id = forms.IntegerField()
collection = forms.ModelChoiceField(queryset=Collection.objects.all())
dry_run = forms.BooleanField(initial=True)
jekyll_repo_url = forms.URLField()
site = forms.ModelChoiceField(queryset=Site.objects.all())
jekyll_repo_folder = forms.CharField(max_length=512)
# def clean(self):
# cleaned_data = super().clean()
# return super().clean()
def clean(self):
cleaned_data = super().clean()
error_list = perform_import(
article_root_page_id=self.cleaned_data["article_root_page_id"],
collection=self.cleaned_data["collection"],
path=self.cleaned_data["jekyll_repo_folder"],
dry_run=self.cleaned_data["dry_run"],
)
for error in error_list:
self.add_error("jekyll_repo_folder", error)
return cleaned_data
def save(self, commit=True):
perform_import(
article_root_page_id=self.cleaned_data["article_root_page_id"],
collection=self.cleaned_data["article_root_page_id"],
path=self.cleaned_data["jekyll_repo_url"],
collection=self.cleaned_data["collection"],
path=self.cleaned_data["jekyll_repo_folder"],
dry_run=self.cleaned_data["dry_run"],
)
return super().save(commit=commit)
......@@ -11,6 +11,7 @@ from django.utils.dateparse import parse_date
from markdown import Markdown
from markdown.extensions import Extension
from markdown.inlinepatterns import InlineProcessor
from wagtail.contrib.redirects.models import Redirect
from wagtail.images.models import Image
# Wagtail to portrebuje https://docs.wagtail.io/en/stable/extending/rich_text_internals.html#data-format
......@@ -66,16 +67,16 @@ def get_perex(text):
POSTS_DIR = "_posts"
TITLE_SUFFIX = " - Piráti České Budějovice"
# TITLE_SUFFIX = " - Piráti České Budějovice"
def get_site_config(path):
def get_site_config(path) -> dict:
with open(os.path.join(path, "_config.yml")) as f:
config = yaml.safe_load(f.read())
return config
def import_post(path, file_path, parrent, title_suffix):
def import_post(path, file_path, parent, title_suffix, dry_run):
from district.models import DistrictArticlePage
with open(os.path.join(path, file_path), "rt") as f:
......@@ -87,8 +88,13 @@ def import_post(path, file_path, parrent, title_suffix):
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]):
stdout.write("Article already imported: %s" % article)
return article
warning = "Article already imported: %s" % article
stdout.write(warning)
if dry_run:
return article, warning
return article, ""
article = DistrictArticlePage()
......@@ -107,13 +113,19 @@ def import_post(path, file_path, parrent, title_suffix):
collection = get_collection()
article.image = get_or_create_image(path, meta["image"], collection=collection)
parrent.add_child(instance=article)
if dry_run:
return article, ""
else:
try:
parent.add_child(instance=article)
stdout.write("Creating article: %s" % article)
rev = article.save_revision()
if meta["published"]:
rev.publish()
except Exception as e:
return article, "Nelze uložit článek {}: {}".format(article.title, str(e))
stdout.write("Creating article: %s" % article)
rev = article.save_revision()
if meta["published"]:
rev.publish()
return article
return article, ""
def get_collection():
......@@ -131,20 +143,24 @@ def get_or_create_image(path, file_path, collection):
return image
def perform_import(article_root_page_id, collection, path):
def get_title_from_site_config(site_config: dict) -> str:
if "title" in site_config:
return " - " + site_config.get("title", "")
return ""
def perform_import(article_root_page_id, collection, path, dry_run):
from district.models import DistrictArticlesPage
error_list = []
articles = DistrictArticlesPage.objects.get(pk=article_root_page_id)
params["kolekce"] = collection
site = articles.get_site()
path = params["path"] = path
site_config = get_site_config(path)
if "title" in site_config:
title_suffix = " - " + site_config.get("title", "")
else:
title_suffix = ""
title_suffix = get_title_from_site_config(site_config)
articlepath = site_config["articlepath"]
......@@ -159,16 +175,32 @@ def perform_import(article_root_page_id, collection, path):
ext = match.group(5)
if ext == "md":
article = import_post(path, fname, articles, title_suffix)
from wagtail.contrib.redirects.models import Redirect
article, error = import_post(
path, fname, articles, title_suffix, dry_run
)
if error:
error_list.append(error)
continue
r, created = Redirect.objects.get_or_create(
if dry_run:
continue
Redirect.objects.get_or_create(
site=site,
old_path="/%s/%s/%s/%s/%s/"
% (articlepath, y, m.zfill(2), d.zfill(2), slug),
defaults={"is_permanent": True, "redirect_page": article},
)
else:
stdout.write("ERROR: Not Implemented: %s" % ext)
error = "ERROR: This extension is not implemented: %s" % ext
error_list.append(error)
stdout.write(error)
else:
stdout.write("WARNING: Skipping: %s" % fn)
warning = "WARNING: Skipping: %s" % fn
stdout.write(warning)
if dry_run:
error_list.append(warning)
return error_list
......@@ -26,7 +26,13 @@ class Command(BaseCommand):
"--kolekce-id",
required=True,
type=int,
help="Id id koekce (Collection) pro import obrázků.",
help="Id kolekce (Collection) pro import obrázků.",
)
parser.add_argument(
"--dry-run",
default=False,
type=bool,
help="Zda je o testovací běh.",
)
def handle(self, *args, **options):
......@@ -34,4 +40,6 @@ class Command(BaseCommand):
article_root_page_id=options["clanky_id"],
collection=Collection.objects.get(pk=options["kolekce_id"]),
path=options["path"],
site=None,
dry_run=options["dry-run"],
)
......@@ -231,6 +231,18 @@ class DistrictHomePage(MenuMixin, MetadataPageMixin, CalendarMixin, Page):
CommentPanel(),
]
import_panels = [
MultiFieldPanel(
[
FieldPanel("article_root_page_id"),
FieldPanel("collection"),
FieldPanel("dry_run"),
FieldPanel("jekyll_repo_folder"),
],
"import z Jekyll repozitáře",
),
]
### EDIT HANDLERS
edit_handler = TabbedInterface(
[
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment