Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
74 lines (51 loc) · 1.9 KB
/
Copy pathmain.py
File metadata and controls
74 lines (51 loc) · 1.9 KB
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
from flask import Flask, jsonify
from flask import request
from sqlalchemy.sql.expression import desc
from model import Store
from model import Offer
from persistence import Configuration
from persistence import DatabaseManager
CONFIGURATION_PATH = '/home/diego/workspaces/pycharm/aprovechalo/etc/aprovechalo.conf'
configuration = Configuration(CONFIGURATION_PATH)
dm = DatabaseManager(configuration.persistence)
# dm.drop_schema()
# dm.create_schema()
app = Flask(__name__)
@app.route('/')
def index():
return jsonify()
@app.route('/offers', methods=['GET'])
def get_offers():
with dm.session_scope() as db:
offers = db.query(Offer).order_by(desc(Offer.when)).all()
reply = [o.serialize() for o in offers]
return jsonify(reply)
@app.route('/products', methods=['GET'])
def get_products():
store_id = request.args.get('store_id', None)
offer_id = request.args.get('offer_id', None)
if store_id and offer_id:
store_id = int(store_id)
offer_id = int(offer_id)
with dm.session_scope() as db:
store = db.query(Store).filter(Store.id == store_id).one()
offer = db.query(Offer).filter(Offer.id == offer_id).one()
pr = offer.product
store.products.remove(pr)
store.products = [pr] + store.products
reply = store.serialize(store=True)
reply['offer'] = offer.serialize(store=False, product=False)
return jsonify(reply)
else:
return jsonify({
})
@app.route('/stores', methods=['GET'])
def get_stores():
with dm.session_scope() as db:
stores = db.query(Store).all()
reply = [s.serialize(products=False) for s in stores]
return jsonify(reply)
if __name__ == '__main__':
app.run(host=configuration.server['host'],
port=configuration.server['port'],
debug=configuration.server['debug'] == 'True')