diff --git a/district/management/commands/district_import_jekyll.py b/district/management/commands/district_import_jekyll.py index 15b6e75094c2f16c246e506b63bc6ec703b3ae8b..186bcb5d8671391e902caf774040bb88b80da3dc 100644 --- a/district/management/commands/district_import_jekyll.py +++ b/district/management/commands/district_import_jekyll.py @@ -44,8 +44,7 @@ Markdown.output_formats["plain"] = unmark_element plain_md = Markdown(output_format="plain") plain_md.stripTopLevelTags = False -global path -PATH = os.path.abspath("../cb.pirati.cz/") +params = {} class ImgProcessor(InlineProcessor): @@ -56,8 +55,8 @@ class ImgProcessor(InlineProcessor): el.attrib["format"] = "left" collection = get_collection() image_obj = get_or_create_image( - path, m.group(2), collection=collection - ) # TODO path + params["path"], m.group(2), collection=collection + ) el.attrib["id"] = str(image_obj.pk) return el, m.start(0), m.end(0) @@ -86,7 +85,7 @@ def get_site_config(path): 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: r = re.split(r"^---\s*$", f.read(), maxsplit=3, flags=re.MULTILINE) meta = yaml.safe_load(r[1]) @@ -96,6 +95,7 @@ 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]): + command.stdout.write("Article already imported: %s" % article) return article article = DistrictArticlePage() @@ -117,6 +117,7 @@ def import_post(path, file_path, parrent, title_suffix): parrent.add_child(instance=article) + command.stdout.write("Creating article: %s" % article) rev = article.save_revision() if meta["published"]: rev.publish() @@ -124,11 +125,7 @@ def import_post(path, file_path, parrent, title_suffix): def get_collection(): - if Collection.objects.filter(name="import").exists(): - collection = Collection.objects.filter(name="import").first() - else: - collection = Collection.add_root(name="import") - return collection + return params["kolekce"] def get_or_create_image(path, file_path, collection): @@ -156,20 +153,24 @@ class Command(BaseCommand): ) parser.add_argument( "--clanky-id", - default=None, + required=True, 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): - 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 = options["path"] + path = params["path"] = options["path"] site_config = get_site_config(path) if "title" in site_config: @@ -177,10 +178,6 @@ class Command(BaseCommand): else: title_suffix = "" - if options["hostname"] is None: - site = Site.objects.first() - else: - site = Site.objects.get(hostname=options["hostname"]) articlepath = site_config["articlepath"] for fn in os.listdir(os.path.join(path, POSTS_DIR)): @@ -194,7 +191,9 @@ class Command(BaseCommand): ext = match.group(5) 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 r, created = Redirect.objects.get_or_create(