django日历忙/闲/可用

mho*_*ost 8 python django calendar

我正在尝试实施一个日历系统,能够安排其他人进行约会.系统必须能够防止在另一个约会期间或在其不可用时间期间安排一个人.

我已经查看了我在互联网上找到的所有现有django日历项目,似乎没有一个内置它们(如果我错过了某种方式,请告诉我).

也许我只是太累了,但我能想到这样做的唯一方法似乎有点混乱.这里是伪代码:

  • 当用户尝试创建新约会时,请抓住新约会的start_time和end_time
  • 对于同一天的每次约会,检查是否
    • existing_start_time <new_start_time AND existing_end_time> new_start_time(是任何现有约会的开始和结束时间之间的新约会开始时间)
    • existing_start_time <new_end_time AND existing_end_time> new_end_time(是任何现有约会的开始和结束时间之间的新约会结束时间)
  • 如果没有找到对象,则继续添加新约会

考虑到Django没有基于时间的过滤,这必须使用查询集上的.extra()来完成.

所以,我在问是否有更好的方法.一个pythonic技巧或模块或任何可能简化这一点的东西.或者是一个现有的项目,它有我需要的东西,或者可以引导我朝着正确的方向前进

谢谢.

Nic*_*sta 13

怎么样使用Django的范围测试.

例如:

appoinment = Appointment()
appointment.start_time = datetime.datetime.now()
# 1 hour appointment
appointment.end_time = appointment.start_time + datetime.timedelta(hours=1)
# more stuff here
appointment.save()

# Checking for collision
# where the start time for an appointment is between the the start and end times
# You would want to filter this on user, etc 
# There is also a problem if you book an appointment within another appointment
start_conflict = Appointment.objects.filter(
                     start_time__range=(appointment.start_time,
                                        appointment.end_time))
end_conflict = Appointment.objects.filter(
                   end_time__range=(appointment.start_time,
                                    appointment.end_time))

during_conflict = Appointment.objects.filter(
                      start_date__lte=appointment.start_time, 
                      end_date__gte=appointment.end_time)

if (start_conflict or end_conflict or during_conflict):
    # reject, for there is a conflict
Run Code Online (Sandbox Code Playgroud)

那样的东西?我自己没试过,所以你可能需要调整一下.

编辑:添加了during_conflict位.