diff --git a/api/api/views.py b/api/api/views.py index e1808e33..08390e1f 100644 --- a/api/api/views.py +++ b/api/api/views.py @@ -6,22 +6,25 @@ from rest_framework import status MAXIMUM_RETURNED_DISCIPLINES = 5 +ERROR_MESSAGE = "no valid argument found for 'search', 'year' or 'period'" + class Search(APIView): def get(self, request: Request, *args, **kwargs) -> Response: name = request.GET.get('search', None) year = request.GET.get('year', None) period = request.GET.get('period', None) - - if name is None or year is None or period is None: + + if name is None or len(name) == 0 or year is None or len(year) == 0 or period is None or len(period) == 0: return Response( { - "errors": "no valid argument found for 'search', 'year' or 'period'" + "errors": ERROR_MESSAGE }, status.HTTP_400_BAD_REQUEST) - disciplines = filter_disciplines_by_name(name=name) | filter_disciplines_by_code(code=name) - filtered_disciplines = filter_disciplines_by_year_and_period(year=year, period=period, disciplines=disciplines) + disciplines = filter_disciplines_by_name( + name=name) | filter_disciplines_by_code(code=name) + filtered_disciplines = filter_disciplines_by_year_and_period( + year=year, period=period, disciplines=disciplines) data = DisciplineSerializer(filtered_disciplines, many=True).data return Response(data[:MAXIMUM_RETURNED_DISCIPLINES], status.HTTP_200_OK) - \ No newline at end of file