2010-02-16 18 views
8

我想實現一個日曆系統,能夠安排其他人約會。系統必須能夠防止在另一次約會期間或在其不可用時間內安排某人。django日曆忙/閒/可用性

我看過了我在互聯網上找到的所有現有的django日曆項目,它們都沒有嵌入到它們中(如果我錯過了它,請讓我知道)。也許我只是太累了,但我能想到做這件事的唯一方式似乎有點混亂。這裏去僞代碼:

    當用戶嘗試創建一個新的約會,抓住新任命的START_TIME和END_TIME
  • 用於在同一天每個約會
  • ,檢查是否
    • existing_start_time < NEW_START_TIME和existing_end_time> NEW_START_TIME(是新的約會開始到任何現有的約會的開始和結束時間之間的時間)
    • existing_start_time < NEW_END_TIME和existing_end_time> NEW_END_TIME(是新任命恩在任何現有的約會的開始和結束時間之間d時間)
  • 如果沒有找到對象,然後繼續前進,添加新的任命

考慮的Django提供基於時間沒有過濾,這一點必須全部在查詢集上使用.extra()完成。

所以,我問是否有更好的方法。一個pythonic技巧或模塊或任何可能會簡化這一點。或者是一個現有的項目,它具有我所需要的或可以帶領我朝着正確的方向發展。

謝謝。

回答

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

+1太棒了!沒有看到範圍測試內置到Django的QuerySet API中。 –

+0

謝謝你的提示。這是錯過了在新任命之前開始並結束的事件。例如:如果客戶約會從1到5,這不會阻止某人預訂2到3.我添加了以下內容以包括以下情況: during_conflict = Appointment.objects.filter(start_date__lte = appointment.start_time, end_date__gte =約會。end_time) if(start_conflict或end_conflict或during_conflict): – mhost

+0

優秀。我很高興這有幫助。我將添加您的案例,以便答案更完整。 –

0

這裏需要注意的是不同用戶的不同時區,並將夏令時混入混合物中變得非常複雜。

您可能想看看pytz模塊,負責處理時區問題。