查询Django中与午夜重叠的多个营业时间范围

时间:2014-06-25 13:04:13

标签: python sql database django django-rest-framework

我试图处理诸如酒吧之类的商店,例如;

  

周六:上午9点 - 下午3点,然后重新开放,晚上10点 - 凌晨4点#multiple open / closures

     

周日:上午11点 - 晚上8点

     周一:关闭

     

星期二至星期五上午9点至晚上9点#每个工作日,每个人都会在数据库中排成一行

我还在酒吧存储特殊活动,其中也有可用时间范围,如上所述,可供客户使用。

移动应用程序将在JSON中请求一系列条形对象,其中嵌套的开放时间和嵌套在条形对象内的特殊内容。每个特殊物品随后将特殊物品可用时间嵌套。我正在使用Django-Rest。

在生成大型集合的输出时,我有点担心查询过程的空间复杂性和时间复杂性。当可能有一种方法将其简化为DateTime Ranges时,将23:59:59放在各处似乎都是错误的。

import datetime
from django.db import models
WEEKDAYS = [
    (1, "Monday"),
    (2, "Tuesday"),
    (3, "Wednesday"),
    (4, "Thursday"),
    (5, "Friday"),
    (6, "Saturday"),
    (7, "Sunday"),
]

class Bar(models.Model):
    name = models.CharField(max_length=200)
    location = models.CharField(max_length=200, blank=True)


class Special(models.Model):
    bar = models.ForeignKey(Bar, related_name="availablespecials")
    name = models.CharField(max_length=500)
    price = models.FloatField()

"""The bar's businesses hours"""    
class OpeningHour(models.Model):
    bar = models.ForeignKey(Bar, related_name="baropeninghours")
    weekday = models.IntegerField(choices=WEEKDAYS)
    from_hour = models.TimeField()
    to_hour = models.TimeField()

"""The ranges for when a special is available"""
class SpecialHour(models.Model):
    special = models.ForeignKey(Special, related_name="specialopeninghours")
    weekday = models.IntegerField(choices=WEEKDAYS)
    from_hour = models.TimeField()
    to_hour = models.TimeField()

获得酒吧的输出

"results": [
        {
            "name": "Santa's Brew House", 
            "Address": "123 Ho Ho Ho Street", 
            "gps_latitude":  90.00000, 
            "gps_longitude": 0.00000, 
            "baropeninghours": [
                #This would mean santas place is open from 10pm to 4am
                "Saturday 22:00:00 to 23:59:59"
                "Sunday 00:00:00 to 04:00:00", 
            ], 
            "availablespecials": [
                {
                    "name": "Captain's Mast", 
                    "specialopeninghours": [
                        "Monday 00:00:00 to 23:59:59", 
                        "Tuesday 00:00:00 to 23:59:59", 
                        "Wednesday 00:00:00 to 23:59:59", 
                        "Thursday 00:00:00 to 23:59:59", 
                        "Friday 00:00:00 to 23:59:59", 
                        "Saturday 00:00:00 to 23:59:59", 
                        "Sunday 00:00:00 to 23:59:59"
                    ]
                }, 
                {
                    "name": "Aggie Punch", 
                    "specialopeninghours": [
                        "Saturday 00:00:00 to 23:59:59", 
                        "Sunday 00:00:00 to 23:59:59"
                    ]
                }
            ]
        }]... continued for each bar

1 个答案:

答案 0 :(得分:1)

如果一个酒吧在周一早上营业到凌晨3点,我当天不会认为它是“开放”的,而是周日的结束时间。所以这里的时间太少了。

    from_hour = models.TimeField()
    to_day = models.IntegerField(choices=WEEKDAYS)
    to_hour = models.TimeField()

您的客户端代码可以弄清楚如何最好地渲染(如果to_day和工作日相同,可以忽略它)。

你的JSON:

...
        "baropeninghours": [
            "Saturday 22:00:00 to Sunday 3:00:00"
...

...
        "baropeninghours": [
            "Tuesday 10:00:00 to Tuesday 22:00:00"
...