Skip to content

Commit

Permalink
Update vacation app URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
Heibert committed Jul 18, 2024
1 parent b078390 commit 455e644
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 35 deletions.
71 changes: 39 additions & 32 deletions INSIGHTSAPI/vacation/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def test_vacation_create(self):
self.vacation_request["hr_approbation"] = True # This is just a check
self.vacation_request["mon_to_sat"] = False
response = self.client.post(
reverse("vacation-list"),
reverse("vacation-request-list"),
self.vacation_request,
)
self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data)
Expand All @@ -74,7 +74,7 @@ def test_vacation_create(self):
def test_vacation_create_no_mon_to_sat(self):
"""Test creating a vacation without mon_to_sat."""
response = self.client.post(
reverse("vacation-list"),
reverse("vacation-request-list"),
self.vacation_request,
)
self.assertEqual(
Expand All @@ -92,7 +92,7 @@ def test_vacation_create_same_month(self):
self.vacation_request["mon_to_sat"] = False
self.vacation_request["start_date"] = "2024-07-22"
response = self.client.post(
reverse("vacation-list"),
reverse("vacation-request-list"),
self.vacation_request,
)
self.assertEqual(
Expand All @@ -111,7 +111,7 @@ def test_vacation_list_user(self):
self.vacation_request["user"] = self.user
self.vacation_request["uploaded_by"] = self.test_user
VacationRequest.objects.create(**self.vacation_request)
response = self.client.get(reverse("vacation-list"))
response = self.client.get(reverse("vacation-request-list"))
vacation_requests = VacationRequest.objects.filter(user=self.user)
serializer = VacationRequestSerializer(vacation_requests, many=True)
self.assertEqual(
Expand All @@ -128,7 +128,7 @@ def test_vacation_list_hr(self):
self.vacation_request["user"] = self.test_user
self.vacation_request["uploaded_by"] = self.test_user
VacationRequest.objects.create(**self.vacation_request)
response = self.client.get(reverse("vacation-list"))
response = self.client.get(reverse("vacation-request-list"))
vacation_requests = VacationRequest.objects.all()
serializer = VacationRequestSerializer(vacation_requests, many=True)
self.assertEqual(response.data, serializer.data)
Expand All @@ -153,7 +153,7 @@ def test_vacation_list_manager(self):
self.vacation_request["user"] = demo_user_admin
self.vacation_request["uploaded_by"] = self.test_user
VacationRequest.objects.create(**self.vacation_request)
response = self.client.get(reverse("vacation-list"))
response = self.client.get(reverse("vacation-request-list"))
vacation_requests = VacationRequest.objects.filter(
(Q(uploaded_by=self.user) | Q(user=self.user))
| (
Expand All @@ -172,7 +172,7 @@ def test_vacation_retrieve(self):
self.vacation_request["uploaded_by"] = self.user
vacation_object = VacationRequest.objects.create(**self.vacation_request)
response = self.client.get(
reverse("vacation-detail", kwargs={"pk": vacation_object.pk})
reverse("vacation-request-detail", kwargs={"pk": vacation_object.pk})
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(
Expand All @@ -188,7 +188,7 @@ def test_vacation_create_end_before_start(self):
self.vacation_request["end_date"] = "2021-01-04"
self.vacation_request["mon_to_sat"] = False
response = self.client.post(
reverse("vacation-list"),
reverse("vacation-request-list"),
self.vacation_request,
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
Expand All @@ -204,7 +204,7 @@ def test_vacation_create_invalid_rank(self):
demo_user.job_position.rank = 8
demo_user.job_position.save()
response = self.client.post(
reverse("vacation-list"),
reverse("vacation-request-list"),
self.vacation_request,
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
Expand All @@ -218,7 +218,7 @@ def test_vacation_create_invalid_user(self):
self.vacation_request["mon_to_sat"] = False
self.vacation_request["user"] = self.user.pk
response = self.client.post(
reverse("vacation-list"),
reverse("vacation-request-list"),
self.vacation_request,
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
Expand All @@ -234,7 +234,7 @@ def test_vacation_create_invalid_user(self):
# self.vacation_request["uploaded_by"] = self.user
# vacation_object = VacationRequest.objects.create(**self.vacation_request)
# response = self.client.patch(
# reverse("vacation-detail", kwargs={"pk": vacation_object.pk}),
# reverse("vacation-request-detail", kwargs={"pk": vacation_object.pk}),
# {"status": "CANCELADA"},
# )
# self.assertEqual(response.status_code, status.HTTP_200_OK, response.data)
Expand All @@ -246,7 +246,7 @@ def test_vacation_owner_cancel_no_owner(self):
self.vacation_request["uploaded_by"] = self.test_user
vacation_object = VacationRequest.objects.create(**self.vacation_request)
response = self.client.patch(
reverse("vacation-detail", kwargs={"pk": vacation_object.pk}),
reverse("vacation-request-detail", kwargs={"pk": vacation_object.pk}),
{"status": "CANCELADA"},
)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN, response.data)
Expand All @@ -259,7 +259,7 @@ def test_vacation_manager_approve(self):
self.vacation_request["uploaded_by"] = self.user
vacation_object = VacationRequest.objects.create(**self.vacation_request)
response = self.client.patch(
reverse("vacation-detail", kwargs={"pk": vacation_object.pk}),
reverse("vacation-request-detail", kwargs={"pk": vacation_object.pk}),
{"manager_approbation": True},
)
self.assertEqual(response.status_code, status.HTTP_200_OK, response.data)
Expand All @@ -275,7 +275,7 @@ def test_vacation_manager_reject(self):
self.vacation_request["uploaded_by"] = self.user
vacation_object = VacationRequest.objects.create(**self.vacation_request)
response = self.client.patch(
reverse("vacation-detail", kwargs={"pk": vacation_object.pk}),
reverse("vacation-request-detail", kwargs={"pk": vacation_object.pk}),
{"manager_approbation": False},
)
self.assertEqual(response.status_code, status.HTTP_200_OK, response.data)
Expand All @@ -290,7 +290,7 @@ def test_vacation_manager_approve_no_manager(self):
self.vacation_request["uploaded_by"] = self.user
vacation_object = VacationRequest.objects.create(**self.vacation_request)
response = self.client.patch(
reverse("vacation-detail", kwargs={"pk": vacation_object.pk}),
reverse("vacation-request-detail", kwargs={"pk": vacation_object.pk}),
{"manager_approbation": True},
)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN, response.data)
Expand All @@ -305,7 +305,7 @@ def test_vacation_hr_approve(self):
self.vacation_request["manager_approbation"] = True
vacation_object = VacationRequest.objects.create(**self.vacation_request)
response = self.client.patch(
reverse("vacation-detail", kwargs={"pk": vacation_object.pk}),
reverse("vacation-request-detail", kwargs={"pk": vacation_object.pk}),
{"hr_approbation": True},
)
self.assertEqual(response.status_code, status.HTTP_200_OK, response.data)
Expand All @@ -322,7 +322,7 @@ def test_vacation_hr_reject(self):
self.vacation_request["manager_approbation"] = True
vacation_object = VacationRequest.objects.create(**self.vacation_request)
response = self.client.patch(
reverse("vacation-detail", kwargs={"pk": vacation_object.pk}),
reverse("vacation-request-detail", kwargs={"pk": vacation_object.pk}),
{"hr_approbation": False},
)
self.assertEqual(response.status_code, status.HTTP_200_OK, response.data)
Expand All @@ -339,7 +339,7 @@ def test_vacation_hr_approve_before_manager(self):
self.vacation_request["uploaded_by"] = self.user
vacation_object = VacationRequest.objects.create(**self.vacation_request)
response = self.client.patch(
reverse("vacation-detail", kwargs={"pk": vacation_object.pk}),
reverse("vacation-request-detail", kwargs={"pk": vacation_object.pk}),
{"hr_approbation": True},
)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN, response.data)
Expand All @@ -350,7 +350,7 @@ def test_vacation_hr_approve_no_hr(self):
self.vacation_request["uploaded_by"] = self.user
vacation_object = VacationRequest.objects.create(**self.vacation_request)
response = self.client.patch(
reverse("vacation-detail", kwargs={"pk": vacation_object.pk}),
reverse("vacation-request-detail", kwargs={"pk": vacation_object.pk}),
{"hr_approbation": True},
)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN, response.data)
Expand All @@ -363,7 +363,7 @@ def test_vacation_payroll_approve(self):
self.vacation_request["hr_approbation"] = True
vacation_object = VacationRequest.objects.create(**self.vacation_request)
response = self.client.patch(
reverse("vacation-detail", kwargs={"pk": vacation_object.pk}),
reverse("vacation-request-detail", kwargs={"pk": vacation_object.pk}),
{"payroll_approbation": True},
)
self.assertEqual(response.status_code, status.HTTP_200_OK, response.data)
Expand All @@ -379,7 +379,7 @@ def test_vacation_payroll_reject(self):
self.vacation_request["hr_approbation"] = True
vacation_object = VacationRequest.objects.create(**self.vacation_request)
response = self.client.patch(
reverse("vacation-detail", kwargs={"pk": vacation_object.pk}),
reverse("vacation-request-detail", kwargs={"pk": vacation_object.pk}),
{"payroll_approbation": False},
)
self.assertEqual(response.status_code, status.HTTP_200_OK, response.data)
Expand All @@ -395,7 +395,7 @@ def test_vacation_payroll_approve_before_hr(self):
self.vacation_request["uploaded_by"] = self.user
vacation_object = VacationRequest.objects.create(**self.vacation_request)
response = self.client.patch(
reverse("vacation-detail", kwargs={"pk": vacation_object.pk}),
reverse("vacation-request-detail", kwargs={"pk": vacation_object.pk}),
{"payroll_approbation": True},
)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN, response.data)
Expand All @@ -406,7 +406,7 @@ def test_vacation_payroll_approve_no_payroll(self):
self.vacation_request["uploaded_by"] = self.user
vacation_object = VacationRequest.objects.create(**self.vacation_request)
response = self.client.patch(
reverse("vacation-detail", kwargs={"pk": vacation_object.pk}),
reverse("vacation-request-detail", kwargs={"pk": vacation_object.pk}),
{"payroll_approbation": True},
)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN, response.data)
Expand All @@ -418,7 +418,7 @@ def test_validate_vacation_request_after_20th(self):
self.vacation_request["mon_to_sat"] = False
self.vacation_request["start_date"] = "2024-08-12"
response = self.client.post(
reverse("vacation-list"),
reverse("vacation-request-list"),
self.vacation_request,
)
self.assertEqual(
Expand All @@ -434,7 +434,7 @@ def test_validate_vacation_request_not_working_day(self):
self.vacation_request["mon_to_sat"] = False
self.vacation_request["start_date"] = "2024-01-01"
response = self.client.post(
reverse("vacation-list"),
reverse("vacation-request-list"),
self.vacation_request,
)
self.assertEqual(
Expand All @@ -450,7 +450,7 @@ def test_validate_vacation_request_not_working_day_sat(self):
self.vacation_request["mon_to_sat"] = False
self.vacation_request["start_date"] = "2024-05-04"
response = self.client.post(
reverse("vacation-list"),
reverse("vacation-request-list"),
self.vacation_request,
)
self.assertEqual(
Expand All @@ -468,7 +468,7 @@ def test_validate_vacation_request_not_working_day_sat_working(self):
self.vacation_request["start_date"] = "2024-05-04"
self.vacation_request["end_date"] = "2024-05-06"
response = self.client.post(
reverse("vacation-list"),
reverse("vacation-request-list"),
self.vacation_request,
)
self.assertEqual(
Expand All @@ -480,7 +480,7 @@ def test_validate_vacation_request_not_working_day_end(self):
self.vacation_request["mon_to_sat"] = False
self.vacation_request["end_date"] = "2024-01-01"
response = self.client.post(
reverse("vacation-list"),
reverse("vacation-request-list"),
self.vacation_request,
)
self.assertEqual(
Expand All @@ -496,7 +496,7 @@ def test_validate_vacation_request_not_working_day_sat_end(self):
self.vacation_request["mon_to_sat"] = False
self.vacation_request["end_date"] = "2024-05-04"
response = self.client.post(
reverse("vacation-list"),
reverse("vacation-request-list"),
self.vacation_request,
)
self.assertEqual(
Expand All @@ -514,7 +514,7 @@ def test_validate_vacation_request_not_working_day_sat_working_end(self):
self.vacation_request["start_date"] = "2024-05-03"
self.vacation_request["end_date"] = "2024-05-04"
response = self.client.post(
reverse("vacation-list"),
reverse("vacation-request-list"),
self.vacation_request,
)
self.assertEqual(
Expand All @@ -526,7 +526,7 @@ def test_validate_vacation_request_more_than_15_days(self):
self.vacation_request["mon_to_sat"] = False
self.vacation_request["end_date"] = "2024-01-24"
response = self.client.post(
reverse("vacation-list"),
reverse("vacation-request-list"),
self.vacation_request,
)
self.assertEqual(
Expand All @@ -535,4 +535,11 @@ def test_validate_vacation_request_more_than_15_days(self):
self.assertEqual(
response.data["non_field_errors"][0],
"No puedes solicitar más de 15 días de vacaciones.",
)
)

# def test_get_working_days_view(self):
# """Test the get_working_days view."""
# query_params = {"start_date": "2024-01-01", "end_date": "2024-01-31", "mon_to_sat": False}
# response = self.client.get(reverse("get-working-days"), query_params)
# self.assertEqual(response.status_code, status.HTTP_200_OK, response.data)
# self.assertEqual(response.data["working_days"], 21)
7 changes: 6 additions & 1 deletion INSIGHTSAPI/vacation/urls.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
"""This file contains the URL patterns for the vacation app."""
from rest_framework.routers import DefaultRouter
# from django.urls import path, include
from .views import VacationRequestViewSet

router = DefaultRouter()
router.register("", VacationRequestViewSet, basename="vacation")
router.register("request", VacationRequestViewSet, basename="vacation-request")

# urlpatterns = [
# path("", include(router.urls)),
# path("get-working-days/", get_working_days_view, name="get-working-days"),
# ]
urlpatterns = router.urls
5 changes: 5 additions & 0 deletions INSIGHTSAPI/vacation/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import holidays
from datetime import datetime, timedelta
from distutils.util import strtobool


def is_working_day(date, sat_is_working=True):
"""Check if a date is a working day."""
if isinstance(sat_is_working, str):
sat_is_working = bool(strtobool(sat_is_working))
if isinstance(date, str):
date = datetime.strptime(date, "%Y-%m-%d")
co_holidays = holidays.CO(years=date.year)
Expand All @@ -21,6 +24,8 @@ def is_working_day(date, sat_is_working=True):
def get_working_days(start_date, end_date, sat_is_working=True):
"""Get the number of working days between two dates."""
working_days = 0
if isinstance(sat_is_working, str):
sat_is_working = bool(strtobool(sat_is_working))
if isinstance(start_date, str):
start_date = datetime.strptime(start_date, "%Y-%m-%d")
if isinstance(end_date, str):
Expand Down
40 changes: 38 additions & 2 deletions INSIGHTSAPI/vacation/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
from rest_framework import viewsets
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.decorators import api_view
from notifications.utils import create_notification
from django.utils import timezone
from django.core.mail import mail_admins
from django.core.mail import send_mail
from users.models import User
from .models import VacationRequest
from .serializers import VacationRequestSerializer
from .utils import get_working_days


class VacationRequestViewSet(viewsets.ModelViewSet):
Expand Down Expand Up @@ -210,7 +211,7 @@ def partial_update(self, request, *args, **kwargs):
status=status.HTTP_400_BAD_REQUEST,
)
response = super().partial_update(request, *args, **kwargs)
if (
if (
response.status_code == status.HTTP_200_OK
and response.data
and response.data["hr_approbation"]
Expand Down Expand Up @@ -269,3 +270,38 @@ def partial_update(self, request, *args, **kwargs):
{"detail": "You do not have permission to perform this action."},
status=status.HTTP_403_FORBIDDEN,
)


# @api_view(["GET"])
# def get_working_days_view(request):
# start_date = request.query_params.get("start_date")
# end_date = request.query_params.get("end_date")
# mon_to_sat = request.query_params.get("mon_to_sat")
# if not start_date or not end_date or not mon_to_sat:
# return Response(
# {
# "detail": "Por favor, proporciona una fecha de inicio, una fecha de finalización y un valor booleano para mon_to_sat."
# },
# status=status.HTTP_400_BAD_REQUEST,
# )
# try:
# mon_to_sat = bool(strtobool(mon_to_sat))
# except:
# return Response(
# {
# "detail": "Por favor, proporciona un valor booleano para mon_to_sat."
# },
# status=status.HTTP_400_BAD_REQUEST,
# )
# try:
# start_date = datetime.datetime.strptime(start_date, "%Y-%m-%d")
# end_date = datetime.datetime.strptime(end_date, "%Y-%m-%d")
# except:
# return Response(
# {
# "detail": "Por favor, proporciona una fecha válida en el formato YYYY-MM-DD."
# },
# status=status.HTTP_400_BAD_REQUEST,
# )
# working_days = get_working_days(start_date, end_date, mon_to_sat)
# return Response({"working_days": working_days})

0 comments on commit 455e644

Please sign in to comment.