diff --git a/article_import_utils/__init__.py b/article_import_utils/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/article_import_utils/apps.py b/article_import_utils/apps.py new file mode 100644 index 0000000000000000000000000000000000000000..d8ba728afe7503caf5aba2cad585a55967d587d2 --- /dev/null +++ b/article_import_utils/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class ArticleImportUtilsConfig(AppConfig): + name = "article_import_utils" diff --git a/article_import_utils/management/__init__.py b/article_import_utils/management/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/article_import_utils/management/commands/__init__.py b/article_import_utils/management/commands/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/article_import_utils/management/commands/update_articles.py b/article_import_utils/management/commands/update_articles.py new file mode 100644 index 0000000000000000000000000000000000000000..f4916750c8d07497aff4705064c58e72654a7e13 --- /dev/null +++ b/article_import_utils/management/commands/update_articles.py @@ -0,0 +1,11 @@ +from django.core.management.base import BaseCommand +from article_import_utils.services import ArticleDownloadService + + +class Command(BaseCommand): + + def handle(self, *args, **options): + ads = ArticleDownloadService() + ads.perform_update() + + self.stdout.write("\nUpdate of articles finished!") diff --git a/article_import_utils/services.py b/article_import_utils/services.py new file mode 100644 index 0000000000000000000000000000000000000000..04822115cdc883b829a839b985ccd8f3e3bd7571 --- /dev/null +++ b/article_import_utils/services.py @@ -0,0 +1,53 @@ +import json +import logging +from typing import TYPE_CHECKING + +import urllib3 + +from article_import_utils.utils import create_image_from_url +from main.models import MainArticlePage, MainArticlesPage, ARTICLE_TYPES + +if TYPE_CHECKING: + pass + +logger = logging.getLogger() + + +class ArticleDownloadService: + api_url = 'https://piratipracuji.cz/api/' + + @staticmethod + def get_existing_articles_slugs() -> list[str]: + return MainArticlePage.objects.filter(article_type=ARTICLE_TYPES.WORK_TIMELINE).values_list('slug', flat=True) + + def get_articles_response(self): + response = urllib3.PoolManager().request('GET', self.api_url) + data = json.loads(response.data) + + return data + + def perform_update(self): + existing_articles_slug_list = self.get_existing_articles_slugs() + + for article in self.get_articles_response(): + if article['slug'] not in existing_articles_slug_list: + if 'thumbnail' in article: + img_filename = 'article-' + str(article['id']) + '.jpg' + img_obj = create_image_from_url(url=article['thumbnail'], filename=img_filename) + else: + img_obj = None + + article_to_save = MainArticlePage(article_type=ARTICLE_TYPES.WORK_TIMELINE, + title=article['title'], + slug=article['slug'], + perex=article['description'].replace(" ", ""), + author='ČESKÁ PIRÁTSKÁ STRANA', + date=article['start_date'], + image=img_obj, + content=json.dumps([ + {'type': 'text', + 'value': article['content'].replace("</p>", "</p><br>")} + ]) + ) + parent = MainArticlesPage.objects.all().first() + parent.add_child(instance=article_to_save) diff --git a/article_import_utils/utils.py b/article_import_utils/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..1320c99e5ff5b66339242a33f0477eee0f9b86c8 --- /dev/null +++ b/article_import_utils/utils.py @@ -0,0 +1,14 @@ +import urllib.request +from wagtail.images.models import Image +from django.core.files import File + + +def create_image_from_url(url, filename): + img_data = urllib.request.urlretrieve(url) + img_obj = Image(title=filename) + img_obj.file.save( + filename, + File(open(img_data[0], 'rb')) + ) + img_obj.save() + return img_obj diff --git a/main/templates/main/includes/work_article_preview.html b/main/templates/main/includes/work_article_preview.html index ea5c047e119282c1d8dd26ba67168492bce5c8c4..d02c51a40b288706c4dd43e9bf727a783709cca9 100644 --- a/main/templates/main/includes/work_article_preview.html +++ b/main/templates/main/includes/work_article_preview.html @@ -1,6 +1,6 @@ <div class="p-7 flex flex-col max-w-xl border border-grey-150 mb-8"> <img - src="https://i.picsum.photos/id/689/576/281.jpg?hmac=yIwOFV185zFy4fwVE3lF1UDqLDAm_bpLr9LZprQ26eo" alt="" + src='/media/original_images/{{ article_page.image }}' alt="" class="mb-7" > <h2 class="head-2xl mb-4"> diff --git a/main/templates/main/main_article_page.html b/main/templates/main/main_article_page.html index f886d0124e169abf01a341ebbbc50833f8e46ff7..ff04cb76b891384712e74f162369a1af7764994a 100644 --- a/main/templates/main/main_article_page.html +++ b/main/templates/main/main_article_page.html @@ -11,7 +11,7 @@ <div class="grid-container mb-2 lg:mb-12"> <div class="grid-left-side h-full bg-grey-150 left-tab"> <div class="p-6"> - <span class="font-bold 3xl:text-xl">AUTOR ČLÁNKU: <br> {{ page.author_page.title }}</span><br> + <span class="font-bold 3xl:text-xl">AUTOR ČLÁNKU: <br> {{ page.author }}</span><br> </div> </div> <div class="grid-content leading-6"> diff --git a/majak/settings/base.py b/majak/settings/base.py index d63a83a05a0a912be97b5e5ea0a5e1cdb4399413..8acb40653302d3a6df864a44ca8b6c36e4e0a0ca 100644 --- a/majak/settings/base.py +++ b/majak/settings/base.py @@ -34,6 +34,7 @@ DEFAULT_AUTO_FIELD = "django.db.models.AutoField" # APPS # ------------------------------------------------------------------------------ INSTALLED_APPS = [ + "article_import_utils", "districts", "senate", "donate",