Advertisement
Guest User

Untitled

a guest
Jul 1st, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. from flask import Flask,render_template,request,url_for,flash,session
  2. from flask_session import Session
  3. from dbconnect import connection
  4. from wtforms importForm,BooleanField,StringField,TextField,PasswordField,validators,IntegerField
  5. from passlib.hash import sha256_crypt
  6. from MySQLdb import escape_string as thwart
  7. from flask_wtf import Form
  8. from wtforms.validators import InputRequired
  9. import gc
  10.  
  11. sess=Session()
  12. SESSION_TYPE = 'memcache'
  13. app=Flask(__name__)
  14.  
  15. @app.route('/')
  16. def test():
  17. return render_template("home.html")
  18.  
  19.  
  20. @app.route('/about/')
  21. def about():
  22. return render_template("about.html")
  23.  
  24. @app.route('/dashbord/')
  25. def dashbord():
  26. return('hello')
  27.  
  28.  
  29. @app.route('/contact/')
  30. def contact():
  31. return render_template("contact.html")
  32.  
  33. @app.route('/login/',methods=['GET','POST'])
  34. def login():
  35. return render_template("login.html")
  36.  
  37.  
  38. class RegistrationForm(Form):
  39. username=TextField('username',[validators.Length(min=4,max=20),validators.Required()])
  40. email=TextField('email',[validators.Length(min=6,max=50),validators.Required()])
  41. password=PasswordField('password',[validators.EqualTo('confirm',message="Password must match"),validators.Required()])
  42. confirm=PasswordField("repeat password")
  43. phone_no=IntegerField('phone_no',[validators.Required()])
  44.  
  45.  
  46. @app.route("/sign_up",methods=['GET','POST'])
  47. def sign():
  48. try:
  49. form=RegistrationForm(request.form)
  50. if request.method == 'POST':
  51. username=form.username.data
  52. email=form.email.data
  53. password=sha256_crypt.encrypt((str(form.password.data)))
  54. phone_no=form.phone_no.data
  55. c,conn =connection()
  56.  
  57. x = c.execute("SELECT * FROM customer WHERE username =(%s)",
  58. (username,))
  59.  
  60. if int(x)>0:
  61. flash("That username is taken")
  62. return render_template('sign.html',form=form)
  63. else:
  64. args="INSERT INTO customer (username,email,password,phone_no) VALUES (%s,%s,%s,%s)",
  65. (thwart(username),thwart(email),thwart(password),thwart(phone_no))
  66. c.execute(*args)
  67.  
  68. conn.commit()
  69. flash("Thanks for registering")
  70. c.close()
  71. conn.close()
  72.  
  73. gc.collect()
  74.  
  75. session['logged_in']=True
  76. session['username']=username
  77. return redirect(url_for('dashbord'))
  78. return render_template("sign.html",form=form)
  79.  
  80. except Exception as e:
  81. return(str(e))
  82.  
  83.  
  84.  
  85. if __name__=="__main__":
  86. app.secret_key = 'super secret key'
  87. app.config['SESSION_TYPE'] = 'filesystem'
  88. sess.init_app(app)
  89. app.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement