Skip to content

Commit

Permalink
task(delete-schedule): deleta grades do usuário e altera rotas (#151)
Browse files Browse the repository at this point in the history
* utils(db-handler): add delete_schedule function

* api(delete-schedules): delete schedule endpoint

* test(deleteschedule): add test for delete schedule

* test(delete-schedule): add test for invalid token

* Delete api/api/tests/test_schedules_api.py

* django(api): rename and delete useless files

* api(urls): update urls

* utils(test): update test urls

* api(save_schedule): remove auth save_schedule

* api(views): add schedule view

* api(views): rename view generate schedule

* fix(typo): nome da classe p/ testar delete

---------

Co-authored-by: Mateus Vieira <[email protected]>
  • Loading branch information
caio-felipee and mateusvrs authored Dec 8, 2023
1 parent 16813ab commit 25375c9
Show file tree
Hide file tree
Showing 11 changed files with 187 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
from random import randint
import json

class TestScheduleAPI(APITestCase):
class TestGenerateScheduleAPI(APITestCase):
def setUp(self):
self.factory = APIRequestFactory()
self.content_type = 'application/json'
self.api_url = '/courses/schedule/'
self.api_url = '/courses/schedules/generate/'
self.department = get_or_create_department(
code='518', year='2023', period='2')
self.discipline = get_or_create_discipline(
Expand Down
6 changes: 3 additions & 3 deletions api/api/tests/test_get_schedules_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ def setUp(self):
tokens = TokenObtainPairSerializer.get_token(self.user)
self.access_token = tokens.access_token

self.url = reverse('api:get-schedules')
self.url = reverse('api:schedules')
self.content_type = 'application/json'

self.schedules = self.client.post(
reverse('api:schedule'), body, content_type=self.content_type).data
reverse('api:generate-schedules'), body, content_type=self.content_type).data

self.schedule_json = json.dumps(self.schedules[0])

self.headers = {
'Authorization': 'Bearer ' + str(self.access_token)
}
self.client.post(reverse('api:save-schedule'),
self.client.post(self.url,
self.schedule_json, content_type=self.content_type, headers=self.headers)

def test_get_schedules(self):
Expand Down
92 changes: 92 additions & 0 deletions api/api/tests/test_schedule_delete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
from rest_framework.test import APITestCase
from rest_framework.reverse import reverse

import utils.db_handler as dbh

from users.models import User

import json


class TestDeleteSchedules(APITestCase):
def setUp(self):
self.department = dbh.get_or_create_department('518', '2023', '2')
self.discipline = dbh.get_or_create_discipline(
'CÁLCULO 1', 'MAT0025', self.department)
self._class = dbh.create_class(['EDSON ALVES DA COSTA JUNIOR'], 'FGA - I8', '35T23', [
'Terça-feira 14:00 às 15:50', 'Quinta-feira 14:00 às 15:50'], '1', [], self.discipline)

body = json.dumps(
{
"classes": [self._class.id]
}
)

self.user, _ = User.objects.get_or_create(
first_name='Aroldo',
last_name='Silva',
picture_url='https://www.photo.com',
email="[email protected]"
)
self.user.save()

tokens = TokenObtainPairSerializer.get_token(self.user)
self.access_token = tokens.access_token

self.url = "api:delete-schedule"
self.content_type = 'application/json'

self.schedules = self.client.post(
reverse('api:generate-schedules'), body, content_type=self.content_type).data

self.schedule_json = json.dumps(self.schedules[0])

self.headers = {
'Authorization': 'Bearer ' + str(self.access_token)
}


def save_schedule(self):
self.client.post(reverse('api:schedules'),
self.schedule_json, content_type=self.content_type, headers=self.headers)

def get_user_schedules(self):
return self.client.get(
reverse('api:schedules'), headers=self.headers)

def test_delete_non_existent_schedule(self):
"""
Testa a deleção de uma grade horária que não existe
"""

content = self.client.delete(
reverse(self.url, kwargs={'id': 1}), headers=self.headers)

self.assertEqual(content.status_code, 400)

def test_delete_schedule(self):
"""
Testa a deleção de uma grade horária
"""

self.save_schedule()

user_schedules = self.get_user_schedules().data
schedule_id = user_schedules[0].get('id')

content = self.client.delete(
reverse(self.url, kwargs={'id': schedule_id}), headers=self.headers)

self.assertEqual(content.status_code, 204)
self.assertEqual(len(self.get_user_schedules().data), 0)

def test_delete_schedule_with_invalid_token(self):
"""
Testa a deleção de uma grade horária com um token inválido
"""

content = self.client.delete(
reverse(self.url, kwargs={'id': 12}), headers={'Authorization': 'Bearer ' + str('invalid_token')})

self.assertEqual(content.status_code, 403)
2 changes: 1 addition & 1 deletion api/api/tests/test_schedule_save.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def setUp(self):
tokens = TokenObtainPairSerializer.get_token(self.user)
self.access_token = tokens.access_token

self.url = reverse('api:save-schedule')
self.url = reverse('api:schedules')
self.content_type = 'application/json'

def make_post_request(self, auth: str = 'correct_token', schedule: str = '[]'):
Expand Down
8 changes: 4 additions & 4 deletions api/api/urls.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from django.urls import path
from api.views import save_schedule, get_schedules, views
from api.views import schedules, delete_schedule, views

app_name = 'api'

urlpatterns = [
path('', views.Search.as_view(), name="search"),
path('year-period/', views.YearPeriod.as_view(), name="year-period"),
path('schedule/save/', save_schedule.SaveSchedule.as_view(), name="save-schedule"),
path('schedule/', views.Schedule.as_view(), name="schedule"),
path('schedules/', get_schedules.GetSchedules.as_view(), name="get-schedules")
path('schedules/', schedules.Schedules.as_view(), name="schedules"),
path('schedules/<int:id>/', delete_schedule.DeleteSchedule.as_view(), name="delete-schedule"),
path('schedules/generate/', views.GenerateSchedule.as_view(), name="generate-schedules"),
]
29 changes: 29 additions & 0 deletions api/api/views/delete_schedule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from utils import db_handler as dbh

from drf_yasg.utils import swagger_auto_schema
from drf_yasg import openapi

from rest_framework.permissions import IsAuthenticated
from rest_framework.views import APIView
from rest_framework import status, request, response

from api.swagger import Errors


class DeleteSchedule(APIView):

permission_classes = [IsAuthenticated]

@swagger_auto_schema(
operation_description="Deleta a grade horária do usuário logado.",
security=[{'Bearer': []}],
responses={
204: openapi.Response('NO CONTENT'),
**Errors([400, 401, 403]).retrieve_erros()
}
)
def delete(self, request: request.Request, id: int) -> response.Response:
"""Delete a grade horária do usuário logado."""
user = request.user

return response.Response(status=status.HTTP_204_NO_CONTENT) if dbh.delete_schedule(user, id) else response.Response(status=status.HTTP_400_BAD_REQUEST)
32 changes: 0 additions & 32 deletions api/api/views/get_schedules.py

This file was deleted.

7 changes: 1 addition & 6 deletions api/api/views/save_schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
from drf_yasg.utils import swagger_auto_schema
from drf_yasg import openapi

from rest_framework.permissions import IsAuthenticated
from rest_framework.views import APIView
from rest_framework import status, request, response

from api.models import Class
Expand All @@ -14,10 +12,7 @@
from api import serializers


class SaveSchedule(APIView):

permission_classes = [IsAuthenticated]

class SaveSchedule():
@swagger_auto_schema(
operation_description="Salva uma grade horária para o usuário logado.",
request_body=serializers.ClassSerializerSchedule(many=True),
Expand Down
45 changes: 45 additions & 0 deletions api/api/views/schedules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from utils.schedule_generator import ScheduleGenerator
from utils import db_handler as dbh

from drf_yasg.utils import swagger_auto_schema
from drf_yasg import openapi

from rest_framework.permissions import IsAuthenticated
from rest_framework.views import APIView
from rest_framework import status, request, response

from api.models import Class
from api.swagger import Errors
from api.views.utils import handle_400_error
from api import serializers

from .save_schedule import SaveSchedule

class Schedules(APIView, SaveSchedule):
permission_classes = [IsAuthenticated]

def get_permissions(self):
"""
Instancia e retorna a lista de permissões que serão usadas por esta view.
"""
if self.request.method in ['GET', 'POST']:
return [permission() for permission in self.permission_classes]
else: # pragma: no cover
return []

@swagger_auto_schema(
operation_description="Retorna as grades horárias do usuário logado.",
security=[{'Bearer': []}],
responses={
200: openapi.Response('OK', serializers.ScheduleSerializer(many=True)),
**Errors([401, 403]).retrieve_erros()
}
)
def get(self, request: request.Request, **kwargs) -> response.Response:
"""Retorna as grades horárias do usuário logado."""

user = request.user
schedules = dbh.get_schedules(user)
data = serializers.ScheduleSerializer(schedules, many=True).data

return response.Response(status=status.HTTP_200_OK, data=data)
2 changes: 1 addition & 1 deletion api/api/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def get(self, request: request.Request, *args, **kwargs) -> response.Response:
return response.Response(data, status.HTTP_200_OK)


class Schedule(APIView):
class GenerateSchedule(APIView):
@swagger_auto_schema(
operation_description="Gera possíveis horários de acordo com as aulas escolhidas com preferência de turno",
security=[],
Expand Down
9 changes: 9 additions & 0 deletions api/utils/db_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,12 @@ def save_schedule(user: User, schedule_to_save: list[Class]) -> bool:
def get_schedules(user: User) -> QuerySet:
"""Retorna as grades horárias de um usuário."""
return Schedule.objects.filter(user=user).all()

def delete_schedule(user: User, id: int) -> bool:
"""Deleta uma grade horária de um usuário."""
try:
Schedule.objects.get(user=user, id=id).delete()
except Schedule.DoesNotExist:
return False

return True

0 comments on commit 25375c9

Please sign in to comment.