Skip to content
Snippets Groups Projects
Commit 50e70acf authored by jan.hosek's avatar jan.hosek
Browse files

parametry pro id stranky clanku a kolekce

parent ea202873
No related branches found
No related tags found
2 merge requests!423Release,!419district import jekyll
Pipeline #6782 passed
...@@ -44,8 +44,7 @@ Markdown.output_formats["plain"] = unmark_element ...@@ -44,8 +44,7 @@ Markdown.output_formats["plain"] = unmark_element
plain_md = Markdown(output_format="plain") plain_md = Markdown(output_format="plain")
plain_md.stripTopLevelTags = False plain_md.stripTopLevelTags = False
global path params = {}
PATH = os.path.abspath("../cb.pirati.cz/")
class ImgProcessor(InlineProcessor): class ImgProcessor(InlineProcessor):
...@@ -56,8 +55,8 @@ class ImgProcessor(InlineProcessor): ...@@ -56,8 +55,8 @@ class ImgProcessor(InlineProcessor):
el.attrib["format"] = "left" el.attrib["format"] = "left"
collection = get_collection() collection = get_collection()
image_obj = get_or_create_image( image_obj = get_or_create_image(
path, m.group(2), collection=collection params["path"], m.group(2), collection=collection
) # TODO path )
el.attrib["id"] = str(image_obj.pk) el.attrib["id"] = str(image_obj.pk)
return el, m.start(0), m.end(0) return el, m.start(0), m.end(0)
...@@ -86,7 +85,7 @@ def get_site_config(path): ...@@ -86,7 +85,7 @@ def get_site_config(path):
return config return config
def import_post(path, file_path, parrent, title_suffix): def import_post(path, file_path, parrent, title_suffix, command):
with open(os.path.join(path, file_path), "rt") as f: with open(os.path.join(path, file_path), "rt") as f:
r = re.split(r"^---\s*$", f.read(), maxsplit=3, flags=re.MULTILINE) r = re.split(r"^---\s*$", f.read(), maxsplit=3, flags=re.MULTILINE)
meta = yaml.safe_load(r[1]) meta = yaml.safe_load(r[1])
...@@ -96,6 +95,7 @@ def import_post(path, file_path, parrent, title_suffix): ...@@ -96,6 +95,7 @@ def import_post(path, file_path, parrent, title_suffix):
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]):
command.stdout.write("Article already imported: %s" % article)
return article return article
article = DistrictArticlePage() article = DistrictArticlePage()
...@@ -117,6 +117,7 @@ def import_post(path, file_path, parrent, title_suffix): ...@@ -117,6 +117,7 @@ def import_post(path, file_path, parrent, title_suffix):
parrent.add_child(instance=article) parrent.add_child(instance=article)
command.stdout.write("Creating article: %s" % article)
rev = article.save_revision() rev = article.save_revision()
if meta["published"]: if meta["published"]:
rev.publish() rev.publish()
...@@ -124,11 +125,7 @@ def import_post(path, file_path, parrent, title_suffix): ...@@ -124,11 +125,7 @@ def import_post(path, file_path, parrent, title_suffix):
def get_collection(): def get_collection():
if Collection.objects.filter(name="import").exists(): return params["kolekce"]
collection = Collection.objects.filter(name="import").first()
else:
collection = Collection.add_root(name="import")
return collection
def get_or_create_image(path, file_path, collection): def get_or_create_image(path, file_path, collection):
...@@ -156,20 +153,24 @@ class Command(BaseCommand): ...@@ -156,20 +153,24 @@ class Command(BaseCommand):
) )
parser.add_argument( parser.add_argument(
"--clanky-id", "--clanky-id",
default=None, required=True,
type=int, type=int,
help="Id nadstránky článků, defaultně vezme první", help="Id nadstránky článků (DistrictArticlesPage), pod kterou se články naimportují",
)
parser.add_argument(
"--kolekce-id",
required=True,
type=int,
help="Id id koekce (Collection) pro import obrázků.",
) )
def handle(self, *args, **options): def handle(self, *args, **options):
if options["clanky_id"] is None:
articles = DistrictArticlesPage.objects.first()
else:
articles = DistrictArticlesPage.objects.get(pk=options["clanky_id"]) articles = DistrictArticlesPage.objects.get(pk=options["clanky_id"])
params["kolekce"] = Collection.objects.get(pk=options["kolekce_id"])
site = articles.get_site()
global path path = params["path"] = options["path"]
path = options["path"]
site_config = get_site_config(path) site_config = get_site_config(path)
if "title" in site_config: if "title" in site_config:
...@@ -177,10 +178,6 @@ class Command(BaseCommand): ...@@ -177,10 +178,6 @@ class Command(BaseCommand):
else: else:
title_suffix = "" title_suffix = ""
if options["hostname"] is None:
site = Site.objects.first()
else:
site = Site.objects.get(hostname=options["hostname"])
articlepath = site_config["articlepath"] articlepath = site_config["articlepath"]
for fn in os.listdir(os.path.join(path, POSTS_DIR)): for fn in os.listdir(os.path.join(path, POSTS_DIR)):
...@@ -194,7 +191,9 @@ class Command(BaseCommand): ...@@ -194,7 +191,9 @@ class Command(BaseCommand):
ext = match.group(5) ext = match.group(5)
if ext == "md": if ext == "md":
article = import_post(path, fname, articles, title_suffix) article = import_post(
path, fname, articles, title_suffix, command=self
)
from wagtail.contrib.redirects.models import Redirect from wagtail.contrib.redirects.models import Redirect
r, created = Redirect.objects.get_or_create( r, created = Redirect.objects.get_or_create(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment