From f7b1e727ec447b1613a5e7e51a1afac1cc5b1741 Mon Sep 17 00:00:00 2001 From: Galen Guyer Date: Thu, 19 Aug 2021 15:24:07 -0400 Subject: add after date query arg --- poller/models.py | 7 +++++++ 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(): -- cgit v1.2.3