From 2b94b782ca5c7aa875c8c8451cd21e305fc89e4d Mon Sep 17 00:00:00 2001 From: Bobby Date: Mon, 31 Jul 2023 20:53:24 -0400 Subject: Search added --- blog/views.py | 61 ++++++++++++++++++++++-- templates/blog/partials/search/comment_list.html | 9 ++++ templates/blog/partials/search/post_list.html | 23 +++++++++ templates/blog/partials/search/user_list.html | 13 +++++ templates/blog/search.html | 37 ++++++++++++-- 5 files changed, 136 insertions(+), 7 deletions(-) create mode 100644 templates/blog/partials/search/comment_list.html create mode 100644 templates/blog/partials/search/post_list.html create mode 100644 templates/blog/partials/search/user_list.html diff --git a/blog/views.py b/blog/views.py index 18bf0424..a1e1455b 100644 --- a/blog/views.py +++ b/blog/views.py @@ -7,6 +7,8 @@ from datetime import datetime from random import choice from string import ascii_letters, digits +from datetime import timedelta +from django.utils import timezone import requests from bs4 import BeautifulSoup from django.contrib import messages @@ -437,19 +439,70 @@ def search(request): 'users': User, 'comments': Comment, } + now = timezone.now() if query: search_results = SearchQuerySet().filter(content=query) if search_in: search_results = search_results.models(*[search_model_map[model] for model in search_in]) - search_results = [result.object for result in search_results] + # search_results = [result.object for result in search_results] + posts = [result.object for result in search_results if isinstance(result.object, Post)] + users = [result.object for result in search_results if isinstance(result.object, User)] + comments = [result.object for result in search_results if isinstance(result.object, Comment)] + + # match-case sort_by + if sort_by == 'relevance' and order == 'ascending': + posts = sorted(posts, key=lambda post: post.views) + users = sorted(users, key=lambda user: user.username) + comments = sorted(comments, key=lambda comment: comment.id) + elif sort_by == 'relevance' and order == 'descending': + posts = sorted(posts, key=lambda post: post.views, reverse=True) + users = sorted(users, key=lambda user: user.username, reverse=True) + comments = sorted(comments, key=lambda comment: comment.id, reverse=True) + elif sort_by == 'date' and order == 'ascending': + posts = sorted(posts, key=lambda post: post.date) + users = sorted(users, key=lambda user: user.date_joined) + comments = sorted(comments, key=lambda comment: comment.created_at) + elif sort_by == 'date' and order == 'descending': + posts = sorted(posts, key=lambda post: post.date, reverse=True) + users = sorted(users, key=lambda user: user.date_joined, reverse=True) + comments = sorted(comments, key=lambda comment: comment.created_at, reverse=True) + + # filter by date_range + if date_range == 'past_day': + posts = [post for post in posts if post.date >= now - timedelta(days=1)] + users = [user for user in users if user.date_joined >= now - timedelta(days=1)] + comments = [comment for comment in comments if comment.created_at >= now - timedelta(days=1)] + elif date_range == 'past_week': + posts = [post for post in posts if post.date >= now - timedelta(days=7)] + users = [user for user in users if user.date_joined >= now - timedelta(days=7)] + comments = [comment for comment in comments if comment.created_at >= now - timedelta(days=7)] + elif date_range == 'past_month': + posts = [post for post in posts if post.date >= now - timedelta(days=30)] + users = [user for user in users if user.date_joined >= now - timedelta(days=30)] + comments = [comment for comment in comments if comment.created_at >= now - timedelta(days=30)] + elif date_range == 'past_year': + posts = [post for post in posts if post.date >= now - timedelta(days=365)] + users = [user for user in users if user.date_joined >= now - timedelta(days=365)] + comments = [comment for comment in comments if comment.created_at >= now - timedelta(days=365)] + elif date_range == 'any': + # no need to filter + pass + + search_results = len(posts) + len(users) + len(comments) else: - search_results = None + search_results = 0 + + # attach user profiles + for user in users: + user.profile = UserProfile.objects.get(user=user) - print(search_results) + # parse comments + for comment in comments: + comment.body = comment_processor(comment.body) - return render(request, 'blog/search.html', {'title': f"Search results for '{query}'", 'query': query, 'search_results': search_results, 'search_in': search_in, 'sort_by': sort_by, 'order': order, 'date_range': date_range}) + return render(request, 'blog/search.html', {'title': f"Search results for '{query}'", 'query': query, 'search_results': search_results, 'search_in': search_in, 'sort_by': sort_by, 'order': order, 'date_range': date_range, 'posts': posts, 'users': users, 'comments': comments}) def articles(request, date=None, cg=None): type = 'articles' diff --git a/templates/blog/partials/search/comment_list.html b/templates/blog/partials/search/comment_list.html new file mode 100644 index 00000000..d45896bb --- /dev/null +++ b/templates/blog/partials/search/comment_list.html @@ -0,0 +1,9 @@ +{% load static %} + diff --git a/templates/blog/partials/search/post_list.html b/templates/blog/partials/search/post_list.html new file mode 100644 index 00000000..811c417e --- /dev/null +++ b/templates/blog/partials/search/post_list.html @@ -0,0 +1,23 @@ +{% load tz %} +{% load static %} +{% for post in posts %} + {% comment %} This is the plain small version for search list {% endcomment %} +

+ {{ post.title }} +

+

Posted by {{ post.author.first_name }} {{ post.author.last_name }} in {{ post.category }} on {% localtime on %}{{ post.date | date:"M d, Y" }}{% endlocaltime %}

+ +
+ {{ post.body|truncatewords:150|safe }} +
+
+

Tags: + {% for tag in post.tags.all %} + {{ tag.name }} + {% endfor %} +

+

+ Read complete post... +

+
+{% endfor %} \ No newline at end of file diff --git a/templates/blog/partials/search/user_list.html b/templates/blog/partials/search/user_list.html new file mode 100644 index 00000000..3493508c --- /dev/null +++ b/templates/blog/partials/search/user_list.html @@ -0,0 +1,13 @@ +{% load static %} +{% for user in users %} +
+ Profile Picture +
+

@{{ user.username }} {% if user.first_name %}| {{ user.first_name }}{% endif %} {% if user.last_name %}{{ user.last_name }}{% endif %}

+ {% if user.profile.bio %} +

Bio: {{ user.profile.bio }}

+ {% endif %} +
+
+
+{% endfor %} \ No newline at end of file diff --git a/templates/blog/search.html b/templates/blog/search.html index a709ccb0..d5bf9812 100644 --- a/templates/blog/search.html +++ b/templates/blog/search.html @@ -14,7 +14,7 @@

Sort By



-
+ {% comment %}
{% endcomment %}

Order



@@ -29,9 +29,40 @@

Search Results

-

Sorry, this feature is under construction, and will be available soon.

-

In case, you were curious, your query returned {{ search_results|length }} results.

+

Your search for "{{ request.GET.q }}" returned {{ search_results }} Result{% if search_results != 1 %}s{% endif %}.

+ + {% if 'posts' in search_in %} +

Posts

+ {% if posts|length != 0 %} + {% include 'blog/partials/search/post_list.html' %} + {% else %} +

No posts found.

+ {% endif %} + + {% endif %} + + {% if 'users' in search_in %} +

Users

+ {% if users|length != 0 %} + {% include 'blog/partials/search/user_list.html' %} + {% else %} +

No users found.

+ {% endif %} + {% endif %} + + {% if 'comments' in search_in %} +

Comments

+ {% if comments|length != 0 %} + {% include 'blog/partials/search/comment_list.html' %} + {% else %} +

No comments found.

+ {% endif %} + {% endif %} + {% endblock %} +{% block scripts %} +{% include 'blog/partials/mathjax.html' %} +{% endblock %} \ No newline at end of file -- cgit v1.2.3