diff --git a/waiting/serializers.py b/waiting/serializers.py index a59de41..feb9754 100644 --- a/waiting/serializers.py +++ b/waiting/serializers.py @@ -3,6 +3,7 @@ from booth.models import Booth from django.contrib.auth.models import User from django.utils import timezone +from datetime import timedelta # 나의 대기에서 부스 정보 확인용 !! - 아래의 시리얼라이저에서 사용됨 !! class BoothSerializer(serializers.ModelSerializer): @@ -27,12 +28,12 @@ class WaitingSerializer(serializers.ModelSerializer): username = serializers.CharField(source='user.username', read_only=True) registered_at = serializers.DateTimeField(read_only=True) waiting_teams_ahead = serializers.SerializerMethodField() # 내 앞에 있는 팀 수를 위한 필드 추가 - confirm_remaining_time = serializers.SerializerMethodField() # 3분 타이머 추가 - arrival_remaining_time = serializers.SerializerMethodField() # 10분 타이머 추가 + confirm_due_time = serializers.SerializerMethodField() # 3분 더한 시간 반환 + arrival_due_time = serializers.SerializerMethodField() # 10분 더한 시간 반환 class Meta: model = Waiting - fields = ['id', 'username', 'booth','party_size', 'waiting_status', 'registered_at', 'ready_to_confirm_at', 'confirmed_at', 'canceled_at', 'waiting_teams_ahead', 'confirm_remaining_time', 'arrival_remaining_time'] + fields = ['id', 'username', 'booth','party_size', 'waiting_status', 'registered_at', 'ready_to_confirm_at', 'confirmed_at', 'canceled_at', 'waiting_teams_ahead', 'confirm_due_time', 'arrival_due_time'] def get_waiting_teams_ahead(self, obj): return Waiting.objects.filter( @@ -41,21 +42,17 @@ def get_waiting_teams_ahead(self, obj): waiting_status='waiting' ).count() - # 3분 타이머 필드 - def get_confirm_remaining_time(self, obj): - if obj.waiting_status == 'ready_to_confirm' and obj.ready_to_confirm_at: - elapsed_time = (timezone.now() - obj.ready_to_confirm_at).total_seconds() - minutes, seconds = divmod(max(0, 180 - elapsed_time), 60) - return f'{int(minutes)}:{int(seconds)}' - return "00:00" + # 3분 더한 시간 반환 + def get_confirm_due_time(self, obj): + if obj.ready_to_confirm_at: + return obj.ready_to_confirm_at + timedelta(minutes=3) + return None - # 10분 타이머 필드 - def get_arrival_remaining_time(self, obj): - if obj.waiting_status == 'confirmed' and obj.confirmed_at: - elapsed_time = (timezone.now() - obj.confirmed_at).total_seconds() - minutes, seconds = divmod(max(0, 600 - elapsed_time), 60) - return f'{int(minutes)}:{int(seconds)}' - return "00:00" + # 10분 더한 시간 반환 + def get_arrival_due_time(self, obj): + if obj.confirmed_at: + return obj.confirmed_at + timedelta(minutes=10) + return None # 나의 웨이팅 디테일 class WaitingDetailSerializer(serializers.ModelSerializer): @@ -63,12 +60,13 @@ class WaitingDetailSerializer(serializers.ModelSerializer): username = serializers.CharField(source='user.username', read_only=True) registered_at = serializers.DateTimeField(read_only=True) waiting_teams_ahead = serializers.SerializerMethodField() - confirm_remaining_time = serializers.SerializerMethodField() # 3분 타이머 추가 - arrival_remaining_time = serializers.SerializerMethodField() # 10분 타이머 추가 + total_waiting_teams = serializers.SerializerMethodField() + confirm_due_time = serializers.SerializerMethodField() # 3분 더한 시간 반환 + arrival_due_time = serializers.SerializerMethodField() # 10분 더한 시간 반환 class Meta: model = Waiting - fields = ['id', 'username', 'booth', 'party_size', 'waiting_status', 'registered_at', 'ready_to_confirm_at', 'confirmed_at', 'canceled_at', 'waiting_teams_ahead', 'confirm_remaining_time', 'arrival_remaining_time'] + fields = ['id', 'username', 'booth', 'party_size', 'waiting_status', 'registered_at', 'ready_to_confirm_at', 'confirmed_at', 'canceled_at', 'waiting_teams_ahead', 'total_waiting_teams', 'confirm_due_time', 'arrival_due_time'] def get_waiting_teams_ahead(self, obj): return Waiting.objects.filter( @@ -76,22 +74,24 @@ def get_waiting_teams_ahead(self, obj): created_at__lt=obj.created_at, waiting_status='waiting' ).count() + + def get_total_waiting_teams(self, obj): + return Waiting.objects.filter( + booth=obj.booth, + waiting_status='waiting' + ).count() - # 3분 타이머 필드 - def get_confirm_remaining_time(self, obj): - if obj.waiting_status == 'ready_to_confirm' and obj.ready_to_confirm_at: - elapsed_time = (timezone.now() - obj.ready_to_confirm_at).total_seconds() - minutes, seconds = divmod(max(0, 180 - elapsed_time), 60) - return f'{int(minutes)}:{int(seconds)}' - return "00:00" + # 3분 더한 시간 반환 + def get_confirm_due_time(self, obj): + if obj.ready_to_confirm_at: + return obj.ready_to_confirm_at + timedelta(minutes=3) + return None - # 10분 타이머 필드 - def get_arrival_remaining_time(self, obj): - if obj.waiting_status == 'confirmed' and obj.confirmed_at: - elapsed_time = (timezone.now() - obj.confirmed_at).total_seconds() - minutes, seconds = divmod(max(0, 600 - elapsed_time), 60) - return f'{int(minutes)}:{int(seconds)}' - return "00:00" + # 10분 더한 시간 반환 + def get_arrival_due_time(self, obj): + if obj.confirmed_at: + return obj.confirmed_at + timedelta(minutes=10) + return None # 웨이팅 등록 관련 시리얼라이저 class WaitingCreateSerializer(serializers.ModelSerializer):