-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
272 lines (166 loc) · 6.51 KB
/
app.py
File metadata and controls
272 lines (166 loc) · 6.51 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
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
"""Blogly application."""
from flask import Flask, request, render_template, redirect, flash, session
from flask_debugtoolbar import DebugToolbarExtension
from models import db, connect_db, User, Post, Tag, PostTag
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql:///blogly'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SECRET_KEY'] = 'mynameisbrian1339'
app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False
debug = DebugToolbarExtension(app)
connect_db(app)
db.create_all()
# *****************
# ** USER ROUTES **
# *****************
@app.route('/')
def redirect_show_users():
return redirect("/users")
@app.route('/users', methods=['GET', 'POST'])
def show_users():
users = User.query.order_by(User.last_name, User.first_name).all()
return render_template('/users/users.html', users=users)
# render form to create a new user
@app.route('/users/new', methods=['GET'])
def show_create_user_form():
return render_template('/users/create_user.html')
# submit form to create a new user
@app.route('/users/new', methods=['POST'])
def create_user():
new_user = User(first_name=request.form['first_name'],
last_name=request.form['last_name'],
image_url=request.form['image_url'] or None)
db.session.add(new_user)
db.session.commit()
return redirect('/users')
@app.route('/users/<int:user_id>', methods=['GET', 'POST'])
def show_user_info(user_id):
user = User.query.get(user_id)
posts = user.posts
# posts = Post.query.filter(Post.user_id == user_id).all()
return render_template('/users/user_info.html', user=user, posts=posts)
@app.route('/users/<int:user_id>/edit')
def edit_user_form(user_id):
user = User.query.get(user_id)
return render_template('/users/edit_user.html', user=user)
@app.route('/users/<int:user_id>/edit', methods=['POST'])
def proccess_edit_user(user_id):
user = User.query.get(user_id)
user.first_name = request.form['first_name']
user.last_name = request.form['last_name']
user.image_url = request.form['image_url']
db.session.add(user)
db.session.commit()
return redirect('/users')
@app.route('/users/<int:user_id>/delete', methods=['POST'])
def delete_user(user_id):
User.query.filter(User.id == user_id).delete()
db.session.commit()
return redirect('/users')
# *****************
# ** POST ROUTES **
# *****************
"""Show form to add a post for that user."""
@app.route('/users/<int:user_id>/posts/new')
def show_create_post_form(user_id):
user = User.query.get(user_id)
tags = Tag.query.all()
return render_template('/posts/create_post.html', user=user, tags=tags)
"""Handle add form add post and redirect to the user detail page."""
@app.route('/users/<int:user_id>/posts/new', methods=["POST"])
def create_post(user_id):
new_post = Post(
title=request.form['title'], content=request.form['content'], user_id=user_id)
tags = Tag.query.all()
for tag in tags:
if request.form.get(f'{tag.name}'):
new_post.tags.append(tag)
db.session.add(new_post)
db.session.commit()
return redirect(f'/users/{user_id}')
"""Show a post - Show buttons to edit and delete the post."""
@app.route('/posts/<int:post_id>')
def show_post(post_id):
post = Post.query.get(post_id)
user = post.user
tags = post.tags
return render_template('/posts/post_info.html', post=post, user=user, tags=tags)
# """Show form to edit a post, and to cancel(back to user page)."""
@app.route('/posts/<int:post_id>/edit')
def show_edit_post_form(post_id):
post = Post.query.get(post_id)
tags = Tag.query.all()
post_tags = PostTag.query.filter(PostTag.post_id == post.id).all()
checked_tags = [Tag.query.get(post_tag.tag_id) for post_tag in post_tags]
return render_template('/posts/edit_post.html', post=post, tags=tags, checked_tags=checked_tags)
# """Handle editing of a post. Redirect back to the post view."""
@app.route('/posts/<int:post_id>/edit', methods=["POST"])
def proccess_edit_post(post_id):
post = Post.query.get(post_id)
post.title = request.form['title']
post.content = request.form['content']
PostTag.query.filter(PostTag.post_id == post_id).delete()
tags = Tag.query.all()
db.session.add(post)
db.session.commit()
for tag in tags:
if request.form.get(f'{tag.name}'):
new_postTag = PostTag(tag_id=tag.id, post_id=post.id)
db.session.add(new_postTag)
db.session.commit()
return redirect(f'/posts/{post_id}')
# """Delete the post."""
@app.route('/posts/<int:post_id>/delete', methods=["POST"])
def delete_post(post_id):
post = Post.query.filter(id == post_id).first()
user_id = post.user_id
Post.query.filter(Post.id == post_id).delete()
db.session.commit()
return redirect(f'/users/{user_id}')
# ****************
# ** TAG ROUTES **
# ****************
# """Lists all tags, with links to the tag detail page."""
@app.route('/tags', methods=['GET', 'POST'])
def show_tags():
tags = Tag.query.all()
return render_template('tags/list_tags.html', tags=tags)
# """Show detail about a tag. Have links to edit form and to delete."""
@app.route('/tags/<int:tag_id>')
def tag_details(tag_id):
tag = Tag.query.get(tag_id)
posts = tag.posts
return render_template('/tags/show_tag.html', posts=posts, tag=tag)
# # """Shows a form to add a new tag."""
@app.route('/tags/new')
def show_create_tag_form():
return render_template(f'/tags/create_tag.html')
# """Process add form, adds tag, and redirect to tag list."""
@app.route('/tags/new', methods=['POST'])
def create_tag():
new_tag = Tag(
name=request.form['tag_name'])
db.session.add(new_tag)
db.session.commit()
return redirect('/tags')
# # """Show edit form for a tag."""
@app.route('/tags/<int:tag_id>/edit')
def show_edit_tag_form(tag_id):
tag = Tag.query.get(tag_id)
return render_template('tags/edit_tag.html', tag=tag)
# # """Process edit form, edit tag, and redirects to the tags list."""
@app.route('/tags/<int:tag_id>/edit', methods=['POST'])
def edit_tag(tag_id):
tag = Tag.query.get(tag_id)
tag.name = request.form['tag_name']
db.session.add(tag)
db.session.commit()
return redirect('/tags')
# # """Delete a tag."""
@app.route('/tags/<int:tag_id>/delete', methods=['POST'])
def delete_tag(tag_id):
tag = Tag.query.get_or_404(tag_id)
db.session.delete(tag)
db.session.commit()
flash(f"Tag '{tag.name}' deleted.")
return redirect("/tags")