def checkEmailInDB(email): mysql = connectToMySQL("loginandregistration") query = "SELECT * FROM loginandregistrations where email = %(email)s;" data = {"email": email} query_check = "SELECT COUNT(*) as doesExist FROM loginandregistrations where email = %(email)s;" if mysql.query_db(query_check, data)[0]['doesExist'] == 0: result = False else: mysql = connectToMySQL("loginandregistration") result = mysql.query_db(query, data) return result
def delete_msg(): mysql = connectToMySQL('loginandregistration') query = "SELECT sent_to_user_id from messages where id = %(message_id)s;" data = {"message_id": request.form['message_id']} # print(mysql.query_db(query,data)) # print("*!*!*!**!*!") if mysql.query_db(query, data)[0]['sent_to_user_id'] != str( session['user_id']): return redirect('/terribleperson') else: mysql = connectToMySQL('loginandregistration') queryDelete = "DELETE from messages where id = %(message_id)s;" data = {"message_id": request.form['message_id']} mysql.query_db(queryDelete, data) return redirect('/wall')
def add_pet(): mysql = connectToMySQL('pets') query = "INSERT INTO pets (name, type, created_at, updated_at) VALUES (%(name)s, %(type)s, NOW(), NOW());" data = {"name": request.form["name"], "type": request.form["type"]} id = mysql.query_db(query, data) return redirect("/")
def register(): current_form = request.form isTrue = True if validateEmail(request.form["email"]) == False: isTrue = False if validateFirstName(request.form["first_name"]) == False: isTrue = False if validateLastName(request.form["last_name"]) == False: isTrue = False if validatePassword(request.form["password"]) == False: isTrue = False if validateConfirmPW(request.form["password"], request.form["pwconfirm"]) == False: isTrue = False if isTrue == False: flash(request.form["first_name"], "holdFName") flash(request.form["last_name"], "holdLName") flash(request.form["email"], "holdEmail") return redirect('/') elif isTrue == True: mysql = connectToMySQL('loginandregistration') pw_hash = bcrypt.generate_password_hash(request.form['password']) query = "INSERT INTO loginandregistrations (FirstName, LastName, Email, Password, created_at, updated_at) VALUES (%(firstname)s, %(lastname)s, %(email)s, %(password)s, NOW(), NOW());" data = { "firstname": request.form["first_name"], "lastname": request.form["last_name"], "email": request.form["email"], "password": pw_hash } id = mysql.query_db(query, data) session["isLoggedIn"] = True session["user_id"] = id session["email_address"] = request.form["email"] return redirect("/wall")
def index(): mysql = connectToMySQL( 'pets') # call the function, passing in the name of our db pets = mysql.query_db( 'SELECT * FROM pets;' ) # call the query_db function, pass in the query as a string print(pets) return render_template("index.html", all_pets=pets)
def email(): found = False mysql = connectToMySQL('loginandregistration') # connect to the database query = "SELECT email from loginandregistrations WHERE loginandregistrations.email = %(email)s;" data = {'email': request.form['email']} result = mysql.query_db(query, data) if result: found = True return render_template('partials/email.html', found=found) # render a partial and return it
def add_user(): mysql = connectToMySQL('users') query = "INSERT INTO users (FirstName, LastName, Email, description, created_at, updated_at) VALUES (%(firstname)s, %(lastname)s, %(email)s, %(description)s, NOW(), NOW());" data = { "firstname": request.form["firstname"], "lastname": request.form["lastname"], "email": request.form["email"], "description": request.form["description"] } id = mysql.query_db(query, data) return redirect("/users/" + str(id))
def update_user(): mysql = connectToMySQL('users') query = "UPDATE users set FirstName = %(firstname)s, LastName = %(lastname)s, Email = %(email)s, description = %(description)s, updated_at = NOW() where id = %(id)s;" data = { "firstname": request.form["firstname"], "lastname": request.form["lastname"], "email": request.form["email"], "description": request.form["description"], "id": request.form["id"] } mysql.query_db(query, data) return redirect("/users/" + str(request.form['id']))
def validateEmail(emailaddress): EMAIL_REGEX = re.compile(r'^[a-zA-Z0-9.+_-]+@[a-zA-Z0-9._-]+\.[a-zA-Z]+$') if not EMAIL_REGEX.match(emailaddress): flash("Invalid Email Address!", "email") return False else: mysql = connectToMySQL("loginandregistration") query = "SELECT COUNT(*) as doesExist FROM loginandregistrations where email = %(email)s;" data = {"email": emailaddress} if (mysql.query_db(query, data)[0]['doesExist'] == 0): return True else: flash("Email already in system", "email") return False
def success(): if session["isLoggedIn"] == False: return redirect("/") else: user = checkEmailInDB(session['email_address']) mysql = connectToMySQL('loginandregistration') query = "SELECT concat(Sender.FirstName, ' ', Sender.LastName) as who_sent_me, messages.id as message_id, messages.sent_to_user_id AS sent_to_user_id, messages.sending_user_id AS sending_user_id, messages.message as message, messages.sent_at AS timestamp, timediff(now(), messages.sent_at) AS timepassed from messages join loginandregistrations as Sender on messages.sending_user_id = Sender.id join loginandregistrations as SentTo on messages.sent_to_user_id = SentTo.id where sent_to_user_id = %(user_id)s;" data = {"user_id": user[0]['id']} received_messages = mysql.query_db(query, data) if received_messages == False: received_messages = [] all_users = allUsersReturn() return render_template("success.html", all_users=all_users, user=user, received_messages=received_messages)
def send_msg(): if len(request.form['message_text']) < 5: flash('Your message must be at least 5 characters long', 'message_send') return redirect('/wall') mysql = connectToMySQL('loginandregistration') query = 'INSERT INTO messages (sending_user_id, sent_to_user_id, message, sent_at) VALUES ( %(sender)s, %(receiver)s, %(message)s, NOW() )' data = { 'sender': session['user_id'], 'receiver': request.form['message_to'], 'message': request.form['message_text'] } print(session['user_id']) print(request.form['message_to']) print(request.form['message_text']) mysql.query_db(query, data) return redirect('/wall')
from flask import Flask, render_template, request, redirect from flask_socketio import SocketIO, join_room, leave_room from mysql import connectToMySQL import random, string app = Flask(__name__) mysql = connectToMySQL("dojopolls") app.config["SECRET_KEY"] = "1234567890qwertyuiop" socketio = SocketIO(app) @app.route('/') def index(): return render_template("index.html") @app.route('/start', methods=["POST", "GET"]) def start_room(): if request.method == "GET": return render_template("create/start.html") else: # generate a random room code room_code = ''.join( random.choices(string.ascii_uppercase + string.digits, k=5)) print(room_code) # save room in db room_created = mysql.query_db( f"INSERT INTO polls (title, admin, password, poll_id) VALUES ('{request.form['title']}','{request.form['name']}', '{request.form['password']}', '{room_code}')" )
def allUsersReturn(): mysql = connectToMySQL("loginandregistration") query = "SELECT * FROM loginandregistrations ;" all_users = mysql.query_db(query) return all_users
def index(): mysql = connectToMySQL('users') users = mysql.query_db('SELECT * FROM users;') return render_template("index.html", all_users=users)
def show_user(id): mysql = connectToMySQL('users') query = 'SELECT * FROM users where id = %(id)s;' data = {"id": id} user = mysql.query_db(query, data) return render_template("show_user.html", user=user, id=id)
def delete_user(id): mysql = connectToMySQL('users') query = "DELETE from users where id = %(id)s;" data = {"id": id} mysql.query_db(query, data) return redirect("/users")