-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
497 lines (387 loc) · 17.7 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
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
import re, os
from flask import Flask, render_template, request, session, redirect, url_for, flash
from flask_sqlalchemy import SQLAlchemy
from flask_login import UserMixin, LoginManager, login_user, logout_user, current_user, login_required
from werkzeug.utils import secure_filename
from flask_principal import Principal
from werkzeug.security import generate_password_hash, check_password_hash
from datetime import datetime
from functools import wraps
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///C:/Users/Modda/PycharmProjects/Web database/Test3.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SECRET_KEY'] = 'Secret key'
db = SQLAlchemy(app)
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
def admin_required(func):
@wraps(func)
def decorated_view(*args, **kwargs):
if current_user.role_id != 1:
flash('You are not authorised', 'warning')
return redirect(url_for('index'))
return func(*args, **kwargs)
return decorated_view
class User(db.Model, UserMixin):
username = db.Column('username', db.String(80), unique=True, nullable=False)
id = db.Column('id', db.Integer, primary_key=True)
email = db.Column('email', db.String(100), unique=True, nullable=False)
password = db.Column('password', db.String(150), nullable=False)
date_added = db.Column('date_added', db.DateTime, default=datetime.utcnow)
role_id = db.Column(db.Integer, db.ForeignKey('role.id'))
active = db.Column('active', db.Boolean, default=True)
class Role(db.Model):
name = db.Column('name', db.String(25), unique=True, nullable=False)
id = db.Column('id', db.Integer, primary_key=True, nullable=False)
Users = db.relationship('User', backref='users')
"""
a = Role(name='Admin')
b = Role(name='Employee')
c = Role(name='User')
db.session.add_all([a,b,c])
db.session.commit()
#db.session.rollback()
#db.session.commit()
"""
"""
email1 = '[email protected]'
email3 = '[email protected]'
email2 = '[email protected]'
password_1 = 'Bongcloud'
password_2 = 'Fianchetto'
password_3 = 'Estoy_loco'
user_1 = User(username='Moda',email=email1, password=generate_password_hash(password_1,method='sha256'),role_id=1)
user_2 = User(username='Iantoz',email=email2, password=generate_password_hash(password_2,method='sha256'),role_id=1)
user_3 = User(username='Aphrodite',email=email3, password=generate_password_hash(password_3,method='sha256'),role_id=1)
db.session.add_all([user_1,user_2,user_3])
db.session.commit()
"""
"""
email1= '[email protected]'
email2= '[email protected]'
email3= '[email protected]'
email4= '[email protected]'
password_1 ='Valor_Morghulis'
password_2 ='Juicy'
password_3 ='Amore'
password_4 = 'Westcoast'
user_1 = User(username='Cross',email=email1, password=generate_password_hash(password_1,method='sha256'),role_id=2)
user_2 = User(username='Biggie',email=email2, password=generate_password_hash(password_2,method='sha256'),role_id=2)
user_3 = User(username='Dawn',email=email3, password=generate_password_hash(password_3,method='sha256'),role_id=2)
user_4 = User(username='Tupac',email=email4, password=generate_password_hash(password_4,method='sha256'),role_id=2)
db.session.add_all([user_1,user_2,user_3,user_4])
db.session.commit()
"""
x = User.query.filter_by(role_id=1).all()
for i in x:
print(str(i.date_added))
# db.session.delete(i)
# db.session.commit()
x = Role.query.all()
for i in x:
print("\n\n" + i.name, "|" + str(i.Users) + "\n")
for e in i.Users:
print(e.email, " | " + str(e.id), " | " + e.username, " | " + str(e.date_added), " | " + e.password)
print('Thanks')
product_tag = db.Table('product_tag',
db.Column('product_id', db.Integer, db.ForeignKey('product.product_id')),
db.Column('tag_id', db.Integer, db.ForeignKey('tag.tag_id'))
)
class Product(db.Model):
product_id = db.Column('product_id', db.Integer, primary_key=True)
file_path = db.Column('file_path', db.String(), nullable=False)
product_name = db.Column('product_name', db.String(80),nullable=False)
product_variations = db.relationship('Variation', backref= 'product')
product_category = db.Column('product_category', db.String(50), nullable=False)
product_description = db.Column('product_description', db.Text, nullable=False)
product_price = db.Column('price', db.Integer,nullable=False)
product_tags = db.relationship('Tag', secondary = product_tag, backref = 'product')
date_added = db.Column('date_added', db.DateTime, default=datetime.utcnow)
class Variation(db.Model):
id = db.Column('var_id', db.Integer, primary_key=True)
file_path = db.Column('file_path', db.String())
parent_product_id = db.Column(db.Integer,db.ForeignKey('product.product_id'))
class Tag(db.Model):
id = db.Column('tag_id', db.Integer, primary_key=True)
tag = db.Column('tag', db.String(25), nullable=False, unique=True)
class Employee(db.Model):
employee_name = db.Column('employee_name', db.String(100))
employee_id = db.Column('employee_id', db.Integer, primary_key=True)
user_id = db.Column('user_id', db.Integer())
department_name = db.Column('department', db.ForeignKey('department.name'))
class Department(db.Model):
name = db.Column('name', db.String(25), unique=True, nullable=False)
id = db.Column('id', db.Integer, primary_key=True, nullable=False)
employees = db.relationship('Employee', backref='employee')
"""
class Orders(db.Model):
customer_name= db.Column('customer_name', db.String(100), nullable=False)
customer_id= db.Column('customer_id', db.Integer, nullable=False)
ordered_products = db.relationship('Ordered_products', backref='ordered_products')
order_id = db.Column('order_id', db.Integer, primary_key=True)
total_quantities = db.Column('quantity', db.Integer)
total_amount = db.Column('total_amount', db.Integer)
location = db.Column('delivery_location', db.String(100))
status = db.Column('delivery_status', db.Boolean)
order_date = db.Column('order_date',db.DateTime, default=datetime.utcnow)
class Ordered_products(db.Model):
row_id = db.Column('row_id', db.Integer, primary_key=True)
product_name = db.Column('product_name', db.String(100), nullable=False)
product_id = db.Column('product_id', db.Integer nullable=False)
quantities = db.Column('quantity', db.Integer(),nullable=False)
total_price = db.Column('total_price',db.Integer,nullable=False)
parent_order_id = db.Column(db.Integer, db.ForeignKey('orders.order_id'))
class Sales(db.Model):
prod_name = db.Column('product_name', db.String(100), nullable=False)
purchases_quantity = db.Column('purchases_quantity', db.Integer, default=0)
returns_quantity = db.Column('returns_quantity', db.Integer, default=0)
revenue = db.Column('revenue', db.Integer, default=0)
category_id = db.Column('category_id', db.Integer, primary_key=True)
"""
@app.route('/')
@login_required
def index():
path = 'static/images/'
images = os.listdir(path)
route = [file for file in images]
images = ['images/' + file for file in images]
print(images)
return render_template("index.html", images=images)
@app.route('/description/<directory>/<filename>')
@login_required
def description(directory, filename):
print(filename)
path = directory + '/' + filename
print(path)
return render_template("description.html", path=path)
@app.route('/signup', methods=['GET', 'POST'])
def sign_up():
if request.method == 'POST':
username = request.form.get("Username")
email = request.form.get("Email")
password = request.form.get("password")
confirm_password = request.form.get("Confirm_password")
pattern = '[a-z A-Z 0-9]+@[a-zA-Z]+\.(com|edu|net)$'
if not username:
flash('Please enter Username')
return redirect(url_for('sign_up'))
elif not email:
flash(" Please enter Email")
return redirect(url_for('sign_up'))
elif not password:
flash('Please enter Password')
return redirect(url_for('sign_up'))
elif not confirm_password:
flash('Please enter Password confirmation')
return redirect(url_for('sign_up'))
elif password != confirm_password:
flash("Password does not match password confirmation")
return redirect(url_for('sign_up'))
elif len(password) <= 7:
flash('Password is too short.At least 8 characters required')
return redirect(url_for('sign_up'))
elif len(email) <= 4:
flash('Email is too short')
return redirect(url_for('sign_up'))
elif not (re.search(pattern, email)):
flash('Invalid email')
return redirect(url_for('sign_up'))
else:
username_exists = User.query.filter_by(username=username).first()
email_exists = User.query.filter_by(email=email).first()
if username_exists:
flash('Username already exists')
return redirect(url_for('sign_up'))
elif email_exists:
flash('Email already has an account. Please login to account')
return redirect(url_for('login'))
else:
new_user = User(email=email, username=username,
password=generate_password_hash(password, method='sha256'),
role_id=3)
db.session.add(new_user)
db.session.commit()
login_user(new_user, remember=False)
y = User.query.all()
for i in y:
if i.email:
print(i.id, ' | ' + i.username, ' | ' + i.email, ' | ' + i.password, '|' + str(i.role_id))
flash('Welcome '+username)
return redirect(url_for('index'))
return render_template("signup.html")
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form.get("Username")
password = request.form.get("password")
if not username:
flash('Please enter a username')
return redirect(url_for('login'))
elif not password:
flash('Please enter Password')
return redirect(url_for('login'))
else:
y = User.query.filter_by(username=username).first()
if y:
if check_password_hash(y.password, password):
login_user(y, remember=False)
if current_user.role_id == 1:
flash('Welcome Admin '+username)
return redirect(url_for('index'))
elif current_user.role_id == 2:
flash('Welcome Employee ' + username)
return redirect(url_for('index'))
else:
flash('Welcome ' + username)
return redirect(url_for('index'))
else:
flash('Please enter correct password')
return redirect(url_for('login'))
else:
flash('Please enter a valid username')
return redirect(url_for('login'))
return render_template("login.html")
@app.route('/admin', methods=['GET', 'POST'])
@login_required
@admin_required
def admin():
return render_template("admin.html")
@app.route('/admin_employees', methods=['GET', 'POST'])
@login_required
@admin_required
def admin_employees():
if request.method == 'POST':
username = request.form.get("Username")
email = request.form.get("Email")
password = request.form.get("password")
department = request.form.get("department")
pattern = '[a-z A-Z 0-9]+@[a-zA-Z]+\.(com|edu|net)$'
if not username:
flash('Please enter Username')
return redirect(url_for('admin_employees'))
elif not email:
flash(" Please enter Email")
return redirect(url_for('admin_employees'))
elif not password:
flash('Please enter Password')
return redirect(url_for('admin_employees'))
elif not department:
flash('Please choose department')
return redirect(url_for('admin_employees'))
elif len(password) <= 7:
flash('Password is too short.At least 8 characters required')
return redirect(url_for('admin_employees'))
elif len(email) <= 4:
flash('Email is too short')
return redirect(url_for('admin_employees'))
elif not (re.search(pattern, email)):
flash('Invalid email')
return redirect(url_for('admin_employees'))
else:
username_exists = User.query.filter_by(username=username).first()
email_exists = User.query.filter_by(email=email).first()
if username_exists:
flash('Username already exists')
return redirect(url_for('admin_employees'))
elif email_exists:
flash('Email already has an account. Please login to account')
return redirect(url_for('admin_employees'))
else:
new_user = User(email=email, username=username,
password=generate_password_hash(password, method='sha256'),
role_id=2)
db.session.add(new_user)
db.session.commit()
x = User.query.filter_by(email=email).first()
new_employee = Employee(employee_name=username, user_id=x.id, department_name=department)
db.session.add(new_employee)
db.session.commit()
login_user(new_user, remember=False)
flash('Employee ' + username + 'added')
return redirect(url_for('admin_employees'))
return render_template("admin_employees.html")
@app.route('/admin_noticeboard', methods=['GET', 'POST'])
@login_required
@admin_required
def admin_noticeboard():
return render_template("admin_noticeboard.html")
app.config['UPLOAD_PATH'] = 'static/images'
@app.route('/admin_product', methods=['GET', 'POST'])
@login_required
@admin_required
def admin_product():
if request.method == 'POST':
product = request.files["Product"]
variations = request.files.getlist("Variations[]")
name = request.form.get("Name")
category = request.form.get("Category")
price = request.form.get("Price")
tags = request.form.get("Tags")
desc = request.form.get("Description")
print(variations)
if not product:
flash('Please add a product file')
return redirect(url_for('admin_product'))
elif not variations:
flash(" Please enter product variations files")
return redirect(url_for('admin_product'))
elif not name:
flash("Please enter Product's name")
return redirect(url_for('admin_product'))
elif not category:
flash("Please enter Product's category")
return redirect(url_for('admin_product'))
elif not price:
flash(" Please enter Product's price")
return redirect(url_for('admin_product'))
elif not desc:
flash("Please enter Product's description")
return redirect(url_for('admin_product'))
else:
prodname = secure_filename(product.filename)
prodpath = os.path.join(app.config['UPLOAD_PATH'], prodname)
new_product = Product(file_path=prodpath, product_name=name, product_category=category, product_price=price, product_description=desc)
db.session.add(new_product)
db.session.commit()
if tags:
for a in tags:
d = Tag.query.filter_by(tag=a).first()
if x:
new_product.product_tags.append(d.tag)
else:
new_tag = Tag(tag=a)
db.session.add(new_tag)
db.session.commit()
new_product.product_tags.append(a)
z = Product.query.filter_by(filepath=prodpath).first()
for file in variations:
filename = secure_filename(file.filename)
filepath = os.path.join(app.config['UPLOAD_PATH'], filename)
new_var = Variation(file_path=filepath, parent_product_id=z.id)
db.session.add(new_var)
db.commit()
file.save(filepath)
return render_template("admin_product.html")
@app.route('/admin_users', methods=['GET', 'POST'])
@login_required
@admin_required
def admin_users():
return render_template("admin_users.html")
@app.route('/admin_data', methods=['GET', 'POST'])
@login_required
@admin_required
def admin_data():
return render_template("admin_users.html")
@app.route('/logout', methods=['GET', 'POST'])
@login_required
def logout():
logout_user()
flash('Logged out Successfully')
return redirect(url_for('login'))
if __name__ == '__main__':
db.create_all()
app.run(debug=False)