60 lines
1.5 KiB
Python
60 lines
1.5 KiB
Python
from flask import Flask, request, jsonify
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
import datetime
|
|
|
|
app = Flask(__name__)
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///../keldermap.sqlite3'
|
|
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
|
|
|
db = SQLAlchemy(app)
|
|
|
|
|
|
class Position(db.Model):
|
|
__tablename__ = 'positions'
|
|
|
|
id = db.Column(db.Integer, autoincrement=True, primary_key=True)
|
|
userId = db.Column(db.Integer, nullable=False)
|
|
long = db.Column(db.Float, nullable=False)
|
|
lat = db.Column(db.Float, nullable=False)
|
|
time = db.Column(db.DateTime, nullable=False)
|
|
|
|
|
|
db.create_all()
|
|
|
|
|
|
def msg(s: str) -> str:
|
|
return jsonify({'message': s})
|
|
|
|
|
|
@app.route('/')
|
|
def home():
|
|
return 'HI'
|
|
|
|
|
|
@app.route('/positions', methods=['GET'])
|
|
def positions():
|
|
res = db.session.query(Position.userId, Position.long, Position.lat, db.func.max(Position.time)).group_by(Position.userId).all()
|
|
return jsonify([{
|
|
"userId": x[0],
|
|
"long": x[1],
|
|
"lat": x[2],
|
|
"time": str(x[3])
|
|
} for x in res])
|
|
|
|
|
|
@app.route('/update', methods=['POST'])
|
|
def update():
|
|
if request.json is not None:
|
|
data: dict = request.json
|
|
if data.keys() == {'userId', 'long', 'lat'}:
|
|
pos = Position(userId=data['userId'], long=data['long'], lat=data['lat'], time=datetime.datetime.now())
|
|
db.session.add(pos)
|
|
db.session.commit()
|
|
return msg('format ok')
|
|
return msg('wrong keys')
|
|
else:
|
|
return msg('json pls')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=False) |