Skip to content
Snippets Groups Projects
Commit 7b0a6b41 authored by OndraRehounek's avatar OndraRehounek
Browse files

main: import

parent a8f0dc1c
Branches
No related tags found
2 merge requests!607Pirati.cz,!575Feature/pirati cz
Pipeline #9543 passed
......@@ -173,6 +173,7 @@ Přes CRON je třeba na pozadí spouštět Django `manage.py` commandy:
* `clearsessions` - maže expirované sessions (denně až týdně)
* `publish_scheduled_pages` - publikuje naplánované stránky (každou hodinu)
* `update_callendars` - stáhne a aktualizuje kalendáře (několikrát denně)
* `update_main_timeline_articles` - aktualizuje články na `pirati.cz` z `https://piratipracuji.cz/api/`
* `update_redmine_issues` - aktualizuje programované body MS a KS stránek napojených na Redmine (několikrát denně)
* `update_tweets` - aktualizuje tweety podle nastavení na Homepage pirati.cz - vyžaduje mít v .env TWITTER_BEARER_TOKEN, parametr --days určuje stáří tweetů (default 1)
......
import json
import logging
from typing import TYPE_CHECKING
import requests
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 = requests.get(self.api_url)
data = json.loads(response.text)
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
from django.core.management.base import BaseCommand
from article_import_utils.services import ArticleDownloadService
from main.services import TimelineArticleDownloadService
class Command(BaseCommand):
class Command(BaseCommand):
def handle(self, *args, **options):
ads = ArticleDownloadService()
ads = TimelineArticleDownloadService()
ads.perform_update()
self.stdout.write("\nUpdate of articles finished!")
import json
import logging
from typing import TYPE_CHECKING
import requests
from main.models import ARTICLE_TYPES, MainArticlePage, MainArticlesPage
from shared.utils import create_image_from_url
if TYPE_CHECKING:
pass
logger = logging.getLogger()
class TimelineArticleDownloadService:
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 = requests.get(self.api_url)
data = json.loads(response.text)
return data
def perform_update(self):
existing_articles_slug_list = self.get_existing_articles_slugs()
parent = MainArticlesPage.objects.all().first()
if not parent:
RuntimeError("No MainArticlesPage to import to")
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("&nbsp;", ""),
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.add_child(instance=article_to_save)
/**
* This stylesheet is for styles you want to include only when displaying demo
* styles for grids, animations, color swatches, etc.
* These styles will not be your production CSS.
*/
#sg-patterns {
-webkit-box-sizing: border-box !important;
box-sizing: border-box !important;
max-width: 100%;
padding: 0 0.5em;
}
.demo-animate {
background: #ddd;
padding: 1em;
margin-bottom: 1em;
text-align: center;
border-radius: 8px;
cursor: pointer;
}
.sg-colors {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
list-style: none !important;
padding: 0 !important;
margin: 0 !important;
}
.sg-colors li {
-webkit-box-flex: 1;
-ms-flex: auto;
flex: auto;
padding: 0.3em;
margin: 0 0.5em 0.5em 0;
min-width: 5em;
max-width: 14em;
border: 1px solid #ddd;
border-radius: 0;
}
.sg-swatch {
display: block;
height: 8rem;
margin-bottom: 0.5rem;
border-radius: 0;
}
.sg-label {
font-size: 1rem;
}
.sg-pattern-example {
font-family: "Roboto", "Helvetica", "Arial", sans-serif !important;
}
.sg-pattern-head .sg-pattern-title a {
font-family: "Roboto", "Helvetica", "Arial", sans-serif !important;
font-weight: bold !important;
font-size: 1.2rem !important;
color: #202020 !important;
}
.sg-pattern-category-title a {
font-family: "Bebas Neue", "Helvetica", "Arial", sans-serif !important;
font-weight: 400 !important;
font-size: 3.5rem !important;
}
......@@ -2551,7 +2551,7 @@ p{
}
.btn-carousel{
@aplly h-11;
height: 2.75rem;
top: 28%;
}
......
......@@ -169,8 +169,9 @@
}
}
}
.btn-carousel{
@aplly h-11;
@apply h-11;
top: 28%;
}
......
import json
import logging
import urllib.request
import bleach
import requests
from django.conf import settings
from django.core.files import File
from django.utils.translation import gettext_lazy
from wagtail.admin.edit_handlers import (
CommentPanel,
......@@ -13,12 +15,21 @@ from wagtail.admin.edit_handlers import (
)
from wagtail.core.models import Page
from wagtail.images.edit_handlers import FieldPanel
from wagtail.images.models import Image
from tuning import admin_help
logger = logging.getLogger()
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
def get_subpage_url(page, dest_page_type):
try:
return page.get_descendants().type(dest_page_type).live().first().get_url()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment