Skip to content
Snippets Groups Projects
Commit 1ec14937 authored by Ondrej Rehounek's avatar Ondrej Rehounek Committed by jan.bednarik
Browse files

district: WIP celery

parent abe10271
No related branches found
No related tags found
2 merge requests!442Release,!432Feature/majak imports
...@@ -3,4 +3,4 @@ ...@@ -3,4 +3,4 @@
line_length = 88 line_length = 88
multi_line_output = 3 multi_line_output = 3
include_trailing_comma = true include_trailing_comma = true
known_third_party = PyPDF2,arrow,bleach,bs4,captcha,django,environ,faker,ics,markdown,modelcluster,pirates,pytest,pytz,requests,sentry_sdk,snapshottest,taggit,wagtail,wagtailmetadata,weasyprint,yaml known_third_party = PyPDF2,arrow,bleach,bs4,captcha,celery,django,environ,faker,ics,markdown,modelcluster,pirates,pytest,pytz,requests,sentry_sdk,snapshottest,taggit,wagtail,wagtailmetadata,weasyprint,yaml
...@@ -3,7 +3,7 @@ from django.contrib.messages import ERROR, WARNING ...@@ -3,7 +3,7 @@ from django.contrib.messages import ERROR, WARNING
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 .jekyll_import import JekyllArticleImporter from .tasks import import_jekyll_articles
class JekyllImportForm(WagtailAdminPageForm): class JekyllImportForm(WagtailAdminPageForm):
...@@ -63,25 +63,26 @@ class JekyllImportForm(WagtailAdminPageForm): ...@@ -63,25 +63,26 @@ class JekyllImportForm(WagtailAdminPageForm):
if len(self.errors): if len(self.errors):
return cleaned_data return cleaned_data
if cleaned_data.get("dry_run"): # if cleaned_data.get("dry_run"):
import_message_list = self.handle_import() # import_message_list = self.handle_import()
for message in import_message_list: # for message in import_message_list:
if message["level"] in (WARNING, ERROR): # if message["level"] in (WARNING, ERROR):
self.add_error("jekyll_repo_url", message["text"]) # self.add_error("jekyll_repo_url", message["text"])
return cleaned_data return cleaned_data
def handle_import(self): def handle_import(self):
import_message_list = JekyllArticleImporter( # import_message_list =
article_parent_page=self.instance, import_jekyll_articles.delay(
collection=self.cleaned_data["collection"], article_parent_page_id=self.instance.id,
collection_id=self.cleaned_data["collection"].id,
url=self.cleaned_data["jekyll_repo_url"], url=self.cleaned_data["jekyll_repo_url"],
dry_run=self.cleaned_data["dry_run"], dry_run=self.cleaned_data["dry_run"],
use_git=self.cleaned_data["use_git"], use_git=self.cleaned_data["use_git"],
).perform_import() )
self.instance.import_message_list = import_message_list # self.instance.import_message_list = import_message_list
return import_message_list # return import_message_list
def save(self, commit=True): def save(self, commit=True):
if self.cleaned_data.get("do_import") and not self.cleaned_data["dry_run"]: if self.cleaned_data.get("do_import") and not self.cleaned_data["dry_run"]:
......
...@@ -256,18 +256,27 @@ params = {} ...@@ -256,18 +256,27 @@ params = {}
class JekyllArticleImporter: class JekyllArticleImporter:
def __init__( def __init__(
self, article_parent_page, collection, url: str, dry_run: bool, use_git: bool self,
article_parent_page_id: int,
collection_id: int,
url: str,
dry_run: bool,
use_git: bool,
): ):
from district.models import DistrictArticlesPage
# Params # Params
self.article_parent_page = article_parent_page self.article_parent_page = DistrictArticlesPage.objects.get(
self.collection = collection id=article_parent_page_id
)
self.collection = Collection.objects.get(id=collection_id)
self.dry_run = dry_run self.dry_run = dry_run
self.use_git = use_git self.use_git = use_git
self.url = url self.url = url
# Computed props # Computed props
self.path, self.repo_name = get_path_and_repo_name(self.url, self.use_git) self.path, self.repo_name = get_path_and_repo_name(self.url, self.use_git)
self.site = article_parent_page.get_site() self.site = self.article_parent_page.get_site()
self.site_config = get_site_config(self.path) self.site_config = get_site_config(self.path)
self.article_path = self.site_config.get("articlepath", None) self.article_path = self.site_config.get("articlepath", None)
......
import logging
from majak.celery import app
from .jekyll_import import JekyllArticleImporter
logger = logging.getLogger(__name__)
@app.task
def import_jekyll_articles(
article_parent_page_id: int,
collection_id: int,
url: str,
dry_run: bool,
use_git: bool,
):
return JekyllArticleImporter(
article_parent_page_id=article_parent_page_id,
collection_id=collection_id,
url=url,
dry_run=dry_run,
use_git=use_git,
).perform_import()
import os
from celery import Celery
# set the default Django settings module for the 'celery' program.
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "majak.settings.dev")
app = Celery("majak")
# Using a string here means the worker don't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
app.config_from_object("django.conf:settings", namespace="CELERY")
# Load task modules from all registered Django app configs.
app.autodiscover_tasks()
#
#
# @app.task(bind=True)
# def debug_task(self):
# print('Request: {0!r}'.format(self.request))
...@@ -190,6 +190,14 @@ CACHES = { ...@@ -190,6 +190,14 @@ CACHES = {
CACHES["default"]["TIMEOUT"] = 60 * 60 * 24 CACHES["default"]["TIMEOUT"] = 60 * 60 * 24
CACHES["renditions"]["TIMEOUT"] = 60 * 60 * 24 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_ACCEPT_CONTENT = ["application/json"]
CELERY_RESULT_SERIALIZER = "json"
CELERY_TASK_SERIALIZER = "json"
# SENTRY # SENTRY
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
......
...@@ -17,6 +17,7 @@ sentry-sdk ...@@ -17,6 +17,7 @@ sentry-sdk
Markdown Markdown
beautifulsoup4 beautifulsoup4
bleach bleach
celery
ipython ipython
weasyprint weasyprint
pypdf2 pypdf2
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment