-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
196 lines (148 loc) · 5.05 KB
/
app.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
from gevent import monkey
monkey.patch_all()
from flask import Flask, jsonify, request
from flask_cors import CORS
from flask_socketio import SocketIO, emit
import json, atexit, time
import subprocess
import sys
if 'nocdp' not in sys.argv:
import cdp
USERS_PATH = 'data/users.json'
ITEMS_PATH = 'data/items.json'
TRANSACTIONS_PATH = 'data/transactions.json'
DEBUG = False
app = Flask(__name__)
app.config.from_object(__name__)
sio = SocketIO(app, cors_allowed_origins='*')
CORS(app, resources={r'/*': {'origins': '*'}})
def send_code(barcode):
sio.emit('codeScanned', barcode)
@app.route('/cdp', methods=['POST'])
def post_cdp():
if 'nocdp' in sys.argv:
return 'CDP not connected!'
data = request.json
top = ''
bottom = ''
if 'top' in data:
if 'center' in data['top']:
top = data['top']['center']
elif 'right' in data['top']:
top = cdp.merge(cdp.left(data['top']['left']), cdp.right(data['top']['right']))
elif 'left' in data['top']:
top = cdp.left(data['top']['left'])
else:
top = data['top']
if 'bottom' in data:
if 'center' in data['bottom']:
bottom = data['bottom']['center']
elif 'right' in data['bottom']:
bottom = cdp.merge(cdp.left(data['bottom']['left']), cdp.right(data['bottom']['right']))
elif 'left' in data['bottom']:
top = cdp.left(data['bottom']['left'])
else:
bottom = data['bottom']
cdp.show(top, bottom)
return 'CDP updated!'
@app.route('/screensaver', methods=['POST'])
def post_screensaver():
subprocess.run(["xscreensaver-command -activate"], shell=True)
return 'Screensaver started!'
@app.route('/<path:path>')
def get_data(path):
segments = path.split('/')
if segments[0] == 'users':
result = users
elif segments[0] == 'items':
result = items
elif segments[0] == 'transactions':
result = transactions
else:
return 'Invalid path!'
segments.pop(0)
for segment in segments:
if segment in result:
result = result[segment]
elif type(result) == list and segment.isdigit() and len(result) > int(segment):
result = result[int(segment)]
else:
return 'Invalid path!'
return jsonify(result)
@app.route('/transactions/add', methods=['POST'])
def post_transactions_add():
transaction = request.json
transaction['date'] = time.strftime('%d.%m.%Y, %H:%M:%S')
user_index = None
for index, user in enumerate(users['users']):
if user['id'] == transaction['user']['id']:
user_index = index
break
if user_index is None:
return 'Invalid user ID!'
transactions.append(transaction)
users['users'][user_index]['balance'] += transaction['impact']
save_json(transactions, TRANSACTIONS_PATH)
save_json(users, USERS_PATH)
return 'Transaction received!'
@app.route('/transactions/undo', methods=['POST'])
def post_transactions_undo():
date = request.json['date']
transaction_index = None
for index, t in enumerate(transactions):
if t['date'] == date:
transaction_index = index
break
if transaction_index is None:
return 'Invalid transaction date!'
transaction = transactions[transaction_index]
user_index = None
for index, user in enumerate(users['users']):
if user['id'] == transaction['user']['id']:
user_index = index
break
if user_index is None:
return 'Transaction user could not be found!'
users['users'][user_index]['balance'] -= transaction['impact']
transactions.pop(transaction_index)
save_json(transactions, TRANSACTIONS_PATH)
save_json(users, USERS_PATH)
return 'Transaction undone!'
@app.route('/favorites/add', methods=['POST'])
def post_favorites_add():
favorite = request.json
id = favorite['id']
barcode = favorite['barcode']
users['users'][id]['favorites'].append(barcode)
save_json(users, USERS_PATH)
return 'Favorite added!'
@app.route('/favorites/remove', methods=['POST'])
def post_favorites_remove():
favorite = request.json
id = favorite['id']
barcode = favorite['barcode']
users['users'][id]['favorites'].remove(barcode)
save_json(users, USERS_PATH)
return 'Favorite removed!'
def load_json(path):
with open(path, 'r', encoding='utf-8') as json_file:
return json.load(json_file)
def save_json(data_to_save, path):
with open(path, 'w', encoding='utf-8') as f:
json.dump(data_to_save, f, ensure_ascii=False, indent=4)
def load_data():
global users, items, transactions
users = load_json(USERS_PATH)
items = load_json(ITEMS_PATH)
transactions = load_json(TRANSACTIONS_PATH)
def save_data():
save_json(users, USERS_PATH)
save_json(items, ITEMS_PATH)
save_json(transactions, TRANSACTIONS_PATH)
def run():
load_data()
atexit.register(save_data)
# app.run(host='0.0.0.0')
sio.run(app, host='0.0.0.0')
if __name__ == '__main__':
run()