Skip to content

Commit

Permalink
Merge pull request #77 from PenseNisso/feature/sugestao_empresa
Browse files Browse the repository at this point in the history
Merge branch feature/sugestao_empresa on develop
  • Loading branch information
Lozavival authored Dec 12, 2023
2 parents 005b187 + 836ad41 commit 5428073
Show file tree
Hide file tree
Showing 11 changed files with 211 additions and 8 deletions.
3 changes: 2 additions & 1 deletion company/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.contrib import admin

from company.models import Company
from company.models import Company, CompanySuggestionModel

# Register your models here.
admin.site.register(Company)
admin.site.register(CompanySuggestionModel)
16 changes: 16 additions & 0 deletions company/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django import forms


class CompanySuggestion(forms.Form):
name = forms.CharField(max_length=100, label="Nome da Empresa:", required=True)
field_of_operation = forms.CharField(
max_length=50, label="Área de Atuação:", required=True
)
link = forms.URLField(max_length=30, required=False)

description = forms.CharField(
max_length=400,
widget=forms.Textarea(),
label="Descrição sobre a empresa e sua área de atuação:",
required=False,
)
28 changes: 28 additions & 0 deletions company/migrations/0003_companysuggestionmodel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 4.2.5 on 2023-12-12 15:11

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('company', '0002_alter_company_options'),
]

operations = [
migrations.CreateModel(
name='CompanySuggestionModel',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)),
('field_of_operation', models.CharField(max_length=50)),
('link', models.URLField(max_length=30, null=True)),
('description', models.TextField(max_length=400, null=True)),
('created_at', models.DateTimeField(auto_now_add=True)),
('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='suggestions_sent', to=settings.AUTH_USER_MODEL)),
],
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Generated by Django 4.2.5 on 2023-12-12 18:54

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('company', '0003_companysuggestionmodel'),
('company', '0003_rate'),
]

operations = [
]
17 changes: 16 additions & 1 deletion company/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,26 @@ class Meta:
verbose_name_plural = "Companies"


class CompanySuggestionModel(models.Model):
name = models.CharField(max_length=100)
field_of_operation = models.CharField(max_length=50)
link = models.URLField(max_length=30, null=True)
description = models.TextField(max_length=400, null=True)
created_at = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(
"user.User",
on_delete=models.SET_NULL,
null=True,
blank=True,
related_name="suggestions_sent",
)


class Rate(models.Model):
company = models.ForeignKey(
"company.Company", on_delete=models.CASCADE, related_name="user_ratings"
)
user = models.ForeignKey(
"user.User", on_delete=models.CASCADE, related_name="rated_companies"
)
score = models.IntegerField()
score = models.IntegerField()
16 changes: 16 additions & 0 deletions company/templates/companies/forms/company_suggestion.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% extends "global/base.html" %} {% block title %} - Sugerir Empresa {% endblock %}
{% block content %}

<div class="flex flex-column justify-center items-center bg-blue-600 text-white w-full h-10 text-center my-6">
<h1 class="text-2xl font-bold">Sugerir uma empresa</h1>
</div>

<div>
<form action="/empresas/suggest" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Enviar">
</form>
</div>

{% endblock content %}
11 changes: 11 additions & 0 deletions company/templates/companies/forms/confirmation.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends "global/base.html" %} {% block title %} - Sugestão Enviada {% endblock %}
{% block content %}

<div class="flex flex-column justify-center items-center bg-blue-600 text-white w-full h-10 text-center my-6">
<h1 class="text-2xl font-bold">Sugestão enviada!</h1>
</div>
<div>
Agradeçemos enormemente a sua colaboração! Se tudo estiver certo, você poderá ver sua sugestão no explorador de empresas dentro dos próximos dias!
</div>

{% endblock content %}
64 changes: 62 additions & 2 deletions company/tests.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from datetime import timedelta

from django.test import TestCase
from django.test import Client, TestCase
from django.urls import reverse
from django.utils.timezone import now

from infos.models import Lawsuit, News, Report, ReportCategory
from user.models import User

from .models import Company, Rate
from .models import Company, CompanySuggestionModel, Rate
from .forms import CompanySuggestion


class CompanyModelTest(TestCase):
Expand Down Expand Up @@ -267,3 +268,62 @@ def test_new_lawsuit_in_list(self):
response = self.client.get(reverse("company:lawsuits", args=[self.company.id]))
self.assertIn(lawsuit, response.context.get("infos"))
print("Teste Company-InfoList-10: Processos atualizados com sucesso.")


class SuggestionTestCase(TestCase):
def setUp(self) -> None:
self.client = Client()

def test_suggestion_request(self):
data = {
"name": "testes",
"field_of_operation": "testes",
"link": "https://teste.com",
"description": "teste teste",
}
response = self.client.post(reverse("company:suggest"), data=data)
print(response.templates)
self.assertEqual(response.status_code, 302)
self.assertRedirects(response, reverse("company:suggest_success"))
print("Teste Company-Suggestion-1: Sugestão enviada com sucesso.")

def test_validation_name_in_form(self):
form = CompanySuggestion(
data={
"name": None,
"field_of_operation": "testes",
"link": "https://teste.com",
"description": "teste teste",
}
)
form.is_valid()
self.assertEquals(list(form.errors.keys())[0], "name")
print("Teste Company-Suggestion-2: Nome inválido negado com sucesso.")

def test_validation_field_of_operation_in_form(self):
form = CompanySuggestion(
data={
"name": "testes",
"field_of_operation": None,
"link": "https://teste.com",
"description": "teste teste",
}
)
form.is_valid()
self.assertEquals(list(form.errors.keys())[0], "field_of_operation")
print(
"Teste Company-Suggestion-3: Área de atuação inválida negada com sucesso."
)

def test_suggestion_linked_to_user(self):
user = User.objects.create_user(username="Test User", password="testpassword")
suggestion = CompanySuggestionModel.objects.create(
name="Test Suggestion",
field_of_operation="Test",
description="Test Description",
link="https://teste.com",
user=user,
)
user.refresh_from_db()
self.assertIn(suggestion, user.suggestions_sent.all())
print("Teste Company-Suggestion-4: Denúncia associada ao usuário com sucesso.")
16 changes: 14 additions & 2 deletions company/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
from django.urls import path

from .views import (CompanyView, LawsuitsList, NewsList, ReportsList,
change_rate, favorite_company)
from .views import (
CompanyFormView,
CompanyView,
LawsuitsList,
NewsList,
ReportsList,
change_rate,
favorite_company,
SuggestionSucessView,
)

app_name = "company"
urlpatterns = [
Expand All @@ -11,4 +19,8 @@
path("<int:company_id>/noticias", NewsList.as_view(), name="news"),
path("<int:company_id>/processos", LawsuitsList.as_view(), name="lawsuits"),
path("<int:company_id>/denuncias", ReportsList.as_view(), name="reports"),
path("suggest", CompanyFormView.as_view(), name="suggest"),
path(
"suggest/confirmation", SuggestionSucessView.as_view(), name="suggest_success"
),
]
29 changes: 27 additions & 2 deletions company/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
from django.contrib.auth import get_user
from django.http import HttpRequest, HttpResponse
from django.shortcuts import get_object_or_404, redirect
from django.views.generic import DetailView, ListView
from django.views.generic import DetailView, FormView, ListView, TemplateView

from infos.models import Lawsuit, News, Report

from .models import Company, Rate
from .forms import CompanySuggestion
from .models import Company, CompanySuggestionModel, Rate


class CompanyView(DetailView):
Expand Down Expand Up @@ -99,3 +100,27 @@ class LawsuitsList(InfosList):
model = Lawsuit
info_type = "Processos"
redirect_page = "infos:lawsuitdetail"


class SuggestionSucessView(TemplateView):
template_name = "companies/forms/confirmation.html"


class CompanyFormView(FormView):
form_class = CompanySuggestion
success_url = "suggest/confirmation"
template_name = "companies/forms/company_suggestion.html"

def form_valid(self, form: CompanySuggestion) -> HttpResponse:
data = form.cleaned_data
current_user = get_user(self.request)
suggestion = CompanySuggestionModel(
name=data["name"],
field_of_operation=data["field_of_operation"],
description=data["description"],
link=data["link"],
user=current_user if current_user.is_authenticated else None,
)

suggestion.save()
return super().form_valid(form)
5 changes: 5 additions & 0 deletions search/templates/search/search_app_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ <h1>
</ul>
</div>
</div>
<div>
<div>
<a href="{% url 'company:suggest' %}">Não encontrou o que procurava? Sugira uma empresa!</a>
</div>
</div>
{% endblock content %}

0 comments on commit 5428073

Please sign in to comment.