-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit1.py
executable file
·123 lines (107 loc) · 3.55 KB
/
init1.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
#Import Flask Library
from flask import Flask, render_template, request, session, url_for, redirect
import pymysql.cursors
#Initialize the app from Flask
app = Flask(__name__)
#new line
#Configure MySQL
conn = pymysql.connect(host='localhost',
user='root',
port=8889,
password='root',
db='blog',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
#Define a route to hello function
@app.route('/')
def hello():
return render_template('index.html')
#Define route for login
@app.route('/login')
def login():
return render_template('login.html')
#Define route for register
@app.route('/register_c')
def register():
return render_template('register_c.html')
#Authenticates the login
@app.route('/loginAuth', methods=['GET', 'POST'])
def loginAuth():
#grabs information from the forms
username = request.form['username']
password = request.form['password']
#cursor used to send queries
cursor = conn.cursor()
#executes query
query = 'SELECT * FROM user WHERE username = %s and password = %s'
cursor.execute(query, (username, password))
#stores the results in a variable
data = cursor.fetchone()
#use fetchall() if you are expecting more than 1 data row
cursor.close()
error = None
if(data):
#creates a session for the the user
#session is a built in
session['username'] = username
return redirect(url_for('home'))
else:
#returns an error message to the html page
error = 'Invalid login or username'
return render_template('login.html', error=error)
#Authenticates the register
@app.route('/registerAuth', methods=['GET', 'POST'])
def registerAuth():
#grabs information from the forms
username = request.form['username']
password = request.form['password']
#cursor used to send queries
cursor = conn.cursor()
#executes query
query = 'SELECT * FROM customer WHERE email = %s'
cursor.execute(query, (email))
#stores the results in a variable
data = cursor.fetchone()
#use fetchall() if you are expecting more than 1 data row
error = None
if(data):
#If the previous query returns data, then user exists
error = "This user already exists"
return render_template('register_c.html', error = error)
else:
ins = 'INSERT INTO customer VALUES(%s, %s)'
cursor.execute(ins, (username, password))
conn.commit()
cursor.close()
return render_template('index.html')
@app.route('/home')
def home():
username = session['username']
cursor = conn.cursor();
query = 'SELECT ts, blog_post FROM blog WHERE username = %s ORDER BY ts DESC'
cursor.execute(query, (username))
data1 = cursor.fetchall()
for each in data1:
print(each['blog_post'])
cursor.close()
return render_template('home.html', username=username, posts=data1)
@app.route('/post', methods=['GET', 'POST'])
def post():
username = session['username']
cursor = conn.cursor();
blog = request.form['blog']
query = 'INSERT INTO blog (blog_post, username) VALUES(%s, %s)'
cursor.execute(query, (blog, username))
conn.commit()
cursor.close()
return redirect(url_for('home'))
@app.route('/logout')
def logout():
session.pop('username')
return redirect('/')
app.secret_key = 'some key that you will never guess'
#Run the app on localhost port 5000
#debug = True -> you don't have to restart flask
#for changes to go through, TURN OFF FOR PRODUCTION
if __name__ == "__main__":
app.run('127.0.0.1', 5000, debug = True)