-
Notifications
You must be signed in to change notification settings - Fork 1
/
flaskblock.py
125 lines (103 loc) · 4.58 KB
/
flaskblock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
from flask import Flask,render_template,request,url_for,redirect,flash
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
from flask_login import LoginManager,UserMixin,login_user,current_user,logout_user
import commands
from flask_wtf import FlaskForm
from wtforms import StringField,PasswordField,SubmitField
#,BooleanField
from wtforms.validators import DataRequired, Length, Email, EqualTo,ValidationError
app=Flask(__name__)
app.config['SECRET_KEY']='91da80d1e75c6638528ddb0bfe6c0596'
app.config['SQLALCHEMY_DATABASE_URI']='sqlite:///site.db'
db=SQLAlchemy(app)
bcrypt=Bcrypt(app)
login_manager=LoginManager(app)
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
class User(db.Model,UserMixin):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
password = db.Column(db.String(60), nullable=False)
def __repr__(self):
return "User({0},{1},{2})".format(self.username,self.email,self.password)
class RegistrationForm(FlaskForm):
username=StringField('Username',
validators=[DataRequired(),Length(min=2,max=20)])
email=StringField('Email',
validators=[DataRequired(),Email()])
password=PasswordField('Password',validators=[DataRequired()])
confirm_password=PasswordField('Confirm_Password',
validators=[DataRequired(),EqualTo('password')])
submit=SubmitField('SignUp')
def validate_username(self,username):
if User.query.filter_by(username=username.data).first():
raise ValidationError('That username is taken.Please choose a different one.')
def validate_email(self,email):
if User.query.filter_by(email=email.data).first():
raise ValidationError('That email is taken.Please choose a different account one.')
class LoginForm(FlaskForm):
email=StringField('Email',
validators=[DataRequired(),Email()])
password=PasswordField('Password',validators=[DataRequired()])
#remember=BooleanField('Remember Me',True)
submit=SubmitField('Login')
@app.route("/",methods=["POST","GET"])
@app.route("/register",methods=["POST","GET"])
def register():
# if current_user.is_authenticated:
# return redirect(url_for('home'))
form=RegistrationForm()
if request.method == 'POST':
username = request.form.get('username')
password = request.form.get('password')
email=request.form.get('email')
user = User(username = username, password = password, email=email)
if form.validate_on_submit():
hashed_password=bcrypt.generate_password_hash(form.password.data).decode('utf-8')
user=User(username=form.username.data,email=form.email.data,password=hashed_password)
db.session.add(user)
db.session.commit()
flash("New Account created for {}.You are now able to log in ".format(form.username.data),'success')
return redirect(url_for('login'))
return render_template('register.html',title='Register',form=form)
@app.route("/home",methods=["POST","GET"])
def home():
return render_template('home.html')
@app.route("/output",methods=["POST","GET"])
def output():
return render_template('output.html')
@app.route("/copy",methods = ["POST", "GET"])
def copy():
code = request.form.get("code")
arg =request.form.get("sys arg")
file = open("file.py","w+")
file.write(code)
file.close()
s=commands.getoutput("python file.py "+str(arg))
l=s.split('\n')
return render_template('output.html',title="output",display=l)
@app.route("/login",methods=['GET','POST'])
def login():
# if current_user.is_authenticated:
# return redirect(url_for('home'))
form=LoginForm()
if request.method == 'POST':
if form.validate_on_submit():
user=User.query.filter_by(email=form.email.data).first()
if user and bcrypt.check_password_hash(user.password,form.password.data):
flash('You have logged in!!','success')
login_user(user)
return redirect(url_for('home'))
else:
flash('Unsuccessful Login.Please check your email and password','danger')
return render_template('login.html',title='Login',form=form)
@app.route("/logout")
def logout():
logout_user()
return redirect(url_for('register'))
if __name__=="__main__":
db.create_all()
app.run(debug=True)