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

@ -107,7 +107,7 @@ available in the result.
<div class="about">by {{ post['username'] }} on {{ post['created'].strftime('%Y-%m-%d') }}</div> <div class="about">by {{ post['username'] }} on {{ post['created'].strftime('%Y-%m-%d') }}</div>
</div> </div>
{% if g.user['id'] == post['author_id'] %} {% 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 %} {% endif %}
</header> </header>
<p class="body">{{ post['body'] }}</p> <p class="body">{{ post['body'] }}</p>
@ -198,16 +198,16 @@ it from each view.
.. code-block:: python .. code-block:: python
:caption: ``flaskr/blog.py`` :caption: ``flaskr/blog.py``
def get_post(id, check_author=True): def get_post(id_, check_author=True):
post = get_db().execute( post = get_db().execute(
'SELECT p.id, title, body, created, author_id, username' 'SELECT p.id, title, body, created, author_id, username'
' FROM post p JOIN user u ON p.author_id = u.id' ' FROM post p JOIN user u ON p.author_id = u.id'
' WHERE p.id = ?', ' WHERE p.id = ?',
(id,) (id_,)
).fetchone() ).fetchone()
if post is None: 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']: if check_author and post['author_id'] != g.user['id']:
abort(403) abort(403)
@ -228,10 +228,10 @@ doesn't matter because they're not modifying the post.
.. code-block:: python .. code-block:: python
:caption: ``flaskr/blog.py`` :caption: ``flaskr/blog.py``
@bp.route('/<int:id>/update', methods=('GET', 'POST')) @bp.route('/<int:id_>/update', methods=('GET', 'POST'))
@login_required @login_required
def update(id): def update(id_):
post = get_post(id) post = get_post(id_)
if request.method == 'POST': if request.method == 'POST':
title = request.form['title'] title = request.form['title']
@ -248,7 +248,7 @@ doesn't matter because they're not modifying the post.
db.execute( db.execute(
'UPDATE post SET title = ?, body = ?' 'UPDATE post SET title = ?, body = ?'
' WHERE id = ?', ' WHERE id = ?',
(title, body, id) (title, body, id_)
) )
db.commit() db.commit()
return redirect(url_for('blog.index')) return redirect(url_for('blog.index'))
@ -256,13 +256,13 @@ doesn't matter because they're not modifying the post.
return render_template('blog/update.html', post=post) return render_template('blog/update.html', post=post)
Unlike the views you've written so far, the ``update`` function takes Unlike the views you've written so far, the ``update`` function takes
an argument, ``id``. That corresponds to the ``<int:id>`` in the route. an argument, ``id_``. That corresponds to the ``<int:id_>`` in the route.
A real URL will look like ``/1/update``. Flask will capture the ``1``, A real URL will look like ``/1/update``. Flask will capture the ``1``,
ensure it's an :class:`int`, and pass it as the ``id`` argument. If you ensure it's an :class:`int`, and pass it as the ``id_`` argument. If you
don't specify ``int:`` and instead do ``<id>``, it will be a string. don't specify ``int:`` and instead do ``<id_>``, it will be a string.
To generate a URL to the update page, :func:`url_for` needs to be passed To generate a URL to the update page, :func:`url_for` needs to be passed
the ``id`` so it knows what to fill in: the ``id_`` so it knows what to fill in:
``url_for('blog.update', id=post['id'])``. This is also in the ``url_for('blog.update', id_=post['id'])``. This is also in the
``index.html`` file above. ``index.html`` file above.
The ``create`` and ``update`` views look very similar. The main The ``create`` and ``update`` views look very similar. The main
@ -290,13 +290,13 @@ tutorial it's clearer to keep them separate.
<input type="submit" value="Save"> <input type="submit" value="Save">
</form> </form>
<hr> <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?');"> <input class="danger" type="submit" value="Delete" onclick="return confirm('Are you sure?');">
</form> </form>
{% endblock %} {% endblock %}
This template has two forms. The first posts the edited data to the This template has two forms. The first posts the edited data to the
current page (``/<id>/update``). The other form contains only a button current page (``/<id_>/update``). The other form contains only a button
and specifies an ``action`` attribute that posts to the delete view and specifies an ``action`` attribute that posts to the delete view
instead. The button uses some JavaScript to show a confirmation dialog instead. The button uses some JavaScript to show a confirmation dialog
before submitting. before submitting.
@ -313,19 +313,19 @@ Delete
------ ------
The delete view doesn't have its own template, the delete button is part The delete view doesn't have its own template, the delete button is part
of ``update.html`` and posts to the ``/<id>/delete`` URL. Since there of ``update.html`` and posts to the ``/<id_>/delete`` URL. Since there
is no template, it will only handle the ``POST`` method and then redirect is no template, it will only handle the ``POST`` method and then redirect
to the ``index`` view. to the ``index`` view.
.. code-block:: python .. code-block:: python
:caption: ``flaskr/blog.py`` :caption: ``flaskr/blog.py``
@bp.route('/<int:id>/delete', methods=('POST',)) @bp.route('/<int:id_>/delete', methods=('POST',))
@login_required @login_required
def delete(id): def delete(id_):
get_post(id) get_post(id_)
db = get_db() db = get_db()
db.execute('DELETE FROM post WHERE id = ?', (id,)) db.execute('DELETE FROM post WHERE id = ?', (id_,))
db.commit() db.commit()
return redirect(url_for('blog.index')) return redirect(url_for('blog.index'))

View file

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

View file

@ -13,7 +13,7 @@
<input type="submit" value="Save"> <input type="submit" value="Save">
</form> </form>
<hr> <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?');"> <input class="danger" type="submit" value="Delete" onclick="return confirm('Are you sure?');">
</form> </form>
{% endblock %} {% endblock %}