Skip to content

Commit

Permalink
For #46 ✨ add categs links
Browse files Browse the repository at this point in the history
on post detail page as menu for search
  • Loading branch information
lissa3 committed Aug 27, 2023
1 parent 33f3973 commit 0c573e7
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 8 deletions.
2 changes: 0 additions & 2 deletions src/posts/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def build_crumbs(request):
crumbs.append({"name": "posts", "url": reverse("posts:post_list")})
if match.url_name == "post_detail":
post = get_object_or_404(Post, slug=match.kwargs["slug"])
print(post)
crumbs.append(
{
"name": post.title,
Expand All @@ -25,5 +24,4 @@ def build_crumbs(request):
),
}
)
print("crumbs are ", crumbs)
return {"crumbs": crumbs}
27 changes: 27 additions & 0 deletions src/posts/mixins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from django.urls import reverse


class CategoryCrumbMixin:
def get_post_categs_path(self) -> list:
"""
list of dict's based on a given p.category:
{key:p.category slug, value: p.category name}
incl ancestors for UI: categs links
"""
post = self.get_object()
cats_names = post.categ.get_name_slug_chain()
slugs_keys = cats_names["path_slug"].split("/")
names_vals = cats_names["path_name"].split("/")
if len(slugs_keys) == len(names_vals):
res = dict(zip(slugs_keys, names_vals, strict=True))
else:
return []
chain_crumbs = []
for slug, name in res.items():
chain_crumbs.append(
{
"path": reverse("posts:cat_search", kwargs={"slug": slug}),
"name": name,
}
)
return chain_crumbs
21 changes: 21 additions & 0 deletions src/posts/models/categ_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,26 @@ def get_full_path(self):
path_slug += f"/{self.slug}"
return path_slug

def get_name_slug_chain(self) -> dict:
"""
create dict of name(s)/slug(s) of a given category
incl ancestors
"""
if self.is_root():
path_name = self.name
path_slug = self.slug

else:
path_name = "/".join(
list(self.get_ancestors().values_list("name", flat=True))
)
path_name += f"/{self.name}"
path_slug = "/".join(
list(self.get_ancestors().values_list("slug", flat=True))
)
path_slug += f"/{self.slug}"

return {"path_name": path_name, "path_slug": path_slug}

def __str__(self):
return f"Category: {self.name}"
2 changes: 1 addition & 1 deletion src/posts/templatetags/tree_cats.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def recursetree(parser, token):
return RecrTreeNode(nodelist, queryset_var)


@register.inclusion_tag("components/categ_menu.html")
@register.inclusion_tag("components/categ_bar.html")
def show_categs(**kwargs):
categs = Category.get_root_nodes()
return {"categs": categs, **kwargs}
36 changes: 35 additions & 1 deletion src/posts/tests/test_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from src.contacts.tests.factories import NewsLetterFactory
from src.core.utils.admin_help import admin_change_url
from src.posts.models.post_model import Post
from src.posts.tests.factories import PostFactory
from src.posts.tests.factories import CategoryFactory, PostFactory

User = get_user_model()
TestApp.__test__ = False
Expand Down Expand Up @@ -101,3 +101,37 @@ def test_tag_seacrh(self):

self.assertEqual(resp2.status_code, 200)
self.assertEqual(posts.count(), 1)


@override_settings(LANGUAGE_CODE="ru", LANGUAGES=(("ru", "Russian"),))
class CategMenuTestCase(WebTest):
def setUp(self) -> None:
self.categ_root = CategoryFactory(name="grand_pa")
self.categ_pa = self.categ_root.add_child(name="pa")
self.categ_kid = self.categ_pa.add_child(name="kid")
self.post = PostFactory(
categ=self.categ_kid, status=Post.CurrentStatus.PUB.value
)

def test_post_detail_categs_menu(self):
"""display post detail page with corresp categs as links"""
chain_ = self.post.categ.get_name_slug_chain()
expected_chain = {
"path_name": "grand_pa/pa/kid",
"path_slug": "grand_pa/pa/kid",
}

start_url = reverse("posts:post_detail", kwargs={"slug": self.post.slug})

resp = self.app.get(start_url)

post_categ_menu = resp.html.find("ol", id="post_categ")
a_links = resp.html.find_all("a", class_="post_categ__link")
href = a_links[0].attrs["href"]
lang = href.split("/")[1]

self.assertEqual(self.categ_root.get_descendants().count(), 2)
self.assertEqual(chain_, expected_chain)
self.assertIsNotNone(post_categ_menu)
self.assertTrue(len(a_links), 3)
self.assertTrue(lang, "ru")
12 changes: 9 additions & 3 deletions src/posts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from src.posts.models.categ_model import Category
from src.posts.models.post_model import Post

from .mixins import CategoryCrumbMixin


class PostList(ListView):

Expand All @@ -21,18 +23,23 @@ class PostList(ListView):
paginate_by = 2

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


class PostDetail(DetailView):
class PostDetail(CategoryCrumbMixin, DetailView):
model = Post
template_name = "posts/post_detail.html"

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


class PostTagSearch(ListView):
template_name = "posts/post_list.html"
Expand Down Expand Up @@ -70,7 +77,6 @@ class SearchPost(ListView):
search in ukrainian: via model manager(no config in this lang)
"""

# model = Post
template_name = "posts/post_list.html"
context_object_name = "posts"
paginate_by = 5
Expand Down
12 changes: 12 additions & 0 deletions src/templates/components/post_categs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% load static %}
<nav style="--bs-breadcrumb-divider: url(&#34;data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='8' height='8'%3E%3Cpath d='M2.5 0L1 1.5 3.5 4 1 6.5 2.5 8l4-4-4-4z' fill='currentColor'/%3E%3C/svg%3E&#34;);"aria-label="breadcrumb">
<ol class="breadcrumb" id="post_categ">
{% for item in categ_dict %}
<li class="breadcrumb-item">
<a href="{{item.path}}" class="post_categ__link">
{{item.name}}
</a>
</li>
{% endfor %}
</ol>
</nav>
5 changes: 4 additions & 1 deletion src/templates/posts/post_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@

<h3>{{post.title}}</h3>
<p>{{post.created_at|date:"F d, Y"}}</p>
<strong>{%trans "category" %}: {{post.categ.name}}</strong>

<div><strong>{%trans "category" %}</strong>:
{% include "components/post_categs.html" with categ_dict=cats_path %}
</div>
<div class="content">
{{post.content|safe}}
</div>
Expand Down

0 comments on commit 0c573e7

Please sign in to comment.