-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
168 lines (121 loc) · 4.16 KB
/
test.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
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
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='http://localhost:8080')
CORS(app, resources={r'/*': {'origins': 'http://localhost:8080'}})
def send_code(barcode):
sio.emit('codeScanned', barcode)
@app.route('/cdp', methods=['POST'])
def post_cdp():
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()