Last active
July 22, 2021 11:01
-
-
Save mossyblog/33011bf177ab2a36890313c60151f980 to your computer and use it in GitHub Desktop.
Sons First Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, request, redirect, url_for, render_template, session, flash | |
import sqlite3 | |
import hashlib | |
import dbMain | |
app = Flask(__name__) | |
app.config["SECRET_KEY"] = "my_secret_key" | |
db_locale = 'igdbaccounts.db' | |
#---------------------------------------------- | |
# DEFAULT PAGE | |
#---------------------------------------------- | |
@app.route('/', methods=['GET', 'POST']) | |
def index(): | |
return render_template("index.html") | |
#---------------------------------------------- | |
# SIGN UP | |
#---------------------------------------------- | |
@app.route('/sign-up', methods=['GET', 'POST']) | |
def sign_up(): | |
if request.method == 'POST': | |
username = request.form['username'] | |
email = request.form['email'] | |
firstname = request.form['firstname'] | |
lastname = request.form['lastname'] | |
password = request.form['password'] | |
genre = request.form['genre'] | |
dbMain.insertItem(email, firstname, lastname, username, password, genre) | |
flash('Account created successfully') | |
return redirect('/') | |
return render_template('sign_up.html') | |
def check_password(hashed_password, user_password): | |
return hashed_password == hashlib.md5(user_password.encode()).hexdigest() | |
#---------------------------------------------- | |
# LOGIN | |
#---------------------------------------------- | |
@app.route('/login', methods=['GET', 'POST']) | |
def login(): | |
if request.method == 'POST': | |
session['email'] = request.form['email'] | |
session['password'] = request.form['password'] | |
isAdmin = validate(session['email'], session['password']) | |
if isAdmin == True: | |
flash('Logged in successfully!') | |
return redirect('/admin') | |
return render_template('login.html') | |
#---------------------------------------------- | |
# ADMIN | |
#---------------------------------------------- | |
@app.route('/admin', methods=['GET', 'POST']) | |
def admin(): | |
# Lets validat that the user is still an admin and we know about them. | |
if session.get('email'): | |
# If the sessions aren't a valid Admin kick them out. | |
isAdmin = validate(session['email'], session['password']) | |
if isAdmin == False: | |
return redirect('login.html') | |
# If they are an admin, show them admin stuff. | |
rows = getAllReviews() | |
return render_template('admin.html', review_data = rows) | |
# if they aren't an admin, redirect back to login. | |
return render_template('login.html') | |
#---------------------------------------------- | |
# LOGOUT | |
#---------------------------------------------- | |
@app.route('/logout', methods=['GET', 'POST']) | |
def logout(): | |
# If Session Details dont have email password as valid admins redirect back to login. | |
session.clear() | |
return redirect('/login') | |
#---------------------------------------------- | |
# GAME REVIEWS | |
#---------------------------------------------- | |
@app.route('/gamereviews') | |
def database(): | |
rows = getAllReviews() | |
return render_template('reviews.html', review_data = rows) | |
@app.route('/addreviews', methods = ['GET', 'POST']) | |
def reivewController(): | |
if request.method == 'POST': | |
addNewReview(request.form['username'], | |
request.form['time'], | |
request.form['date'], | |
request.form['rating'], | |
request.form["game"], | |
request.form['comment']) | |
return redirect('/reviews') | |
return render_template('reviewcreate.html') | |
#---------------------------------------------- | |
# LOOKING FOR GROUP | |
#---------------------------------------------- | |
@app.route('/lookingforgroup') | |
def database1(): | |
con = sqlite3.connect('igdbaccounts.db') | |
cursor = con.cursor() | |
cursor.execute("SELECT * FROM lfg") | |
rows = cursor.fetchall() | |
return render_template('lfg.html', lfg_data = rows) | |
@app.route('/addlfg', methods = ['GET', 'POST']) | |
def lfgController(): | |
if request.method == 'POST': | |
addNewLookingForGroups(request.form['username'], | |
request.form['time'], | |
request.form['date'], | |
request.form['players'], | |
request.form["game"], | |
request.form['space'], | |
request.form['communication']) | |
return redirect('/lfg') | |
return render_template('lgfcreate.html') | |
#---------------------------------------------- | |
# FUNCTIONS | |
#---------------------------------------------- | |
def check_password(hashed_password, user_password): | |
return hashed_password == hashlib.md5(user_password.encode()).hexdigest() | |
def validate(email, password): | |
con = sqlite3.connect('igdbaccounts.db') | |
isfound = False | |
with con: | |
cur = con.cursor() | |
cur.execute("SELECT email, password FROM accounts WHERE isadmin = 1 AND email = '" + email + "' AND password = '" + password + "'") | |
row = cur.fetchone() | |
if row != None: | |
session['email'] = email | |
session['password'] = password | |
session['isLoggedin'] = True | |
isfound = True | |
return isfound | |
# Returns a Review based on Id passed in. | |
def getReviewById(reviewId): | |
con = sqlite3.connect('igdbaccounts.db') | |
db = sqlite3.connect(db_locale) | |
cursor = db.cursor() | |
cursor.execute("SELECT * FROM reviews ORDER BY review_id = " + str(reviewId) + "") | |
return cursor.fetchall() | |
db.commit() | |
# Adds a new Review. | |
def addNewReview(username, time, date, rating, game, comment): | |
con = sqlite3.connect('igdbaccounts.db') | |
db = sqlite3.connect(db_locale) | |
cursor = db.cursor() | |
cursor.execute("INSERT INTO reviews (username, time, date, rating, game, comment) VALUES (?, ?, ?, ?, ?, ?)", | |
(username, time, date, rating, game, comment)) | |
db.commit() | |
# Returns all Reviews. | |
def getAllReviews(): | |
con = sqlite3.connect('igdbaccounts.db') | |
db = sqlite3.connect(db_locale) | |
cursor = db.cursor() | |
cursor.execute("SELECT * FROM reviews") | |
return cursor.fetchall() | |
db.commit() | |
# Returns all LookingForGroup Rows | |
def getAllLookingForGroups(): | |
con = sqlite3.connect('igdbaccounts.db') | |
db = sqlite3.connect(db_locale) | |
cursor = db.cursor() | |
cursor.execute("SELECT * FROM lfg") | |
return cursor.fetchall() | |
db.commit() | |
def getLFGById(lfgId): | |
con = sqlite3.connect('igdbaccounts.db') | |
db = sqlite3.connect(db_locale) | |
cursor = db.cursor() | |
cursor.execute("SELECT * FROM lfg ORDER BY lfg_id = " + str(lfgId) + "") | |
return cursor.fetchall() | |
db.commit() | |
# Adds a new Review. | |
def addNewLookingForGroups(username, time, date, players, game, space, communication): | |
con = sqlite3.connect('igdbaccounts.db') | |
db = sqlite3.connect(db_locale) | |
cursor = db.cursor() | |
cursor.execute("INSERT INTO lfg(username, time, date, players, game, space, communication) VALUES (?, ?, ?, ?, ?, ?, ?)", | |
(username, time, date, players, game, space, communication)) | |
db.commit() | |
if __name__ == '__main__': | |
app.run(debug=True) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment