diff --git a/INSIGHTSAPI/services/tests.py b/INSIGHTSAPI/services/tests.py index e15b0ed..00351c6 100644 --- a/INSIGHTSAPI/services/tests.py +++ b/INSIGHTSAPI/services/tests.py @@ -2,6 +2,7 @@ import os import requests +import holidays from rest_framework.test import APITestCase from django.test import TestCase, Client, override_settings from django.urls import reverse @@ -115,3 +116,21 @@ def test_send_report_ethical_line_with_contact(self): }, ) self.assertEqual(response.status_code, 200, response.data) + +class HolidayTest(TestCase): + """Test for holidays.""" + + def test_holiday(self): + """Test that the holiday is a holiday.""" + self.assertTrue(holidays.Colombia().get("2022-01-01")) + + def test_non_holiday(self): + """Test that the day is not a holiday.""" + self.assertFalse(holidays.Colombia().get("2022-01-02")) + + def test_get_holidays(self): + """Test that the holidays are retrieved.""" + data = {"year": 2024} + response = self.client.get("/services/holidays/", data) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.data, holidays.CO(years=2024).items()) \ No newline at end of file diff --git a/INSIGHTSAPI/services/urls.py b/INSIGHTSAPI/services/urls.py index e8daa83..2e14f40 100644 --- a/INSIGHTSAPI/services/urls.py +++ b/INSIGHTSAPI/services/urls.py @@ -2,13 +2,14 @@ from django.urls import path from django.conf import settings -from .views import send_report_ethical_line, trigger_error +from .views import send_report_ethical_line, trigger_error, get_holidays from .test_endpoint import test_endpoint urlpatterns = [ path("send-ethical-line/", send_report_ethical_line, name="send_ethical_line"), path("trigger-error/", trigger_error, name="trigger_error"), + path("holidays/", get_holidays, name="get_holidays"), ] if settings.DEBUG: urlpatterns.append(path("test-endpoint/", test_endpoint, name="test_endpoint")) diff --git a/INSIGHTSAPI/services/views.py b/INSIGHTSAPI/services/views.py index 515c82c..850aa72 100644 --- a/INSIGHTSAPI/services/views.py +++ b/INSIGHTSAPI/services/views.py @@ -2,6 +2,7 @@ import logging import os +import holidays from rest_framework.response import Response from rest_framework.decorators import api_view from rest_framework.views import APIView @@ -69,3 +70,16 @@ def send_report_ethical_line(request): def trigger_error(request): """Trigger an error for testing purposes.""" raise Exception("Test error") + +@api_view(["GET"]) +def get_holidays(request): + """Get the holidays of the year.""" + year = request.GET.get("year", None) + if year is None: + return Response({"error": "El año es requerido"}, status=400) + try: + year = int(year) + except ValueError: + return Response({"error": "El año debe ser un número"}, status=400) + holidays_year = holidays.CO(years=year).items() + return Response(holidays_year, status=200) \ No newline at end of file diff --git a/INSIGHTSAPI/vacation/utils.py b/INSIGHTSAPI/vacation/utils.py new file mode 100644 index 0000000..0c0c4e2 --- /dev/null +++ b/INSIGHTSAPI/vacation/utils.py @@ -0,0 +1,15 @@ +"""Utility functions for the vacation app.""" + +import holidays + + +def is_working_day(date, sat_is_working=True): + """Check if a date is a working day.""" + us_holidays = holidays.CO(years=date.year) + if date.weekday() == 5: + return sat_is_working + elif date.weekday() == 6: + return False + elif date in us_holidays: + return False + return True \ No newline at end of file