-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
205 lines (162 loc) · 5.15 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
from flask import Flask, render_template, url_for, redirect, flash, request, session
import mysql.connector as sql
import re
app = Flask(__name__)
app.secret_key = "secret key"
fn = ""
ln = ""
em = ""
pwd = ""
# Database connection function
def get_db_connection():
connection = sql.connect(
host="localhost", database="CISCO", user="root", password="manu"
)
return connection
# Initialize the database and create users table
def init_db():
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute(
"""
create table IF NOT EXISTS records (
fname varchar(50) not null,
lname varchar(50) not null,
email varchar(50) not null unique,
password varchar(50) not null
)
"""
)
conn.commit()
conn.close()
# Home route
@app.route("/")
def d():
return render_template("signup.html")
@app.route("/home")
def home():
return render_template("home.html")
# Dashboard route (protected page)
@app.route("/dashboard")
def dashboard():
if "username" in session:
flash("username")
return render_template("dashboard.html")
else:
return redirect(url_for("login"))
# Blog route
@app.route("/blog")
def blog():
return render_template("blog.html")
# Services route
@app.route("/services")
def services():
return render_template("services.html")
# About route
@app.route("/about")
def about():
return render_template("about.html")
# Sign-up route
@app.route("/signup", methods=["GET", "POST"])
def signup():
msg = ""
global fn, ln, em, pwd
if request.method == "POST":
d = request.form
for key, value in d.items():
if key == "fname":
fn = value
if key == "lname":
ln = value
if key == "email":
em = value
if key == "password":
pwd = value
try:
conn = get_db_connection()
cursor = conn.cursor()
"select * from records where email='{}'".format(em)
record = cursor.fetchone()
if record:
msg = "record already exists !"
flash(msg)
return redirect(url_for("signup"))
elif not re.match(r"[A-Za-z]+", fn):
msg = "First name must contain only characters!"
flash(msg, "danger")
return redirect(url_for("signup"))
elif not re.match(r"[A-Za-z]+", ln):
msg = "Last name must contain only characters!"
flash(msg, "danger")
return redirect(url_for("signup"))
elif not fn or not ln or not em or not pwd:
msg = "Please fill out the form !"
flash(msg, "danger")
return redirect(url_for("signup"))
else:
query = "insert into records Values('{}','{}','{}','{}')".format(
fn, ln, em, pwd
)
cursor.execute(query)
conn.commit()
msg = "You have successfully registered !"
flash(msg, "danger")
return redirect(url_for("home"))
# except sql.IntegrityError:
except Exception:
msg = "Username already exists. Please try a different one."
flash(msg, "danger")
return redirect(url_for("signup"))
msg = "Please fill out the form !"
flash(msg, "danger")
return render_template("signup.html")
# Login route
@app.route("/login", methods=["GET", "POST"])
def login():
msg = ""
global em, pwd
if request.method == "POST":
d = request.form
for key, value in d.items():
if key == "email":
em = value
if key == "password":
pwd = value
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute(
"select * from records where email='{}' and password='{}'".format(em, pwd)
)
record = cursor.fetchone()
if record:
session["loggedin"] = True
session["username"] = record[1]
msg = "Logged in successfully !"
flash(msg, "danger")
return redirect(url_for("dashboard"))
elif not em:
msg = "Email should not be empty !"
# messages.append(msg)
flash(msg, "danger")
return redirect(url_for("login"))
elif not pwd:
msg = "Password should not be empty !"
# messages.append(msg)
flash(msg, "danger")
return redirect(url_for("login"))
else:
msg = "Incorrect email / password !. Try Again"
# messages.append(msg)
flash(msg, "danger")
return redirect(url_for("login"))
msg = "Please fill out the form !"
flash(msg, "danger")
return render_template("login.html")
@app.route("/logout")
def logout():
session.pop("loggedin", None)
session.pop("username", None)
return redirect(url_for("home"))
if __name__ == "__main__":
init_db() # Ensure the database is set up
app.run(debug=True)