Updated SQL command

This commit is contained in:
Ryan Alder 2020-02-17 19:25:10 -08:00
parent 8839eedd40
commit 5a20130c37
2 changed files with 12 additions and 14 deletions

View File

@ -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)

View File

@ -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/<int:id>/history')