Commit 684623a3 authored by Tyler Hallada's avatar Tyler Hallada
Browse files

Moved all open times to a seperate model. Improved isOpen methods.

Previous implementation would fail with 24/7 restaurants or restaurants
that are open over a day change (Pilot House).
parent fe79c6a4
from django.contrib import admin
from website.models import Restaurant, Schedule
from website.models import Restaurant, Schedule, OpenTime
class OpenTimeInline(admin.TabularInline):
model = OpenTime
class ScheduleAdmin(admin.ModelAdmin):
inlines = [OpenTimeInline, ]
admin.site.register(Restaurant)
admin.site.register(Schedule)
admin.site.register(Schedule, ScheduleAdmin)
# -*- coding: utf-8 -*-
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models
class Migration(SchemaMigration):
def forwards(self, orm):
# Adding model 'OpenTime'
db.create_table('website_opentime', (
('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
('schedule', self.gf('django.db.models.fields.related.ForeignKey')(related_name='open_times', to=orm['website.Schedule'])),
('start_day', self.gf('django.db.models.fields.IntegerField')()),
('start_time', self.gf('django.db.models.fields.TimeField')()),
('end_day', self.gf('django.db.models.fields.IntegerField')()),
('end_time', self.gf('django.db.models.fields.TimeField')()),
))
db.send_create_signal('website', ['OpenTime'])
# Deleting field 'Schedule.mon_open'
db.delete_column('website_schedule', 'mon_open')
# Deleting field 'Schedule.mon_close'
db.delete_column('website_schedule', 'mon_close')
# Deleting field 'Schedule.sat_open'
db.delete_column('website_schedule', 'sat_open')
# Deleting field 'Schedule.fri_close'
db.delete_column('website_schedule', 'fri_close')
# Deleting field 'Schedule.sat_close'
db.delete_column('website_schedule', 'sat_close')
# Deleting field 'Schedule.fri_open'
db.delete_column('website_schedule', 'fri_open')
# Deleting field 'Schedule.sun_open'
db.delete_column('website_schedule', 'sun_open')
# Deleting field 'Schedule.tue_close'
db.delete_column('website_schedule', 'tue_close')
# Deleting field 'Schedule.thu_open'
db.delete_column('website_schedule', 'thu_open')
# Deleting field 'Schedule.wed_close'
db.delete_column('website_schedule', 'wed_close')
# Deleting field 'Schedule.thu_close'
db.delete_column('website_schedule', 'thu_close')
# Deleting field 'Schedule.tue_open'
db.delete_column('website_schedule', 'tue_open')
# Deleting field 'Schedule.sun_close'
db.delete_column('website_schedule', 'sun_close')
# Deleting field 'Schedule.wed_open'
db.delete_column('website_schedule', 'wed_open')
def backwards(self, orm):
# Deleting model 'OpenTime'
db.delete_table('website_opentime')
# Adding field 'Schedule.mon_open'
db.add_column('website_schedule', 'mon_open',
self.gf('django.db.models.fields.TimeField')(null=True, blank=True),
keep_default=False)
# Adding field 'Schedule.mon_close'
db.add_column('website_schedule', 'mon_close',
self.gf('django.db.models.fields.TimeField')(null=True, blank=True),
keep_default=False)
# Adding field 'Schedule.sat_open'
db.add_column('website_schedule', 'sat_open',
self.gf('django.db.models.fields.TimeField')(null=True, blank=True),
keep_default=False)
# Adding field 'Schedule.fri_close'
db.add_column('website_schedule', 'fri_close',
self.gf('django.db.models.fields.TimeField')(null=True, blank=True),
keep_default=False)
# Adding field 'Schedule.sat_close'
db.add_column('website_schedule', 'sat_close',
self.gf('django.db.models.fields.TimeField')(null=True, blank=True),
keep_default=False)
# Adding field 'Schedule.fri_open'
db.add_column('website_schedule', 'fri_open',
self.gf('django.db.models.fields.TimeField')(null=True, blank=True),
keep_default=False)
# Adding field 'Schedule.sun_open'
db.add_column('website_schedule', 'sun_open',
self.gf('django.db.models.fields.TimeField')(null=True, blank=True),
keep_default=False)
# Adding field 'Schedule.tue_close'
db.add_column('website_schedule', 'tue_close',
self.gf('django.db.models.fields.TimeField')(null=True, blank=True),
keep_default=False)
# Adding field 'Schedule.thu_open'
db.add_column('website_schedule', 'thu_open',
self.gf('django.db.models.fields.TimeField')(null=True, blank=True),
keep_default=False)
# Adding field 'Schedule.wed_close'
db.add_column('website_schedule', 'wed_close',
self.gf('django.db.models.fields.TimeField')(null=True, blank=True),
keep_default=False)
# Adding field 'Schedule.thu_close'
db.add_column('website_schedule', 'thu_close',
self.gf('django.db.models.fields.TimeField')(null=True, blank=True),
keep_default=False)
# Adding field 'Schedule.tue_open'
db.add_column('website_schedule', 'tue_open',
self.gf('django.db.models.fields.TimeField')(null=True, blank=True),
keep_default=False)
# Adding field 'Schedule.sun_close'
db.add_column('website_schedule', 'sun_close',
self.gf('django.db.models.fields.TimeField')(null=True, blank=True),
keep_default=False)
# Adding field 'Schedule.wed_open'
db.add_column('website_schedule', 'wed_open',
self.gf('django.db.models.fields.TimeField')(null=True, blank=True),
keep_default=False)
models = {
'website.opentime': {
'Meta': {'object_name': 'OpenTime'},
'end_day': ('django.db.models.fields.IntegerField', [], {}),
'end_time': ('django.db.models.fields.TimeField', [], {}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'schedule': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'open_times'", 'to': "orm['website.Schedule']"}),
'start_day': ('django.db.models.fields.IntegerField', [], {}),
'start_time': ('django.db.models.fields.TimeField', [], {})
},
'website.restaurant': {
'Meta': {'object_name': 'Restaurant'},
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'main_schedule': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'restaurant_main'", 'to': "orm['website.Schedule']"}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'special_schedules': ('django.db.models.fields.related.ManyToManyField', [], {'blank': 'True', 'related_name': "'restaurant_special'", 'null': 'True', 'symmetrical': 'False', 'to': "orm['website.Schedule']"})
},
'website.schedule': {
'Meta': {'object_name': 'Schedule'},
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'valid_end': ('django.db.models.fields.DateField', [], {'null': 'True', 'blank': 'True'}),
'valid_start': ('django.db.models.fields.DateField', [], {'null': 'True', 'blank': 'True'})
}
}
complete_apps = ['website']
\ No newline at end of file
......@@ -46,40 +46,54 @@ class Schedule(models.Model):
# inclusive:
valid_start = models.DateField(null=True, blank=True)
valid_end = models.DateField(null=True, blank=True)
mon_open = models.TimeField(null=True, blank=True)
mon_close = models.TimeField(null=True, blank=True)
tue_open = models.TimeField(null=True, blank=True)
tue_close = models.TimeField(null=True, blank=True)
wed_open = models.TimeField(null=True, blank=True)
wed_close = models.TimeField(null=True, blank=True)
thu_open = models.TimeField(null=True, blank=True)
thu_close = models.TimeField(null=True, blank=True)
fri_open = models.TimeField(null=True, blank=True)
fri_close = models.TimeField(null=True, blank=True)
sat_open = models.TimeField(null=True, blank=True)
sat_close = models.TimeField(null=True, blank=True)
sun_open = models.TimeField(null=True, blank=True)
sun_close = models.TimeField(null=True, blank=True)
def isOpenNow(self):
"""Return true if this schedule is open right now."""
for time in OpenTime.objects.filter(schedule=self):
if time.isOpenNow():
return True
return False
def __unicode__(self):
return self.name
class OpenTime(models.Model):
"""Represents a period time when a Restaurant is open"""
schedule = models.ForeignKey('Schedule', related_name='open_times')
start_day = models.IntegerField() # 0-6, Monday == 0
start_time = models.TimeField()
end_day = models.IntegerField() # 0-6, Monday == 0
end_time = models.TimeField()
def isOpenNow(self):
"""Return true if the current time is this OpenTime's range"""
today = datetime.datetime.today()
weekday = today.weekday()
days = [
(self.mon_open, self.mon_close),
(self.tue_open, self.tue_close),
(self.wed_open, self.wed_close),
(self.thu_open, self.thu_close),
(self.fri_open, self.fri_close),
(self.sat_open, self.sat_close),
(self.sun_open, self.sun_close),
]
start, end = days[weekday] # Get opening/closing times for today
if (start is not None and end is not None and
start <= today.time() <= end):
return True
if self.start_day <= self.end_day:
if self.start_day == today.weekday():
if self.start_time > today.time():
return False
elif self.start_day > today.weekday():
return False
if self.end_day == today.weekday():
if self.end_time < today.time():
return False
elif self.end_day < today.weekday():
return False
else:
return False
if self.start_day == today.weekday():
if self.start_time > today.time():
return False
if self.end_day == today.weekday():
if self.end_time < today.time():
return False
if self.end_day < today.weekday() < self.start_day:
return False
return True
def __unicode__(self):
return self.name
weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',
'Saturday', 'Sunday']
return '%s %s to %s %s' % (weekdays[self.start_day],
self.start_time.strftime("%H:%M:%S"), weekdays[self.end_day],
self.end_time.strftime("%H:%M:%S"))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment