From aa41c3770a895e0b98445c3e47f59b0313840d05 Mon Sep 17 00:00:00 2001
From: OndraRehounek <ondra.rehounek@seznam.cz>
Date: Fri, 1 Apr 2022 12:37:51 +0200
Subject: [PATCH] Celery from env, working management command again

---
 README.md                                      | 10 ++++++++++
 district/jekyll_import.py                      |  3 ++-
 .../commands/district_import_jekyll.py         | 18 ++++++++++++++----
 majak/settings/base.py                         |  4 ++--
 4 files changed, 28 insertions(+), 7 deletions(-)

diff --git a/README.md b/README.md
index d46e9130..3ff84507 100644
--- a/README.md
+++ b/README.md
@@ -99,6 +99,16 @@ Kalendář se stáhne při uložení modelu obsahujícího `CalendarMixin`.
 Appka přidává management command `update_callendars`, který stahuje a updatuje
 kalendáře. Je třeba ho pravidelně volat na pozadí (přes CRON).
 
+### Celery (Import z Jekyllu)
+Import z Jekyll GitHub repozitářů pirátských webů je řešen asynchroně přes Celery.
+Celery využívá Redis, který může běžet např. lokálně, typicky 6379:
+V envu pak je pak potřeba mít nastavený Celery broker:
+
+```
+CELERY_BROKER_URL=redis://localhost:6379/6
+CELERY_RESULT_BACKEND=redis://localhost:6379/6
+```
+
 ### Stránka 404
 
 Pokud je třeba vlastní 404 pro web, stačí do kořenové `xxxHomePage` webu
diff --git a/district/jekyll_import.py b/district/jekyll_import.py
index e60c3325..d07d94d7 100644
--- a/district/jekyll_import.py
+++ b/district/jekyll_import.py
@@ -13,13 +13,14 @@ from urllib.error import HTTPError
 
 import markdown.serializers
 import yaml
-from django.contrib.messages import ERROR, INFO, SUCCESS, WARNING
+from django.contrib.messages import INFO, SUCCESS, WARNING
 from django.core.files.images import ImageFile
 from django.utils import timezone
 from markdown import Markdown
 from markdown.extensions import Extension
 from markdown.inlinepatterns import InlineProcessor
 from wagtail.contrib.redirects.models import Redirect
+from wagtail.core.models.collections import Collection
 from wagtail.core.rich_text import RichText
 from wagtail.images.models import Image
 from yaml.scanner import ScannerError
diff --git a/district/management/commands/district_import_jekyll.py b/district/management/commands/district_import_jekyll.py
index ddfd464a..bd4fa44c 100644
--- a/district/management/commands/district_import_jekyll.py
+++ b/district/management/commands/district_import_jekyll.py
@@ -1,7 +1,6 @@
 from django.core.management.base import BaseCommand
-from wagtail.core.models.collections import Collection
 
-from ...jekyll_import import perform_import
+from ...jekyll_import import JekyllArticleImporter
 
 
 class Command(BaseCommand):
@@ -34,7 +33,18 @@ class Command(BaseCommand):
             type=bool,
             help="Zda je o testovací běh.",
         )
+        parser.add_argument(
+            "--use-git",
+            default=True,
+            type=bool,
+            help="Zda je o testovací běh.",
+        )
 
     def handle(self, *args, **options):
-        pass
-        # perform_import()  # TODO
+        return JekyllArticleImporter(
+            article_parent_page_id=options["clanky_id"],
+            collection_id=options["kolekce_id"],
+            url=options["path"],
+            dry_run=options["dry_run"],
+            use_git=options["use_git"],
+        ).perform_import()
diff --git a/majak/settings/base.py b/majak/settings/base.py
index 630c9337..14e6a7ac 100644
--- a/majak/settings/base.py
+++ b/majak/settings/base.py
@@ -192,8 +192,8 @@ CACHES["renditions"]["TIMEOUT"] = 60 * 60 * 24
 
 # CELERY
 # ------------------------------------------------------------------------------
-CELERY_BROKER_URL = "redis://localhost:6379/6"  # TODO set from env
-CELERY_RESULT_BACKEND = "redis://localhost:6379/6"
+CELERY_BROKER_URL = env.str("CELERY_BROKER_URL")
+CELERY_RESULT_BACKEND = env.str("CELERY_RESULT_BACKEND")
 CELERY_ACCEPT_CONTENT = ["application/json"]
 CELERY_RESULT_SERIALIZER = "json"
 CELERY_TASK_SERIALIZER = "json"
-- 
GitLab