Skip to content
Snippets Groups Projects
Verified Commit b0ba0f78 authored by jindra12's avatar jindra12
Browse files

Fix tags API

parent 7fff1d02
Branches
No related tags found
2 merge requests!816Release,!801Prepare basic shared tags
Pipeline #13879 passed
...@@ -465,11 +465,15 @@ class DistrictArticlePage( ...@@ -465,11 +465,15 @@ class DistrictArticlePage(
def get_context(self, request): def get_context(self, request):
context = super().get_context(request) context = super().get_context(request)
context["related_articles"] = ( context["related_articles"] = (
(
self.get_siblings(inclusive=False) self.get_siblings(inclusive=False)
.live() # TODO? filtrovat na stejné tagy? nebo sdílené články? .live() # TODO? filtrovat na stejné tagy? nebo sdílené články?
.specific() .specific()
.order_by("-districtarticlepage__date")[:3] .order_by("-districtarticlepage__date")[:3]
) )
if self.shared_from is None
else []
)
return context return context
...@@ -626,12 +630,7 @@ class DistrictArticlesPage( ...@@ -626,12 +630,7 @@ class DistrictArticlesPage(
Getuje Tagy pouze pro DistrictArticlePage omezeno IDčky getnutých přes Getuje Tagy pouze pro DistrictArticlePage omezeno IDčky getnutých přes
root_page. Počítá, kolik článků je s daným tagem. root_page. Počítá, kolik článků je s daným tagem.
""" """
return self.search_tags_by_unioned_id_query( return self.search_tags_with_count(articles)
articles,
tags_model_query=lambda query: query.annotate(count=models.Count("slug"))
.order_by("slug")
.values("name", "slug", "count"),
)
class DistrictContactPage( class DistrictContactPage(
......
...@@ -114,7 +114,7 @@ class ArticleMixin(models.Model): ...@@ -114,7 +114,7 @@ class ArticleMixin(models.Model):
""" """
Returns "rel" property for a link to this article Returns "rel" property for a link to this article
""" """
return 'rel="noindex"' if self.get_no_index else "" return "rel=noindex" if self.get_no_index else ""
@property @property
def get_url(self): def get_url(self):
...@@ -646,67 +646,81 @@ class ArticlesMixin(models.Model): ...@@ -646,67 +646,81 @@ class ArticlesMixin(models.Model):
Returns a temporary article class with pk, shared and date as the only properties. Returns a temporary article class with pk, shared and date as the only properties.
Useful when optimizing large article queries Useful when optimizing large article queries
""" """
TmpArticle = namedtuple( TmpArticle = namedtuple("TemporaryArticle", field_names=["page_ptr"])
"TemporaryArticle", field_names=["page_ptr", "shared_type"]
)
TmpPrimaryKey = namedtuple("TemporaryPk", field_names=["id"]) TmpPrimaryKey = namedtuple("TemporaryPk", field_names=["id"])
return list( return list(
map( map(
lambda unioned: TmpArticle( lambda unioned: TmpArticle(
page_ptr=TmpPrimaryKey(id=unioned["union_page_ptr_id"]), page_ptr=TmpPrimaryKey(id=unioned["union_page_ptr_id"]),
shared_type=unioned["union_shared_type"],
), ),
articles.values("union_page_ptr_id", "union_shared_type", "union_date"), articles.values("union_page_ptr_id", "union_date"),
) )
) )
def search_tags_with_count(self, articles: list):
"""
Returns a list of tags based on article ids with each count
"""
if isinstance(articles, models.QuerySet):
articles = self.materialize_articles_as_id_only(articles)
article_ids = list(map(lambda article: article.page_ptr.id, articles))
tag_list = list(
Tag.objects.filter(
district_districtarticletag_items__content_object_id__in=article_ids
)
.union(
Tag.objects.filter(
main_mainarticletag_items__content_object_id__in=article_ids
),
Tag.objects.filter(
uniweb_uniwebarticletag_items__content_object_id__in=article_ids
),
all=True,
)
.order_by("slug")
.values()
)
tag_aggregate = reduce(
lambda aggregate, tag: self.merge_dict(
aggregate,
{tag["slug"]: aggregate[tag["slug"]] + 1}
if tag["slug"] in aggregate
else {tag["slug"]: 1},
),
tag_list,
{},
)
already_present = {}
unique_tags = []
for tag in tag_list:
tag["count"] = tag_aggregate[tag["slug"]]
if tag["slug"] not in already_present:
unique_tags.append(tag)
already_present[tag["slug"]] = True
return unique_tags
def search_tags_by_unioned_id_query( def search_tags_by_unioned_id_query(
self, self,
articles: list, articles: list,
tags_model_query=None,
): ):
""" """
Search tags based on article query or list of articles. Search tags based on article query or list of articles.
Returns a list of Tag objects Returns a list of Tag objects, unique and sorted by slug
""" """
if isinstance(articles, models.QuerySet): if isinstance(articles, models.QuerySet):
articles = self.materialize_articles_as_id_only(articles) articles = self.materialize_articles_as_id_only(articles)
own_page_type = self.determine_page_type() article_ids = list(map(lambda article: article.page_ptr.id, articles))
get_ids_by_page_type = lambda page_type: list(
map(
lambda article: article.page_ptr.id,
filter(
lambda article: article.shared_type == page_type
if article.shared_type is not None
else own_page_type.value == page_type,
articles,
),
)
)
tag_query = Tag.objects.filter( tag_query = Tag.objects.filter(
Q( Q(district_districtarticletag_items__content_object_id__in=article_ids)
district_districtarticletag_items__content_object_id__in=get_ids_by_page_type( | Q(main_mainarticletag_items__content_object_id__in=article_ids)
SharedArticlesPageType.DISTRICT.value | Q(uniweb_uniwebarticletag_items__content_object_id__in=article_ids)
) )
)
| Q(
main_mainarticletag_items__content_object_id__in=get_ids_by_page_type(
SharedArticlesPageType.MAIN.value
)
)
| Q(
uniweb_uniwebarticletag_items__content_object_id__in=get_ids_by_page_type(
SharedArticlesPageType.UNIWEB.value
)
)
)
if tags_model_query is not None:
tag_query = tags_model_query(tag_query)
return tag_query return tag_query.order_by("slug").distinct("slug")
def search_articles( def search_articles(
self, self,
......
{% load wagtailcore_tags wagtailimages_tags %} {% load wagtailcore_tags wagtailimages_tags %}
<article class="card card--hoveractive article-card " itemtype="http://schema.org/BlogPosting" itemscope=""> <article class="card card--hoveractive article-card " itemtype="http://schema.org/BlogPosting" itemscope="">
<link itemprop="mainEntityOfPage" href="{% pageurl article %}"> <link itemprop="mainEntityOfPage" href="{{ article.get_url }}">
<div class="article-card-cover"> <div class="article-card-cover">
<a href="{{ article.get_url }}" {{ article.get_rel }} itemprop="image" itemtype="http://schema.org/ImageObject" itemscope=""> <a href="{{ article.get_url }}" {{ article.get_rel }} itemprop="image" itemtype="http://schema.org/ImageObject" itemscope="">
......
...@@ -610,11 +610,15 @@ class UniwebArticlePage( ...@@ -610,11 +610,15 @@ class UniwebArticlePage(
def get_context(self, request): def get_context(self, request):
context = super().get_context(request) context = super().get_context(request)
context["related_articles"] = ( context["related_articles"] = (
(
self.get_siblings(inclusive=False) self.get_siblings(inclusive=False)
.live() .live()
.specific() .specific()
.order_by("-uniwebarticlepage__date")[:3] .order_by("-uniwebarticlepage__date")[:3]
) )
if self.shared_from is None
else []
)
return context return context
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment