Commit 7ff03b4f authored by Tyler Hallada's avatar Tyler Hallada
Browse files

Adding caching.

parent 04dea28c
from django.db import models
import datetime
from django.core.cache import cache
class Restaurant(models.Model):
name = models.CharField(max_length = 100)
mainSchedule = models.ForeignKey('Schedule', related_name = 'restaurant_main')
specialSchedules = models.ManyToManyField('Schedule', related_name = 'restaurant_special', null = True, blank = True)
name = models.CharField(max_length=100)
mainSchedule = models.ForeignKey('Schedule',
related_name='restaurant_main')
specialSchedules = models.ManyToManyField('Schedule',
related_name='restaurant_special', null=True, blank=True)
#function
#def analyzer(dayOpen, dayClose):
# if dayOpen is null or dayClose is null:
# return False
# elif timeNow >= dayOpen and timeNow <= dayClose:
# return True
# else:
# return False
def save(self, *args, **kwargs):
cache.clear() # Invalidate cache on restaurant change/creation
super(Restaurant, self).save(*args, **kwargs)
def isOpen():
timeNow = datetime.datetime.now().time()
day = datetime.date.today().weekday()
if day is 1:
if monOpen is null or monClose is null:
return False
elif timeNow >= monOpen and timeNow <= monClose:
return True
else:
return False
elif day is 2:
if tueOpen is null or tueClose is null:
return False
elif timeNow >= tueOpen and timeNow <= tueClose:
return True
else:
return False
elif day is 3:
if wedOpen is null or wedClose is null:
return False
elif timeNow >= wedOpen and timeNow <= wedClose:
return True
else:
return False
elif day is 4:
if thuOpen is null or thuClose is null:
return False
elif timeNow >= thuOpen and timeNow <= thuClose:
return True
else:
return False
elif day is 5:
if friOpen is null or friClose is null:
return False
elif timeNow >= friOpen and timeNow <= friClose:
return True
else:
return False
elif day is 6:
if satOpen is null or satClose is null:
return False
elif timeNow >= satOpen and timeNow <= satClose:
return True
else:
return False
elif day is 7:
if sunOpen is null or sunClose is null:
return False
elif timeNow >= sunOpen and timeNow <= sunClose:
return True
else:
return False
def delete(self, *args, **kwargs):
cache.clear() # Invalidate cache on restuarant deletion
super(Restaurant, self).delete(*args, **kwargs)
#def isC`losing:
class Schedule(models.Model):
name = models.CharField(max_length=100)
dateValidStart = models.DateField(null=True, blank=True)
dateValidEnd = models.DateField(null=True, blank=True)
monOpen = models.TimeField(null=True, blank=True)
monClose = models.TimeField(null=True, blank=True)
tueOpen = models.TimeField(null=True, blank=True)
tueClose = models.TimeField(null=True, blank=True)
wedOpen = models.TimeField(null=True, blank=True)
wedClose = models.TimeField(null=True, blank=True)
thuOpen = models.TimeField(null=True, blank=True)
thuClose = models.TimeField(null=True, blank=True)
friOpen = models.TimeField(null=True, blank=True)
friClose = models.TimeField(null=True, blank=True)
satOpen = models.TimeField(null=True, blank=True)
satClose = models.TimeField(null=True, blank=True)
sunOpen = models.TimeField(null=True, blank=True)
sunClose = models.TimeField(null=True, blank=True)
def save(self, *args, **kwargs):
cache.clear() # Invalidate cache on schedule change/creation
super(Schedule, self).save(*args, **kwargs)
class Schedule(models.Model):
name = models.CharField(max_length = 100)
dateValidStart = models.DateField(null = True, blank = True)
dateValidEnd = models.DateField(null = True, blank = True)
monOpen = models.TimeField(null = True, blank = True)
monClose = models.TimeField(null = True, blank = True)
tueOpen = models.TimeField(null = True, blank = True)
tueClose = models.TimeField(null = True, blank = True)
wedOpen = models.TimeField(null = True, blank = True)
wedClose = models.TimeField(null = True, blank = True)
thuOpen = models.TimeField(null = True, blank = True)
thuClose = models.TimeField(null = True, blank = True)
friOpen = models.TimeField(null = True, blank = True)
friClose = models.TimeField(null = True, blank = True)
satOpen = models.TimeField(null = True, blank = True)
satClose = models.TimeField(null = True, blank = True)
sunOpen = models.TimeField(null = True, blank = True)
sunClose = models.TimeField(null = True, blank = True)
def delete(self, *args, **kwargs):
cache.clear() # Invalidate cache on schedule deletion
super(Schedule, self).delete(*args, **kwargs)
......@@ -10,10 +10,12 @@ ADMINS = (
MANAGERS = ADMINS
PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': '/home/daniel/Documents/CS_Personal/Django/whats_open/database.db', # Or path to database file if using sqlite3.
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': os.path.join(PROJECT_PATH, '..', 'database.db'), # Or path to database file if using sqlite3.
'USER': '', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
......@@ -21,6 +23,14 @@ DATABASES = {
}
}
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
'TIMOUT': 259200,
}
}
# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
......@@ -89,6 +99,9 @@ TEMPLATE_LOADERS = (
)
MIDDLEWARE_CLASSES = (
'django.middleware.cache.UpdateCacheMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
......@@ -103,8 +116,6 @@ ROOT_URLCONF = 'whats_open.urls'
# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'whats_open.wsgi.application'
PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))
TEMPLATE_DIRS = (
os.path.join(PROJECT_PATH, 'templates'),
)
......@@ -152,3 +163,8 @@ LOGGING = {
},
}
}
CACHE_MIDDLEWARE_ALIAS = 'default'
CACHE_MIDDLEWARE_SECONDS = 259200
CACHE_MIDDLEWARE_KEY_PREFIX = ''
{% extends 'base.html' %}
{% block content %}
<!-- {% now "jS F Y H:i" %} -->
{% for list in restaurants %}
<div class = "row">
{% for restaurant in list %}
......@@ -11,8 +11,7 @@
{% endfor %}
</div>
{% empty %}
There aren't any restaurants
Solution: suck some cocks :-O c===3
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