diff options
-rw-r--r-- | poller/models.py | 7 | ||||
-rw-r--r-- | poller/routes.py | 15 |
2 files changed, 20 insertions, 2 deletions
diff --git a/poller/models.py b/poller/models.py index 4bcca06..3c45f2c 100644 --- a/poller/models.py +++ b/poller/models.py @@ -33,6 +33,13 @@ class Day(db.Model): """ return cls.query.all() + @classmethod + def get_after(cls, after): + """ + Helper to get all values after a given date + """ + return cls.query.filter(cls.last_updated > after).all() + def serialize(self): """ used for json serialization diff --git a/poller/routes.py b/poller/routes.py index 6911fdd..676ba78 100644 --- a/poller/routes.py +++ b/poller/routes.py @@ -1,10 +1,21 @@ -from flask import Flask, jsonify +from flask import Flask, jsonify, request +from datetime import datetime +from time import mktime, strptime + from . import APP from .models import Day @APP.route('/api/v0/history') def _get_api_v0_history(): - return jsonify([day.serialize() for day in Day.get_all()]) + after_str = request.args.get("after") + if after_str == None: + return jsonify([day.serialize() for day in Day.get_all()]) + try: + after = datetime.fromtimestamp(mktime(strptime(after_str, "%Y-%m-%d"))) + return jsonify([day.serialize() for day in Day.get_after(after)]) + except: + return jsonify({"error": "bad request", "details": "error parsing query 'after'"}), 400 + @APP.route('/api/v0/latest') def _get_api_v0_latest(): |