-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
119 lines (101 loc) · 3.31 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from flask import Flask, request, jsonify
from flask_restx import fields, Api, Resource, reqparse
from flask_sqlalchemy import SQLAlchemy
import os
app = Flask(__name__)
api = Api(
app,
version="1.0",
title="LocMVC API",
description="A simple LocMVC API",
)
ns_loc = api.namespace("locs", description="Locational data operations")
ns_item = api.namespace("loc", description="Specific locational data operations")
app.config[
"SQLALCHEMY_DATABASE_URI"
] = os.environ.get('DB_URI', None)
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.secret_key = "secret string"
db = SQLAlchemy(app)
model = api.model(
"Loc",
{
"phone_number": fields.String(required=True),
"longitude": fields.String(required=True),
"latitude": fields.String(required=True),
"emergency": fields.Boolean(required=True),
"sent_at": fields.DateTime(required=True),
},
)
parser = reqparse.RequestParser()
parser.add_argument("phone_number", type=str)
parser.add_argument("longitude", type=str)
parser.add_argument("latitude", type=str)
parser.add_argument("emergency", type=bool)
parser.add_argument("sent_at")
# Loc Model
class Loc(db.Model):
# TODO: Create as uuid
id = db.Column(db.Integer, primary_key=True)
phone_number = db.Column(db.String, nullable=False)
longitude = db.Column(db.String, nullable=False)
latitude = db.Column(db.String, nullable=False)
emergency = db.Column(db.Boolean, nullable=False)
sent_at = db.Column(db.DateTime, nullable=False)
def __init__(self, phone_number, longitude, latitude, emergency, sent_at):
self.phone_number = phone_number
self.longitude = longitude
self.latitude = latitude
self.emergency = emergency
self.sent_at = sent_at
@ns_loc.route("")
class LocList(Resource):
@ns_loc.doc("list_loc")
def get(self):
all_loc = Loc.query.all()
response = [
{
"id": loc.id,
"phone_number": str(loc.phone_number),
"longitude": str(loc.longitude),
"latitude": str(loc.latitude),
"emergency": loc.emergency,
"sent_at": str(loc.sent_at),
}
for loc in all_loc
]
return jsonify(response)
@ns_loc.doc("create_loc")
@ns_loc.expect(model)
def post(self):
print(request.get_json(force=True))
args = parser.parse_args()
entry = Loc(
args["phone_number"],
args["longitude"],
args["latitude"],
bool(args["emergency"]),
args["sent_at"],
)
db.session.add(entry)
db.session.commit()
return "OK!", 201
@ns_item.route("/<phone_number>")
class LocSpecific(Resource):
@ns_loc.doc("list_specific_loc")
def get(self, phone_number):
all_loc = Loc.query.filter_by(phone_number=phone_number).all()
response = [
{
"id": loc.id,
"phone_number": str(loc.phone_number),
"longitude": str(loc.longitude),
"latitude": str(loc.latitude),
"emergency": loc.emergency,
"sent_at": str(loc.sent_at),
}
for loc in all_loc
]
return jsonify(response)
if __name__ == "__main__":
app.run()