-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.py
303 lines (246 loc) · 9.3 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# from crypt import methods
from distutils.log import debug
from email.policy import default
import json
import MySQLdb
from colorama import Cursor
from flask import Flask, redirect, render_template, url_for, request, flash
from flask_mysqldb import MySQL
from classes.dotDict import dotdict
from forms import LoginForm, RegistrationForm
from flask_wtf import FlaskForm
# import os
app = Flask(__name__)
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = '' # nfakira
app.config['MYSQL_DB'] = 'flaskdb'
mysql = MySQL(app)
app.config['SECRET_KEY'] = '9adda8bf738de307aea09ba4faebcb19140a6223'
data = {}
def get_results(db_cursor):
desc = [d[0] for d in db_cursor.description]
results = [dotdict(dict(zip(desc, res))) for res in db_cursor.fetchall()]
return results
# -----------------------------------------------------------------
# -- home --
# -----------------------------------------------------------------
@app.route("/")
@app.route("/home")
def home():
# filename = os.path.join(app.static_folder, 'data/data.json')
with open("data/data.json", "r") as file:
data = json.load(file)
cursor = mysql.connection.cursor()
cursor.execute('select * from courses')
courses = get_results(cursor)
# courses = cursor.fetchall()
cursor.close()
cursor = mysql.connection.cursor()
cursor.execute('select * from lessons')
lessons = get_results(cursor)
# lessons = cursor.fetchall()
cursor.close()
return render_template("home.html", lessons=lessons, courses=courses)
@app.route("/about")
def about():
return render_template("about.html", title="About")
# -----------------------------------------------------------------
# -- register --
# -----------------------------------------------------------------
@app.route("/register", methods=['GET', 'POST'])
def registration():
form = RegistrationForm()
return render_template('register.html', title='register', form=form)
# -----------------------------------------------------------------
# -- login --
# -----------------------------------------------------------------
@app.route("/login", methods=['GET', 'POST'])
def login():
form = LoginForm()
if request.method == 'POST' and 'email' in request.form and 'password' in request.form:
email = request.form['email']
password = request.form['password']
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute(
'SELECT * FROM users WHERE email = %s AND password = %s', (email, password,))
# user = get_results(cursor)
user = cursor.fetchone()
cursor.close()
if form.validate_on_submit():
if user:
flash(f"user {user['username']} connected", 'success')
return redirect(url_for('home'))
else:
flash(f"email or password not correct", 'danger')
return render_template('login.html', title='login', form=form)
# -----------------------------------------------------------------
# -- register --
# -----------------------------------------------------------------
# @app.route("/register", methods=['POST'])
# def register():
# form = RegistrationForm()
# if request.method == 'POST':
# fname = request.form['fname']
# lname = request.form['lname']
# username = request.form['username']
# email = request.form['email']
# password = request.form['password']
# cursor = mysql.connection.cursor()
# cursor.execute('INSERT INTO users VALUES(%s,%s,%s,%s,%s)',
# (fname, lname, username, email, password))
# mysql.connection.commit()
# cursor.close()
# if form.validate_on_submit():
# flash(f"user created successfully", 'success')
# return redirect(url_for('registration'))
@app.route('/insert', methods=['GET', 'POST'])
def insert():
form = RegistrationForm()
if form.validate_on_submit():
fname = form.fname.data
lname = form.lname.data
username = form.username.data
email = form.email.data
password = form.password.data
cursor = mysql.connection.cursor()
cursor.execute('INSERT INTO users (fname, lname, username, email, password) VALUES (%s, %s, %s, %s, %s)',
(fname, lname, username, email, password))
mysql.connection.commit()
cursor.close()
flash('User created successfully', 'success')
return redirect(url_for('home'))
return render_template('register.html', title='Register', form=form)
# -----------------------------------------------------------------
# -- sign in --
# -----------------------------------------------------------------
# @app.route("/select" , methods=['POST'])
# def select():
# form = LoginForm()
# if request.method == 'POST':
# email = request.form['email']
# # password = request.form['password']
# cursor = mysql.connection.cursor()
# cursor.execute(f"select * from users where email='{email}'")
# # user = get_results(cursor)
# user = cursor.fetchall()
# cursor.close()
# # if form.validate_on_submit():
# if user[3] == email: # and user.password == password
# flash(f"user connected")
# return redirect(url_for('login'))
# else:
# flash(f"not connected")
# return render_template('login.html' , title='login', form = form)
if __name__ == "__main__":
app.run(debug=True)
# app.run(host='localhost', port=9000)
"""
from distutils.log import debug
from flask import Flask,render_template, request
from flask_mysqldb import MySQL
app = Flask(__name__)
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = ''
app.config['MYSQL_DB'] = 'flaskDB'
mysql = MySQL(app)
# #Creating a connection cursor
# with app.app_context():
# cursor = mysql.connection.cursor()
# #Executing SQL Statements
# # cursor.execute('CREATE TABLE table_name(field1 int, field2 int)')
# cursor.execute('INSERT INTO Test VALUES(1,"ZZ","DD")')
# # cursor.execute(' DELETE FROM table_name WHERE condition ')
# #Saving the Actions performed on the DB
# mysql.connection.commit()
# #Closing the cursor
# cursor.close()
@app.route('/form')
def form():
return render_template('form.html')
@app.route('/login', methods = ['POST', 'GET'])
def login():
if request.method == 'GET':
return "Login via the login Form"
if request.method == 'POST':
id = request.form['id']
name = request.form['name']
prenom = request.form['prenom']
cursor = mysql.connection.cursor()
cursor.execute('INSERT INTO Test VALUES(%s,%s,%s)',(id,name,prenom))
mysql.connection.commit()
cursor.close()
return f"Done!!"
app.run(host='localhost', port=5000)
"""
# lessons = [
# {
# "title": "Request Library Course",
# "course": "Python",
# "author": "Omar",
# "thumbnail": "thumbnail.jpg",
# },
# {
# "title": "Request Library Course",
# "course": "Python",
# "author": "Omar",
# "thumbnail": "thumbnail.jpg",
# },
# {
# "title": "Request Library Course",
# "course": "Python",
# "author": "Omar",
# "thumbnail": "thumbnail.jpg",
# },
# {
# "title": "Request Library Course",
# "course": "Python",
# "author": "Omar",
# "thumbnail": "thumbnail.jpg",
# },
# {
# "title": "Request Library Course",
# "course": "Python",
# "author": "Omar",
# "thumbnail": "thumbnail.jpg",
# },
# {
# "title": "Request Library Course",
# "course": "Python",
# "author": "Omar",
# "thumbnail": "thumbnail.jpg",
# },
# ]
# courses = [
# {
# "name": "Python",
# "icon": "python.svg",
# "description": "Lorem ipsum dolor sit amet consectetur adipisicing elit. Neque quidem nihil dolor officiis at magni!",
# },
# {
# "name": "Data Analysis",
# "icon": "analysis.png",
# "description": "Lorem ipsum dolor sit amet consectetur adipisicing elit. Neque quidem nihil dolor officiis at magni!",
# },
# {
# "name": "Machine Learning",
# "icon": "machine-learning.png",
# "description": "Lorem ipsum dolor sit amet consectetur adipisicing elit. Neque quidem nihil dolor officiis at magni!",
# },
# {
# "name": "Web Design",
# "icon": "web.png",
# "description": "Lorem ipsum dolor sit amet consectetur adipisicing elit. Neque quidem nihil dolor officiis at magni!",
# },
# {
# "name": "Blockchain",
# "icon": "blockchain.png",
# "description": "Lorem ipsum dolor sit amet consectetur adipisicing elit. Neque quidem nihil dolor officiis at magni!",
# },
# {
# "name": "Tips & Tricks",
# "icon": "idea.png",
# "description": "Lorem ipsum dolor sit amet consectetur adipisicing elit. Neque quidem nihil dolor officiis at magni!",
# },
# ]