Commit d7121ef2 authored by Tyler Hallada's avatar Tyler Hallada
Browse files

Move processing of schedule to grid.js

parent 925073cc
...@@ -4,7 +4,7 @@ from website.models import Restaurant ...@@ -4,7 +4,7 @@ from website.models import Restaurant
def export_data(): def export_data():
restaurants = list() restaurants = list()
for restaurant in Restaurant.objects.all(): for restaurant in Restaurant.objects.all():
restaurant_data = {'name': restaurant.name} restaurant_data = {'name': restaurant.name, 'id': restaurant.id}
open_times = list() open_times = list()
for time in restaurant.main_schedule.open_times.all(): for time in restaurant.main_schedule.open_times.all():
open_times.append({ open_times.append({
...@@ -15,7 +15,6 @@ def export_data(): ...@@ -15,7 +15,6 @@ def export_data():
}) })
restaurant_data['main_schedule'] = { restaurant_data['main_schedule'] = {
'name': restaurant.main_schedule.name, 'name': restaurant.main_schedule.name,
'id': restaurant.id,
'open_times': open_times 'open_times': open_times
} }
special_schedules = list() special_schedules = list()
...@@ -30,7 +29,6 @@ def export_data(): ...@@ -30,7 +29,6 @@ def export_data():
}) })
special_schedules.append({ special_schedules.append({
'name': schedule.name, 'name': schedule.name,
'id': restaurant.id,
'start': schedule.valid_start.isoformat(), 'start': schedule.valid_start.isoformat(),
'end': schedule.valid_end.isoformat(), 'end': schedule.valid_end.isoformat(),
'open_times': open_times 'open_times': open_times
......
...@@ -30,6 +30,8 @@ class Restaurant(BaseModel): ...@@ -30,6 +30,8 @@ class Restaurant(BaseModel):
if schedule.valid_start <= today <= schedule.valid_end: if schedule.valid_start <= today <= schedule.valid_end:
if schedule.isOpenNow(): if schedule.isOpenNow():
return True return True
else:
return False
if self.main_schedule.isOpenNow(): if self.main_schedule.isOpenNow():
return True return True
return False return False
......
function close(id) {
$('#grid #' + id).removeClass('open');
$('#grid #' + id).addClass('closed');
return false;
}
function open(id) {
$('#grid #' + id).removeClass('closed');
$('#grid #' + id).addClass('open');
return false;
}
$.ajax({ $.ajax({
url: '/ajax/schedule/', url: '/ajax/schedule/',
}).done(function (data) { }).done(function (data) {
$('#grid').append('<div class="row"></div>'); $('#grid').append('<div class="row"></div>');
$.each(data.data, function (idx, restaurant) { $.each(data.data, function (idx, restaurant) {
// Note: This assumes grid does not already exist. TODO do not assume.
if ($('#grid .row').last().children().length < 4) { if ($('#grid .row').last().children().length < 4) {
$('#grid .row').append( $('#grid .row').append(
'<div class="span3 closed" id="' + restaurant.id + '">' + restaurant.name + '</div>' '<div class="span3 closed" id="' + restaurant.id + '">' + restaurant.name + '</div>'
...@@ -13,21 +26,59 @@ $.ajax({ ...@@ -13,21 +26,59 @@ $.ajax({
); );
} }
var now = new Date(); var now = new Date();
var date = new Date().setHours(0,0,0,0);
// JavaScript sets 0 to Sunday instead of Monday
var day = now.getDay() - 1;
if (day === -1) {
day = 6;
}
var schedule = undefined; var schedule = undefined;
// If there exists a valid special schedule choose it.
$.each(restaurant.special_schedules, function (idx, special) { $.each(restaurant.special_schedules, function (idx, special) {
if (now >= Date.parse(special.start) if (day >= Date.parse(special.start)
&& now <= Date.parse(special.end)) { && day <= Date.parse(special.end)) {
schedule = special; schedule = special;
} }
}); });
// If there was no special schedule, then use main_schedule.
if (schedule === undefined) { if (schedule === undefined) {
schedule = restaurant.main_schedule; schedule = restaurant.main_schedule;
} }
// Open the restaurants that are open, leave the rest closed.
$.each(schedule.open_times, function (idx, time) { $.each(schedule.open_times, function (idx, time) {
if (now >= Date.parse(time.start_time) var start_day = time.start_day;
&& now <= Date.parse(time.end_time)) { var end_day = time.end_day;
$('#grid #' + restaurant.id).removeClass('closed'); var start_time = Date.parse(time.start_time);
$('#grid #' + restaurant.id).addClass('open'); var end_time = Date.parse(time.end_time);
if (day === start_day) {
if (now >= Date.parse(time.start_time)) {
if (day === end_day) {
if (now <= Date.parse(time.end_time)) {
return open(restaurant.id);
}
} else {
return open(restaurant.id);
}
}
} else if (day === end_day) {
if (now <= Date.parse(time.end_time)) {
if (day === start_day) {
if (now >= Date.parse(time.start_time)) {
return open(restaurant.id);
}
} else {
return open(restauant.id);
}
}
} else if (start_day < end_day) {
if (day > start_day && day < end_day) {
return open(restaurant.id);
}
} else if (start_day > end_day) {
if (day < start_day || day > end_day) {
return open(restaurant.id);
}
} }
}); });
}); });
......
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