Rename id variable and mentions to id_

This commit is contained in:
Matheus Felipe 2023-01-01 18:21:17 -03:00
parent 74e5263c88
commit 0edad296fa
No known key found for this signature in database
GPG key ID: 3FB416EDA5965D0A
4 changed files with 34 additions and 34 deletions

View file

@ -25,13 +25,13 @@ def index():
return render_template("blog/index.html", posts=posts)
def get_post(id, check_author=True):
def get_post(id_, check_author=True):
"""Get a post and its author by id.
Checks that the id exists and optionally that the current user is
the author.
:param id: id of post to get
:param id_: id of post to get
:param check_author: require the current user to be the author
:return: the post with author information
:raise 404: if a post with the given id doesn't exist
@ -43,13 +43,13 @@ def get_post(id, check_author=True):
"SELECT p.id, title, body, created, author_id, username"
" FROM post p JOIN user u ON p.author_id = u.id"
" WHERE p.id = ?",
(id,),
(id_,),
)
.fetchone()
)
if post is None:
abort(404, f"Post id {id} doesn't exist.")
abort(404, f"Post id {id_} doesn't exist.")
if check_author and post["author_id"] != g.user["id"]:
abort(403)
@ -83,11 +83,11 @@ def create():
return render_template("blog/create.html")
@bp.route("/<int:id>/update", methods=("GET", "POST"))
@bp.route("/<int:id_>/update", methods=("GET", "POST"))
@login_required
def update(id):
def update(id_):
"""Update a post if the current user is the author."""
post = get_post(id)
post = get_post(id_)
if request.method == "POST":
title = request.form["title"]
@ -102,7 +102,7 @@ def update(id):
else:
db = get_db()
db.execute(
"UPDATE post SET title = ?, body = ? WHERE id = ?", (title, body, id)
"UPDATE post SET title = ?, body = ? WHERE id = ?", (title, body, id_)
)
db.commit()
return redirect(url_for("blog.index"))
@ -110,16 +110,16 @@ def update(id):
return render_template("blog/update.html", post=post)
@bp.route("/<int:id>/delete", methods=("POST",))
@bp.route("/<int:id_>/delete", methods=("POST",))
@login_required
def delete(id):
def delete(id_):
"""Delete a post.
Ensures that the post exists and that the logged in user is the
author of the post.
"""
get_post(id)
get_post(id_)
db = get_db()
db.execute("DELETE FROM post WHERE id = ?", (id,))
db.execute("DELETE FROM post WHERE id = ?", (id_,))
db.commit()
return redirect(url_for("blog.index"))

View file

@ -16,7 +16,7 @@
<div class="about">by {{ post['username'] }} on {{ post['created'].strftime('%Y-%m-%d') }}</div>
</div>
{% if g.user['id'] == post['author_id'] %}
<a class="action" href="{{ url_for('blog.update', id=post['id']) }}">Edit</a>
<a class="action" href="{{ url_for('blog.update', id_=post['id']) }}">Edit</a>
{% endif %}
</header>
<p class="body">{{ post['body'] }}</p>

View file

@ -13,7 +13,7 @@
<input type="submit" value="Save">
</form>
<hr>
<form action="{{ url_for('blog.delete', id=post['id']) }}" method="post">
<form action="{{ url_for('blog.delete', id_=post['id']) }}" method="post">
<input class="danger" type="submit" value="Delete" onclick="return confirm('Are you sure?');">
</form>
{% endblock %}