-
Notifications
You must be signed in to change notification settings - Fork 5
/
run.py
105 lines (95 loc) · 3.27 KB
/
run.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
import os
from truewallet import Truewallet
from flask import Flask, render_template, flash, redirect, url_for, request, session
tw = Truewallet()
app = Flask(__name__)
app.config["SECRET_KEY"] = os.urandom(24)
app.config["PERMANENT_SESSION_LIFETIME"] = 1500
"""
Index
"""
@app.route("/")
def index():
if not 'access_token' in session:
return redirect(url_for('login'))
else:
return redirect(url_for('profiles'))
"""
Truewallet Login
"""
@app.route("/login", methods=['GET', 'POST'])
def login():
if request.method == 'POST':
email = request.form['email']
password = request.form['password']
tw.setCredentials(email, password)
res = tw.RequestLoginOTP()
if res:
if res['code'] == "200":
session.permanent = True
session['email'] = email
session['password'] = password
session['mobile_number'] = res['data']['mobile_number']
return redirect(url_for('otp', mobile_number=res['data']['mobile_number'], otp_reference=res['data']['otp_reference']))
else:
return render_template("login.html", error="Invalid email or password. Please try again!")
else:
return render_template("login.html", error="Please try again!")
return render_template("login.html")
"""
Truewallet otp login
"""
@app.route("/otp", methods=['GET', 'POST'])
def otp():
if request.args.get("mobile_number") and request.args.get("otp_reference"):
mobile_number = request.args.get("mobile_number")
otp_reference = request.args.get("otp_reference")
return render_template("otp.html", mobile_number=mobile_number, otp_reference=otp_reference)
if request.method == "POST":
otp_code = request.form['otp_code']
mobile_number = request.form['mobile_number']
otp_reference = request.form['otp_reference']
res = tw.SubmitLoginOTP(otp_code, mobile_number, otp_reference)
if res:
if res['code'] == "200":
flash('You were successfully logged in', 'success')
session['access_token'] = res['data']['access_token']
session['reference_token'] = res['data']['reference_token']
session['Full_name'] = "{} {}".format(res['data']['firstname_en'], res['data']['lastname_en'])
session['current_balance'] = float(res['data']['current_balance'])
tw.setAccessToken(res['data']['access_token'])
tw.setReferenceToken(res['data']['reference_token'])
return redirect(url_for('profiles'))
else:
flash("Invalid OTP code. Please try again!", "danger")
return render_template("otp.html", mobile_number=mobile_number, otp_reference=otp_reference)
else:
flash("Please try again!", "danger")
return redirect(url_for('otp', mobile_number=mobile_number, otp_reference=otp_reference))
return redirect(url_for('index'))
"""
Truewallet Activities to profiles ?
"""
@app.route("/profiles", methods=['GET', 'POST'])
def profiles():
if 'access_token' in session:
res = tw.GetTransaction()
if res:
if res['code'] == "UPC-200":
return render_template("profiles.html", data=res)
else:
return render_template("profiles.html", error="Please try again!", data=res)
else:
return redirect(url_for("profiles"))
return redirect(url_for('index'))
"""
Truewallet logout
"""
@app.route("/logout")
def logout():
session.pop('email', None)
session.pop('password', None)
session.pop('mobile_number', None)
return redirect(url_for('index'))
if __name__ == "__main__":
app.run(debug=True)