def success(): if 'current_user_id' not in session: return redirect('/') else: mysql = connectToMySQL('private_wall') query = "SELECT * FROM accounts WHERE id=%(id)s;" data = {"id": session['current_user_id']} welcome = mysql.query_db(query, data) mysql = connectToMySQL('private_wall') all_users = mysql.query_db("SELECT * FROM accounts;") mysql = connectToMySQL('private_wall') sent_messages = mysql.query_db( "SELECT messages.id, accounts.first_name, accounts.last_name, messages.content, messages.created_at FROM accounts JOIN messages ON accounts.id = messages.recipient_id WHERE messages.sender_id = %(id)s ORDER BY messages.created_at DESC;", {"id": session['current_user_id']}) mysql = connectToMySQL('private_wall') received_messages = mysql.query_db( "SELECT messages.id, accounts.first_name, accounts.last_name, messages.content, messages.created_at FROM accounts JOIN messages ON accounts.id = messages.sender_id WHERE messages.recipient_id = %(id)s ORDER BY messages.created_at DESC;", {"id": session['current_user_id']}) return render_template('wall.html', name=welcome, list=all_users, sent_messages=sent_messages, received_messages=received_messages)
def success(): mysql = connectToMySQL('email_val') query = "SELECT * FROM emails;" select_all = mysql.query_db(query) return render_template('success.html', emails=select_all)
def process(): mysql = connectToMySQL('email_val') if not EMAIL_REGEX.match(request.form['email']): flash("Invalid Email format") if '_flashes' in session.keys(): return redirect('/') else: mysql = connectToMySQL('email_val') query = "INSERT INTO emails (email, created_at, updated_at) VALUES (%(email)s, NOW(), NOW());" data = {"email": request.form['email']} new_email = mysql.query_db(query, data) session['id'] = new_email return redirect('/success')
def single_user(id): mysql = connectToMySQL('users_assignment') query = "SELECT * FROM users WHERE id=%(id)s;" data = {"id": id} this_user = mysql.query_db(query, data) return render_template('user.html', person=this_user)
def edit_user(id): mysql = connectToMySQL('users_assignment') query = "SELECT * FROM users WHERE id=%(id)s;" data = {"id": id} edit_user = mysql.query_db(query, data) return render_template('edit.html', edit=edit_user)
def delete_user(id): mysql = connectToMySQL('users_assignment') query = "DELETE FROM users WHERE id=%(id)s;" data = {"id": id} delete_user = mysql.query_db(query, data) return redirect('/')
def delete_message(id): mysql = connectToMySQL('private_wall') query = "DELETE FROM messages WHERE id=%(message_id)s;" data = {"message_id": id} message_id_delete = mysql.query_db(query, data) return redirect('/success')
def add_user(): mysql = connectToMySQL('users_assignment') query = "INSERT INTO users (first_name, last_name, email, created_at, updated_at) VALUES (%(fn)s, %(ln)s, %(email)s, NOW(), NOW());" data = { "fn": request.form['first'], "ln": request.form['last'], "email": request.form['email'] } new_user_id = mysql.query_db(query, data) return redirect('/')
def process_edit(id): mysql = connectToMySQL('users_assignment') query = "UPDATE users SET first_name=%(fn)s, last_name=%(ln)s, email=%(em)s, updated_at=NOW() WHERE id=%(id)s;" data = { "id": id, "fn": request.form['editfirst'], "ln": request.form['editlast'], "em": request.form['editemail'] } process_edit = mysql.query_db(query, data) return redirect('/')
def login(): mysql = connectToMySQL('private_wall') query = "SELECT id, email, pw_hash FROM accounts WHERE email=%(email)s;" data = {"email": request.form['login_email']} result = mysql.query_db(query, data) if result: if bcrypt.check_password_hash(result[0]['pw_hash'], request.form['login_pass']): session['current_user_id'] = result[0]['id'] return redirect('/success') flash("Your information is incorrect.") return redirect('/')
def send(): if len(request.form['message']) < 2: flash("Your message is too short.") if request.form['recipient'] == 'default': flash("Please choose a recipient.") if '_flashes' in session.keys(): return redirect('/success') else: mysql = connectToMySQL('private_wall') query = "INSERT INTO messages (sender_id, recipient_id, content, created_at, updated_at) VALUES (%(sender)s, %(recipient)s, %(content)s, NOW(), NOW());" data = { "sender": session['current_user_id'], "recipient": request.form['recipient'], "content": request.form['message'] } new_message = mysql.query_db(query, data) return redirect('/success')
def index(): mysql = connectToMySQL('users_assignment') users = mysql.query_db('SELECT * FROM users;') return render_template('users.html', users=users)
def add(): mysql = connectToMySQL('users_assignment') return render_template('add_user.html')
def register(): mysql = connectToMySQL('private_wall') if request.form['first_name'] == "": flash("First name is required.") elif len(request.form['first_name']) < 2: flash("Name must be at least 2 characters.") if request.form['last_name'] == "": flash("Last name is required.") elif len(request.form['last_name']) < 2: flash("Name must be at least 2 characters.") if request.form['email'] == "": flash("Email is required.") elif not EMAIL_REGEX.match(request.form['email']): flash("Invalid email format") other_emails = mysql.query_db( "SELECT email FROM accounts WHERE email=%(email)s;", {"email": request.form['email']}) if other_emails: flash("This email has already been used to create an account.") if request.form['password'] == "": flash("Password is required.") elif len(request.form['password']) < 8: flash("Password must be at least 8 characters.") if request.form['confirm_pass'] == "": flash("Password confirmation is required.") elif request.form['confirm_pass'] != request.form['password']: flash("You must enter the same password in both fields.") if '_flashes' in session.keys(): return redirect('/') else: mysql = connectToMySQL('private_wall') pw_hash = bcrypt.generate_password_hash(request.form['password']) print(pw_hash) query = "INSERT INTO accounts (first_name, last_name, email, pw_hash, updated_at, created_at) VALUES (%(fn)s, %(ln)s, %(em)s, %(pwh)s, NOW(), NOW());" data = { "fn": request.form['first_name'], "ln": request.form['last_name'], "em": request.form['email'], "pwh": pw_hash } new_user = mysql.query_db(query, data) if new_user: session['current_user_id'] = new_user else: return redirect('/') return redirect('/success')