Skip to content

Commit

Permalink
update booking.py
Browse files Browse the repository at this point in the history
  • Loading branch information
ikramovna committed Feb 14, 2025
1 parent 75463ac commit b6035af
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
37 changes: 16 additions & 21 deletions beauty/serializers/booking.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ def validate(self, attrs):



from rest_framework import serializers
from django.utils import timezone
from beauty.models.service import Service
from beauty.models.booking import Time, WorkingDays

class MasterFreeTimeSerializer(serializers.Serializer):
date = serializers.DateField()
service_ids = serializers.ListField(child=serializers.IntegerField())
Expand All @@ -69,13 +74,12 @@ def validate(self, attrs):
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
day = days[day_index]

# for service_id in service_ids:
# service = Service.objects.get(id=service_id)
# master_working_time = Time.objects.filter(user=service.user, day__day=day)
#
# if not master_working_time.exists():
# raise serializers.ValidationError("The requested date is not a working day for the master")
for service_id in service_ids:
service = Service.objects.get(id=service_id)
master_working_time = Time.objects.filter(user=service.user, day=day_index)

if not master_working_time.exists():
raise serializers.ValidationError("The requested date is not a working day for the master")
return attrs

def get_free_times(self):
Expand Down Expand Up @@ -138,21 +142,6 @@ def validate(self, attrs):
current_date = timezone.now().date()
current_time = timezone.now().time()

for service_id in service_ids:
if not Service.objects.filter(pk=service_id).exists():
raise serializers.ValidationError(f"Service with ID {service_id} does not exist")

day_index = date.weekday()
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
day = days[day_index]

for service_id in service_ids:
service = Service.objects.get(id=service_id)
master_working_time = Time.objects.filter(user=service.user, day__day=day)

if not master_working_time.exists():
raise serializers.ValidationError("The requested date is not a working day for the master")

if not service_ids:
raise serializers.ValidationError({"service_ids": ["At least one service must be selected"]})
if not all(isinstance(service_id, int) for service_id in service_ids):
Expand All @@ -171,6 +160,12 @@ def validate(self, attrs):
if date == current_date and datetime.strptime(time, "%H:%M").time() < current_time:
raise serializers.ValidationError("The time cannot be in the past")

day_index = date.weekday()
for service in services:
master_working_time = Time.objects.filter(user=service.user, day=day_index)
if not master_working_time.exists():
raise serializers.ValidationError("The requested date is not a working day for the master")

return attrs

def to_representation(self, instance):
Expand Down
1 change: 0 additions & 1 deletion beauty/views/booking.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ class BookingUpdateAPIView(UpdateAPIView, DestroyAPIView):
Example request:
# pending approved rejected
"""
queryset = Booking.objects.all()
serializer_class = BookingUpdateSerializer
Expand Down

0 comments on commit b6035af

Please sign in to comment.