2016-11-24 47 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