Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Flask,render_template,request,url_for,flash,session
- from flask_session import Session
- from dbconnect import connection
- from wtforms importForm,BooleanField,StringField,TextField,PasswordField,validators,IntegerField
- from passlib.hash import sha256_crypt
- from MySQLdb import escape_string as thwart
- from flask_wtf import Form
- from wtforms.validators import InputRequired
- import gc
- sess=Session()
- SESSION_TYPE = 'memcache'
- app=Flask(__name__)
- @app.route('/')
- def test():
- return render_template("home.html")
- @app.route('/about/')
- def about():
- return render_template("about.html")
- @app.route('/dashbord/')
- def dashbord():
- return('hello')
- @app.route('/contact/')
- def contact():
- return render_template("contact.html")
- @app.route('/login/',methods=['GET','POST'])
- def login():
- return render_template("login.html")
- class RegistrationForm(Form):
- username=TextField('username',[validators.Length(min=4,max=20),validators.Required()])
- email=TextField('email',[validators.Length(min=6,max=50),validators.Required()])
- password=PasswordField('password',[validators.EqualTo('confirm',message="Password must match"),validators.Required()])
- confirm=PasswordField("repeat password")
- phone_no=IntegerField('phone_no',[validators.Required()])
- @app.route("/sign_up",methods=['GET','POST'])
- def sign():
- try:
- form=RegistrationForm(request.form)
- if request.method == 'POST':
- username=form.username.data
- email=form.email.data
- password=sha256_crypt.encrypt((str(form.password.data)))
- phone_no=form.phone_no.data
- c,conn =connection()
- x = c.execute("SELECT * FROM customer WHERE username =(%s)",
- (username,))
- if int(x)>0:
- flash("That username is taken")
- return render_template('sign.html',form=form)
- else:
- args="INSERT INTO customer (username,email,password,phone_no) VALUES (%s,%s,%s,%s)",
- (thwart(username),thwart(email),thwart(password),thwart(phone_no))
- c.execute(*args)
- conn.commit()
- flash("Thanks for registering")
- c.close()
- conn.close()
- gc.collect()
- session['logged_in']=True
- session['username']=username
- return redirect(url_for('dashbord'))
- return render_template("sign.html",form=form)
- except Exception as e:
- return(str(e))
- if __name__=="__main__":
- app.secret_key = 'super secret key'
- app.config['SESSION_TYPE'] = 'filesystem'
- sess.init_app(app)
- app.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement