2016-06-18 5 views
1

現在、私は別の予約プロジェクトに取り掛かっています。ここに私の予約モデルさ:Djangoスケジューリング、日付が2つの日付の間にあるかどうかをチェックする方法

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 

私はどのようにこれをアプローチに何か良いアイデアを持っていないので、これは私のための巨大なバリケードです。

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 

あなたは私が従業員を必要とするだろう見ることができるように:

私の最初のアイデアは、私は、whileループでは30分ごとにユーザーが選択した日付、従業員ID、時間を取り、作業時間を反復処理ということでした。 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

'Employee'スキーマを投稿に追加できますか?それは助けるかもしれません。 – 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

関連する問題