<filename>app.py
from flask import Flask, render_template, flash, redirect, request, url_for, session, logging
from data import Articles
from flask_mysqldb import MySQL
from wtforms import Form, StringField, TextAreaField, PasswordField, validators
from passlib.hash import sha256_crypt

app = Flask(__name__)
app.debug = True

app.config['MYSQL_HOST'] = 'database.mysql'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = 'root'
app.config['MYSQL_DB'] = 'flask_blog'
app.config['MYSQL_CURSORCLASS'] = 'DictCursor'

mysql = MySQL(app)

articlesData = Articles()

@app.route('/')
def index():
    return render_template('home.html')

@app.route('/about')
def about():
    return render_template('about.html')

@app.route('/articles')
def articles():
    return render_template('articles.html', articles = articlesData)

@app.route('/article/<string:id>/')
def article(id):
    return render_template('article.html', id = id)

class RegisterForm(Form):
    name = StringField('Name', validators=[validators.Length(min=1, max=256), validators.input_required()])
    username  = StringField('Username', validators=[validators.Length(min=4, max=256)])
    email  = StringField('E-mail', validators=[validators.Length(min=6, max=256)])
    confirm = PasswordField('Confirm Password')
    password  = PasswordField('Password', validators=[
        validators.DataRequired(),
        validators.EqualTo('confirm', message='Passwords do not match')
    ])

@app.route('/register', methods=['GET', 'POST'])
def register():
    form = RegisterForm(request.form)

    if (request.method == 'POST' and form.validate):
        name = form.name.data
        email = form.email.data
        username = form.username.data
        password = <PASSWORD>5<PASSWORD>(str(form.password.data))

        cur = mysql.connection.cursor()

        cur.execute("INSERT INTO users(`name`, `email`, `username`, `password`) VALUES (%s, %s, %s, %s)", (name, email, username, password))

        mysql.connection.commit()

        cur.close()

        flash('You are now registered can log in', 'success')

        return redirect(url_for('index'))
    
    return render_template('register.html', form=form)

@app.route('/login', methods=['GET', 'POST'])
def login():
    if (request.method == 'POST'):
        username = request.form['username']
        password_candidate = request.form['password']

        cur = mysql.connection.cursor()

        result = cur.execute("SELECT * FROM `users` WHERE `username` = %s", [username])

        if result > 0:
            data = cur.fetchone()
            password = data['password']

            if sha256_crypt.verify(password_candidate, password):
                app.logger.info('PASSWORD MATCHED')
            else:
                app.logger.info('PASSWORD NOT MATCHED')
        else:
            render_template('login.html')

    return render_template('login.html')

if __name__ == '__main__':
    app.secret_key = 'AC643BA6F0DEFEC18F96C9DF272E50A9441CC1E5D7D91'
    app.run()

<filename>app.py
from flask import Flask, render_template, flash, redirect, request, url_for, session, logging
from data import Articles
from flask_mysqldb import MySQL
from wtforms import Form, StringField, TextAreaField, PasswordField, validators
from passlib.hash import sha256_crypt

app = Flask(__name__)
app.debug = True

app.config['MYSQL_HOST'] = 'database.mysql'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = 'root'
app.config['MYSQL_DB'] = 'flask_blog'
app.config['MYSQL_CURSORCLASS'] = 'DictCursor'

mysql = MySQL(app)

articlesData = Articles()

@app.route('/')
def index():
    return render_template('home.html')

@app.route('/about')
def about():
    return render_template('about.html')

@app.route('/articles')
def articles():
    return render_template('articles.html', articles = articlesData)

@app.route('/article/<string:id>/')
def article(id):
    return render_template('article.html', id = id)

class RegisterForm(Form):
    name = StringField('Name', validators=[validators.Length(min=1, max=256), validators.input_required()])
    username  = StringField('Username', validators=[validators.Length(min=4, max=256)])
    email  = StringField('E-mail', validators=[validators.Length(min=6, max=256)])
    confirm = PasswordField('Confirm Password')
    password  = PasswordField('Password', validators=[
        validators.DataRequired(),
        validators.EqualTo('confirm', message='Passwords do not match')
    ])

@app.route('/register', methods=['GET', 'POST'])
def register():
    form = RegisterForm(request.form)

    if (request.method == 'POST' and form.validate):
        name = form.name.data
        email = form.email.data
        username = form.username.data
        password = <PASSWORD>5<PASSWORD>(str(form.password.data))

        cur = mysql.connection.cursor()

        cur.execute("INSERT INTO users(`name`, `email`, `username`, `password`) VALUES (%s, %s, %s, %s)", (name, email, username, password))

        mysql.connection.commit()

        cur.close()

        flash('You are now registered can log in', 'success')

        return redirect(url_for('index'))
    
    return render_template('register.html', form=form)

@app.route('/login', methods=['GET', 'POST'])
def login():
    if (request.method == 'POST'):
        username = request.form['username']
        password_candidate = request.form['password']

        cur = mysql.connection.cursor()

        result = cur.execute("SELECT * FROM `users` WHERE `username` = %s", [username])

        if result > 0:
            data = cur.fetchone()
            password = data['password']

            if sha256_crypt.verify(password_candidate, password):
                app.logger.info('PASSWORD MATCHED')
            else:
                app.logger.info('PASSWORD NOT MATCHED')
        else:
            render_template('login.html')

    return render_template('login.html')

if __name__ == '__main__':
    app.secret_key = 'AC643BA6F0DEFEC18F96C9DF272E50A9441CC1E5D7D91'
    app.run()
