Skip to content

Commit

Permalink
Add holiday endpoint to retrieve holidays for a specific year
Browse files Browse the repository at this point in the history
  • Loading branch information
Heibert committed Jul 15, 2024
1 parent 79fefae commit 353f8f4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
19 changes: 19 additions & 0 deletions INSIGHTSAPI/services/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
3 changes: 2 additions & 1 deletion INSIGHTSAPI/services/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
14 changes: 14 additions & 0 deletions INSIGHTSAPI/services/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
15 changes: 15 additions & 0 deletions INSIGHTSAPI/vacation/utils.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 353f8f4

Please sign in to comment.