forked from redhat-beyond/tiger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
121 lines (91 loc) · 3.2 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
#!/usr/bin/env python3
from flask import Flask, render_template, request, session
import logging
from mysql import connector
app = Flask(__name__)
app.config["SECRET_KEY"] = "OCML3BRawWEUeaxcuKHLpw"
def definedlog(fileHandler):
logger = logging.getLogger(__name__)
logger.setLevel(logging.ERROR)
handler = logging.FileHandler(fileHandler)
handler.setLevel(logging.ERROR)
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s : %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
return logger
def connect_db(host, user, password, database):
connection = connector.connect(
user=user, password=password, host=host, database=database)
return connection
conn = connect_db('localhost', 'root', 'LoginPass@@11223344', 'tiger')
@app.route('/')
def home():
return render_template('/home.html')
@app.route('/contact_us')
def contact_us():
return render_template('/contact_us.html')
@app.route('/send_message', methods=['GET', 'POST'])
def send_message():
if request.method == 'POST':
userDetails = request.form
username = userDetails['username']
msg = userDetails['content']
mycursor = conn.cursor()
sql = "INSERT INTO messages (username, content) VALUES (%s, %s)"
val = (username, msg)
mycursor.execute(sql, val)
conn.commit()
return render_template('/send_message.html')
def authenticate_user(username, password):
if check_username(username):
if check_password(username, password):
return True
else:
# TODO: flash a message about incorrect password
return False
# TODO: flash a message about incorrect username
return False
def check_password(username, password):
# TODO: check password against the db
pass
@app.route('/messages_view')
def messages_view():
message = conn.cursor()
message.execute("SELECT * FROM tiger.messages ORDER BY create_date DESC")
view = message.fetchall()
return render_template('/messages_view.html', view=view)
def search_messages(conn):
filter = conn.cursor()
word = input()
filter.execute("SELECT * FROM tiger.messages WHERE content LIKE % s" +
"ORDER BY create_date DESC", (" % {} % ".format(word),))
filtering = filter.fetchall()
return filtering
@app.route('/log_in', methods=['GET', 'POST'])
def log_in():
if request.method == "POST":
req = request.form
email = req.get("email")
password = req.get("password")
# if authenticate_user(username, password):
session["EMAIL"] = email
session["PASSWORD"] = password
return render_template('home.html', email=session["EMAIL"])
return render_template('/sign_up.html')
@app.route('/log_out')
def log_out():
return render_template('home.html')
if __name__ == '__main__':
app.run(host='0.0.0.0')
def check_username(username):
maulers = conn.cursor()
Fender = "SELECT * FROM users WHERE username =" + username
maulers.execute(Fender)
result = maulers.fetchall()
if not result:
# the user doesnt exist
return False
else:
# the user exists
return True