forked from itzhv14/raitkart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
587 lines (451 loc) · 17 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
import json
import os
from datetime import datetime
from functools import wraps
from flask_wtf import CsrfProtect
from MySQLdb.cursors import Cursor
from flask import Flask, render_template, flash, redirect, url_for, session, request
from flask_mail import Mail
from flask_mysqldb import MySQL
from flask_wtf import FlaskForm
from flask_wtf.file import FileField, FileAllowed
from passlib.handlers import mysql
from werkzeug.utils import secure_filename
from wtforms import Form, StringField, PasswordField, validators, TextAreaField, SelectField
# json file contains email id and pass
with open('config.json', 'r') as c:
params = json.load(c)["params"]
app = Flask(__name__, static_url_path='/static')
app.secret_key = "super secret key"
app.config['UPLOAD_FOLDER'] = 'static/images'
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = ''
app.config['MYSQL_DB'] = 'raitkart'
app.config['MYSQL_CURSORCLASS'] = 'DictCursor'
app.config.update(
MAIL_SERVER='smtp.gmail.com',
MAIL_PORT='465',
MAIL_USE_SSL=True,
MAIL_USERNAME=params['gmail-user'],
MAIL_PASSWORD=params['gmail-password']
)
mail = Mail(app)
# init MYSQL
mysql = MySQL(app)
csrf = CsrfProtect()
def create_app():
app = Flask(__name__)
app.config.from_object('config.settings')
csrf.init_app(app)
@app.route('/')
def main():
return render_template('dashboard.html')
# about page banana hai
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/home')
def home():
return render_template('home.html')
# Check if user logged in
def is_logged_in(f):
@wraps(f)
def wrap(*args, **kwargs):
if 'logged_in' in session:
return f(*args, **kwargs)
else:
flash('Unauthorized, Please login', 'danger')
return redirect(url_for('login'))
return wrap
# Logout
@app.route('/logout')
@is_logged_in
def logout():
session.clear()
flash('You are now logged out', 'success')
return redirect(url_for('login'))
# Dashboard
@app.route('/dashboard')
@is_logged_in
def dashboard():
return render_template("dashboard.html")
@app.route('/contact', methods=['GET', 'POST'])
def contact():
form = Contact(request.form)
if request.method == 'POST' and form.validate():
name = form.name.data
roll_no = form.roll_no.data
phone_no = form.phone_no.data
email = form.email.data
message = form.message.data
# date = datetime.now()
# Create Cursor
cur: Cursor = mysql.connection.cursor()
# Execute
cur.execute(
"INSERT INTO contact(name, roll_no, phone_no, email, message, date) VALUES(%s, %s, %s, %s, %s, %s)",
(name, roll_no, phone_no, email, message, datetime.now())
)
# Commit to DB
mysql.connection.commit()
# Close connection
cur.close()
mail.send_message('Mail from RAITKART',
sender=[params['gmail-user']],
recipients=[email],
body="Thank you for contacting us"
)
mail.send_message('New message from ' + name,
sender=email,
recipients=[params['gmail-user']],
body=f"Message: {message}\n"
f"Roll no: {roll_no}\n"
f"Phone no: {phone_no}\n"
)
flash('Message Sent. Thank You for your feedback', 'success')
return redirect(url_for("dashboard"))
return render_template('contact.html', params=params, form=form)
# User Register
@app.route('/register', methods=['GET', 'POST'])
def register():
session.clear()
form = RegisterForm(request.form)
cur = mysql.connection.cursor()
if request.method == 'POST' and form.validate():
roll_no = form.roll_no.data
result = cur.execute("SELECT * FROM users WHERE roll_no = %s", [roll_no])
if result == 1:
cur.close()
flash("Already Registered, Please Login", "danger")
return redirect('login')
else:
roll_no = form.roll_no.data
name = form.name.data
email = form.email.data
phone_no = form.phone_no.data
password = form.password.data
# Create cursor
cur = mysql.connection.cursor()
# Execute query insert karne k liye users table me
cur.execute("INSERT INTO users (roll_no, name, email, phone_no, password) VALUES (%s, %s, %s, %s, %s)",
(roll_no, name, email, phone_no, password))
# Commit to DB
mysql.connection.commit()
# Close connection
cur.close()
# msg bhi flash honge
flash('You are registered, Please login', 'success')
return redirect(url_for('login'))
return render_template('register.html', form=form)
# User login
@app.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm(request.form)
if request.method == 'POST':
# Get Form Fields
roll_no = form.roll_no.data
password_candidate = form.password.data
# Create cursor
cur = mysql.connection.cursor()
# Get user by roll_no
result = cur.execute("SELECT * FROM users WHERE roll_no = %s", [roll_no])
if result == 1:
# Get stored hash
data = cur.fetchone()
password = data['password']
name = data['name']
email = data['email']
phone_no = data['phone_no']
# Compare Passwords
if password_candidate == password:
# Passed
session['logged_in'] = True
session['roll_no'] = roll_no
session['name'] = name
session['email'] = email
session['phone_no'] = phone_no
flash('You are now logged in', 'success')
return redirect(url_for('dashboard'))
else:
# Close connection
cur.close()
error = 'Invalid password'
return render_template('login.html', form=form, error=error)
else:
error = 'Roll No. not found. Please register!'
return render_template('login.html', form=form, error=error)
return render_template('login.html', form=form)
# Add product
@app.route('/sell', methods=['GET', 'POST'])
@is_logged_in
def sell():
form = Sell()
if form.validate_on_submit():
title = form.title.data
category = form.category.data
description = form.description.data
price = form.price.data
date = datetime.now()
f = request.files['picture']
f.save(os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(f.filename)))
url = f.filename
# Create Cursor
cur: Cursor = mysql.connection.cursor()
# Execute
cur.execute(
"INSERT INTO products (title, category, description, roll_no, price, date, phone_no, photo) VALUES(%s, %s, "
"%s, %s, %s, %s, %s, %s)",
(title, category, description, session['roll_no'], price, date, session['phone_no'], url))
# Commit to DB
mysql.connection.commit()
# Close connection
cur.close()
flash('Product Added', 'success')
return redirect(url_for('products'))
return render_template('sell.html', form=form)
@app.route('/buy')
@is_logged_in
def buy():
# Create cursor
cur = mysql.connection.cursor()
# Get products
result = cur.execute("SELECT * FROM products EXCEPT SELECT * FROM products WHERE roll_no=%s", [session['roll_no']])
products = cur.fetchall()
# Close connection
cur.close()
if result > 0:
return render_template('buy.html', products=products)
else:
msg = 'No Products Found'
return render_template('buy.html', msg=msg)
@app.route('/products')
@is_logged_in
def products():
# Create cursor
cur = mysql.connection.cursor()
# Show products only from the user logged in
result = cur.execute("SELECT * FROM products WHERE roll_no = %s", [session['roll_no']])
products = cur.fetchall()
cur.close()
if result > 0:
return render_template('products.html', products=products)
else:
msg = 'No Product Found'
return render_template('products.html', msg=msg)
# Single product
@app.route('/product/<string:id>/')
def product(id):
# Create cursor
cur = mysql.connection.cursor()
# Get product
result = cur.execute("SELECT * FROM products WHERE id = %s", [id])
product = cur.fetchone()
return render_template('product.html', product=product)
# Edit product
@app.route('/edit_product/<string:id>', methods=['GET', 'POST'])
@is_logged_in
def edit_products(id):
# Create cursor
cur = mysql.connection.cursor()
# Get product by id
cur.execute("SELECT * FROM products WHERE id = %s", [id])
product = cur.fetchone()
cur.close()
# Get form
form = Sell(request.form)
# Populate product form fields
form.title.data = product['title']
form.category.data = product['category']
form.description.data = product['description']
form.price.data = product['price']
if request.method == 'POST' and form.validate():
title = request.form['title']
f = request.files['picture']
f.save(os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(f.filename)))
url = f.filename
category = request.form['category']
description = request.form['description']
price = request.form['price']
# Create Cursor
cur = mysql.connection.cursor()
# Execute
cur.execute("UPDATE products SET title=%s, category=%s, description=%s, price=%s, photo=%s WHERE id=%s",
(title, category, description, price, url, id))
# Commit to DB
mysql.connection.commit()
# Close connection
cur.close()
flash('Product Updated', 'success')
return redirect(url_for('products'))
return render_template('edit_product.html', form=form)
# Delete product
@app.route('/delete_product/<string:id>', methods=['POST'])
@is_logged_in
def delete_product(id):
# Create cursor
cur = mysql.connection.cursor()
# Execute
cur.execute("DELETE FROM products WHERE id = %s", [id])
# Commit to DB
mysql.connection.commit()
# Close connection
cur.close()
flash('Product removed', 'success')
return redirect(url_for('products'))
@app.route("/profile", methods=['GET', 'POST'])
@is_logged_in
def profile():
roll_no = session['roll_no']
cur = mysql.connection.cursor()
# Get product
cur.execute("SELECT * FROM users WHERE roll_no = %s", [roll_no])
user = cur.fetchone()
cur.close()
return render_template('profile.html', user=user)
@app.route("/edit_profile", methods=['GET', 'POST'])
@is_logged_in
def edit_profile():
form = ProfileForm(request.form)
form.name.data = session['name']
form.email.data = session['email']
form.phone_no.data = session['phone_no']
if request.method == 'POST' and form.validate():
name = request.form['name']
email = request.form['email']
phone_no = request.form['phone_no']
roll_no = session['roll_no']
# Create Cursor
cur = mysql.connection.cursor()
# Execute
cur.execute("UPDATE users SET name=%s, email=%s, phone_no=%s WHERE roll_no = %s",
(name, email, phone_no, roll_no))
# Commit to DB
mysql.connection.commit()
# Close connection
cur.close()
session.clear()
flash('Profile Updated. Login again!', 'success')
return redirect(url_for('login'))
return render_template('edit_profile.html', form=form)
@app.route("/edit_password", methods=['GET', 'POST'])
@is_logged_in
def change_pass():
form = PassForm(request.form)
if request.method == 'POST' and form.validate():
password = form.password.data
roll_no = session['roll_no']
cur = mysql.connection.cursor()
# Execute
cur.execute("UPDATE users SET password=%s WHERE roll_no = %s",
(password, roll_no))
# Commit to DB
mysql.connection.commit()
# Close connection
cur.close()
session.clear()
flash('Password Changed. Login again!', 'success')
return redirect(url_for('login'))
return render_template('edit_password.html', form=form)
@app.route('/chat', methods=['GET', 'POST'])
@is_logged_in
def chat():
lis = []
# Create cursor
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM msg WHERE sender = %s or receiver= %s",
[session['roll_no'], session['roll_no']])
messages = cur.fetchall()
for i in messages:
lis.append(i['sender'].upper())
lis.append(i['receiver'].upper())
lis = list(dict.fromkeys(lis))
cur.close()
return render_template('chat.html', messages=messages, lis=lis)
@app.route('/chat/<string:receiver>/', methods=['GET', 'POST'])
def chats(receiver):
receiver = receiver
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM msg WHERE (receiver = %s and sender= %s) or (receiver = %s and sender= %s) ",
[session['roll_no'], receiver, receiver, session['roll_no']])
messages = cur.fetchall()
cur.close()
form = ChatForm()
if form.validate_on_submit():
receiver = receiver
message = form.message.data
sender = session['roll_no']
date = datetime.now()
cur: Cursor = mysql.connection.cursor()
cur.execute(
"INSERT INTO msg (message, sender, receiver, date) VALUES(%s, %s, "
"%s, %s)",
(message, sender, receiver, date))
# Commit to DB
mysql.connection.commit()
cur.close()
render_template('chats.html', messages=messages, form=form, receiver=receiver)
return redirect('')
return render_template('chats.html', messages=messages, form=form, receiver=receiver)
class Contact(Form):
roll_no: StringField = StringField('Roll No', [
validators.Length(max=8, min=8),
validators.DataRequired()])
name = StringField('Name', [validators.Length(min=1, max=50), validators.DataRequired()])
email = StringField('Email', [validators.Length(min=6, max=50), validators.DataRequired(), ])
phone_no = StringField('Phone No', [
validators.Length(max=10, min=10),
validators.DataRequired(), ])
message = TextAreaField('Message', [
validators.DataRequired(), ])
class RegisterForm(Form):
roll_no: StringField = StringField('Roll No', [
validators.Length(max=8, min=8),
validators.DataRequired()])
name = StringField('Name', [validators.Length(min=1, max=50), validators.DataRequired()])
email = StringField('Email', [validators.Length(min=6, max=50), validators.DataRequired(), ])
phone_no = StringField('Phone No', [
validators.Length(max=10, min=10),
validators.DataRequired(),
])
password = PasswordField('Password', [
validators.DataRequired(),
validators.EqualTo('confirm', message='Passwords do not match')
])
confirm = PasswordField('Confirm Password')
class LoginForm(Form):
roll_no: StringField = StringField('Roll No', [
validators.Length(max=8, min=8),
validators.DataRequired()])
password = PasswordField('Password', [
validators.DataRequired(),
validators.EqualTo('confirm', message='Passwords do not match')
])
class NewSelectField(SelectField):
def pre_validate(self, form):
pass
class Sell(FlaskForm):
title = StringField('Title', [validators.Length(min=1, max=20), validators.DataRequired()])
description = TextAreaField('Description', [validators.DataRequired()])
category = NewSelectField('Category', choices=[('Books', 'Books'), ('Drawing_Stuffs', 'Drawing_Stuffs'),
('Electronic_Gadgets', 'Electronic_Gadgets'), ('Others', 'Others')])
price = StringField('Price', [validators.DataRequired()])
date = datetime.now()
picture = FileField('Product Picture', validators=[FileAllowed(['jpg', 'png', 'jpeg'])])
class ChatForm(FlaskForm):
message = TextAreaField('Message:', [validators.DataRequired()])
class ProfileForm(Form):
name = StringField('Name', [validators.Length(min=1, max=50), validators.DataRequired()])
email = StringField('Email', [validators.Length(min=6, max=50), validators.DataRequired(), ])
phone_no = StringField('Phone No', [
validators.Length(max=10, min=10),
validators.DataRequired(),
])
class PassForm(Form):
password = PasswordField('New Password', [
validators.DataRequired(),
validators.EqualTo('confirm', message='Passwords do not match')
])
confirm = PasswordField('Confirm Password')
if __name__ == '__main__':
app.run(debug=True)