Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Flask, render_template, flash, redirect, url_for, session, logging, request
- from data import articles #import the function
- from flask_mysqldb import MySQL
- from wtforms import Form, StringField, TextAreaField, PasswordField, validators
- from passlib.hash import sha256_crypt
- from flask_mail import Mail, Message
- app = Flask(__name__) # object app of class Flask, pass __name__ argument to __init__
- app.config.update(dict(
- DEBUG = True,
- MAIL_SERVER = 'smtp.gmail.com',
- MAIL_PORT = 587,
- MAIL_USE_TLS = True,
- MAIL_USE_SSL = False,
- MAIL_USERNAME = 'limory37@gmail.com',
- MAIL_PASSWORD = 'shit7265',
- ))
- mail = Mail(app)
- mysql = MySQL(app)
- MySqlSettings = {"MYSQL_HOST":"localhost","MYSQL_USER":"root","MYSQL_PASSWORD":"eaea7265","MYSQL_DB":"myflaskapp","MYSQL_CURSORCLASS":"DictCursor"}
- for k,v in MySqlSettings.items(): # k=key, v=value
- app.config[k]=v
- Articles = articles()
- @app.route('/') # what URL should activate the function below
- def index():
- return render_template('index.html')
- @app.route('/about')
- def about():
- return render_template('about.html')
- @app.route('/articles')
- def articles():
- return render_template('articles.html', articles = Articles)
- @app.route('/articles/<string:id>/')
- def article(id):
- return render_template('article.html', id=id, articles=Articles)
- class RegisterForm(Form): # subclass of Form
- name = StringField('Name', [validators.Length(min=1, max=50)]) #pass args to __init__ of StringField class
- username = StringField('Username', [validators.Length(min=4, max=25)])
- email = StringField('Email', [validators.Length(min=6, max=50)])
- password = PasswordField('Password', [
- validators.Length(min=6, max=25),
- validators.EqualTo('confirm', message='Passwords do not match')
- ])
- confirm = PasswordField('Confirm Password')
- @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 = sha256_crypt.encrypt(str(form.password.data))
- cur = mysql.connection.cursor()
- data = cur.execute("SELECT * FROM users WHERE username = %s", [form.username.data])
- if data is 0:
- cur.execute("INSERT INTO users(name, email, username, password) VALUES(%s, %s, %s, %s)", (name, email, username, password))
- mysql.connection.commit()
- flash('You are now registered and can log in', 'success')
- return redirect(url_for('login'))
- else:
- flash('Username already exists','danger')
- cur.close()
- return render_template('register.html', form=form)
- @app.route('/login', methods=['POST','GET'])
- 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()
- app.logger.info(data['email'])
- password = data['password']
- if sha256_crypt.verify(password_candidate, password):
- app.logger.info('password matched homo')
- else:
- app.logger.info('password did not match still homo')
- else:
- app.logger.info('no user')
- return render_template('login.html')
- if __name__ == '__main__':
- app.secret_key = 'secret123'
- app.run(debug=True) # run() method of Flask class, passing debug argument with value True
- #we can also do app.debug = True, to set the debug property of the app object to True.
Add Comment
Please, Sign In to add comment