-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestFlask.py
107 lines (99 loc) · 3.98 KB
/
TestFlask.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
import os
import datetime
from flask import Flask, request, jsonify, make_response, url_for, json
from werkzeug.security import generate_password_hash, check_password_hash
from functools import wraps
import jwt
import datetime
from markupsafe import escape
from flask_cors import CORS, cross_origin
app = Flask(__name__)
CORS(app)
app.config['SECRET_KEY']='Tsh1Sha256Tls2Sha512'
def token_required(f):
@wraps(f)
def decorator(*args, **kwargs):
token = None
if 'Authorization' in request.headers:
token = request.headers['Authorization']
print(token)
if not token:
return jsonify({'success':0, 'message': 'Token missing'})
try:
data = jwt.decode(token, app.config['SECRET_KEY'], algorithms=["HS256"])
current_user = data["name"]
except:
return jsonify({'success':0, 'message': 'Invalid token'})
return f(*args, **kwargs)
return decorator
@app.route('/api/authenticate', methods=["POST"])
@cross_origin()
def authenticate():
login_data = request.get_json()
# print(generate_password_hash(login_data['password'], method='sha256'))
if not login_data or not login_data["username"] or not login_data["password"]:
return make_response('could not verify', 401, {'WWW.Authentication': 'Basic realm: "login required"'})
user = filterUserByUsername(login_data)
if user:
if check_password_hash(user["Password"], login_data["password"]):
token = jwt.encode({'name': user["Name"], 'username': user["Username"], 'exp' : datetime.datetime.utcnow() + datetime.timedelta(minutes=30)}, app.config['SECRET_KEY'], algorithm="HS256")
return jsonify({'token' : token, 'success':1 ,'message': 'Login successfully'})
# return make_response('could not verify', 401, {'WWW.Authentication': 'Basic realm: "login required"'})
return jsonify({'success':0 ,'message': 'Invalid login details'})
@app.route('/api/products', methods=["POST"])
@cross_origin()
@token_required
def get_products():
request_data = request.get_json()
#print(request_data['product_name'])
SITE_ROOT = os.path.realpath(os.path.dirname(__file__))
json_data_url = os.path.join(SITE_ROOT, "data", "products.json")
data = json.load(open(json_data_url))
data = process_data(data)
if request_data['SearchQuery']:
data = search(request_data['SearchQuery'],data)
json_meta_url = os.path.join(SITE_ROOT, "data", "meta.json")
meta = json.load(open(json_meta_url))
return jsonify({'success':1 ,'message': 'Data fetch successfully', 'data': data, 'meta': meta})
def search(searchObj,data):
product_name = searchObj['product_name']
product_price = searchObj['product_price']
expiry_date = searchObj['expiry_date']
search_data = list()
for product in data:
conditions = []
if product_name != "":
condition = product_name in product['title']
conditions.append(condition)
if product_price != '':
condition = product['price'] <= float(product_price)
conditions.append(condition)
if expiry_date != '':
product_expiry = datetime.datetime.strptime(product['expiry'],'%Y-%m-%d')
search_date = datetime.datetime.strptime(expiry_date,'%Y-%m-%d')
condition = product_expiry == search_date
conditions.append(condition)
# print(conditions)
if all(conditions):
search_data.append(product)
return search_data
def process_data(data):
today_date = datetime.datetime.strptime(str(datetime.date.today()), '%Y-%m-%d')
for product in data:
product["display_date"] = datetime.datetime.strptime(product['expiry'],'%Y-%m-%d')
if today_date > product["display_date"]:
product["expire"] = "Expired"
else:
product["expire"] = "Not Expired"
product["display_date"] = product["display_date"].strftime("%d/%m/%Y")
product["display_price"] = str(product["price"]) + " AED"
product["discount"] = str(product["discount"]) + " AED"
return data
def filterUserByUsername(login_data):
SITE_ROOT = os.path.realpath(os.path.dirname(__file__))
json_data_url = os.path.join(SITE_ROOT, "data", "users.json")
users = json.load(open(json_data_url))
for user in users:
if user["Username"] == login_data["username"]:
return user
return False