from flask import Flask, request, render_template, redirect, session, flash from connection import MySQLConnector import re app = Flask(__name__) app.secret_key = '\x87\xa0\x99\x0c\xc3LX"\xd1\xb2\xaf\x16\x91L\xad\xfet\xb9\xdd\xb5@\x10\x11\xc3' db = MySQLConnector(app, 'emaildb') EMAIL_REGEX = re.compile(r'^[a-zA-Z0-9\.\+_-]+@[a-zA-Z0-9\._-]+\.[a-zA-Z]+$') queries = { "show_all": "SELECT id, email, DATE_FORMAT(created_at,'%b %D, %Y, %l:%i %p') AS created_at FROM emails", "create": "INSERT INTO emails (email, created_at, updated_at) VALUES (:email, NOW(), NOW())", "delete": "DELETE FROM emails WHERE id = :id" } @app.route('/') def index(): try: session['email'] except: session['email'] = "" return render_template('index.html') @app.route('/emails/create', methods=['POST']) def create(): session['email'] = request.form['email'] if not EMAIL_REGEX.match(session['email']):
from flask import Flask, render_template, request, redirect, session, flash from connection import MySQLConnector from flask_bcrypt import Bcrypt import re EMAIL_REGEX = re.compile(r'^[a-zA-Z0-9.+_-]+@[a-zA-Z0-9._-]+.[a-zA-Z]*$') import datetime app = Flask(__name__) mydb = MySQLConnector(app, "wall") bcrypt = Bcrypt(app) @app.route('/') def index(): session['user'] = "******" return render_template('index.html') @app.route('/login', methods = ['post']) def login(): print request.form query = "SELECT * FROM users where email = :email" data = { 'email':request.form['email'] } user = mydb.query_db(query,data) # [], [{}] if user and bcrypt.check_password_hash(user[0]['password'], request.form['email']): session['user_id'] = user[0]['id'] session['user_name'] = user[0]['first_name'] return redirect('/wall') pass @app.route('/register', methods= ['post'])
# MODULES from flask import Flask, render_template, session, redirect, request, flash from connection import MySQLConnector import re # GLOBAL VARIABLES app = Flask(__name__) app.secret_key = 'ThisIsSecret' db = MySQLConnector(app, 'semirestfuldb') email_regex = re.compile(r'^[\w\.+_-]+@[\w\._-]+\.[\w]*$') # QUERIES queries = { 'display_all': 'SELECT users.id, concat(users.first_name, " ", users.last_name) AS full_name, users.email, users.created_at FROM users', 'select_user_id': 'SELECT users.id, users.first_name, users.last_name, users.email, users.created_at FROM users WHERE id=:id', 'select_user_email': 'SELECT users.id, users.first_name, users.last_name, users.email, users.created_at FROM users WHERE email=:email', 'create': 'INSERT INTO users(first_name, last_name, email, created_at, updated_at) VALUES(:first_name, :last_name, :email, NOW(), NOW())', 'update': 'UPDATE users SET first_name=:first_name, last_name=:last_name, email=:email WHERE id=:id', 'delete': 'DELETE FROM users WHERE id=:id' } # ROUTES # Redirect to /users. @app.route('/') def go_to_index(): return redirect('/users') # Index method (GET) => Shows all users. @app.route('/users', methods=['GET']) def index(): session['all_users'] = db.query_db(queries['display_all'])
from flask import Flask, render_template, request, redirect, flash from connection import MySQLConnector import re # APP, DB, CONSTANTS app = Flask(__name__) mysql = MySQLConnector(app, 'email_validation') EMAIL_REGEX = re.compile(r'^[\w\.+_-]+@[\w\._-]+\.[\w]*$') app.secret_key = 'some_secret' # SQL QUERIES queries = { 'create' : "INSERT INTO emails (email, created_at) VALUES (:email, NOW());", 'index' : "SELECT * FROM emails", 'delete' : "DELETE FROM emails WHERE id = :id" } @app.route('/', methods=["GET", "POST"]) def index(): if request.method == "POST": # Logic that (1) tests whether an email is valid, (2) if so, adds it to DB if(validateEmail(request.form['email'])): query = queries['create'] data = { 'email' : request.form['email'] } mysql.query_db(query, data) flash('Successfully created email record!') return redirect('/success') else: # Email isn't valid, flash an error message flash('Not a valid email!')
from flask import Flask, request, render_template, redirect, session, flash from connection import MySQLConnector import re app = Flask(__name__) app.secret_key = '\x87\xa0\x99\x0c\xc3LX"\xd1\xb2\xaf\x16\x91L\xad\xfet\xb9\xdd\xb5@\x10\x11\xc3' db = MySQLConnector(app, 'friendsdb') EMAIL_REGEX = re.compile(r'^[a-zA-Z0-9\.\+_-]+@[a-zA-Z0-9\._-]+\.[a-zA-Z]+$') queries = { "show_all": "SELECT id, first_name, last_name, email, DATE_FORMAT(created_at, '%Y %b %d, %l:%i %p') AS updated FROM friends", "create": "INSERT INTO friends (first_name, last_name, email, created_at) VALUES (:first, :last, :email, NOW())", "show_one": "SELECT id, first_name, last_name, email FROM friends WHERE id =:id", "update": "UPDATE friends SET first_name=:first_name, last_name=:last_name, email=:email, created_at=NOW() WHERE id=:id", "delete": "DELETE FROM friends WHERE id=:id" } @app.route('/') def index(): friends = db.query_db(queries['show_all']) return render_template('index.html', friends=friends) @app.route('/friends', methods=['POST']) def create(): errors = validate("create", request.form) if not errors:
from flask import Flask, render_template, redirect, request, session, flash from connection import MySQLConnector from flask_bcrypt import Bcrypt import re app = Flask(__name__) app.secret_key = "\xec\x03H\x9fZ\\I^\x98\x9d1\x9d\xf8\xd0\xa9\xb8\xa8iC'K\xa32#" db = MySQLConnector(app, "usersdb") bcrypt = Bcrypt(app) EMAIL_REGEX = re.compile(r'^[a-zA-Z0-9\.\+_-]+@[a-zA-Z0-9\._-]+\.[a-zA-Z]+$') PW_REGEX = re.compile('.*\s') queries = { "select_user" : "SELECT * FROM users WHERE email=:email", "add_user" : "INSERT INTO users (first_name, last_name, email, pw_hash, created_at, updated_at) VALUES (:first, :last, :email, :pw, NOW(), NOW())" } @app.route('/') def index(): if 'user' in session: return redirect('/success') return render_template('index.html') @app.route('/register', methods=['POST']) def register(): user = request.form errors = validate("register", user) if not errors: pw_hash = bcrypt.generate_password_hash(user['pw']) query = queries['add_user'] data = {
from flask import Flask, render_template, request, flash, redirect, session from flask.ext.bcrypt import Bcrypt from connection import MySQLConnector import re # CONSTANTS app = Flask(__name__) EMAIL_REGEX = re.compile(r'^[\w\.+_-]+@[\w\._-]+\.[\w]*$') app.secret_key = 'some_secret' db = MySQLConnector(app, 'login_reg_db') bcrypt = Bcrypt(app) # SQL QUERIES queries = { 'create': 'INSERT INTO users (first_name, last_name, email, pw_hash, created_at, updated_at) VALUES (:first_name, :last_name, :email, :pw_hash, NOW(), NOW())', 'get_by_id': "SELECT first_name, last_name, email FROM users WHERE id = :id", 'get_by_email': "SELECT id, pw_hash FROM users WHERE email = :email" } # ROUTING @app.route('/') def index(): if 'user' not in session: return render_template('index.html') return redirect('/welcome') @app.route('/login', methods=["POST"])
from flask import Flask, render_template, redirect, request, session, flash from connection import MySQLConnector from flask_bcrypt import Bcrypt import re app = Flask(__name__) app.secret_key = "\xec\x03H\x9fZ\\I^\x98\x9d1\x9d\xf8\xd0\xa9\xb8\xa8iC'K\xa32#" db = MySQLConnector(app, "walldb") bcrypt = Bcrypt(app) EMAIL_REGEX = re.compile(r'^[a-zA-Z0-9\.\+_-]+@[a-zA-Z0-9\._-]+\.[a-zA-Z]+$') PW_REGEX = re.compile('.*\s') queries = { "user": { "select": "SELECT * FROM users WHERE email=:email", "create": "INSERT INTO users (first_name, last_name, email, pw_hash, created_at, updated_at) VALUES (:first, :last, :email, :pw, NOW(), NOW())" }, "message": { "select": "SELECT users.id AS user_id, messages.id AS msg_id, first_name, last_name, message, TIMESTAMPDIFF(MINUTE, messages.created_at, NOW()) AS time_offset, TIMESTAMPDIFF(MINUTE, messages.created_at, NOW()) AS offset, DATE_FORMAT(messages.created_at, '%M %D %Y') AS msg_created FROM users JOIN messages ON messages.user_id = users.id ORDER BY messages.created_at DESC", "create": "INSERT INTO messages (user_id, message, created_at, updated_at) VALUES (:user_id, :message, NOW(), NOW())", "select_del": "SELECT * FROM messages WHERE TIMESTAMPDIFF(MINUTE, messages.created_at, NOW()) <= 30 and id =:id", "delete_com": "DELETE FROM comments WHERE message_id = :id", "delete": "DELETE FROM messages WHERE id = :id" }, "comment": { "select":
from flask import Flask, render_template, redirect, request from connection import MySQLConnector app = Flask(__name__) mysql = MySQLConnector(app, 'notesdb') queries = { "show_all_notes": "SELECT * FROM notes ORDER BY created_at DESC", "insert_note": "INSERT INTO notes (title, description, created_at, updated_at) VALUES (:title, '', NOW(), NOW())", "update_note": "UPDATE notes SET title=:title, description=:description, created_at=NOW(), updated_at=NOW() WHERE id=:id", "delete_note": "DELETE FROM notes WHERE id = :id" } @app.route('/') def index(): query = queries['show_all_notes'] notes = mysql.query_db(query) return render_template('index.html', notes=notes) @app.route('/notes/show') def show(): query = queries['show_all_notes'] notes = mysql.query_db(query) return render_template('partials/notes.html', notes=notes) @app.route('/notes/create', methods=['POST']) def create(): print "Title:", request.form['title']
from flask import Flask, render_template, redirect app = Flask(__name__) from connection import MySQLConnector db = MySQLConnector(app, 'world') @app.route('/') def index(): cities = db.query_db('SELECT id, name FROM cities') return render_template('index.html', cities=cities) @app.route('/<id>') def show(id): query = "SELECT name, population FROM cities WHERE id = :id" data = { 'id' : id } city_info = db.query_db(query, data) return render_template('show.html', name=city_info[0]['name'], population=city_info[0]['population']) app.run(debug=True)
from flask import Flask, render_template, redirect, request from connection import MySQLConnector app = Flask(__name__) mysql = MySQLConnector(app, 'postsdb') queries = { "show_all_posts": "SELECT * FROM posts ORDER BY created_at DESC", "insert_post": "INSERT INTO posts (description, created_at, updated_at) VALUES (:description, NOW(), NOW())" } @app.route('/') def index(): query = queries['show_all_posts'] posts = mysql.query_db(query) return render_template('index.html', posts=posts) @app.route('/posts/create', methods=['POST']) def create(): print request.form['post'] query = queries['insert_post'] data = {'description': request.form['post']} mysql.query_db(query, data) query = queries['show_all_posts'] posts = mysql.query_db(query) return render_template('partials/posts.html', posts=posts) if __name__ == '__main__':
from flask import Flask, render_template, request, redirect, session, flash from connection import MySQLConnector app = Flask(__name__) app.secret_key = 'my_secret_key' mydb = MySQLConnector(app, "classespy2") @app.route('/') def index(): query = "SELECT * FROM users" values = {} users = mydb.query_db(query, values) query2 = "SELECT * FROM classes" values2 = {} classes = mydb.query_db(query2, values2) print(users) print(classes) return render_template('index.html', classes=classes, users=users) @app.route('/users', methods=['POST']) def createusers(): print(request.form) query = "Insert into users (first_name, last_name, created_at, updated_at) values (:first_name, :last_name, NOW(), NOW())" values = { "first_name": request.form['first_name'], "last_name": request.form['last_name'], } mydb.query_db(query, values) return redirect('/')