-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
195 lines (163 loc) · 5.71 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
from functools import wraps
from flask import render_template, flash, request, redirect, url_for, session
from passlib.hash import sha256_crypt
from config import app, db
from forms import RegisterForm, VideoForm, CommentForm
from models import Users, Videos, Comments
from werkzeug.contrib.fixers import ProxyFix
app = app
db.init_app(app)
db.create_all()
# TODO: Decorator login required
def login_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if session['logged_in'] == False:
return redirect(url_for('login', next=request.url))
return f(*args, **kwargs)
return decorated_function
# TODO: HomePage
@app.route('/')
def index():
data = Videos.query.all()
if len(data) > 0:
return render_template('home.html', videos=data)
else:
msg = 'No Videos Found'
return render_template('home.html', msg=msg)
# TODO: About
@app.route('/about')
def about():
return render_template('about.html')
# TODO: Register
@app.route('/register', methods=['GET', 'POST'])
def register():
form = RegisterForm(request.form)
if request.method == 'POST' and form.validate():
name = form.name.data
username = form.username.data
email = form.email.data
password = sha256_crypt.encrypt(str(form.password.data))
user = Users(name=name, username=username, email=email, password=password)
try:
db.session.add(user)
db.session.commit()
except:
flash("Something went wrong with DB")
flash('You are now registered and can login!', 'success')
return redirect(url_for('login'))
return render_template('register.html', form=form)
# TODO: Login
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password_candidate = request.form['password']
result = Users.query.filter_by(username=username).first()
if result is not None:
password = result.password
if sha256_crypt.verify(password_candidate, password):
session['logged_in'] = True
session['username'] = username
flash('You are now logged in', 'success')
return redirect(url_for('dashboard'))
else:
error = 'Invalid login'
return render_template('login_html', error=error)
else:
error = 'Username not found'
return render_template('login.html', error=error)
return render_template('login.html')
# TODO: Logout
@app.route('/logout')
@login_required
def logout():
session.clear()
flash('You are now logged out', 'success')
return redirect(url_for('login'))
# TODO: Videos
@app.route('/videos')
def videos():
data = Videos.query.all()
if len(data) > 0:
return render_template('videos.html', videos=data)
else:
msg = 'No Videos Found'
return render_template('videos.html', msg=msg)
# TODO: Video Details
@app.route('/video/<int:id>/', methods=['GET', 'POST'])
def video(id):
data = Videos.query.get(id)
form = CommentForm(request.form)
if request.method == 'POST' and form.validate():
user = Users.query.filter_by(username=data.author).first()
comment_candidate = form.comment.data
comment = Comments(body=comment_candidate, video_id=data.id, user_id=user.id)
db.session.add(comment)
db.session.commit()
msg = "Comment added!"
return render_template('video.html', video=data, form=form, msg=msg)
if data:
return render_template('video.html', video=data, form=form)
else:
msg = "No video found!"
return render_template('video.html', msg=msg)
# TODO: Add Video
@app.route('/add_video', methods=["GET", "POST"])
@login_required
def add_video():
form = VideoForm(request.form)
if request.method == "POST" and form.validate():
title = form.title.data
link = form.link.data
author = session['username']
video = Videos(title, link, author)
db.session.add(video)
db.session.commit()
flash("Video created", "success")
return redirect(url_for('dashboard'))
else:
error = form.errors
return render_template('add_video.html', form=form, error=error)
return render_template('add_video.html', form=form)
# TODO: Dashboard
@app.route('/dashboard')
@login_required
def dashboard():
data = Videos.query.all()
if len(data) > 0:
return render_template('dashboard.html', videos=data)
else:
msg = 'No Videos Found'
return render_template('dashboard.html', msg=msg)
# TODO: Edit Video
@app.route('/edit_video/<int:id>/', methods=['GET', 'POST'])
@login_required
def edit_video(id):
data = Videos.query.get(id)
form = VideoForm(request.form)
form.title.data = data.title
form.link.data = data.link
if request.method == 'POST' and form.validate():
data.title = request.form["title"]
data.link = request.form["link"]
db.session.commit()
flash("Video updated!", "success")
return redirect(url_for('dashboard'))
return render_template('edit_video.html', form=form)
# TODO: Delete Video
@app.route('/delete_video/<int:id>', methods=['GET', 'POST'])
@login_required
def delete_video(id):
data = Videos.query.get(id)
if data:
db.session.delete(data)
db.session.commit()
msg = "Video deleted!"
return render_template('dashboard.html', msg=msg)
else:
error = "Video was not deleted!"
return render_template('dashboard.html', error=error)
app.wsgi_app = ProxyFix(app.wsgi_app)
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)