aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGalen Guyer <galen@galenguyer.com>2021-08-19 15:24:07 -0400
committerGalen Guyer <galen@galenguyer.com>2021-08-19 15:24:12 -0400
commitf7b1e727ec447b1613a5e7e51a1afac1cc5b1741 (patch)
tree0e175834fe16f4b1dd97fdc12424af3dadf18def
parentd64b42a0c837d0d2a076bbd81586c39dfb2622a4 (diff)
add after date query arg
-rw-r--r--poller/models.py7
-rw-r--r--poller/routes.py15
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():