Skip to content

Commit

Permalink
For #51 add cache for posts menu;
Browse files Browse the repository at this point in the history
ipv context variable  to cached
  • Loading branch information
lissa3 committed Aug 29, 2023
1 parent 0c573e7 commit cd5bef5
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 45 deletions.
1 change: 0 additions & 1 deletion sandbox/conf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
"src.contacts.context_processors.check_subscription",
"src.posts.context_processors.build_crumbs",
],
},
},
Expand Down
9 changes: 9 additions & 0 deletions src/core/management/commands/clear_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.core.cache import cache
from django.core.management import BaseCommand


class Command(BaseCommand):
help = "clear cache" # noqa

def handle(self, *args, **options):
cache.clear()
27 changes: 0 additions & 27 deletions src/posts/context_processors.py

This file was deleted.

7 changes: 5 additions & 2 deletions src/posts/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ def get_review(self):

def get_public(self):
"""filter qs for public posts"""
return self.filter(status=2, is_deleted=False)
return self.select_related("author", "categ", "letter").filter(
status=2, is_deleted=False
)

def get_soft_deleted(self):
"""filter qs for soft deleted;
Expand All @@ -39,7 +41,8 @@ def search_uk(self, query_text=""):
search_query,
)
return (
self.filter(status=2, is_deleted=False)
self.select_related("author", "categ", "letter")
.filter(status=2, is_deleted=False)
.annotate(search=vector, headline=search_headline)
.filter(search=search_query)
.annotate(rank=search_rank)
Expand Down
16 changes: 16 additions & 0 deletions src/posts/mixins.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
from django.urls import reverse
from django.utils.functional import cached_property


class PostListMenuMixin:
@cached_property
def crumbs(self):
"""build post breadcrumbs"""
return [
{"name": "Home", "url": reverse("home")},
{"name": "posts", "url": reverse("posts:post_list")},
]

def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
ctx["crumbs"] = self.crumbs
return ctx


class CategoryCrumbMixin:
Expand Down
18 changes: 6 additions & 12 deletions src/posts/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from django.contrib import messages
from django.http import Http404
from django.shortcuts import get_object_or_404
from django.utils.translation import get_language
from django.utils.translation import gettext_lazy as _
Expand All @@ -10,10 +8,10 @@
from src.posts.models.categ_model import Category
from src.posts.models.post_model import Post

from .mixins import CategoryCrumbMixin
from .mixins import CategoryCrumbMixin, PostListMenuMixin


class PostList(ListView):
class PostList(PostListMenuMixin, ListView):

"""display only public posts"""

Expand All @@ -24,11 +22,7 @@ class PostList(ListView):

def get_queryset(self):
# print("looking for posts")
return (
Post.objects.get_public()
.select_related("categ", "author")
.prefetch_related("tags")
)
return Post.objects.get_public().prefetch_related("tags")


class PostDetail(CategoryCrumbMixin, DetailView):
Expand All @@ -41,7 +35,7 @@ def get_context_data(self, **kwargs):
return ctx


class PostTagSearch(ListView):
class PostTagSearch(PostListMenuMixin, ListView):
template_name = "posts/post_list.html"
context_object_name = "posts"
paginate_by = 2
Expand All @@ -52,7 +46,7 @@ def get_queryset(self):
return Post.objects.get_public().filter(tags__slug__in=[tag])


class PostCategSearch(ListView):
class PostCategSearch(PostListMenuMixin, ListView):
"""retrieve all posts linked to a given category"""

template_name = "posts/post_list.html"
Expand All @@ -71,7 +65,7 @@ def get_queryset(self):
return Post.objects.get_public().filter(categ_id=categ.id)


class SearchPost(ListView):
class SearchPost(PostListMenuMixin, ListView):
"""
search ru and en: via vector_ru,vector_en(triggers in migration files)
search in ukrainian: via model manager(no config in this lang)
Expand Down
4 changes: 1 addition & 3 deletions src/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@
<div class="col col-md-8 text-center">
{% show_search_form %}
</div>
<div class="col col-md-8 mb-2">
{% include "components/crumbs.html" %}
</div>

</div>
</section>
<main id="center-part">
Expand Down
8 changes: 8 additions & 0 deletions src/templates/posts/post_list.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
{% extends 'base.html' %}
{% load static sidebars i18n %}
{% block content %}
<div class="row d-flex justify-content-center dmb-lg-4 mb-sm-1 pe-sm-0">
<div class="col col-md-8 mb-2">
{% include "components/crumbs.html" %}
</div>

</div>
<div class="row">
<aside class="col-md-3 col-sm-12">
<!-- <nav role="navigation" aria-label="Sidebar Navigation">
Expand All @@ -9,6 +15,8 @@
<!-- </div>
</nav> -->
</aside>


<div class="col-md-6 col-sm-12">
{% if posts %}
{% for post in posts%}
Expand Down

0 comments on commit cd5bef5

Please sign in to comment.