-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.py
144 lines (119 loc) · 3.69 KB
/
routes.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
from flask import (
Flask,
render_template,
redirect,
flash,
url_for,
session
)
from datetime import timedelta
from sqlalchemy.exc import (
IntegrityError,
DataError,
DatabaseError,
InterfaceError,
InvalidRequestError,
)
from werkzeug.routing import BuildError
from flask_bcrypt import Bcrypt,generate_password_hash, check_password_hash
from flask_login import (
UserMixin,
login_user,
LoginManager,
current_user,
logout_user,
login_required,
)
from app import create_app,db,login_manager,bcrypt
from models import User
from forms import login_form,register_form
from camera import VideoCamera
from flask import Flask, render_template, Response
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
app = create_app()
@app.before_request
def session_handler():
session.permanent = True
app.permanent_session_lifetime = timedelta(minutes=1)
@app.route("/", methods=("GET", "POST"), strict_slashes=False)
def index():
return render_template("index.html",title="Home")
def gen(camera):
while True:
frame = camera.get_frame()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
@app.route('/video_feed')
def video_feed():
return Response(gen(VideoCamera()),
mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route("/login/", methods=("GET", "POST"), strict_slashes=False)
def login():
form = login_form()
if form.validate_on_submit():
try:
user = User.query.filter_by(email=form.email.data).first()
if check_password_hash(user.pwd, form.pwd.data):
login_user(user)
return redirect(url_for('index'))
else:
flash("Invalid Username or password!", "danger")
except Exception as e:
flash(e, "danger")
return render_template("auth.html",
form=form,
text="Login",
title="Login",
btn_action="Login"
)
# Register route
@app.route("/register/", methods=("GET", "POST"), strict_slashes=False)
def register():
form = register_form()
if form.validate_on_submit():
try:
email = form.email.data
pwd = form.pwd.data
username = form.username.data
newuser = User(
username=username,
email=email,
pwd=bcrypt.generate_password_hash(pwd),
)
db.session.add(newuser)
db.session.commit()
flash(f"Account Succesfully created", "success")
return redirect(url_for("login"))
except InvalidRequestError:
db.session.rollback()
flash(f"Something went wrong!", "danger")
except IntegrityError:
db.session.rollback()
flash(f"User already exists!.", "warning")
except DataError:
db.session.rollback()
flash(f"Invalid Entry", "warning")
except InterfaceError:
db.session.rollback()
flash(f"Error connecting to the database", "danger")
except DatabaseError:
db.session.rollback()
flash(f"Error connecting to the database", "danger")
except BuildError:
db.session.rollback()
flash(f"An error occured !", "danger")
return render_template("auth.html",
form=form,
text="Create account",
title="Register",
btn_action="Register account"
)
@app.route("/logout")
@login_required
def logout():
logout_user()
return redirect(url_for('login'))
if __name__ == "__main__":
app.run(debug=True)