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

Models now have isOpen methods. Also some fixes and code style changes.

parent 0e2d7e82
......@@ -7,3 +7,4 @@
venv
build
local_settings.py
.ropeproject
from django.db import models
from django.core.cache import cache
import datetime
class Restaurant(models.Model):
"""Represents a dining location on campus."""
name = models.CharField(max_length=100)
mainSchedule = models.ForeignKey('Schedule',
related_name='restaurant_main')
......@@ -17,9 +19,34 @@ class Restaurant(models.Model):
cache.clear() # Invalidate cache on restuarant deletion
super(Restaurant, self).delete(*args, **kwargs)
def isOpen(self):
"""
Return true if this restaurant is currently open.
First checks any valid special schedules and then checks the
main default schedule.
"""
today = datetime.datetime.today()
for schedule in self.specialSchedules.all():
if schedule.dateValidStart <= today <= schedule.dateValidEnd:
if schedule.isOpenNow():
return True
if self.mainSchedule.isOpenNow():
return True
return False
class Schedule(models.Model):
"""
Contains opening and closing times for each day in a week.
For special (temporary) schedules, start and end dates for
when this schedule will be valid can also be set.
"""
name = models.CharField(max_length=100)
# inclusive:
dateValidStart = models.DateField(null=True, blank=True)
dateValidEnd = models.DateField(null=True, blank=True)
monOpen = models.TimeField(null=True, blank=True)
......@@ -44,3 +71,34 @@ class Schedule(models.Model):
def delete(self, *args, **kwargs):
cache.clear() # Invalidate cache on schedule deletion
super(Schedule, self).delete(*args, **kwargs)
def isOpenNow(self):
"""Return true if this schedule is open right now."""
today = datetime.datetime.today()
weekday = today.weekday()
if weekday == 0:
start = self.monOpen
end = self.monClose
elif weekday == 1:
start = self.tueOpen
end = self.tueClose
elif weekday == 2:
start = self.wedOpen
end = self.wedClose
elif weekday == 3:
start = self.thuOpen
end = self.thuClose
elif weekday == 4:
start = self.friOpen
end = self.friClose
elif weekday == 5:
start = self.satOpen
end = self.satClose
elif weekday == 6:
start = self.sunOpen
end = self.sunClose
if (start is not None and end is not None and
start <= today.time() <= end):
return True
else:
return False
from django.conf.urls import patterns, include, url
urlpatterns = patterns('website.views',
url(r'^$', 'alpha_grid', name = 'alpha_grid'),
url(r'^$', 'restaurant_grid', name = 'restaurant_grid'),
)
......@@ -4,9 +4,14 @@ from django.shortcuts import render_to_response, get_object_or_404, get_list_or_
from django.conf import settings
def alpha_grid(request):
def restaurant_grid(request):
"""Display the restaurants in a grid. Main page."""
if 'sort' in request.GET:
if request.GET['sort'] == 'location':
# Display the grid by location (instead of listing alphabetically)
pass # Not implemented yet
restaurants = Restaurant.objects.all()
# Restaurants in lists of 4 to easily create rows in template
restRows = [restaurants[x:x + 4] for x in xrange(0, len(restaurants), 4)]
return render_to_response('alpha.html', {'restRows': restRows,
return render_to_response('restaurant_grid.html', {'restRows': restRows,
'restaurants': restaurants})
......@@ -17,7 +17,7 @@
</head>
{% endblock %}
<body>
<div class = 'container'>
<div class = "container">
{% block content%}
{% endblock %}
......
{% extends 'base.html' %}
{% block content %}
<!-- {% now "jS F Y H:i" %} -->
{% for list in restRows %}
<div class = "row">
{% for restaurant in list %}
<div class = "span3 {% if restaurant.isOpen %}open{% else %}closed{% endif %}">
{{ restaurant.name }}
</div>
{% endfor %}
</div>
{% empty %}
There aren't any restaurants.
{% endfor %}
{% endblock %}
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