From 0edad296faf90553b93d3386625414b8b7d721fa Mon Sep 17 00:00:00 2001 From: Matheus Felipe Date: Sun, 1 Jan 2023 18:21:17 -0300 Subject: [PATCH] Rename `id` variable and mentions to `id_` --- docs/tutorial/blog.rst | 40 +++++++++---------- examples/tutorial/flaskr/blog.py | 24 +++++------ .../tutorial/flaskr/templates/blog/index.html | 2 +- .../flaskr/templates/blog/update.html | 2 +- 4 files changed, 34 insertions(+), 34 deletions(-) diff --git a/docs/tutorial/blog.rst b/docs/tutorial/blog.rst index b06329ea..84b3613b 100644 --- a/docs/tutorial/blog.rst +++ b/docs/tutorial/blog.rst @@ -107,7 +107,7 @@ available in the result.
by {{ post['username'] }} on {{ post['created'].strftime('%Y-%m-%d') }}
{% if g.user['id'] == post['author_id'] %} - Edit + Edit {% endif %}

{{ post['body'] }}

@@ -198,16 +198,16 @@ it from each view. .. code-block:: python :caption: ``flaskr/blog.py`` - def get_post(id, check_author=True): + def get_post(id_, check_author=True): post = get_db().execute( '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) @@ -228,10 +228,10 @@ doesn't matter because they're not modifying the post. .. code-block:: python :caption: ``flaskr/blog.py`` - @bp.route('//update', methods=('GET', 'POST')) + @bp.route('//update', methods=('GET', 'POST')) @login_required - def update(id): - post = get_post(id) + def update(id_): + post = get_post(id_) if request.method == 'POST': title = request.form['title'] @@ -248,7 +248,7 @@ doesn't matter because they're not modifying the post. db.execute( 'UPDATE post SET title = ?, body = ?' ' WHERE id = ?', - (title, body, id) + (title, body, id_) ) db.commit() 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) Unlike the views you've written so far, the ``update`` function takes -an argument, ``id``. That corresponds to the ```` in the route. +an argument, ``id_``. That corresponds to the ```` in the route. 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 -don't specify ``int:`` and instead do ````, it will be a string. +ensure it's an :class:`int`, and pass it as the ``id_`` argument. If you +don't specify ``int:`` and instead do ````, it will be a string. To generate a URL to the update page, :func:`url_for` needs to be passed -the ``id`` so it knows what to fill in: -``url_for('blog.update', id=post['id'])``. This is also in the +the ``id_`` so it knows what to fill in: +``url_for('blog.update', id_=post['id'])``. This is also in the ``index.html`` file above. The ``create`` and ``update`` views look very similar. The main @@ -290,13 +290,13 @@ tutorial it's clearer to keep them separate.
-
+
{% endblock %} This template has two forms. The first posts the edited data to the -current page (``//update``). The other form contains only a button +current page (``//update``). The other form contains only a button and specifies an ``action`` attribute that posts to the delete view instead. The button uses some JavaScript to show a confirmation dialog before submitting. @@ -313,19 +313,19 @@ Delete ------ The delete view doesn't have its own template, the delete button is part -of ``update.html`` and posts to the ``//delete`` URL. Since there +of ``update.html`` and posts to the ``//delete`` URL. Since there is no template, it will only handle the ``POST`` method and then redirect to the ``index`` view. .. code-block:: python :caption: ``flaskr/blog.py`` - @bp.route('//delete', methods=('POST',)) + @bp.route('//delete', methods=('POST',)) @login_required - def delete(id): - get_post(id) + def delete(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')) diff --git a/examples/tutorial/flaskr/blog.py b/examples/tutorial/flaskr/blog.py index 3704626b..42eaf719 100644 --- a/examples/tutorial/flaskr/blog.py +++ b/examples/tutorial/flaskr/blog.py @@ -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("//update", methods=("GET", "POST")) +@bp.route("//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("//delete", methods=("POST",)) +@bp.route("//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")) diff --git a/examples/tutorial/flaskr/templates/blog/index.html b/examples/tutorial/flaskr/templates/blog/index.html index 3481b8e1..5b10694d 100644 --- a/examples/tutorial/flaskr/templates/blog/index.html +++ b/examples/tutorial/flaskr/templates/blog/index.html @@ -16,7 +16,7 @@
by {{ post['username'] }} on {{ post['created'].strftime('%Y-%m-%d') }}
{% if g.user['id'] == post['author_id'] %} - Edit + Edit {% endif %}

{{ post['body'] }}

diff --git a/examples/tutorial/flaskr/templates/blog/update.html b/examples/tutorial/flaskr/templates/blog/update.html index 2c405e63..cfff44d1 100644 --- a/examples/tutorial/flaskr/templates/blog/update.html +++ b/examples/tutorial/flaskr/templates/blog/update.html @@ -13,7 +13,7 @@
-
+
{% endblock %}