From 24ccd489ca100b8c16a5030d82e6bf6f932a6041 Mon Sep 17 00:00:00 2001
From: Colin Copeland <copelco@caktusgroup.com>
Date: Sun, 11 May 2014 13:32:42 -0400
Subject: [PATCH] add background task to import Durham data nightly

---
 eatsmart/locations/durham/tasks.py | 17 +++++++++++++++++
 eatsmart/settings/base.py          |  9 +++++++++
 2 files changed, 26 insertions(+)
 create mode 100644 eatsmart/locations/durham/tasks.py

diff --git a/eatsmart/locations/durham/tasks.py b/eatsmart/locations/durham/tasks.py
new file mode 100644
index 0000000..79f6f20
--- /dev/null
+++ b/eatsmart/locations/durham/tasks.py
@@ -0,0 +1,17 @@
+from logging import getLogger
+
+from eatsmart.celery import app
+from eatsmart.locations.durham import api
+
+
+logger = getLogger(__file__)
+
+
+@app.task
+def import_durham_data():
+    """Import Durham data"""
+    logger.info("Starting Durham Import")
+    api.EstablishmentImporter().run()
+    api.InspectionImporter().run()
+    api.ViolationImporter().run()
+    logger.info("Finished Durham Import")
diff --git a/eatsmart/settings/base.py b/eatsmart/settings/base.py
index d3dc13c..50e2499 100644
--- a/eatsmart/settings/base.py
+++ b/eatsmart/settings/base.py
@@ -1,5 +1,7 @@
 # Django settings for eatsmart project.
 import os
+from celery.schedules import crontab
+
 
 PROJECT_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
 PROJECT_ROOT = os.path.abspath(os.path.join(PROJECT_PATH, os.pardir))
@@ -222,3 +224,10 @@
 # Default user location
 LATITUDE = 35.988611
 LONGITUDE = -78.907222
+
+CELERYBEAT_SCHEDULE = {
+    'import-durham-data': {
+        'task': 'eatsmart.locations.durham.tasks.import_durham_data',
+        'schedule': crontab(minute=0, hour=0),  # Execute daily at midnight
+    },
+}