본문 바로가기

etc

django 예약기능

@마크다운




def validate(self, data, **kwargs):
start_lesson_date = data['start_lesson_date']
end_lesson_date = data['end_lesson_date']
tutor = get_object_or_404(Tutor, pk=data['tutor'].pk)
customer = get_object_or_404(SnowPeopleUser, pk=data['customer'].pk)
print(tutor, customer)
schedule_of_lesson = (end_lesson_date + timedelta(hours=1)) - start_lesson_date
reservated_list = [start_lesson_date + timedelta(hours=n) for n in range(schedule_of_lesson.seconds // 3600)]
disable_date_list = [i.date for i in tutor.disable_days.all()]
print(disable_date_list)
for day in reservated_list:
if day in disable_date_list:
raise serializers.ValidationError('이미 예약이 되어있는 날짜를 선택하셨습니다.')

return data




def save(self, *args, **kwargs):
start_lesson_date = self.validated_data.get('start_lesson_date', '')
end_lesson_date = self.validated_data.get('end_lesson_date', '')
customer = get_object_or_404(SnowPeopleUser, pk=self.validated_data.get('customer', '').pk)
tutor = get_object_or_404(Tutor, pk=self.validated_data.get('tutor', '').pk)
Reservation.objects.create(
customer=customer,
tutor=tutor,
start_lesson_date=start_lesson_date,
end_lesson_date=end_lesson_date,
reservation_person=self.initial_data.get('reservation_person'),
)

schedule_of_lesson = (end_lesson_date + timedelta(hours=1)) - start_lesson_date
reservated_list = [start_lesson_date + timedelta(hours=n) for n in range(schedule_of_lesson.seconds // 3600)]
for date in reservated_list:
date_instance, _ = disable_time.objects.get_or_create(tutor=tutor, date=date)
tutor.disable_days.add(date_instance)


이건 그냥 안까먹으려고 적어둠