Django调度,如何检查日期是否在两个日期之间
目前我正在做另一个预订项目。这是我的预订模式:Django调度,如何检查日期是否在两个日期之间,django,date,schedule,Django,Date,Schedule,目前我正在做另一个预订项目。这是我的预订模式: class Reservation(models.Model): user = models.ForeignKey(User, null = True, blank = True) employee = models.ForeignKey(Employee) service = models.ForeignKey(Service) starttime = models.DateTimeField('reservati
class Reservation(models.Model):
user = models.ForeignKey(User, null = True, blank = True)
employee = models.ForeignKey(Employee)
service = models.ForeignKey(Service)
starttime = models.DateTimeField('reservation start')
endtime = models.DateTimeField('reservation end')
def __unicode__(self):
return u"Nr: %s" % self.id
下面是我想象我的应用程序应该如何工作——用户选择员工。用户选择服务(持续时间取决于特定服务)。然后用户选择日历上的日期。现在,日期被传递给方法,该方法每隔30分钟检查所选员工是否可用。然后显示所有可用的预订时间。例如:
这对我来说是一个巨大的障碍,因为我不知道如何处理这个问题
我的第一个想法是,我可以根据用户、员工id、持续时间选择日期,并在while循环中每30分钟迭代一次工作时间:
time_interval = 30 #interval
working_day_start = 10 #starts working at 10 am
working_day_end = 20 #ends working at 8 pm
duration = service_duration #how long service takes
start = choosen_date + datetime.timedelta(hours = working_day_start)
end = choosen_date + datetime.timedelta(hours = working_day_end)
availibility_table = []
while start <= end – datetime.timedelta(hours = duration):
is_available = employee.isAvailable(start, employee_id, duration)
if is_available:
availibility_date = [start, start + datetime.timedelta(hours = duration)]
availibility_table.append(availibility_date)
start += datetime.timedelta(minutes = time_interval)
return availability_table
这应该起作用:
def isAvailable(start, employee_id, duration):
employee = Employee.objects.get(pk=employee_id)
# See if the employee has any reservations that overlap
# with this window. The logic here is that the *start* of
# the reservation is before the *end* of the time period we
# are checking, and the *end* of the reservation is after the
# *start* of the time period we are checking.
this_period_end = start + datetime.timedelta(hours=duration)
existing_reservations = employee.reservation_set.filter(
starttime__lte=this_period_end,
endtime__gte=start
)
# If we found matches, then the employee is busy for some part of
# this period
return not existing_reservations.exists()
它确实意味着对您要测试的每个时段进行查询。从概念上讲,我觉得必须有一个更有效的解决方案,但目前我还没有找到。在任何情况下,只要确认此逻辑有效,您就应该能够对其进行细化。您可以将您的
员工
模式添加到帖子中吗?这可能会有所帮助。@MiteshNinja我添加了员工模型。这是尽可能简单的。它似乎工作,但显然我不得不改变lte和gte运营商的lt和gt。否则会有30分钟的偏移。很好的回答我真的很感谢你的帮助!
class Employee(models.Model):
first_name = models.CharField(max_length = 30, blank = False)
last_name = models.CharField(max_length = 30, blank = False)
def __unicode__(self):
return self.first_name + ' ' + self.last_name
def isAvailable(start, employee_id, duration):
employee = Employee.objects.get(pk=employee_id)
# See if the employee has any reservations that overlap
# with this window. The logic here is that the *start* of
# the reservation is before the *end* of the time period we
# are checking, and the *end* of the reservation is after the
# *start* of the time period we are checking.
this_period_end = start + datetime.timedelta(hours=duration)
existing_reservations = employee.reservation_set.filter(
starttime__lte=this_period_end,
endtime__gte=start
)
# If we found matches, then the employee is busy for some part of
# this period
return not existing_reservations.exists()