Skip to content

Commit

Permalink
Add posts search option to feed (#131)
Browse files Browse the repository at this point in the history
- Search posts by username, profession, decripation of post.
- Delete search from tool bar.
- Create search.html file.
- Add tests to search.

Co-authored-by: yulisuliman <[email protected]>
Co-authored-by: TomerNewmanPrograms <[email protected]>

Co-authored-by: TomerNewmanPrograms <[email protected]>
  • Loading branch information
yulisuliman and TomerNewmanPrograms authored May 23, 2022
1 parent f26b5e3 commit d75b7f9
Show file tree
Hide file tree
Showing 7 changed files with 253 additions and 30 deletions.
79 changes: 79 additions & 0 deletions posts/static/posts/feed.css
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,85 @@
padding: 5px 0px 0px 5px;
}

form {
background-color: #ffffff;
width: 300px;
height: 44px;
border-radius: 5px;
display: flex;
flex-direction: row;
align-items: center;
}

input {
all: unset;
font: 16px system-ui;
color: rgb(39, 36, 36);
height: 100%;
width: 100%;
padding: 6px 10px;
}

::placeholder {
color: rgb(39, 36, 36);
opacity: 0.7;
}

button {
background-color: #ffffff;
all: unset;
cursor: pointer;
width: 44px;
height: 44px;
}

svg {
color: rgb(39, 36, 36);
fill: currentColor;
width: 44px;
height: 44px;
padding: 10px;
}

form {
background-color: #ffffff;
width: 300px;
height: 44px;
border-radius: 5px;
display: flex;
flex-direction: row;
align-items: center;
}

input {
all: unset;
font: 16px system-ui;
color: rgb(39, 36, 36);
height: 100%;
width: 100%;
padding: 6px 10px;
}

::placeholder {
color: rgb(39, 36, 36);
opacity: 0.7;
}
button {
background-color: #ffffff;
all: unset;
cursor: pointer;
width: 44px;
height: 44px;
}

svg {
color: rgb(39, 36, 36);
fill: currentColor;
width: 44px;
height: 44px;
padding: 10px;
}

.menu-nav {
place-self: end;
}
Expand Down
53 changes: 34 additions & 19 deletions posts/templates/posts/feed.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@
<link rel="stylesheet" type="text/css" href="{% static 'posts/feed.css' %}">
{% endblock styles %}
{% block content %}
<form role="search" id="form" class="d-flex" method=GET action="{% url 'posts-search' %}">
<input type="search" placeholder="Search Posts..." name="searched">
{% csrf_token %}
<button button type="submit">
<svg viewBox="0 0 1024 1024">
<path class="path1" d="M848.471 928l-263.059-263.059c-48.941 36.706-110.118 55.059-177.412 55.059-171.294 0-312-140.706-312-312s140.706-312 312-312c171.294 0 312 140.706 312 312 0 67.294-24.471 128.471-55.059 177.412l263.059 263.059-79.529 79.529zM189.623 408.078c0 121.364 97.091 218.455 218.455 218.455s218.455-97.091 218.455-218.455c0-121.364-103.159-218.455-218.455-218.455-121.364 0-218.455 97.091-218.455 218.455z"> </path>
</svg>
</button>
</form>
{% for post in posts %}
<article class="content-section">
<div class="media-body">
Expand All @@ -17,16 +26,22 @@
<div class="dropdown-container" tabindex="-1">
<div class="three-dots"></div>
<div class="dropdown">
<a href="{% url 'post-detail' post.pk %}" class="btn btn-outline-secondary btn-light btn-sm" ><div>Go to post</div></a>
<a href="{% url 'post-detail' post.pk %}" class="btn btn-outline-secondary btn-light btn-sm" >
<div>Go to post</div>
</a>
{% if post.author == user %}
{% if post.resume %}
<a href="{% url 'resume-update' post.pk %}" class="btn btn-outline-secondary btn-light btn-sm"><div>Edit post</div></a>
<a href="{% url 'resume-update' post.pk %}" class="btn btn-outline-secondary btn-light btn-sm">
<div>Edit post</div>
</a>
{% endif %}
<a href="{% url 'post-delete' post.pk %}" class="btn btn-outline-danger btn-light btn-sm" ><div>Delete post</div></a>
<a href="{% url 'post-delete' post.pk %}" class="btn btn-outline-danger btn-light btn-sm" >
<div>Delete post</div>
</a>
{% endif %}
</div>
</div>
</div>
</div>
</div>
</div>
<p class="article-content">{{ post.description }}</p>
<div class="file-and-comments">
Expand All @@ -35,26 +50,26 @@
{% endif %}
<div class="comments-block">
<div class="buttons">
{% if post.resume %}
{% include 'posts/rating_view.html' %}
{% if post.author != user %}
<div>
<a href="{% url 'create-a-rate' post.pk %}"><button type="button" class="btn btn-secondary">Rate ResuMe</button></a>
<hr>
</div>
{% if post.resume %}
{% include 'posts/rating_view.html' %}
{% if post.author != user %}
<div>
<a href="{% url 'create-a-rate' post.pk %}"><button type="button" class="btn btn-secondary">Rate ResuMe</button></a>
<hr>
</div>
{% endif %}
{% endif %}
{% endif %}
<a href="{% url 'comment-create' post.pk %}"><button type="button" class="btn btn-secondary">Add comment</button></a>
<hr>
<a href="{% url 'comment-create' post.pk %}"><button type="button" class="btn btn-secondary">Add comment</button></a>
<hr>
</div>
{% for comment in post.comment_set.all %}
<div class="comment-data">
{% for comment in post.comment_set.all %}
<div class="comment-data">
<img class="rounded-circle article-img" src="{{ comment.author.profile.profile_pic.url }}">
<a class="mr-2" href="#">{{ comment.author }}</a>
<hr class="comment-hr">
<p class="comment-content">{{ comment.comment_text }}</p>
</div>
{% endfor %}
</div>
{% endfor %}
</div>
</div>
</div>
Expand Down
68 changes: 68 additions & 0 deletions posts/templates/posts/search.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,73 @@
{% extends "base.html" %}
{% load static %}
{% block styles %}
<link rel="stylesheet" type="text/css" href="{% static 'posts/feed.css' %}">
{% endblock styles %}
{% block content %}
{% if searched %}
<br/>
{% if posts %}
<h5>You Searched For: {{searched}}</h5>
{% for post in posts %}
<article class="content-section">
<div class="media-body">
<div class="post-header">
<div class="article-metadata">
<img class="rounded-circle article-img" src="{{ post.author.profile.profile_pic.url }}">
<a class="mr-2" href="#">{{ post.author }}</a>
<small class="text-muted">{{ post.date_posted|date:"F d, Y" }}</small>
</div>
<div class="menu-nav">
<div class="dropdown-container" tabindex="-1">
<div class="three-dots"></div>
<div class="dropdown">
<a href="{% url 'post-detail' post.pk %}" class="btn btn-outline-secondary btn-light btn-sm" ><div>Go to post</div></a>
{% if post.author == user %}
{% if post.resume %}
<a href="{% url 'resume-update' post.pk %}" class="btn btn-outline-secondary btn-light btn-sm"><div>Edit post</div></a>
{% endif %}
<a href="{% url 'post-delete' post.pk %}" class="btn btn-outline-danger btn-light btn-sm" ><div>Delete post</div></a>
{% endif %}
</div>
</div>
</div>
</div>
<p class="article-content">{{ post.description }}</p>
<div class="file-and-comments">
{% if post.resume %}
<iframe src="{{ post.resume.resume_file.url }}" style="width: 560px;height: 780px ;border: none;"></iframe>
{% endif %}
<div class="comments-block">
<div class="buttons">
{% if post.resume %}
{% include 'posts/rating_view.html' %}
{% if post.author != user %}
<div>
<a href="{% url 'create-a-rate' post.pk %}"><button type="button" class="btn btn-secondary">Rate ResuMe</button></a>
<hr>
</div>
{% endif %}
{% endif %}
<a href="{% url 'comment-create' post.pk %}"><button type="button" class="btn btn-secondary">Add comment</button></a>
<hr>
</div>
{% for comment in post.comment_set.all %}
<div class="comment-data">
<img class="rounded-circle article-img" src="{{ comment.author.profile.profile_pic.url }}">
<a class="mr-2" href="#">{{ comment.author }}</a>
<hr class="comment-hr">
<p class="comment-content">{{ comment.comment_text }}</p>
</div>
{% endfor %}
</div>
</div>
</div>
</article>
{% endfor %}
{% else %}
<h5>{{searched}} does not match any results!</h5>
{% endif %}
{% else %}
<h5>You Forgot To Search ...</h5>
{% endif %}
{% endblock content %}
12 changes: 9 additions & 3 deletions posts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
)
from .models import Post, Resume, Comment, Rating
from django import forms
from django.contrib.auth.decorators import login_required


class PostListView(ListView):
Expand Down Expand Up @@ -126,6 +125,13 @@ def about(request):
return render(request, 'posts/about.html', {'title': 'about'})


@login_required
def search(request):
return render(request, 'posts/search.html', {'title': 'search'})
if request.method == "GET":
searched = request.GET['searched']
posts_by_description = Post.objects.filter(description__contains=searched)
posts_by_user = Post.objects.filter(author__username__contains=searched)
posts_by_user_profession = Post.objects.filter(author__profile__profession__contains=searched)
posts = (posts_by_description.union(posts_by_user)).union(posts_by_user_profession)
return render(request, 'posts/search.html', {'searched': searched, 'posts': posts})
else:
return render(request, 'posts/search.html', {})
7 changes: 0 additions & 7 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,6 @@ <h1>Guest mode</h1>
Direct
</a>

<a href="{% url 'posts-search' %}" {%if title == "search"%} class="active"> {% endif %}
<li class="icon">
<i class="ri-search-line"></i>
</li>
Search
</a>

<a href="{% url 'posts-about' %}" {%if title == "about"%} class="active"> {% endif %}
<li class="icon">
<i class="ri-question-line"></i>
Expand Down
14 changes: 13 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from posts.models import Resume, Rating, Poll, PollFile
from posts.models import Resume, Rating, Poll, PollFile, Post
from django.contrib.auth.models import User
from direct_message.models import Message
from django.utils import timezone
Expand Down Expand Up @@ -75,6 +75,18 @@ def persist_resume(new_resume):
return new_resume


@pytest.fixture
def new_post(new_user):
return Post(author=new_user, description=DESCRIPTION, date_posted=CREATION_DATE)


@pytest.fixture
def persist_post(new_post):
new_post.author.save()
new_post.save()
return new_post


@pytest.fixture
def new_rater():
user = User(username=RATER_USERNAME, first_name=RATER_FIRSTNAME, last_name=RATER_LASTNAME, email=RATER_EMAIL)
Expand Down
50 changes: 50 additions & 0 deletions tests/posts_tests/test_search_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import pytest
from pytest_django.asserts import assertTemplateUsed


@pytest.mark.django_db
class TestViews:

search_url = '/search/?searched='

def test_searching_usernames_posts_view(self, client, persist_user, persist_post):
'''test searching for an exist post by username,
also testing for template and status code response'''
response = client.get(f'{self.search_url}{persist_user.username}')
assert response.status_code == 200
assertTemplateUsed(response, 'posts/search.html')
assert persist_post in response.context['posts']

def test_serching_professions_posts_view(self, client, persist_user, persist_post):
'''test searching for an exist post by user's profession
also testing for template and status code response'''
response = client.get(f'{self.search_url}{persist_user.profile.profession}')
assert response.status_code == 200
assertTemplateUsed(response, 'posts/search.html')
assert persist_post in response.context['posts']

def test_searching_descriptions_posts_view(self, client, persist_post):
'''test searching for an exist post by post's description,
also testing for template and status code response'''
response = client.get(f'{self.search_url}{persist_post.description}')
assert response.status_code == 200
assertTemplateUsed(response, 'posts/search.html')
assert persist_post in response.context['posts']

def test_searching_for_a_non_existing_user_posts_in_view(self, client, persist_user, persist_post):
'''test searching for a non exist post by username,
also testing for template and status code response'''
persist_user.delete()
response = client.get(f'{self.search_url}{persist_user.username}')
assert response.status_code == 200
assertTemplateUsed(response, 'posts/search.html')
assert persist_post not in response.context['posts']

def test_searcing_a_non_existing_post_view(self, client, persist_user, persist_post):
'''test searching for a non exist post,
also testing for template and status code response'''
persist_post.delete()
response = client.get(f'{self.search_url}{persist_post.description}')
assert response.status_code == 200
assertTemplateUsed(response, 'posts/search.html')
assert persist_post not in response.context['posts']

0 comments on commit d75b7f9

Please sign in to comment.