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
def export_data():
restaurants = list()
for restaurant in Restaurant.objects.all():
restaurant_data = {'name': restaurant.name}
restaurant_data = {'name': restaurant.name, 'id': restaurant.id}
open_times = list()
for time in restaurant.main_schedule.open_times.all():
open_times.append({
......@@ -15,7 +15,6 @@ def export_data():
})
restaurant_data['main_schedule'] = {
'name': restaurant.main_schedule.name,
'id': restaurant.id,
'open_times': open_times
}
special_schedules = list()
......@@ -30,7 +29,6 @@ def export_data():
})
special_schedules.append({
'name': schedule.name,
'id': restaurant.id,
'start': schedule.valid_start.isoformat(),
'end': schedule.valid_end.isoformat(),
'open_times': open_times
......
......@@ -30,6 +30,8 @@ class Restaurant(BaseModel):
if schedule.valid_start <= today <= schedule.valid_end:
if schedule.isOpenNow():
return True
else:
return False
if self.main_schedule.isOpenNow():
return True
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({
url: '/ajax/schedule/',
}).done(function (data) {
$('#grid').append('<div class="row"></div>');
$.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) {
$('#grid .row').append(
'<div class="span3 closed" id="' + restaurant.id + '">' + restaurant.name + '</div>'
......@@ -13,21 +26,59 @@ $.ajax({
);
}
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;
// If there exists a valid special schedule choose it.
$.each(restaurant.special_schedules, function (idx, special) {
if (now >= Date.parse(special.start)
&& now <= Date.parse(special.end)) {
if (day >= Date.parse(special.start)
&& day <= Date.parse(special.end)) {
schedule = special;
}
});
// If there was no special schedule, then use main_schedule.
if (schedule === undefined) {
schedule = restaurant.main_schedule;
}
// Open the restaurants that are open, leave the rest closed.
$.each(schedule.open_times, function (idx, time) {
if (now >= Date.parse(time.start_time)
&& now <= Date.parse(time.end_time)) {
$('#grid #' + restaurant.id).removeClass('closed');
$('#grid #' + restaurant.id).addClass('open');
var start_day = time.start_day;
var end_day = time.end_day;
var start_time = Date.parse(time.start_time);
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