-3

I have to store restaurants on my table in the Django model. Each restaurant has its opening and closing time. For each I have to check if a particular restaurant is open or not with respect to the current time a user search for available restaurants.

Ishwar Jangid
  • 279
  • 2
  • 17

1 Answers1

2

For the comparison, and a very simple case, you would do something like this:

 is_open = user_request_time > restaurant.open and user_request_time < restaruant.close

But this is probably too simple. Restaurants are open different hours on different days.

So you might try giving a Schedule object to each restaurant:

class Schedule(models.Model):
    sunday_open = TimeField()
    sunday_close = TimeFiled()
    ....

Then make a class method in Restaurant:

class Restaurant(models.Model):
    schedule = models.ForeignKey(Schedule)
    ....

    def is_open(self, time=None)
        if not time:
            time = timezone.now()
        today = time.weekday()
        if today == 0:
            open = self.schedule.sunday_open
            close = self.schedule.sunday_close
        elif today == 1:
             ....

        return time > open and time < close

This is where I think I would start with something like this.

Rob L
  • 3,634
  • 2
  • 19
  • 38