2016-11-24 74 views
1

我正在爲醫生辦公室開發一個約會管理器。 有一個選項可以創建一個時間表,例如,如果一個人想要一週三次(星期一,星期三和星期五)在17:00開始,直到2017年2月24日用戶可以通過僅提供該數據來創建時間表:開始日期,結束日期,時間,一週中的幾天。 該應用程序會提醒用戶,如果約會已經在那個時間預訂,所以用戶可以選擇更改日程安排或接受,這將創建跳過先前佔用的約會的時間表。 我使用Django框架開發應用 在我Schedule模型我有以下代碼Django:評估病情的最佳方法

monday = models.BooleanField(default=False) 
tuesday = models.BooleanField(default=False) 
wednesday = models.BooleanField(default=False) 
thursday = models.BooleanField(default=False) 
friday = models.BooleanField(default=False) 
saturday = models.BooleanField(default=False) 

我無法找出最好的方式來檢查,如果約會是在輸入日期間已經預訂。

我不知道這個問題是清楚的,所以我會離開一個例子:

我想從17年1月2日安排約會到17年2月24日每週二和週四在9 :00。 約會在01/19/17 9:00之前爲另一位患者創建。該應用程序必須提醒用戶。

我想知道是否有更好的方法來檢查這個,而不是:

appointments = Appointment.objects.filter(date__range=[start_date, end_date]) 
for appointment in appointments: 
    if monday and appointment.weekday() == 0: 
     check if appointment time == input time 
     do something 
    elif tuesday and appointment.weekday() == 1: 
     check if appointment time == input time 
     do something 
    elif wednesday and appointment.weekday() == 2: 
     check if appointment time == input time 
     do something 
    elif thursday and appointment.weekday() == 3: 
     check if appointment time == input time 
     do something 
    elif friday and appointment.weekday() == 4: 
     check if appointment time == input time 
     do something 
    elif saturday and appointment.weekday() == 5: 
     check if appointment time == input time 
     do something 

也許有辦法從數據庫中檢索只有那些具有相同的日期之間的約會時間作爲時間表的輸入時間。

希望這有些可以理解。

回答

0

我不確定,我理解了這個問題。但是,也許,你可以在一週中的每一天與價值聯繫起來,讓所有的天,這是真實的,然後通過這些值過濾約會:

days = [monday, tuesday, wednesday, thursday, friday, saturday] 
days = zip(days, range(6)) 
true_days = [d[1] for d in days if d[0]] 
appointments = appointments.filter(weekday__in=true_days) 

所以,你將只能得到那些約會,這平日對應真平日。

編輯: 我沒有注意到,工作日是一種方法。您可以在一個循環中篩選約會,然後:

new_appointments = [] 
for a in appointments: 
    if a.weekday() in true_days: 
     check if a time == input time 
     do something