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

Adding caching.

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