diff --git a/fgs/__init__.py b/fgs/__init__.py index 4dff75a..7443aeb 100644 --- a/fgs/__init__.py +++ b/fgs/__init__.py @@ -3,7 +3,7 @@ from flask_sqlalchemy import SQLAlchemy import os app = Flask(__name__) -app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///./data.sqlite' +app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.getenv('DB_LOC') app.secret_key = os.environ.get('FGS_SECRET_KEY') or 'default secret key' db = SQLAlchemy(app) diff --git a/fgs/views.py b/fgs/views.py index ba4f721..4da57e1 100644 --- a/fgs/views.py +++ b/fgs/views.py @@ -1,4 +1,4 @@ -from . import app +from . import app, db from .jwt import jwt_required from .model import * from flask import g, jsonify, request, abort @@ -25,18 +25,16 @@ def me(): @app.route('/collars') @jwt_required def collars(): - active_collars = Collar.query.\ - filter_by(active=True).\ - join(DataPoint, isouter=True).\ - order_by(DataPoint.datetime.desc()).\ - group_by(Collar.id).\ - with_entities( - Collar.id, Collar.name, - DataPoint.longitude, DataPoint.latitude).\ - all() - active_collars = [ - {'id': id, 'name': name, 'pos': {'longitude': lo, 'latitude': la}} - for (id, name, lo, la) in active_collars] + active_collars = [] + for collar in Collar.query.filter_by(active=True).all(): + max_point = DataPoint.query.filter_by(collar_id=collar.id).\ + order_by(DataPoint.datetime.desc()).\ + first() + active_collars.append( + {'id': collar.id, 'name': collar.name, 'pos': + {'longitude': max_point.longitude, 'latitude': max_point.latitude}} + ) + return jsonify(active_collars) @app.route('/collars//history')