-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
341 lines (279 loc) · 11.4 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# svblog - app.py
# Author: James Gray
# June 2013
#
# This file contains the main Flask
# application logic for the project.
import os
import re
from datetime import datetime, timedelta
from flask import Flask, request, session, redirect, \
url_for, render_template, flash, send_from_directory
from werkzeug.utils import secure_filename
from tools import urlify, get_user, valid_file, valid_user, display
from user import User, udb
from upload import Upload, fdb
from entry import Entry, edb
app = Flask(__name__)
app.config.from_pyfile('settings.cfg')
def init_db():
"""
Create the initial user, file and entry database tables.
"""
udb.create_all()
fdb.create_all()
edb.create_all()
@app.after_request
def after_request(response):
"""
Add header to prevent browser from caching pages.
This will prevent a user from accessing their files
after logging out, or after logging in as another user.
"""
response.headers['Cache-Control'] = 'private, max-age=0'
return response
@app.route('/login/', methods=['GET', 'POST'])
def login():
"""
Allows a user to log in to their account.
Note that the index page redirects here by default.
"""
error = None
if 'logged_in' not in session:
session['theme'] = app.config['DEFAULT_THEME']
if request.method == 'POST':
username = request.form['username']
# Check to see if the user exists.
user_instance = get_user(username)
if user_instance is None:
error = 'Invalid username.'
elif not user_instance.check_pw(request.form['password']):
error = 'Invalid password.'
else:
session['logged_in'], session['posting_enabled'], session['theme'] = \
username, user_instance.posting_enabled, user_instance.theme
flash('Successfully logged in.')
return redirect(url_for('entries', name=username))
else:
return render_template('login.html', theme=session['theme'])
display(error)
return render_template('login.html', theme=session['theme'])
@app.route('/')
def index():
return redirect(url_for('login'))
@app.route('/logout/')
def logout():
"""
Logs a user out before returning to the login screen.`
"""
session.pop('logged_in', None)
session.pop('posting_enabled', None)
session['theme'] = app.config['DEFAULT_THEME']
flash('Successfully logged out.')
return redirect(url_for('login'))
@app.route('/create/', methods=['GET', 'POST'])
def create():
"""
Allows a user to create a new account.
"""
error = None
if 'logged_in' not in session:
session['theme'] = app.config['DEFAULT_THEME']
if request.method == 'POST':
# Get submitted values from fields.
username, password = request.form['username'], request.form['password-a']
# Check if there is a user by the same username in the database.
user_instance = get_user(username)
if user_instance is not None:
error = 'Username is already taken.'
elif re.search(r'[^_a-zA-Z0-9]', username):
error = 'Username must only contain letters, numbers, and underscores.'
elif password == '':
error = 'Please enter a password.'
elif password != request.form['password-b']:
error = 'Passwords do not match.'
else:
# Add user to the database and log user in.
user_instance = User(username, password)
user_instance.theme = app.config['DEFAULT_THEME']
udb.session.add(user_instance)
udb.session.commit()
flash('Account created!')
session['logged_in'], session['posting_enabled'], session['theme'] = \
username, user_instance.posting_enabled, user_instance.theme
return redirect(url_for('entries', name=username))
display(error)
return render_template('create.html', theme=session['theme'])
@app.route('/<name>/', methods=['GET', 'POST'])
def entries(name):
"""
This page presents a user's blog posts and file uploads,
allowing visitors to download the files individually.
"""
if get_user(name) is not None:
session['theme'] = get_user(name).theme
entries = [e for e in Entry.query.all()]
entries.reverse()
uploads = [dict(userid=f.userid, filename=f.filename, filetype=f.filetype) \
for f in Upload.query.all()]
if request.method == "POST":
title, text = request.form['title'], request.form['text']
text = urlify(text)
entry_instance = Entry(name, title, text)
edb.session.add(entry_instance)
edb.session.commit()
flash('Post successful!')
return redirect(url_for('entries', name=name))
else:
return render_template('entries.html', username=name,
uploads=uploads, entries=entries, theme=session['theme'])
else:
display("User does not exist.")
return redirect(url_for('login'))
@app.route('/<name>/upload/', methods=['GET', 'POST'])
def upload(name):
"""
This page allows a user to upload a text or image file.
"""
# Refuse access if posting is disabled for the user.
if "posting_enabled" in session and session['posting_enabled'] == False:
error = "Access denied."
display(error)
if 'logged_in' in session:
return redirect(url_for('entries', name=session['logged_in']))
else:
return redirect(url_for('login'))
# Check if the user is logged in before allowing to upload files.
error = valid_user(name)
if error is None:
if request.method == 'POST':
file = request.files['file']
if file and valid_file(file.filename):
# Sanitize the filename, save the file to the uploads
# folder, and add the file and owner info to the file database.
old_filename = filename = secure_filename(file.filename)
filetype = filename.rsplit('.', 1)[1].lower()
# Prevents duplicate filenames by appending (1), (2), etc.
# e.g. if two "images.jpg" are uploaded, the second one would
# become "images(1).jpg".
x = 0
while os.path.isfile(os.path.join(app.config['UPLOAD_FOLDER'], filename)):
x += 1
filename = ("%s(%s).%s" % (old_filename.rsplit('.', 1)[0], x, filetype))
# Save the file to the uploads folder.
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
file_instance = Upload(name, filename, filetype)
# Insert the upload object into the database.
fdb.session.add(file_instance)
fdb.session.commit()
flash('File was uploaded successfully.')
return redirect(url_for('entries', name=name))
else:
flash("Invalid filename or file type.")
return render_template('upload.html', username=name, theme=session['theme'])
# If an error occurs, display the error and
# redirect to the appropriate page.
display(error)
if 'logged_in' in session:
return redirect(url_for('upload', name=session['logged_in']))
else:
return redirect(url_for('login'))
@app.route('/<name>/delete/post_<id>')
def delete_entry(name, id):
"""
This page will delete an entry from the database.
"""
# Check if the user is logged in before allowing to delete files.
error = valid_user(name)
if error is None:
entry_instance = Entry.query.filter_by(id=id, userid=name).first()
if entry_instance and entry_instance.userid == name:
# Delete the entry from the database if it exists.
edb.session.delete(entry_instance)
edb.session.commit()
flash('Entry was deleted successfully.')
return redirect(url_for('entries', name=name))
else:
error = "Specified entry does not exist."
# If an error occurs, display the error and
# redirect to the appropriate page.
display(error)
if 'logged_in' in session:
return redirect(url_for('entries', name=session['logged_in']))
else:
return redirect(url_for('login'))
@app.route('/<name>/delete/<filename>', methods=['GET', 'POST'])
def delete_file(name, filename):
"""
This page will delete a file from the database and uploads folder.
"""
# Check if the user is logged in before allowing to delete files.
error = valid_user(name)
if error is None:
file = Upload.query.filter_by(filename=filename).first()
if file and file.userid == name:
# Delete the file from the upload folder if it exists.
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
if os.path.isfile(filepath):
os.remove(filepath)
# Delete the upload object from the database.
fdb.session.delete(file)
fdb.session.commit()
flash('File was deleted successfully.')
return redirect(url_for('entries', name=name))
else:
error = "Specified file does not exist."
# If an error occurs, display the error and
# redirect to the appropriate page.
display(error)
if 'logged_in' in session:
return redirect(url_for('entries', name=session['logged_in']))
else:
return redirect(url_for('login'))
@app.route('/<name>/<filename>')
def uploaded_file(name, filename):
"""
This page will fetch a given file from the uploads folder.
"""
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
@app.route('/<name>/theme/', methods=['GET', 'POST'])
def change_theme(name):
"""
This page will allow the user to change the appearance of their blog.
"""
# Check if the user is logged in before allowing to change theme.
error = valid_user(name)
if error is None:
if request.method == 'POST':
new_theme = request.form['theme']
user_instance = get_user(name)
# Change the user's theme, change the theme in browser and
# store the changed theme in the user database.
user_instance.theme = session['theme'] = new_theme
udb.session.commit()
flash('Theme changed to %s.' % new_theme.lower())
return redirect(url_for('change_theme', name=name))
return render_template('theme.html', username=name, theme=session['theme'])
# If an error occurs, display the error and
# redirect to the appropriate page.
display(error)
if 'logged_in' in session:
return redirect(url_for('upload', name=session['logged_in']))
else:
return redirect(url_for('login'))
@app.route('/<name>/all/')
def view_users(name):
"""
This page will allow the administrator to view the blogs of all registered users.
"""
# Check if the logged in user is the administrator.
if 'logged_in' in session and session['logged_in'] == "admin":
users = [u for u in User.query.all()]
return render_template('all.html', username=name, users=users, theme=session['theme'])
else:
error = "Must be logged in as the administrator to access this page."
display(error)
return redirect(url_for('login'))
if __name__ == "__main__":
init_db()
app.run()