-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
130 lines (105 loc) · 4.17 KB
/
main.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
from flask import Flask, render_template, request, redirect, url_for, session, jsonify, make_response
import os
from werkzeug.utils import secure_filename
from flaskext.mysql import MySQL
app = Flask(__name__)
@app.route("/")
def main():
# # Connecting to mysql server
conn = myMysqlApp.connect()
cursor = conn.cursor()
# # getting data
cursor.execute("SELECT * FROM `products__info`")
fetchAllProductsData = cursor.fetchall()
data = { "prodctData" : fetchAllProductsData, "lenght" : len(fetchAllProductsData)}
template = 'index.html'
return render_template(template, productsDataForAdding=data)
@app.route("/admin")
def admin():
if 'admin' in session:
if 'productAddedMsg' in session:
# it will delete specifed session that's done
session.pop('productAddedMsg')
return render_template('admin.html', vari="Product Added")
return render_template('admin.html')
else:
return render_template('login.html')
@app.route('/productdata', methods=['POST', 'GET'])
def product_data_save():
if request.method == 'POST':
form_data = request.form
prductName = form_data['producName']
productDescription = form_data['discription']
priceProduct = form_data['price']
file_form = request.files['productFile']
# Connecting to mysql
conn = myMysqlApp.connect()
cursor = conn.cursor()
# secure filename for on space and no error while saving
fileSeries = 0
cursor.execute("SELECT * FROM `products__info`")
product_info_data = cursor.fetchall()
if product_info_data != ():
print(product_info_data)
# product_info_data
fileSeries = len(product_info_data)
fileSeries+=1
procductFileName = 'static/products/'+secure_filename(f'productImage_{str(fileSeries)}')
cursor.execute(f"INSERT INTO `products__info` (`Sn.`, `Products Name`, `Description`, `Price`, `Filename`) VALUES (NULL, '{prductName}', '{productDescription}', '{priceProduct}', '{procductFileName}');")
file_form.save(procductFileName)
session['productAddedMsg'] = 'productadded'
return redirect(url_for('admin'))
else:
return "Get Method Not Allowed"
@app.route("/login")
def login():
print(session)
if 'admin' in session:
return redirect(url_for('admin'))
elif 'logoutSuccessfully' in session:
session['logoutSuccessfully'] = ''
vari = 'Logout Successfully'
app.secret_key = 'SeSsIoNkEyFoRLoginR'
session.clear()
return render_template('login.html', vari=vari)
elif 'wrongPass' in session:
print("ehree")
session.clear()
return render_template('login.html', vari="Wrong Username and Password Combination. Try Again")
else:
return render_template('login.html')
@app.route("/checkLogin", methods=['POST'])
def checkLogin():
data = request.form
print(data['username'])
if data['username'] == 'admin':
session.clear()
session['admin'] = data['username']
return redirect("/admin")
else:
session['wrongPass'] = 'wrongPass'
return redirect(url_for('login'))
@app.route("/adminsignout")
def adminSignOut():
if 'admin' in session:
session.clear()
session['logoutSuccessfully'] = 'logoutSuccessfully'
return redirect(url_for('login'))
else:
return "There is no any problem"
@app.route("/signup")
def signup():
return "Coming Soon"
if __name__ == '__main__':
myMysqlApp = MySQL()
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = ''
app.config['MYSQL_DATABASE_PORT'] = 3308
app.config['MYSQL_DATABASE_DB'] = "mydatabase"
myMysqlApp.init_app(app)
app.secret_key = 'SeSsIoNkEyFoRLogin'
app.config['SESSION_TYPE'] = 'login'
app.config['UPLOAD_FOLDER'] = 'static/products'
app.config['MAX_CONTENT_LENGTH'] = 1024 * 1024
app.run(debug=True)