django日历忙/闲/可用性

时间:2010-02-16 07:17:53

标签: 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技巧或模块或任何可能简化这一点的东西。或者是一个现有的项目,它有我需要的东西,或者可以引导我朝着正确的方向前进。

感谢。

2 个答案:

答案 0 :(得分:13)

如何使用Django的range test

例如:

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

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

编辑:添加了during_conflict位。

答案 1 :(得分:0)

这里需要注意的是不同用户的不同时区,并将日光节约时间变得非常复杂。

您可能需要查看 pytz 模块来处理时区问题。

相关问题