Skip to content
Snippets Groups Projects
Commit af0efa66 authored by martin.zumr's avatar martin.zumr
Browse files

[ADD] update_articles command

parent def9d315
No related branches found
No related tags found
3 merge requests!607Pirati.cz,!592[ADD] update_articles command,!575Feature/pirati cz
Pipeline #9469 passed
from django.apps import AppConfig
class ArticleImportUtilsConfig(AppConfig):
name = "article_import_utils"
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!")
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)
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
<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">
......
......@@ -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">
......
......@@ -34,6 +34,7 @@ DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
# APPS
# ------------------------------------------------------------------------------
INSTALLED_APPS = [
"article_import_utils",
"districts",
"senate",
"donate",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment