init.py - from flask import Flask render_template request...

This preview shows page 1 - 3 out of 8 pages.

from flask import Flask, render_template, request, session, url_for, redirect import pymysql.cursors import os import time import hashlib import datetime app = Flask(__name__) SALT = 'cs3083' app.config["IMAGES_DIR"] = "static" #Configure MySQL conn = pymysql.connect(host='localhost', port = 3306, user='root', password='root', db='finstagram', charset='utf8', cursorclass=pymysql.cursors.DictCursor) #Define a route to hello function @app.route('/') def index(): return render_template('index.html') #Define route for login @app.route('/login') def login(): return render_template('login.html') #Define route for register @app.route('/register') def register(): return render_template('register.html') #Authenticates the login @app.route('/loginAuth', methods=['GET', 'POST']) def loginAuth(): #grabs information from the forms username = request.form['username'] password = request.form['password']+SALT hashed = hashlib.sha256(password.encode('utf-8')).hexdigest() #cursor used to send queries cursor = conn.cursor() #executes query query = 'SELECT * FROM person WHERE username = %s and password = %s' cursor.execute(query, (username, hashed)) #stores the results in a variable data = cursor.fetchone() #use fetchall() if you are expecting more than 1 data row cursor.close() error = None if(data): #creates a session for the the user #session is a built in session['username'] = username return redirect(url_for('home')) else: #returns an error message to the html page error = 'Invalid login or username' return render_template('login.html', error=error)
Image of page 1
#Authenticates the register @app.route('/registerAuth', methods=['GET', 'POST']) def registerAuth(): #grabs information from the forms username = request.form['username'] password = request.form['password']+SALT lastName = request.form['lastName'] firstName = request.form['firstName'] email = request.form['email'] hashed = hashlib.sha256(password.encode('utf-8')).hexdigest() #cursor used to send queries cursor = conn.cursor() #executes query query = 'SELECT * FROM person WHERE username = %s' cursor.execute(query, (username)) #stores the results in a variable data = cursor.fetchone() #use fetchall() if you are expecting more than 1 data row error = None if(data): #If the previous query returns data, then user exists error = "This user already exists" return render_template('register.html', error = error) else: ins = 'INSERT INTO person VALUES(%s, %s, %s, %s, %s)' cursor.execute(ins, (username, hashed, firstName, lastName, email)) conn.commit() cursor.close() return render_template('index.html') @app.route('/home') def home(): user = session['username'] cursor = conn.cursor(); query = ''' (SELECT pID, filePath, postingDate FROM SharedWith NATURAL JOIN BelongTo NATURAL JOIN Photo WHERE username= %s) UNION (SELECT pID, filePath, postingDate FROM SharedWith NATURAL JOIN BelongTo NATURAL JOIN Photo WHERE username= %s) UNION (SELECT pID, filePath, postingDate FROM photo JOIN follow ON photo.poster = follow.followee WHERE follow.follower = %s AND follow.followStatus = %s AND photo.allFollowers = %s) ORDER BY postingDate DESC''' cursor.execute(query, (user, user, user, 1, 1)) data = cursor.fetchall() cursor.close() return render_template('home.html', username=user, posts=data) @app.route("/upload_image", methods=["GET"]) def upload_image(): user = session['username'] cursor = conn.cursor()
Image of page 2
Image of page 3

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture