2016-06-18 146 views
1

目前我正在處理另一個預訂項目。這裏是我的預訂模式:Django日程安排,如何檢查日期是否在兩個日期之間

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分鐘的時間間隔進行檢查。然後顯示所有可用的預約時間。例如:

Employee choice: 
John Doe 
Service choice: 
Air Filter Replacement 
Duration: 1 hour 
Date picked: 
30/06/2016 
Available reservation time: 
12:30 - 13:30 
15:30 - 16:30 
17:00 - 18:00 

Employee choice: 
John Doe 
Service choice: 
Suction Manifold Flaps Removal 
Duration: 2 hours 
Date picked: 
1/07/2016 
Available reservation time: 
13:00 - 15:00 
17:00 - 19:00 

這對我來說是一個巨大的障礙,因爲我沒有什麼好辦法如何處理這個。

我的第一個想法是,我可以把日期由用戶選擇,員工ID,持續時間和通過工作時間循環,每30分鐘在while循環:

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 

正如你可以看到我需要的員工。 isAvailable功能,我不知道如何寫它。基本上,它必須告訴我,從開始到開始+持續時間是否及時,所述員工已經被分配到任何保留。

此外,該方法是否正確和最優?有沒有更簡單的方法來實現我所需要的?

編輯:

這是我的員工模型。它就像它一樣簡單。

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 
+0

你可以添加你的'員工'架構的職位?它可能有幫助。 – oxalorg

+0

@MiteshNinja我加了Employee模型。它儘可能簡單。 – Kesto

回答

1

這應該工作:

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() 

它意味着這樣做的,你要測試每個時期的查詢。從概念上講,我覺得必須有一個更有效的解決方案,但目前我無法迴避。無論如何,一旦你確認這個邏輯起作用,你應該能夠改進它。

+0

它似乎工作,但顯然我不得不改變lte和gte運營商到lt和gt。否則有30分鐘抵消。很好的回答,我非常感謝你的幫助! – Kesto

相關問題