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

WIP moved to DistrcitArticlePage

parent 0936ad95
No related branches found
No related tags found
2 merge requests!442Release,!432Feature/majak imports
from django import forms from django import forms
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
from wagtail.core.models.sites import Site
from .jekyll_import import perform_import from .jekyll_import import perform_import
class JekyllImportForm(WagtailAdminPageForm): class JekyllImportForm(WagtailAdminPageForm):
# TODO resolve circular import and make ModelChoiceField do_import = forms.BooleanField(
article_root_page_id = forms.IntegerField() initial=False, required=False, label="Provést import z Jekyllu"
collection = forms.ModelChoiceField(queryset=Collection.objects.all()) )
dry_run = forms.BooleanField(initial=True) collection = forms.ModelChoiceField(
jekyll_repo_folder = forms.CharField(max_length=512) queryset=Collection.objects.all(), required=False, label="Kolekce obrázků"
)
dry_run = forms.BooleanField(
initial=True,
required=False,
label="Jenom na zkoušku",
help_text="Žádné články se neuloží, vypíše případné problémy či "
"již existující články - 'ostrému' importu existující "
"články nevadí, přeskočí je",
)
use_git = forms.BooleanField(
initial=False,
required=False,
label="Použít Git",
help_text="Umožňuje jednodušší zpracování, ale vyžaduje nainstalovaný Git.",
)
jekyll_repo_url = forms.URLField(
max_length=512,
required=False,
help_text="V GitHubu tlačítko Code -> a odkaz z Download zip"
"např. 'https://github.com/pirati-web/cb.pirati.cz/archive/refs/heads/gh-pages.zip',"
"pokud máte nainstalovaný Git, zvolte Použít Git a vložte"
"jednoduše URL repozitáře "
"např. 'https://github.com/pirati-web/cb.pirati.cz'",
)
# def __init__(self):
# pass
def clean(self): def clean(self):
cleaned_data = super().clean() cleaned_data = super().clean()
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("use_git"):
if cleaned_data.get("jekyll_repo_url", "").endswith(".zip"):
self.add_error(
"jekyll_repo_url", "Vložte odkaz pouze na repozitář, ne na zip"
)
else:
if not cleaned_data.get("jekyll_repo_url", "").endswith(".zip"):
self.add_error("jekyll_repo_url", "Odkaz nesměřuje na soubor '.zip'")
if len(self.errors):
return cleaned_data
if cleaned_data.get("dry_run"):
error_list = perform_import( error_list = perform_import(
article_root_page_id=self.cleaned_data["article_root_page_id"], article_parent_page=self.instance,
collection=self.cleaned_data["collection"], collection=self.cleaned_data["collection"],
path=self.cleaned_data["jekyll_repo_folder"], url=self.cleaned_data["jekyll_repo_url"],
dry_run=self.cleaned_data["dry_run"], dry_run=True,
use_git=self.cleaned_data["use_git"],
) )
# self.instance.import_error_list = error_list
for error in error_list: for error in error_list:
self.add_error("jekyll_repo_folder", error) self.add_error("jekyll_repo_url", error)
return cleaned_data return cleaned_data
def save(self, commit=True): def save(self, commit=True):
perform_import( if self.cleaned_data.get("do_import") and not self.cleaned_data["dry_run"]:
article_root_page_id=self.cleaned_data["article_root_page_id"], error_list = perform_import(
article_parent_page=self.instance,
collection=self.cleaned_data["collection"], collection=self.cleaned_data["collection"],
path=self.cleaned_data["jekyll_repo_folder"], url=self.cleaned_data["jekyll_repo_url"],
dry_run=self.cleaned_data["dry_run"], 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)
import os import os
import re import re
import urllib
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
import zipfile
from io import StringIO from io import StringIO
from shutil import rmtree
from sys import stdout from sys import stdout
from typing import List
import markdown.serializers import markdown.serializers
import yaml import yaml
...@@ -132,6 +136,13 @@ def get_collection(): ...@@ -132,6 +136,13 @@ def get_collection():
return params["kolekce"] return params["kolekce"]
def get_path(url: str, use_git: bool) -> str:
if use_git:
return clone_repo(url)
else:
return download_repo_as_zip(url)
def get_or_create_image(path, file_path, collection): def get_or_create_image(path, file_path, collection):
file_path = file_path.lstrip("/") file_path = file_path.lstrip("/")
if Image.objects.filter(title=file_path).exists(): if Image.objects.filter(title=file_path).exists():
...@@ -149,16 +160,60 @@ def get_title_from_site_config(site_config: dict) -> str: ...@@ -149,16 +160,60 @@ def get_title_from_site_config(site_config: dict) -> str:
return "" return ""
def perform_import(article_root_page_id, collection, path, dry_run): def clone_repo(url: str) -> str:
from district.models import DistrictArticlesPage """
Naclonuje repo do tmp s využitím gitu a vrátí cestu k němu.
Pokud URL končí lomítkem, odebereme ho, a vezmeme jako název repozitáře
string za posledním lomítkem jako název repa. To použijeme i pro promazání
takového adresáře, pokud už existuje.
"""
path = "/tmp/"
if url.endswith("/"):
url = url[:-1]
repo_name = url.split("/")[-1]
repo_path = os.path.join(path, repo_name)
if os.path.exists(repo_path):
rmtree(repo_path)
os.chdir(path)
os.system("git clone {}".format(url))
return repo_path
def download_repo_as_zip(url: str) -> str:
"""
Stáhne .zip repa, extrahuje a vrátí cestu k extrahovanému repu.
Hodně nešikovné je, že extrahovaná složka má ještě suffix "-gh-pages"
a to nevím, jestli platí vždy... regex taky pro název repa také není optimální,
ale ve finále nehraje moc roli, pokud vrátí cokoliv použitelného pro file name.
"""
path = "/tmp/"
repo_name = re.search("pirati-web/(.*)/archive/", url).group(1)
zip_path = "{}{}.zip".format(path, repo_name)
if os.path.exists(zip_path):
os.remove(zip_path)
urllib.request.urlretrieve(url, zip_path)
with zipfile.ZipFile(zip_path, "r") as zip_ref:
zip_ref.extractall(path)
return os.path.join(path, "{}-gh-pages".format(repo_name))
def perform_import(
article_parent_page, collection, url: str, dry_run: bool, use_git: bool
) -> "List[str]":
error_list = [] error_list = []
articles = DistrictArticlesPage.objects.get(pk=article_root_page_id)
params["kolekce"] = collection params["kolekce"] = collection
site = articles.get_site() site = article_parent_page.get_site()
path = params["path"] = path path = params["path"] = get_path(url=url, use_git=use_git)
site_config = get_site_config(path) site_config = get_site_config(path)
title_suffix = get_title_from_site_config(site_config) title_suffix = get_title_from_site_config(site_config)
...@@ -176,7 +231,7 @@ def perform_import(article_root_page_id, collection, path, dry_run): ...@@ -176,7 +231,7 @@ def perform_import(article_root_page_id, collection, path, dry_run):
if ext == "md": if ext == "md":
article, error = import_post( article, error = import_post(
path, fname, articles, title_suffix, dry_run path, fname, article_parent_page, title_suffix, dry_run
) )
if error: if error:
......
...@@ -36,10 +36,5 @@ class Command(BaseCommand): ...@@ -36,10 +36,5 @@ class Command(BaseCommand):
) )
def handle(self, *args, **options): def handle(self, *args, **options):
perform_import( pass
article_root_page_id=options["clanky_id"], # perform_import() # TODO
collection=Collection.objects.get(pk=options["kolekce_id"]),
path=options["path"],
site=None,
dry_run=options["dry-run"],
)
...@@ -231,18 +231,6 @@ class DistrictHomePage(MenuMixin, MetadataPageMixin, CalendarMixin, Page): ...@@ -231,18 +231,6 @@ class DistrictHomePage(MenuMixin, MetadataPageMixin, CalendarMixin, Page):
CommentPanel(), 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 HANDLERS
edit_handler = TabbedInterface( edit_handler = TabbedInterface(
[ [
...@@ -269,8 +257,6 @@ class DistrictHomePage(MenuMixin, MetadataPageMixin, CalendarMixin, Page): ...@@ -269,8 +257,6 @@ class DistrictHomePage(MenuMixin, MetadataPageMixin, CalendarMixin, Page):
### OTHERS ### OTHERS
base_form_class = JekyllImportForm
class Meta: class Meta:
verbose_name = "Místní sdružení" verbose_name = "Místní sdružení"
...@@ -388,6 +374,29 @@ class DistrictArticlesPage(SubpageMixin, MetadataPageMixin, Page): ...@@ -388,6 +374,29 @@ class DistrictArticlesPage(SubpageMixin, MetadataPageMixin, Page):
settings_panels = [CommentPanel()] settings_panels = [CommentPanel()]
import_panels = [
MultiFieldPanel(
[
FieldPanel("do_import"),
FieldPanel("collection"),
FieldPanel("dry_run"),
FieldPanel("use_git"),
FieldPanel("jekyll_repo_url"),
],
"import z Jekyll repozitáře",
),
]
### EDIT HANDLERS
edit_handler = TabbedInterface(
[
ObjectList(content_panels, heading="Obsah"),
ObjectList(settings_panels, heading="Nastavení"),
ObjectList(import_panels, heading="Import"),
]
)
### RELATIONS ### RELATIONS
parent_page_types = ["district.DistrictHomePage"] parent_page_types = ["district.DistrictHomePage"]
...@@ -395,6 +404,8 @@ class DistrictArticlesPage(SubpageMixin, MetadataPageMixin, Page): ...@@ -395,6 +404,8 @@ class DistrictArticlesPage(SubpageMixin, MetadataPageMixin, Page):
### OTHERS ### OTHERS
base_form_class = JekyllImportForm
class Meta: class Meta:
verbose_name = "Aktuality" verbose_name = "Aktuality"
......
from wagtail.admin import messages
from wagtail.core import hooks
from .models import DistrictHomePage
@hooks.register("after_edit_page")
@hooks.register("after_edit_page")
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))
# import re # import re
# #
# from wagtail.core import hooks # from wagtail.core import hooks
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment