Browse Source

Return a Cache-Control max-age on the API's isp view

Gu1 10 years ago
parent
commit
b22b46b412
2 changed files with 18 additions and 1 deletions
  1. 1 0
      ffdnispdb/default_settings.py
  2. 17 1
      ffdnispdb/views_api.py

+ 1 - 0
ffdnispdb/default_settings.py

@@ -4,6 +4,7 @@ SQLALCHEMY_DATABASE_URI = 'sqlite:///../ffdn-db.sqlite'
 CRAWLER_MIN_CACHE_TIME = 60*60 # 1 hour
 CRAWLER_MAX_CACHE_TIME = 60*60*24*14 # 2 week
 CRAWLER_DEFAULT_CACHE_TIME = 60*60*12 # 12 hours
+CRAWLER_CRON_INTERVAL = 60*20 # used to return valid cache info in the API
 SYSTEM_TIME_ZONE = 'Europe/Paris'
 LANGUAGES = {
     'en': u'English',

+ 17 - 1
ffdnispdb/views_api.py

@@ -6,6 +6,7 @@ from collections import OrderedDict
 import sys
 import json
 import datetime
+import time
 
 from . import utils, db
 from .models import ISP, CoveredArea
@@ -193,7 +194,22 @@ class ISPResource(Resource):
             s = ISP.query.filter_by(id=isp_id, is_disabled=False).scalar()
             if not s:
                 raise ObjectNotFound
-            return self.isp_to_dict(s)
+            if s.json_url:
+                # compute next update time, based on crawler cron interval.
+                interval = current_app.config['CRAWLER_CRON_INTERVAL']
+                nupdt = time.mktime(s.next_update.timetuple())
+                nupdt = datetime.datetime.fromtimestamp(nupdt - (nupdt % interval) + interval + 60)
+
+                now = datetime.datetime.utcnow()
+                if nupdt > now:
+                    max_age = (nupdt - now).seconds
+                else:
+                    max_age = current_app.config['CRAWLER_MIN_CACHE_TIME']
+            else:
+                # default max-age for isp using the form
+                max_age = current_app.config['CRAWLER_MIN_CACHE_TIME']
+            headers = {'Cache-Control': 'max-age='+str(max_age)}
+            return self.isp_to_dict(s), 200, headers
         else:
             s = ISP.query.filter_by(is_disabled=False)
             return self.handle_list(s, self.isp_to_dict)