SHARE
TWEET

Untitled

a guest Nov 14th, 2017 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from flask import Flask, render_template, redirect, url_for, flash, session
  2. from flask_wtf import Form
  3. from wtforms import StringField, PasswordField, BooleanField, TextField, TextAreaField, SubmitField, validators, ValidationError
  4. from wtforms.validators import InputRequired, Email, Length, EqualTo
  5. from flask_sqlalchemy  import SQLAlchemy
  6. from werkzeug.security import generate_password_hash, check_password_hash
  7. from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user
  8. from flask.ext.mail import Message, Mail
  9.  
  10.  
  11. app = Flask(__name__)
  12.  
  13. app.config['SECRET_KEY'] = 'Thisissupposedtobesecret!'
  14. app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////Users/Joanna/Desktop/coursework2/sourcecode/database.db'
  15. db = SQLAlchemy(app)
  16.  
  17.  
  18. class LoginForm(Form):
  19.     username = StringField('username', validators=[InputRequired(), Length(min=4, max=15)])
  20.     password = PasswordField('password', validators=[InputRequired(), Length(min=8, max=80)])
  21.     remember = BooleanField('remember me')
  22.  
  23. class RegisterForm(Form):
  24.     email = StringField('email', validators=[InputRequired(), Email(message='Invalid email'), Length(min=6, max=40)])
  25.     username = StringField('username', validators=[InputRequired(), Length(min=4, max=15)])
  26.     password = PasswordField('password', validators=[InputRequired(), Length(min=4, max=40)])
  27.     confirm = PasswordField('Repeat Password', validators=[InputRequired(), EqualTo('password')])
  28.  
  29. class User(db.Model):
  30.  
  31.     __tablename__ = 'user';
  32.  
  33.         id = db.Column(db.Integer, primary_key=True)
  34.         username = db.Column(db.String(15), unique=True)
  35.         email = db.Column(db.String(40), unique=True)
  36.         password = db.Column(db.String(40))
  37.  
  38.         def __init__(self, username, email, password):
  39.             self.username = username
  40.             self.email = email
  41.             self.password = password
  42.  
  43.         def __repr__(self):
  44.             return '<User {0}>'.format(self.name)
  45.  
  46. @app.route('/')
  47. def index():
  48.     return render_template('index.html')
  49.  
  50. @app.route('/signup/', methods=['GET', 'POST'])
  51. def signup():
  52.     form = RegisterForm(request.form)
  53.     if request.method == 'POST':
  54.         if form.validate_on_submit():
  55.             try:
  56.                 new_user = User(form.username.data, form.email.data, form.password.data)
  57.                 new_user.authenticated = True
  58.                 db.session.add(new_user)
  59.                 db.session.commit()
  60.                 flash('Thanks for registering!', 'success')
  61.                 return redirect(url_for('index'))
  62.             except IntegrityError:
  63.                 db.session.rollback()
  64.                 flash('ERROR! Email({}) already exists.'.format(form.email.data), 'error')
  65.         return render_template('signup.html', form=form)
  66.  
  67. @app.route('/login/', methods=['GET', 'POST'])
  68. def login():
  69.     return render_template('login.html')
  70.  
  71. @app.route('/logout/')
  72. @login_required
  73. def logout():
  74.     logout_user()
  75.     return redirect(url_for('index'))
  76.  
  77. if __name__ == '__main__':
  78.     app.run('0.0.0.0', debug=True)
RAW Paste Data
Top