Skip to content
Snippets Groups Projects
Commit dcaa9ec2 authored by Tomáš Valenta's avatar Tomáš Valenta
Browse files

finish homepage instagram post display

parent 33588d91
No related branches found
No related tags found
3 merge requests!787Release,!743Add Redmine datasets to charts, Instagram feed to homepage,!742Add Instagram feed to homepage
Pipeline #12231 passed
......@@ -254,18 +254,6 @@ class RegionsBlock(StructBlock):
label = "Články pro regiony"
class TweetsBlock(StructBlock):
title = CharBlock(
label="Titulek",
help_text="Tweety budou načteny pro všechny profily uvedené v nastavení webu automaticky",
)
class Meta:
template = "main/blocks/twitter_block.html"
icon = "openquote"
label = "Tweety"
class PersonContactBlock(StructBlock):
position = CharBlock(label="Název pozice", required=False)
# email, phone?
......@@ -374,6 +362,7 @@ class CardLinkWithHeadlineBlock(CardLinkWithHeadlineBlockMixin):
class InstagramAccessBlock(StructBlock):
name = CharBlock(label="Zobrazované jméno")
username = CharBlock(label="Username", help_text="Např. pirati.cz, bez @ na začátku!")
access_token = CharBlock(label="Přístupový token")
class Meta:
......@@ -385,4 +374,13 @@ class InstagramAccessBlock(StructBlock):
)
# TwitterCarouselBlock
class InstagramPostsBlock(StructBlock):
title = CharBlock(
label="Titulek",
help_text="Instagramové posty budou načteny pro všechny profily uvedené v nastavení webu automaticky",
)
class Meta:
template = "main/blocks/instagram_block.html"
icon = "openquote"
label = "Instagramové posty"
# Generated by Django 4.1.6 on 2023-04-06 11:09
from django.db import migrations
import main.blocks
import wagtail.blocks
import wagtail.fields
import wagtail.images.blocks
class Migration(migrations.Migration):
dependencies = [
('main', '0050_alter_mainhomepage_instagram_access'),
]
operations = [
migrations.RemoveField(
model_name='mainhomepage',
name='twitter_usernames',
),
migrations.AlterField(
model_name='mainhomepage',
name='content',
field=wagtail.fields.StreamField([('carousel', wagtail.blocks.StructBlock([('slides', wagtail.blocks.ListBlock(main.blocks.HomePageCarouseSlideBlock, label='Obrázky s nadpisy - carouselu'))])), ('news', wagtail.blocks.StructBlock([('title', wagtail.blocks.CharBlock(help_text='Nejnovější články se načtou automaticky', label='Titulek')), ('image', wagtail.images.blocks.ImageChooserBlock(label='Obrázek pozadí', required=False))])), ('people', wagtail.blocks.StructBlock([('title', wagtail.blocks.CharBlock(label='Hlavní titulek')), ('list', wagtail.blocks.ListBlock(main.blocks.BoxBlock, label='Boxíky'))])), ('regions', wagtail.blocks.StructBlock([('title', wagtail.blocks.CharBlock(help_text='Články pro regiony se načtou automaticky', label='Titulek')), ('image', wagtail.images.blocks.ImageChooserBlock(label='Obrázek pozadí', required=False))])), ('instagram_posts', wagtail.blocks.StructBlock([('title', wagtail.blocks.CharBlock(help_text='Instagramové posty budou načteny pro všechny profily uvedené v nastavení webu automaticky', label='Titulek'))])), ('boxes', wagtail.blocks.StructBlock([('title', wagtail.blocks.CharBlock(label='Nadpis')), ('list', wagtail.blocks.ListBlock(main.blocks.BoxBlock, label='Boxíky')), ('image', wagtail.images.blocks.ImageChooserBlock(label='Obrázek pozadí', required=False))]))], blank=True, use_json_field=True, verbose_name='Hlavní obsah'),
),
migrations.AlterField(
model_name='mainhomepage',
name='instagram_access',
field=wagtail.fields.StreamField([('instagram_access', wagtail.blocks.StructBlock([('name', wagtail.blocks.CharBlock(label='Zobrazované jméno')), ('username', wagtail.blocks.CharBlock(help_text='Např. pirati.cz, bez @ na začátku!', label='Username')), ('access_token', wagtail.blocks.CharBlock(label='Přístupový token'))]))], blank=True, use_json_field=True, verbose_name='Uživatelská jména a přístupové tokeny pro synchronizované Instagram účty'),
),
]
......@@ -35,7 +35,7 @@ from shared.models import ( # MenuMixin,
)
from shared.utils import make_promote_panels, subscribe_to_newsletter
from tuning import admin_help
from twitter_utils.models import Tweet
from instagram_utils.models import InstagramPost
from . import blocks
from .constants import MONTH_NAMES
......@@ -86,7 +86,7 @@ class MainHomePage(
("news", blocks.NewsBlock()),
("people", blocks.PeopleOverviewBlock()),
("regions", blocks.RegionsBlock()),
("tweets", blocks.TweetsBlock()),
("instagram_posts", blocks.InstagramPostsBlock()),
("boxes", blocks.BoxesBlock()),
],
verbose_name="Hlavní obsah",
......@@ -134,14 +134,6 @@ class MainHomePage(
use_json_field=True,
)
twitter_usernames = StreamField(
[("username", CharBlock(label="Twitter uživatelské jméno"))],
verbose_name="Uživatelská jména pro synchronizované Twitter účty",
blank=True,
max_num=64,
use_json_field=True,
)
instagram_access = StreamField(
[("instagram_access", blocks.InstagramAccessBlock())],
verbose_name="Uživatelská jména a přístupové tokeny pro synchronizované Instagram účty",
......@@ -166,7 +158,6 @@ class MainHomePage(
FieldPanel("donation_page_text"),
FieldPanel("social_links"),
FieldPanel("matomo_id"),
FieldPanel("twitter_usernames"),
FieldPanel("instagram_access"),
]
......@@ -211,15 +202,20 @@ class MainHomePage(
def get_context(self, request, *args, **kwargs):
context = super().get_context(request, args, kwargs)
twitter_username_list = [
username_data["value"] for username_data in self.twitter_usernames.raw_data
instagram_username_list = [
access_data["value"]["username"]
for access_data in self.instagram_access.raw_data
]
tweet_list = Tweet.objects.username_list(twitter_username_list).order_by(
"-twitter_id"
instagram_post_list = (
InstagramPost.
objects.
filter(author_username__in=instagram_username_list).
order_by("-timestamp")
)
context["tweet_list"] = tweet_list[:4]
context["show_next_tweet"] = len(tweet_list) > 4
context["instagram_post_list"] = instagram_post_list[:4]
context["show_next_instagram_post"] = len(instagram_post_list) > 4
context["regions"] = REGION_CHOICES
......@@ -258,24 +254,35 @@ class MainHomePage(
}
return JsonResponse(data=data, safe=False)
def get_twitter_response(self, request):
twitter_username_list = [
username_data["value"] for username_data in self.twitter_usernames.raw_data
def get_instagram_response(self, request):
instagram_username_list = [
access_data["value"]["username"]
for access_data in self.instagram_access.raw_data
]
tweet_qs = Tweet.objects.username_list(twitter_username_list).order_by(
"-twitter_id"
instagram_post_list_queryset = (
InstagramPost.
objects.
filter(author_username__in=instagram_username_list).
order_by("-timestamp")
)
tweet_paginator = Paginator(tweet_qs, 4)
tweet_page = tweet_paginator.get_page(request.GET.get("page", 1))
context = {"tweet_list": tweet_page.object_list}
instagram_post_paginator = Paginator(instagram_post_list_queryset, 4)
instagram_post_page = instagram_post_paginator.get_page(request.GET.get("page", 1))
context = {"instagram_post_list": instagram_post_page.object_list}
html_content = render(
request, "main/includes/twitter_widget.html", context
request,
"main/includes/instagram_widget.html",
context
).content
data = {
"html": html_content.decode("utf-8"),
"has_next": tweet_page.has_next(),
"has_next": instagram_post_page.has_next(),
}
return JsonResponse(data=data, safe=False)
def serve(self, request, *args, **kwargs):
......@@ -283,7 +290,7 @@ class MainHomePage(
if "region" in request.GET:
return self.get_region_response(request)
else:
return self.get_twitter_response(request)
return self.get_instagram_response(request)
return super().serve(request, *args, **kwargs)
......
This diff is collapsed.
This diff is collapsed.
......@@ -4,13 +4,13 @@
{{ self.title }}
</h2>
</div>
<div id="tweetsList">
{% include 'main/includes/twitter_widget.html' with tweet_list=tweet_list %}
</div>
{% if show_next_tweet %}
<ul class="flex flex-wrap justify-center gap-3" id="instagram-posts-list">
{% include 'main/includes/instagram_widget.html' with instagram_post_list=instagram_post_list %}
</ul>
{% if show_next_instagram_post %}
<div class="flex justify-center mt-8 lg:mt-24">
<a
onclick="showMoreTweets(event, this)"
onclick="showMorePosts(event, this)"
href="#"
data-url="{{ page_url }}?page="
data-page="2"
......@@ -27,11 +27,11 @@
</div>
<script type="text/javascript">
async function showMoreTweets(event, btn) {
async function showMorePosts(event, btn) {
event.preventDefault()
const tweetsList = document.getElementById('tweetsList');
const postList = document.getElementById("instagram-posts-list");
const url = btn.getAttribute('data-url') + btn.getAttribute('data-page')
const url = btn.getAttribute("data-url") + btn.getAttribute("data-page")
const response = await fetch(url, {
method: "GET",
headers: {
......@@ -40,10 +40,10 @@
})
const data = await response.json()
tweetsList.innerHTML += data.html;
if (!data.has_next) { btn.style.display = 'none'; }
postList.innerHTML += data.html;
if (!data.has_next) { btn.style.display = "none"; }
const dataPage = parseInt(btn.getAttribute('data-page')) + 1
btn.setAttribute('data-page', dataPage)
const dataPage = parseInt(btn.getAttribute("data-page")) + 1
btn.setAttribute("data-page", dataPage)
}
</script>
{% for post in instagram_post_list %}
<li class="flex max-w-sm max-w-xs w-full h-[20rem]">
<a
href="{{ post.url }}"
class="group h-full w-full flex flex-col align-center overflow-hidden text-center border border-grey-100 relative hover:no-underline"
>
<div class="md:min-h-[20rem] p-4 opacity-0 group-focus:opacity-100 group-hover:opacity-100 duration-150 z-10">
<div class="flex flex-col items-center">
<div class="mb-4 flex items-center justify-between xl:flex-col gap-3">
<div class="flex flex-col">
<h5 class="font-alt text-xl mt-3 mb-1 text-left sm:text-center">
{{ post.author_name }}
</h5>
<small class="text-brands-instagram text-left sm:text-center">
@{{ post.author_username }}
</small>
</div>
</div>
<p class="text-small sm:text-base leading-6 mb-2">
{{ post.caption }}
</p>
</div>
</div>
<div class="absolute inset-0 flex-shrink-0 z-0 duration-150 group-focus:blur-lg group-focus:opacity-25 group-hover:blur-lg group-hover:opacity-25">
<div class="relative">
<div class="absolute left-4 top-4 bg-white rounded-lg p-1.5 drop-shadow-md">
<i class="ico--instagram text-brands-instagram text-2xl"></i>
</div>
<img
class="h-[20rem] object-cover"
src="{{ post.image.url }}"
alt="Obrázek v Instagramovém postu, popis „{{ post.caption }}“"
>
</div>
</div>
</a>
</li>
{% endfor %}
<div class="flex flex-wrap justify-center">
{% for tweet in tweet_list %}
<div class="flex max-w-sm max-w-xs w-full">
<a
href="https://twitter.com/{{ tweet.author_username }}"
class="group mb-5 w-full flex flex-col align-center overflow-hidden text-center border border-grey-100 relative sm:mb-0 hover:no-underline"
>
<div class="md:min-h-[21rem] p-4{% if tweet.image %} opacity-0 group-focus:opacity-100 group-hover:opacity-100 duration-150 z-10{% endif %}">
<div class="flex flex-col items-center">
<div class="mb-4 flex items-center justify-between xl:flex-col gap-3">
<img
class="rounded-full shadow-sm w-12"
src="{{ tweet.author_img.url }}"
alt="user image"
/>
<div class="flex flex-col">
<h5 class="font-alt text-xl mb-1 text-left sm:text-center">
{{ tweet.author_name }}
</h5>
<small class="text-turquoise-400 text-left sm:text-center">
{{ tweet.author_username }}
</small>
</div>
</div>
<p class="text-small sm:text-base leading-6 mb-2">
{{ tweet.text|truncatechars:240 }}
</p>
</div>
</div>
<div class="flex-shrink-0 h-10">
<i class="ico--twitter text-turquoise-400 text-3xl sm:text-xl"></i>
</div>
{% if tweet.image %}
<div class="absolute inset-0 flex-shrink-0 z-0 duration-150 group-focus:blur-lg group-focus:opacity-25 group-hover:blur-lg group-hover:opacity-25">
<img src="{{ tweet.image.url }}"
class="tweet-image"
alt="Obrázek Tweetu"
>
</div>
{% endif %}
</a>
</div>
{% endfor %}
</div>
......@@ -47,7 +47,6 @@ INSTALLED_APPS = [
"calendar_utils",
"maps_utils",
"redmine_utils",
"twitter_utils",
"instagram_utils",
"users",
"pirates",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment