-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapplication.py
More file actions
77 lines (62 loc) · 2.45 KB
/
application.py
File metadata and controls
77 lines (62 loc) · 2.45 KB
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
from flask import render_template, request, url_for, flash
from flask_login import current_user, login_user, login_required, logout_user
from werkzeug.utils import redirect
from werkzeug.urls import url_parse
from csv_app.utils import valid_filename, valid_rows, import_csv
from csv_app.models import User
from csv_app.forms import LoginForm, RegistrationForm
from csv_app import application, db
@application.route('/')
@application.route('/index')
def index():
return render_template('index.html')
@application.route('/upload')
@login_required
def upload():
return render_template('upload.html')
@application.route('/message', methods=['GET', 'POST'])
@login_required
def upload_file():
if request.method == 'POST':
uploaded_file = request.files['file']
lines = uploaded_file.readlines()
if valid_filename(uploaded_file) and valid_rows(lines):
import_csv(lines[1:])
return render_template('thanks.html')
else:
return render_template('error.html')
@application.route('/login', methods=['GET', 'POST'])
def login():
if current_user.is_authenticated:
return redirect(url_for('index'))
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(username=form.username.data).first()
if user is None or not user.check_password(form.password.data):
flash('Invalid username or password')
return redirect(url_for('login'))
login_user(user, remember=form.remember_me.data)
next_page = request.args.get('next')
if not next_page or url_parse(next_page).netloc != '':
next_page = url_for('index')
return redirect(next_page)
return render_template('login.html', title='Sign In', form=form)
@application.route('/logout')
def logout():
logout_user()
return redirect(url_for('index'))
@application.route('/register', methods=['GET', 'POST'])
def register():
if current_user.is_authenticated:
return redirect(url_for('index'))
form = RegistrationForm()
if form.validate_on_submit():
user = User(username=form.username.data, email=form.email.data)
user.set_password(form.password.data)
db.session.add(user)
db.session.commit()
flash('Congrats, you are now a registered user!')
return redirect(url_for('login'))
return render_template('register.html', title='Register', form=form)
if __name__ == '__main__':
application.run()