Skip to content

Commit

Permalink
For #45 ✨ menu:breadcrumples
Browse files Browse the repository at this point in the history
  • Loading branch information
lissa3 committed Aug 26, 2023
1 parent 09551bc commit 33f3973
Show file tree
Hide file tree
Showing 11 changed files with 444 additions and 30 deletions.
2 changes: 1 addition & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
env_path=./.env
DJANGO_SETTINGS_MODULE=sandbox.conf.dev
python_files=tests.py test_*.py *_tests.py
addopts = -p no:warnings --reuse-db
addopts = -p no:warnings
norecursedirs = venv dist

filterwarnings=ignore::DeprecationWarning
1 change: 1 addition & 0 deletions sandbox/conf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"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
29 changes: 29 additions & 0 deletions src/posts/context_processors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from django.shortcuts import get_object_or_404
from django.urls import Resolver404, resolve, reverse

from src.posts.models.post_model import Post


def build_crumbs(request):
"""build breadcrumbs"""
crumbs = [{"name": "Home", "url": reverse("home")}]
try:
match = resolve(request.path_info)
except Resolver404:
return {"crumbs": []}
# if match.url_name == "post_list":
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,
"url": reverse(
f"{match.app_names[0]}:{match.url_name}",
args=[match.kwargs["slug"]],
),
}
)
print("crumbs are ", crumbs)
return {"crumbs": crumbs}
3 changes: 2 additions & 1 deletion src/posts/urls.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from django.urls import path, re_path

from .views import PostCategSearch, PostList, PostTagSearch, SearchPost
from .views import PostCategSearch, PostDetail, PostList, PostTagSearch, SearchPost

app_name = "posts"

urlpatterns = [
path("", PostList.as_view(), name="post_list"),
path("detail/<slug:slug>/", PostDetail.as_view(), name="post_detail"),
path("categ/<slug:slug>/", PostCategSearch.as_view(), name="cat_search"),
path("search/", SearchPost.as_view(), name="search_posts"),
re_path(
Expand Down
9 changes: 8 additions & 1 deletion src/posts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.shortcuts import get_object_or_404
from django.utils.translation import get_language
from django.utils.translation import gettext_lazy as _
from django.views.generic import ListView
from django.views.generic import DetailView, ListView

from src.core.utils.views_help import make_query, search_qs
from src.posts.forms import SearchForm
Expand All @@ -12,6 +12,7 @@


class PostList(ListView):

"""display only public posts"""

template_name = "posts/post_list.html"
Expand All @@ -20,13 +21,19 @@ class PostList(ListView):
paginate_by = 2

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


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


class PostTagSearch(ListView):
template_name = "posts/post_list.html"
context_object_name = "posts"
Expand Down
Loading

0 comments on commit 33f3973

Please sign in to comment.