我正在为我的 a-level 类(class)构建一个酒店预订系统,我的问题是当用户预订房间时,我想确保没有其他用户可以在房间被占用的时间段内预订该房间.
我尝试使用迭代来遍历日期,并有一个 bool 值,当房间被预订时设置为 true,当房间未预订时设置为 false。
这是我的模型
class Room(models.Model):
name = models.CharField(max_length = 200)
img = models.ImageField(upload_to='Pictures')
desc = models.TextField()
price = models.IntegerField()
is_reserved = models.BooleanField(default=False)
number_of_people = models.PositiveIntegerField()
def __str__(self):
return self.name
class Meta:
verbose_name = 'Room'
verbose_name_plural = 'Rooms'
class Reservation(models.Model):
check_in = models.DateField(default=timezone.now)
check_out = models.DateField()
room = models.ForeignKey(Room, on_delete = models.CASCADE)
guest = models.ForeignKey(User, on_delete= models.CASCADE)
class Meta:
verbose_name = 'Reservation'
verbose_name_plural = 'Reservations'
这是我在 View 中的功能,它将遍历用户输入的 checkin 和 checkout 日期,并确保没有其他用户可以为这些日期预订房间
def confirm(request, pk = None):
if request.method == 'POST':
if pk:
room_id = Room.objects.get(pk = pk)
guest_id = request.user
check_in = request.session['check_in']
check_out = request.session['check_out']
reservation = Reservation(
check_in = check_in,
check_out = check_out,
room_id = room_id.id,
guest_id = guest_id.id
)
reservation.save()
book_in = datetime.strptime(check_in, '%Y-%m-%d').date()
book_out = datetime.strptime(check_out, '%Y-%m-%d').date()
reserved = False
delta = timedelta(days = 1)
while book_in <= book_out:
room_id.reserved = True
book_in += delta
else:
room_id.reserved = False
return render(request, "system/reserve.html", args)
如果入住和退房日期与同一房间的预订表中的日期一致,我想显示房间已预订的 html 页面
<body>
<form method="POST" action="{% url 'system:confirm' room.id %}">
{% csrf_token %}
<h1>{{room.name}}</h1>
<img src="{{room.img.url}}">
<h3 >£{{room.price}}</h3>
<h5 >{{room.desc}}</h5>
{% if room_id.reserve == True %}
<h4>This room has been reserved</h4>
{% else %}
<button name="confirm_reservation" type="submit"> Reserve Room
</button>
{% endif %}
</form>
</body>
如果入住日期或退房日期在预订表中并且介于入住日期和退房日期之间,我希望收到一条消息,表明房间已预订(显示在我的 html block 中)。例如:如果房间是从 2019 年 8 月 20 日到 2019 年 8 月 25 日预订的,那么如果我想预订我的房间,我的入住或退房日期不得介于 2019 年 8 月 20 日和 2019 年 8 月 25 日之间(包括在内)应该向我显示我不能的消息。相反,我可以随时预订房间,因为我的 bool 值始终为假。我想让该值仅在该时间段内为真,如果入住和退房不在预订表中的入住和退房日期之间,比如 2020 年 9 月 8 日至 2020 年 9 月 10 日,那么我的 bool 值应该是假的,我可以预订那个房间。
最佳答案
因此,您要做的是确保此房间没有与请求的入住和退房日期重叠的现有预订。我们可以将其分为三种情况:
- 没有现有预订的入住日期早于请求的入住日期,退房日期在请求的入住日期之后的任何时间结束。此外,为这两个字段添加索引将有助于我们在您的网站繁忙时加快查询速度
- 没有现有预订的入住日期在请求的退房日期之前开始,并且退房日期在请求的退房日期之后的任何时间结束
- 在两个要求的入住/退房日期之间有一个预订
下图说明了这些情况。蓝色代表请求的预订,而橙色代表使请求无效的现有预订——请注意第三种情况有错误,应交换颜色:
(来源:imge.to)
我们用两种情况覆盖这两种情况,然后使用 exists()
方法检查是否没有匹配的查询。如果存在任何匹配的预订,我们将返回同一页面,并显示错误。否则,我们继续确认请求的预订,然后重定向到另一个页面以显示成功消息:
views.py
from django.shortcuts import redirect
def confirm(request, pk = None):
if request.method == 'POST':
if pk:
invalid_dates = False
#get the room
room = Room.objects.get(pk = pk)
guest_id = request.user
check_in = request.session['check_in']
check_out = request.session['check_out']
# check wether the dates are valid
# case 1: a room is booked before the check_in date, and checks out after the requested check_in date
case_1 = Reservation.objects.filter(room=room, check_in__lte=check_in, check_out__gte=check_in).exists()
# case 2: a room is booked before the requested check_out date and check_out date is after requested check_out date
case_2 = Reservation.objects.filter(room=room, check_in__lte=check_out, check_out__gte=check_out).exists()
case_3 = Reservation.objects.filter(room=room, check_in__gte=check_in, check_out__lte=check_out).exists()
# if either of these is true, abort and render the error
if case_1 or case_2 or case_3:
return render(request, "system/reserve.html", {"errors": "This room is not available on your selected dates"})
# dates are valid
reservation = Reservation(
check_in = check_in,
check_out = check_out,
room_id = room.id,
guest_id = guest_id.id
)
reservation.save()
#redirect to success page (need to define this as a separate view)
return redirect("/reservation_success")
return render(request, "system/reserve.html", args)
html
body>
<p>{{errors}}</p>
<form method="POST" action="{% url 'system:confirm' room.id %}">
{% csrf_token %}
<h1>{{room.name}}</h1>
<img src="{{room.img.url}}">
<h3 >£{{room.price}}</h3>
<h5 >{{room.desc}}</h5>
{% if room_id.reserve == True %}
<h4>This room has been reserved</h4>
{% else %}
<button name="confirm_reservation" type="submit"> Reserve Room
</button>
{% endif %}
</form>
</body>
注意事项
- 这个解决方案不是最优的。我们正在执行 2 个可以合并为一个的查询,但这需要使用逻辑 OR,并使用 Django 的
Q
- 为了更好的设计,最好使用
表单
进行验证,并在其中添加验证。但为了您的项目,这会奏效。 - 您的房间模型中不需要 bool 字段,因为我们会根据入住和退房日期处理检查。
关于python - Django 中的酒店预订系统 : How to make a room unavailable to other users for a period of time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57545684/