-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
146 lines (110 loc) · 4.49 KB
/
app.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from flask import Flask,render_template, url_for, redirect
from flask_sqlalchemy import SQLAlchemy
from flask_login import UserMixin, login_user, LoginManager, login_required, logout_user, current_user
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField
from wtforms.validators import InputRequired, Length, ValidationError
from flask_bcrypt import Bcrypt
db = SQLAlchemy()
bcrypt = Bcrypt()
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = 'sqlite:///database1.db'
app.config["SECRET_KEY"] = 'thisisasecretkey'
db.init_app(app)
app.app_context().push()
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
@app.route('/')
def home():
return render_template('homepage.html')
@app.route('/home')
def home1():
return render_template('homepage.html')
@app.route('/formfilled')
def formfilled():
return render_template('formFilled.html')
@app.route('/about')
def about():
return render_template('About.html')
@app.route('/complaint')
def complaint():
return render_template('complaintForm.html')
@app.route('/contact')
def contact():
return render_template('contact.html')
@app.route('/dashboard', methods=['GET', 'POST'])
@login_required
def dashboard():
return render_template('adminDash.html')
@app.route('/dashboard2', methods=['GET', 'POST'])
@login_required
def dashboard2():
return render_template('studentDash.html')
@app.route('/login', methods=['GET','POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(username=form.username.data).first()
if user:
if bcrypt.check_password_hash(user.password, form.password.data):
login_user(user)
return redirect(url_for('dashboard'))
return render_template('adminLogin.html', form=form)
@app.route('/student-login', methods=['GET','POST'])
def login1():
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(username=form.username.data).first()
if user:
if bcrypt.check_password_hash(user.password, form.password.data):
login_user(user)
return redirect(url_for('dashboard2'))
return render_template('studentLogin.html', form=form)
@app.route('/register', methods=['GET','POST'])
def register():
form = RegisterForm()
if form.validate_on_submit():
hashed_password = bcrypt.generate_password_hash(form.password.data)
new_user = User(username=form.username.data, password=hashed_password)
db.session.add(new_user)
db.session.commit()
return redirect(url_for('login'))
return render_template('adminSignup.html', form=form)
@app.route('/register2', methods=['GET','POST'])
def register2():
form = RegisterForm()
if form.validate_on_submit():
hashed_password = bcrypt.generate_password_hash(form.password.data)
new_user = User(username=form.username.data, password=hashed_password)
db.session.add(new_user)
db.session.commit()
return redirect(url_for('login'))
return render_template('studentSignup.html', form=form)
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), nullable=False, unique=True)
password = db.Column(db.String(80), nullable=False)
class RegisterForm(FlaskForm):
username = StringField(validators=[
InputRequired(), Length(min=4, max=20)], render_kw={"placeholder": "Username"})
password = PasswordField(validators=[
InputRequired(), Length(min=8, max=20)], render_kw={"placeholder": "Password"})
submit = SubmitField('Register')
def validate_username(self, username):
existing_user_username = User.query.filter_by(
username=username.data).first()
if existing_user_username:
raise ValidationError(
'That username already exists. Please choose a different one.')
class LoginForm(FlaskForm):
username = StringField(validators=[
InputRequired(), Length(min=4, max=120)], render_kw={"placeholder": "email"})
password = PasswordField(validators=[
InputRequired(), Length(min=8, max=20)], render_kw={"placeholder": "Password"})
submit = SubmitField('Login')
if __name__ == '__main__':
app.run(debug=True)