-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
106 lines (81 loc) · 2.81 KB
/
api.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
from flask import Flask
from flask import url_for, redirect
from flask.ext.restful import Resource, Api
from sqlalchemy import select, and_
from schema.schema import metadata, properties, areas, buildings, sales, zones
import schema.settings as settings
from sqlalchemy import create_engine
from api.property_arg_parser import property_parser
from api.area_arg_parser import area_parser
from api.building_arg_parser import building_parser
from api.zone_arg_parser import zone_parser
from api.sales_arg_parser import sales_parser
app = Flask(__name__)
api = Api(app)
engine = create_engine(settings.DB_STRING)
def get_select_results(s):
data = None
s = s.limit(1)
with engine.begin() as conn:
results = conn.execute(s)
data = params_from_query(results)
return data
#this is going to need to clean up some type stuff later
def params_from_query(results):
data = []
for row in results:
rowDict = dict(zip(row.keys(), row))
for k in rowDict.keys():
rowDict[k] = str(rowDict[k])
data.append(rowDict)
return data
def process_args_and_select(parser,table):
args = parser.parse_args()
s = select([table])
s = get_where_clause(args,s,table)
return get_select_results(s)
def get_where_clause(args,s,t):
valid_args = {}
for k in args.keys():
if args[k] != None:
valid_args[k] = args[k]
print(valid_args.keys())
for k in valid_args.keys():
if isinstance(valid_args[k],tuple):
s = s.where(and_(getattr(t.c,k) >= valid_args[k][0],
getattr(t.c,k) <= valid_args[k][1] ))
else:
s = s.where(getattr(t.c,k) == valid_args[k])
return s
class Property(Resource):
def get(self):
data = process_args_and_select(property_parser,
properties)
print(len(data))
return data
class Area(Resource):
def get(self):
return process_args_and_select(area_parser,
areas)
class Building(Resource):
def get(self):
return process_args_and_select(building_parser,
buildings)
class Sales(Resource):
def get(self):
return process_args_and_select(sales_parser,
sales)
class Zone(Resource):
def get(self):
return process_args_and_select(zone_parser,
zones)
@app.route('/')
def root():
return redirect(url_for('static', filename='index.html'))
api.add_resource(Property,'/Property/')
api.add_resource(Area,'/Area/')
api.add_resource(Building,'/Building/')
api.add_resource(Sales,'/Sales/')
api.add_resource(Zone,'/Zone/')
if __name__ == '__main__':
app.run(debug=True)