-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
231 lines (177 loc) · 6.57 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import json
import os
from functools import wraps
import sqlite3
from flask import Flask, jsonify, request
from flask_cors import CORS
import jwt
import datetime
app = Flask(__name__)
CORS(app)
SECRET_KEY = os.getenv('JWT_KEY')
ADMIN_ALIAS = os.getenv('ADMIN_ALIAS')
ADMIN_PASSKEY = os.getenv('ADMIN_KEY')
if not all([SECRET_KEY,ADMIN_ALIAS,ADMIN_PASSKEY]):
raise EnvironmentError("\n\nEnvironment variables not found!\nrequired variables: ADMIN_ALIAS, ADMIN_KEY, JWT_KEY\n\nPlease run: export <variable>=<value>\n\nDon't get afraid, you can set any value you want :)\n")
def verify_token(token):
decoded = jwt.decode(token, SECRET_KEY, algorithms=["HS256"])
try:
decoded = jwt.decode(token, SECRET_KEY, algorithms=["HS256"])
if decoded.get("user") == ADMIN_ALIAS:
return {'auth': True}
except jwt.ExpiredSignatureError:
return {'auth': False, 'message': 'Token has expired'}
except jwt.InvalidTokenError:
return {'auth': False, 'message': 'Invalid token'}
return {'auth': False, 'message': 'User is not admin'}
def protected_route(func):
@wraps(func)
def validator(*args, **kwargs):
data = request.get_json()
token = data.get('token')
if not token:
return jsonify({'auth': False, 'message': 'Token not provided'})
status = verify_token(token)
if status['auth']:
return func(*args, **kwargs)
return jsonify(status)
return validator
def get_godown_connection():
conn = sqlite3.connect('godowns.db')
conn.row_factory = sqlite3.Row
return conn
def get_items_connection():
conn = sqlite3.connect('items.db')
conn.row_factory = sqlite3.Row
return conn
@app.route('/root-godowns', methods=['POST'])
@protected_route
def get_root_godowns():
conn = get_godown_connection()
cursor = conn.cursor()
cursor.execute("SELECT * FROM godowns WHERE parent_godown IS NULL")
godowns = cursor.fetchall()
godown_list = [dict(godown) for godown in godowns]
conn.close()
return jsonify(godown_list)
@app.route('/sub-godowns/<id>', methods=['POST'])
@protected_route
def get_sub_godowns(id):
conn = get_godown_connection()
cursor = conn.cursor()
if id is None:
return jsonify({})
cursor.execute("SELECT * FROM godowns WHERE parent_godown IS ?",(id,))
sub_godowns = cursor.fetchall()
sub_godown_list = [dict(sub_godown) for sub_godown in sub_godowns]
conn.close()
return jsonify(sub_godown_list)
@app.route('/godown-items/<id>', methods=['POST'])
@protected_route
def get_items(id):
conn = get_items_connection()
cursor = conn.cursor()
if id is None:
return jsonify({})
available_items = []
unavailable_items = []
cursor.execute("SELECT * FROM items WHERE godown_id IS ? AND status IS ?",(id,"in_stock"))
items = cursor.fetchall()
available_items = [dict(item) for item in items]
cursor.execute("SELECT * FROM items WHERE godown_id IS ? AND status IS ?",(id,"out_of_stock"))
items = cursor.fetchall()
unavailable_items = [dict(item) for item in items]
conn.close()
filtered_items = available_items + unavailable_items
return jsonify(filtered_items)
@app.route('/item/<id>', methods=['POST'])
@protected_route
def get_item_data(id):
conn = get_items_connection()
cursor = conn.cursor()
if id is None:
return jsonify({})
cursor.execute("SELECT * FROM items WHERE item_id IS ?",(id,))
item = cursor.fetchone()
item = dict(item)
conn.close()
conn = get_godown_connection()
cursor = conn.cursor()
location = list([])
cursor.execute("SELECT * FROM godowns WHERE id IS ?",(item["godown_id"],))
godown = cursor.fetchone()
godown = dict(godown)
location.append(godown["name"])
while godown["parent_godown"]:
cursor.execute("SELECT * FROM godowns WHERE id IS ?",(godown["parent_godown"],))
godown = cursor.fetchone()
godown = dict(godown)
location.append(godown["name"])
location.reverse()
location = '/'.join(location)
conn.close()
if item:
item["attributes"]={}
attr_1 = item["attribute_1"].split("~")
item["attributes"][attr_1[0]]=attr_1[1]
item.pop("attribute_1")
attr_2 = item["attribute_2"].split("~")
item["attributes"][attr_2[0]]=attr_2[1]
item.pop("attribute_2")
attr_3 = item["attribute_3"].split("~")
item["attributes"][attr_3[0]]=attr_3[1]
item.pop("attribute_3")
item["location"] = location
return(jsonify(item))
else:
return jsonify({
"item_id": None,
"name": "Product Name",
"quantity": 0,
"category": "Category",
"price": 0.00,
"status": "out_of_stock",
"godown_id": "gid",
"brand": "Brand",
"attributes": {
"color": "NaN",
"size": "NaN",
"type": "NaN"
},
"image_url": "https://psediting.websites.co.in/obaju-turquoise/img/product-placeholder.png"
})
@app.route('/search/<param>', methods=['POST'])
@protected_route
def search(param):
conn = get_items_connection()
cursor = conn.cursor()
cursor.execute("SELECT * FROM items WHERE name LIKE ?",('%'+str(param)+'%',))
items = cursor.fetchall()
items = [dict(item) for item in items]
conn.close()
return jsonify(items)
@app.route('/login', methods=['POST'])
def login():
data = request.get_json()
if 'passKey' not in data:
return jsonify({'auth': False, 'message': 'passKey not provided'})#, 400
alias = data['alias']
passKey = data['passKey']
if passKey == ADMIN_PASSKEY and alias == ADMIN_ALIAS:
token = jwt.encode({
'user': ADMIN_ALIAS,
'exp': datetime.datetime.utcnow() + datetime.timedelta(hours=1)
}, SECRET_KEY, algorithm='HS256')
return jsonify({'auth': True, 'token': token, 'message': 'Login successful'})
return jsonify({'auth': False, 'message': 'Invalid Alias or Passkey'})#, 403
@app.route('/verify', methods=['POST'])
def verify():
data = request.get_json()
token = data.get('token')
if not token:
return jsonify({'auth': False, 'message': 'Token not provided'})#, 401
return jsonify(verify_token(token))
if __name__ == '__main__':
app.run(debug=True)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=True)