Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

72 barra de pesquisa 1 #113

Merged
merged 7 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions forunb/forunb/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
'django.contrib.staticfiles',
'main',
'users',
'search',
]

MIDDLEWARE = [
Expand Down
1 change: 1 addition & 0 deletions forunb/forunb/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@
path('admin/', admin.site.urls),
path('', include('main.urls')),
path('users/', include('users.urls')),
path('', include('search.urls')),
]
4 changes: 2 additions & 2 deletions forunb/main/templates/main/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
<!-- Botão para mostrar a barra lateral em telas pequenas -->
<button class="btn btn-primary d-xl-none btn-menu" id="sidebarToggle"><i class="bi bi-list"></i></button>
<!-- Barra de pesquisa -->
<form class="d-flex flex-grow-1 me-2" role="search">
<input class="form-control search-bar me-2" type="search" placeholder="Pesquisar" aria-label="Search">
<form class="d-flex flex-grow-1 me-2" role="search" method="get" action="{% url 'search_forum' %}">
<input class="form-control search-bar me-2" type="search" name="search" placeholder="Pesquisar" aria-label="Search">
</form>
<!-- Botão de login -->
<div class="d-flex align-items-center">
Expand Down
15 changes: 10 additions & 5 deletions forunb/main/templates/main/forums.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,25 @@

</style>

<h2 class="titulo fw-bold p-4">Meus fóruns</h2>

<h2 class="titulo fw-bold p-4">Meus fóruns</h2>
<div class="scroll-container p-3 rounded container"> <!--Controle do scroll dos foruns-->
{% if query %}
<h2 class="titulo fw-bold p-4">Resultados da pesquisa para "{{ query }}"</h2>
{% endif %}

<div class="scroll-container p-3 rounded container">
{% for forum in forums %}
<div class="list-group col-xxl-12 "> <!--Listagem dos foruns-->
<div class="list-group col-xxl-12">
<a class="foruns list-group-item list-group-item-action" href="{% url 'forum_detail' forum.id %}">
<div class=" d-flex w-100 justify-content-between">
<div class="d-flex w-100 justify-content-between">
<h5 class="fw-bold mb-1">{{ forum.title }}</h5>
</div>
<p class="mb-1">{{ forum.description }}</p>
</a>
<br>

</div>
{% empty %}
<p>Nenhum fórum encontrado.</p>
{% endfor %}
</div>
{% endblock %}
49 changes: 49 additions & 0 deletions forunb/main/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,52 @@ def test_new_answer_view(self):
'text': 'Another Test Answer',
})
self.assertEqual(response.status_code, 302) # Redirect after success


# TESTANDO VIEWS DO APP SEARCH

class SearchForumTestCase(TestCase):

def setUp(self):
# Configuração inicial para criar alguns objetos de fórum
self.client = Client()
self.forum1 = Forum.objects.create(title="Python Programming", description="Discuss all things Python.")
self.forum2 = Forum.objects.create(title="Django Tips", description="Tips and tricks for Django.")
self.forum3 = Forum.objects.create(title="Web Development", description="General web development discussion.")

def test_search_forum_with_query(self):
# Envia uma solicitação GET com um parâmetro de pesquisa
response = self.client.get(reverse('search_forum') + '?search=python')

# Verifica se a resposta foi bem-sucedida
self.assertEqual(response.status_code, 200)

# Verifica se o template correto foi renderizado
self.assertTemplateUsed(response, 'main/forums.html')

# Verifica se o contexto contém os fóruns filtrados
self.assertIn('forums', response.context)
forums = response.context['forums']
self.assertEqual(forums.count(), 1)
self.assertEqual(forums[0], self.forum1)

# Verifica se a consulta de pesquisa foi passada corretamente para o contexto
self.assertEqual(response.context['query'], 'python')

def test_search_forum_with_no_results(self):
# Envia uma solicitação GET com um parâmetro de pesquisa que não deve retornar resultados
response = self.client.get(reverse('search_forum') + '?search=java')

# Verifica se a resposta foi bem-sucedida
self.assertEqual(response.status_code, 200)

# Verifica se o template correto foi renderizado
self.assertTemplateUsed(response, 'main/forums.html')

# Verifica se o contexto contém a lista de fóruns vazia
self.assertIn('forums', response.context)
forums = response.context['forums']
self.assertEqual(forums.count(), 0)

# Verifica se a consulta de pesquisa foi passada corretamente para o contexto
self.assertEqual(response.context['query'], 'java')
Empty file added forunb/search/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions forunb/search/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions forunb/search/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class SearchConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'search'
Empty file.
3 changes: 3 additions & 0 deletions forunb/search/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
16 changes: 16 additions & 0 deletions forunb/search/templates/search/not_found.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Não Encontrado</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous" />
</head>
<body>
<div class="container mt-5">
<h1>Fórum não encontrado</h1>
<p>Não conseguimos encontrar um fórum com o título "{{ query }}"</p>
<a href="{% url 'forum_list' %}" class="btn btn-primary">Voltar à lista de fóruns</a>
</div>
</body>
</html>
6 changes: 6 additions & 0 deletions forunb/search/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.urls import path
from . import views

urlpatterns = [
path('search/', views.search_forum, name='search_forum'),
]
7 changes: 7 additions & 0 deletions forunb/search/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.shortcuts import render
from main.models import Forum

def search_forum(request):
query = request.GET.get('search', '') # Obtém o parâmetro de pesquisa da URL
forums = Forum.objects.filter(title__icontains=query) # Filtra os fóruns com base na pesquisa
return render(request, 'main/forums.html', {'forums': forums, 'query': query}) # Renderiza a página de fóruns com os resultados