-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
162 lines (128 loc) · 5.12 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
from flask import Flask, render_template, redirect, session, flash
from flask_debugtoolbar import DebugToolbarExtension
from models import db, connect_db, User, Feedback
from forms import UserForm, LoginForm, FeedbackForm
from sqlalchemy.exc import IntegrityError
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgres:///user_feedback'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_ECHO'] = True
app.config['SECRET_KEY'] = 'feedbackexercise1234'
app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False
connect_db(app)
toolbar = DebugToolbarExtension(app)
@app.route('/')
def show_register():
return redirect('/register')
@app.route('/register', methods=['GET', 'POST'])
def register_user():
form = UserForm()
if form.validate_on_submit():
username = form.username.data
password = form.password.data
email = form.email.data
first_name = form.first_name.data
last_name = form.last_name.data
new_user = User.register(username, password, email, first_name, last_name)
db.session.add(new_user)
try:
db.session.commit()
except IntegrityError:
form.username.errors.append('Username taken. Please pick another one.')
return render_template('register.html', form=form)
session['username'] = new_user.username
return redirect(f'/users/{new_user.username}')
else:
return render_template('register.html', form=form)
@app.route('/login', methods=['GET', 'POST'])
def login_form():
form = LoginForm()
if form.validate_on_submit():
username = form.username.data
password = form.password.data
user = User.authenticate(username, password)
if user:
session['username'] = user.username
return redirect(f'/users/{user.username}')
else:
form.username.errors.append('Invalid username/password')
return render_template('login.html', form=form)
@app.route('/users/<username>')
def show_user(username):
if 'username' not in session:
flash('Please login first!')
return redirect('/login')
if (username != session['username']):
flash("You cannot view another user's information!")
username = session['username']
return redirect(f'/users/{username}')
user = User.query.get_or_404(username)
feedback = Feedback.query.filter_by(username=username)
return render_template('userinfo.html', user=user, feedback=feedback)
@app.route('/users/<username>/feedback/add', methods=['GET', 'POST'])
def add_feedback(username):
if 'username' not in session:
flash('Please login first!')
return redirect('/login')
if (username != session['username']):
flash('You cannot add feedback for another user')
username = session['username']
return redirect(f'/users/{username}')
form = FeedbackForm()
if form.validate_on_submit():
title = form.title.data
content = form.content.data
new_feedback = Feedback(title=title, content=content, username=username)
db.session.add(new_feedback)
db.session.commit()
return redirect(f'/users/{username}')
else:
return render_template('feedback.html', form=form)
@app.route('/feedback/<int:id>/update', methods=['GET', 'POST'])
def update_feedback(id):
feedback = Feedback.query.get_or_404(id)
if 'username' not in session:
flash('Please login first!')
return redirect('/login')
if (feedback.username != session['username']):
flash('You cannot update feedback for another user')
return redirect(f"/users/{session['username']}")
form = FeedbackForm(obj=feedback)
if form.validate_on_submit():
feedback.title = form.title.data
feedback.content = form.content.data
db.session.commit()
return redirect(f"/users/{session['username']}")
else:
return render_template('feedback.html', form=form)
@app.route('/feedback/<int:id>/delete', methods=['POST'])
def delete_feedback(id):
if 'username' not in session:
flash('Please login first!')
return redirect('/login')
feedback = Feedback.query.get_or_404(id)
if (feedback.username != session['username']):
flash('You cannot delete feedback for another user')
return redirect(f"/users/{session['username']}")
db.session.delete(feedback)
db.session.commit()
return redirect(f"/users/{session['username']}")
@app.route('/users/<username>/delete', methods=['POST'])
def delete_user(username):
if 'username' not in session:
flash('Please login first!')
return redirect('/login')
if (username != session['username']):
flash('You cannot delete another user')
return redirect(f"/users/{session['username']}")
Feedback.query.filter_by(username=username).delete()
user = User.query.get_or_404(username)
db.session.delete(user)
db.session.commit()
session.pop('username')
return redirect('/')
@app.route('/logout')
def logout_user():
session.pop('username')
flash('Goodbye!')
return redirect('/')