-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #149 from unb-mds/task/get-schedule
task(get-schedule): Cria API para obtenção de grades do usuário
- Loading branch information
Showing
8 changed files
with
136 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Generated by Django 4.2.5 on 2023-12-08 01:10 | ||
|
||
from django.db import migrations, models | ||
import django.utils.timezone | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('api', '0007_schedule'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='schedule', | ||
name='created_at', | ||
field=models.DateTimeField(default=django.utils.timezone.now), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
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 TestGetSchedules(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 = reverse('api:get-schedules') | ||
self.content_type = 'application/json' | ||
|
||
self.schedules = self.client.post( | ||
reverse('api:schedule'), 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.schedule_json, content_type=self.content_type, headers=self.headers) | ||
|
||
def test_get_schedules(self): | ||
""" | ||
Testa a obtenção de horários salvos | ||
""" | ||
|
||
content = self.client.get( | ||
self.url, headers=self.headers) | ||
|
||
self.assertEqual(len(content.data), 1) | ||
self.assertEqual(content.status_code, 200) | ||
|
||
def test_get_schedules_with_invalid_token(self): | ||
""" | ||
Testa a obtenção de horários salvos com um token inválido | ||
""" | ||
|
||
content = self.client.get( | ||
self.url, headers={'Authorization': 'Bearer ' + str(self.access_token) + '1'}) | ||
|
||
self.assertEqual(content.status_code, 403) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
from django.urls import path | ||
from api.views import save_schedule, views | ||
from api.views import save_schedule, get_schedules, 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('schedule/', views.Schedule.as_view(), name="schedule"), | ||
path('schedules/', get_schedules.GetSchedules.as_view(), name="get-schedules") | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from drf_yasg.utils import swagger_auto_schema | ||
from drf_yasg import openapi | ||
|
||
from rest_framework.views import APIView | ||
from rest_framework.permissions import IsAuthenticated | ||
from rest_framework import status, request, response | ||
|
||
from api.swagger import Errors | ||
from api.serializers import ScheduleSerializer | ||
|
||
from utils.db_handler import get_schedules | ||
|
||
class GetSchedules(APIView): | ||
|
||
permission_classes = [IsAuthenticated] | ||
|
||
@swagger_auto_schema( | ||
operation_description="Retorna as grades horárias do usuário logado.", | ||
security=[{'Bearer': []}], | ||
responses={ | ||
200: openapi.Response('OK', ScheduleSerializer(many=True)), | ||
**Errors([401, 403]).retrieve_erros() | ||
} | ||
) | ||
def get(self, request: request.Request) -> response.Response: | ||
"""Retorna as grades horárias do usuário logado.""" | ||
|
||
user = request.user | ||
schedules = get_schedules(user) | ||
data = ScheduleSerializer(schedules, many=True).data | ||
|
||
return response.Response(status=status.HTTP_200_OK, data=data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters