-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.py
More file actions
430 lines (363 loc) · 15.5 KB
/
Main.py
File metadata and controls
430 lines (363 loc) · 15.5 KB
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
from flask import Flask, render_template, request, flash, jsonify, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
from flask_bootstrap import Bootstrap
from flask_login import UserMixin, LoginManager, login_required, current_user, logout_user, login_user
from werkzeug.utils import secure_filename
from werkzeug.security import generate_password_hash, check_password_hash
from flask_wtf import FlaskForm
from wtforms import SubmitField, SelectField, RadioField, HiddenField, StringField, IntegerField, FloatField
from wtforms.validators import InputRequired, Length, Regexp, NumberRange
from datetime import date
import sqlite3
import os
app = Flask(__name__)
# Flask-WTF requires an enryption key - the string can be anything
app.config['SECRET_KEY'] = 'TRYt0Br3akTh1saaaMLXH243GssUWwKdTWS7FDhdwYF56wPj8'
# Flask-Bootstrap requires this line
Bootstrap(app)
# the name of the database; add path if necessary
db_name = 'database.db'
UPLOAD_FOLDER = '/uploads'
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg'])
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + db_name
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
# this variable, db, will be used for all SQLAlchemy commands
db = SQLAlchemy(app)
#Unsure if necessary
# app.register_blueprint(views, url_prefix='/')
# app.register_blueprint(auth, url_prefix='/')
login_manager = LoginManager()
login_manager.login_view = 'app.login'
login_manager.init_app(app)
@login_manager.user_loader
def load_user(id):
return User.query.get(int(id))
class SPost(db.Model):
# Defines the Table for sales postings
__tablename__ = 'SPost'
# Makes four columns into the table title, email, price, description
_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
title = db.Column(db.String(100), nullable=False)
email = db.Column(db.String(100), nullable=False)
price = db.Column(db.Float, nullable=False)
description = db.Column(db.String(1000), nullable=False)
updated = db.Column(db.String)
# A constructor function where we will pass the input information to a db
def __init__(self, title, email, price, description, updated):
self.title = title
self.email = email
self.price = price
self.description = description
self.updated = updated
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(150), unique=True)
password = db.Column(db.String(150))
first_name = db.Column(db.String(25))
class AddRecord(FlaskForm):
# id used only by update/edit
id_field = HiddenField()
title = StringField('Listing name', [ InputRequired(),
Regexp(r'^[A-Za-z\s\-\']+$', message="Invalid title name"),
Length(min=3, max=35, message="Invalid title length")
])
description = StringField('Description', [ InputRequired(),
Regexp(r'^[a-zA-Z0-9_.,!\(\)\' ]+$', message="Invalid description"),
Length(min=3, max=1000, message="Invalid description length")
])
price = FloatField('Price', [ InputRequired(),
NumberRange(min=0.01, max=9999.99, message="Invalid price range")
])
email = StringField('Email address', [ InputRequired(),
Regexp(r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$', message="Invalid email"),
Length(min=6, max=50, message="Invalid email length")
])
# updated - date - handled in the route function
updated = HiddenField()
submit = SubmitField('Add/Update Record')
# small form
class DeleteForm(FlaskForm):
id_field = HiddenField()
purpose = HiddenField()
submit = SubmitField('Delete This Card')
# +++++++++++++++++++++++
# get local date - does not account for time zone
# note: date was imported at top of script
def stringdate():
today = date.today()
date_list = str(today).split('-')
# build string in format 01-01-2000
date_string = date_list[1] + "-" + date_list[2] + "-" + date_list[0]
return date_string
# +++++++++++++++++++++++
# routes
'''
@app.route('/', methods=['GET', 'POST'])
@login_required
def home():
return render_template("MPHome.html", user=current_user)
'''
@app.route('/MPHome')
@login_required
def MPHome():
# get a list of unique values in the style column
titles = SPost.query.with_entities(SPost.title).distinct()
return render_template('MPHome.html', titles=titles, user=current_user)
@app.route('/', methods=['GET', 'POST'])
def defaultHome():
return render_template("defaultHome.html", user=current_user)
@app.route('/inventory/<title>')
@login_required
def inventory(title):
cards = SPost.query.filter_by(title=title).order_by(SPost.title).all()
return render_template('list.html', cards=cards, title=title)
#Add a new entry to the DB
@app.route('/MPHome/add_record', methods=['GET', 'POST'])
@login_required
def add_record():
form1 = AddRecord()
if form1.validate_on_submit():
title = request.form['title']
email = request.form['email']
price = request.form['price']
description = request.form['description']
#Capitalize first letter of each word for sorting later
strList = title.split()
newString = ''
for val in strList:
newString += val.capitalize()+ ' '
title = newString
# get today's date from function, above all the routes
updated = stringdate()
# the data to be inserted into card model - the table, card
record = SPost(title, email, price, description, updated)
# Flask-SQLAlchemy magic adds record to database
db.session.add(record)
db.session.commit()
# create a message to send to the template
message = f"The data for {title} has been submitted."
return render_template('add_record.html', message=message, user=current_user)
else:
# show validaton errors
for field, errors in form1.errors.items():
for error in errors:
flash("Error in {}: {}".format(
getattr(form1, field).label.text,
error
), 'error')
return render_template('add_record.html', form1=form1, user=current_user)
# select a record to edit or delete
@app.route('/select_record/<letters>')
@login_required
def select_record(letters):
# alphabetical lists by card name, chunked by letters between _ and _
# .between() evaluates first letter of a string
a, b = list(letters)
cards = SPost.query.filter(SPost.title.between(a, b)).order_by(SPost.title).all()
return render_template('select_record.html', cards=cards, user=current_user)
# edit or delete - come here from form in /select_record
@app.route('/edit_or_delete', methods=['POST'])
@login_required
def edit_or_delete():
_id = request.form['id']
choice = request.form['choice']
card = SPost.query.filter(SPost._id == _id).first()
# two forms in this template
form1 = AddRecord()
form2 = DeleteForm()
return render_template('edit_or_delete.html', card=card, form1=form1, form2=form2, choice=choice, user=current_user)
# result of delete - this function deletes the record
@app.route('/delete_result', methods=['POST'])
@login_required
def delete_result():
id = request.form['id_field']
purpose = request.form['purpose']
card = SPost.query.filter(SPost._id == id).first()
if purpose == 'delete':
db.session.delete(card)
db.session.commit()
message = f"The card {card.title} has been deleted from the database."
return render_template('result.html', message=message, user=current_user)
else:
# this calls an error handler
abort(405)
# result of edit - this function updates the record
@app.route('/edit_result', methods=['POST'])
@login_required
def edit_result():
_id = request.form['id_field']
# call up the record from the database
card = SPost.query.filter(SPost._id == _id).first()
# update all values
card.title = request.form['title']
card.email = request.form['email']
card.description = request.form['description']
card.price = request.form['price']
# get today's date from function, above all the routes
card.updated = stringdate()
#Capitalize first letter of each word for sorting later
strList = card.title.split()
newString = ''
for val in strList:
newString += val.capitalize()+ ' '
card.title = newString
form1 = AddRecord()
if form1.validate_on_submit():
# update database record
db.session.commit()
# create a message to send to the template
message = f"The data for card {card.title} has been updated."
return render_template('result.html', message=message, user=current_user)
else:
# show validaton errors
card._id = _id
for field, errors in form1.errors.items():
for error in errors:
flash("Error in {}: {}".format(
getattr(form1, field).label.text,
error
), 'error')
return render_template('edit_or_delete.html', form1=form1, card=card, choice='edit', user=current_user)
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
email = request.form.get('email')
password = request.form.get('password')
user = User.query.filter_by(email=email).first()
if user:
if check_password_hash(user.password, password):
flash('Logged in successfully!', category='success')
login_user(user, remember=True)
return redirect(url_for('MPHome'))
else:
flash('Incorrect password, try again.', category='error')
else:
flash('Email does not exist.', category='error')
return render_template("login.html", user=current_user)
@app.route('/logout')
def logout():
logout_user()
return redirect(url_for('defaultHome'))
@app.route('/sign-up', methods=['GET', 'POST'])
def sign_up():
if request.method == 'POST':
email = request.form.get('email')
first_name = request.form.get('firstName')
password1 = request.form.get('password1')
password2 = request.form.get('password2')
user = User.query.filter_by(email=email).first()
first_nameCheck = User.query.filter_by(first_name=first_name).first()
if user:
flash('Email already exists.', category='error')
elif first_nameCheck:
flash('Username already exists.', category='error')
elif len(email) < 4:
flash('Email must be greater than 3 characters.', category='error')
elif len(first_name) < 2:
flash('Username must be greater than 1 character.', category='error')
elif password1 != password2:
flash('Passwords don\'t match.', category='error')
elif len(password1) < 7:
flash('Password must be at least 7 characters.', category='error')
else:
new_user = User(email=email, first_name=first_name, password=generate_password_hash(
password1, method='sha256'))
db.session.add(new_user)
db.session.commit()
login_user(new_user, remember=True)
flash('Account created!', category='success')
return redirect(url_for('MPHome'))
return render_template("sign_up.html", user=current_user)
@app.route('/changePassword', methods=['GET', 'POST'])
@login_required
def changePassword():
if request.method == 'POST':
user = current_user
oldPassword = user.password
newPassword1 = request.form.get('password2')
newPassword2 = request.form.get('password3')
if check_password_hash(oldPassword, newPassword1):
flash('Your new password must be different from your old password.', category='error')
elif newPassword1 != newPassword2:
flash('Passwords don\'t match.', category='error')
elif len(newPassword1) < 7:
flash('Password must be at least 7 characters.', category='error')
else:
user.password = generate_password_hash(newPassword1, method='sha256')
db.session.add(user)
db.session.commit()
flash('Password Updated!', category='success')
return redirect(url_for('MPHome'))
return render_template("changePW.html", user=current_user)
@app.route('/accountManagement', methods=['GET', 'POST'])
@login_required
def accountManagement():
return render_template("accountM.html", user=current_user)
@app.route('/changeUsername', methods=['GET', 'POST'])
@login_required
def changeUsername():
if request.method == 'POST':
user = current_user
oldUsername = user.first_name
newUsername = request.form.get('firstName')
newUsername1 = request.form.get('firstName1')
newUsernameCheck = User.query.filter_by(first_name=newUsername).first()
if newUsername == oldUsername:
flash('Your new username must be different from your old username.', category='error')
elif len(newUsername) < 2:
flash('Username must be greater than 1 character.', category='error')
elif newUsernameCheck:
flash('Username already exists.', category='error')
elif newUsername != newUsername1:
flash('Usernames don\'t match.', category='error')
else:
user.first_name = newUsername
db.session.add(user)
db.session.commit()
flash('Username Updated!', category='success')
return redirect(url_for('accountManagement'))
return render_template("changeUsername.html", user=current_user)
@app.route('/deleteAccount', methods=['GET', 'POST'])
@login_required
def deleteAccount():
if request.method == 'POST':
user = current_user
password = request.form.get('password1')
if check_password_hash(user.password, password) == False:
flash('Your password is incorrect.', category='error')
else:
flash('Account Deleted', category='success')
db.session.delete(user)
db.session.commit()
return redirect(url_for('defaultHome'))
return render_template("deleteAccount.html", user=current_user)
@app.route('/managerPage', methods=['GET', 'POST'])
@login_required
def managerPage():
connection = sqlite3.connect("database.db")
crsr = connection.cursor()
cardCrsr = connection.cursor()
crsr.execute("SELECT email, first_name FROM user")
cardCrsr.execute("SELECT * FROM SPost")
ans = crsr.fetchall()
cardAns = cardCrsr.fetchall()
length = len(ans)
cardLength = len(cardAns)
connection.close()
return render_template("managerPage.html", user=current_user, ans=ans, length = length, cardAns=cardAns, cardLength=cardLength)
# +++++++++++++++++++++++
# error routes
@app.errorhandler(404)
def page_not_found(e):
return render_template('error.html', pagetitle="404 Error - Page Not Found", pageheading="Page not found (Error 404)", error=e), 404
@app.errorhandler(405)
def form_not_posted(e):
return render_template('error.html', pagetitle="405 Error - Form Not Submitted", pageheading="The form was not submitted (Error 405)", error=e), 405
@app.errorhandler(500)
def internal_server_error(e):
return render_template('error.html', pagetitle="500 Error - Internal Server Error", pageheading="Internal server error (500)", error=e), 500
# +++++++++++++++++++++++
if __name__ == '__main__':
db.create_all()
app.run(debug=True)