作成した予約システムで正しく予約することができるようにしたい
カレンダーより、日付と時間を選択したところ、予約確認画面で不正な日付と時間が表示され、登録後エラーが発生してしまう
例)2024年7月21日18:00を選択すると、確認画面には"2024年07月211日08:00"と表示されてしまう
https://www.youtube.com/watch?v=QTV4BGeoa9Y&list=PLoSZs76tLtJi7P0S4MN1tLNPB5TYD1gOt&index=3
この動画の24:00辺りです
'''class CalendarView(View):
def get(self, request, *args, **kwargs):
staff_data = Staff.objects.filter(id=self.kwargs['pk']).select_related('user').select_related('store')[0]
today = date.today()
year = self.kwargs.get('year')
month = self.kwargs.get('month')
day = self.kwargs.get('day')
if year and month and day:
start_date = date(year=year, month=month, day=day)
else:
start_date = today
days = [start_date + timedelta(days=day) for day in range(7)]
start_day = days[0]
end_day = days[-1]
calendar = {}
for hour in range(10, 21):
row = {}
for day in days:
row[day] = True
calendar[hour] = row
start_time = make_aware(datetime.combine(start_day, time(hour=10, minute=0, second=0)))
end_time = make_aware(datetime.combine(end_day, time(hour=20, minute=0, second=0)))
booking_data = Booking.objects.filter(staff=staff_data).exclude(Q(start__gt=end_time) | Q(end__lt=start_time))
for booking in booking_data:
local_time = localtime(booking.start)
booking_date = local_time.date()
booking_hour = local_time.hour
if (booking_hour in calendar) and (booking_date in calendar[booking_hour]):
calendar[booking_hour][booking_date] = False
return render(request, 'app/calendar.html', {
'staff_data': staff_data,
'calendar': calendar,
'days': days,
'start_day': start_day,
'end_day': end_day,
'before': days[0] - timedelta(days=7),
'next': days[-1] + timedelta(days=1),
'today': today
})
class BookingView(View):
def get(self, request, *args, **kwargs):
staff_data = Staff.objects.filter(id=self.kwargs['pk']).select_related('user').select_related('store')[0]
year = self.kwargs.get('year')
month = self.kwargs.get('month')
day = self.kwargs.get('day')
hour = self.kwargs.get('hour')
form = BookingForm(request.POST or None)
return render(request, 'app/booking.html', {
'staff_data': staff_data,
'year': year,
'month': month,
'day': day,
'hour': hour,
'form': form
})
def post(self, request, *args, **kwargs):
staff_data = get_object_or_404(Staff, id=self.kwargs['pk'])
year = self.kwargs.get('year')
month = self.kwargs.get('month')
day = self.kwargs.get('day')
hour = self.kwargs.get('hour')
start_time = make_aware(datetime(year=year, month=month, day=day, hour=hour))
end_time = make_aware(datetime(year=year, month=month, day=day, hour=hour + 1))
booking_data = Booking.objects.filter(staff=staff_data, start=start_time)
form = BookingForm(request.POST or None)
if booking_data.exists():
form.add_error(None, '既に予約があります。\n別の日時で予約をお願いします。')
else:
if form.is_valid():
booking = Booking()
booking.staff = staff_data
booking.start = start_time
booking.end = end_time
booking.first_name = form.cleaned_data['first_name']
booking.last_name = form.cleaned_data['last_name']
booking.tel = form.cleaned_data['tel']
booking.remarks = form.cleaned_data['remarks']
booking.save()
return redirect('store')
return render(request, 'app/booking.html', {
'staff_data': staff_data,
'year': year,
'month': month,
'day': day,
'hour': hour,
'form': form
})'''