Skip to content

Commit 69b3c45

Browse files
committed
Doc: Update format of document
1 parent 9ca2139 commit 69b3c45

File tree

1 file changed

+33
-32
lines changed

1 file changed

+33
-32
lines changed

comment_moderation.md

+33-32
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ As an administrator of a website, one of the things you would really want to do
44

55
![Comment Moderation](images/comment_moderation/comment_moderation.gif)
66

7-
You can [browse this repository on GitHub](https://github.com/GitauHarrison/user-comment-moderation-in-flask) to see the code for this tutorial.
7+
You can browse the source code on [this repository on GitHub](https://github.com/GitauHarrison/user-comment-moderation-in-flask).
88

99
## Create A Simple Flask Application
1010

@@ -24,7 +24,7 @@ As our application grows, certain configurations will be needed. At the moment,
2424

2525
If you look carefully, I have a module called `config` in the top-level directory. Following the principle of _separtion of concerns_, all the configurations that our application will need will be added here.
2626

27-
`config.py: Secret key configuration`
27+
`config.py`: Secret key configuration
2828
```python
2929
import os
3030

@@ -52,14 +52,14 @@ b'\x1eh\xfcIWC\x91\xd7\xb3\xfd\x02dK\xe0\xb5z'
5252

5353
Hard to guess, right? I will add this value to my environment variable.
5454

55-
`.env: Add secret configuration keys`
55+
`.env`: Add secret configuration keys
5656
```python
5757
SECRET_KEY=b'\x1eh\xfcIWC\x91\xd7\xb3\xfd\x02dK\xe0\xb5z'
5858
```
5959

6060
With the variable set, we can now update our application instance to read and apply our configurations.
6161

62-
`__init__.py: Regiser the config module in application instance`
62+
`__init__.py`: Regiser the config module in application instance
6363
```python
6464
from flask import Flask
6565
from flask_bootstrap import Bootstrap
@@ -90,7 +90,7 @@ Let us begin by creating a simple comment form. The information we want from a u
9090

9191
Let us now create the class that defines all the fields we want in our comments form.
9292

93-
`forms.py: Create a user comment form`
93+
`forms.py`: Create a user comment form
9494
```python
9595
from flask_wtf import FlaskForm
9696
from wtforms import StringField, SubmitField, TextAreaField
@@ -140,7 +140,7 @@ I have used Bootstrap to quicky display my comment form, though you can manually
140140

141141
With the form ready to be displayed, we will now update our `index()` view function to render it.
142142

143-
`routes.py: Render the comments form`
143+
`routes.py`: Render the comments form
144144

145145
```python
146146
from app import app
@@ -189,7 +189,7 @@ We will use `flask-migrate` to perform this migration. Install it in the virtual
189189

190190
SQLite, being the most convinient choice in developing small applications, expects certain configurations from `Flask-SQLAlchemy`. We first provide the location of the database in the application through `SQLALCHEMY_DATABASE_URI` configuration variable. This variable will source its value from the `DATABASE_URL` environment variable.
191191

192-
`config.py: Database Configuration`
192+
`config.py`: Database Configuration
193193
```python
194194
import os
195195
basedir = os.path.abspath(os.path.dirname(__file__)) # < ---- update
@@ -211,7 +211,7 @@ If `DATABASE_URL` does not exist, then I have provided a fallback value where I
211211

212212
The database will be referenced through a database instance. We will create a `db` variable that will be used to access the database.
213213

214-
`__init__.py: Database Instance`
214+
`__init__.py`: Database Instance
215215
```python
216216
from flask import Flask
217217
from flask_bootstrap import Bootstrap
@@ -242,7 +242,7 @@ Let is create a new module called `models.py` that will contain our database mod
242242

243243
The translation of our database into code will look like this:
244244

245-
`models.py: Comment database Schema`
245+
`models.py`: Comment database Schema
246246
```python
247247
from app import db
248248
from datetime import datetime
@@ -325,7 +325,7 @@ Since all user information is now stored in our `User` database which is linked
325325

326326
The first step is to update our database every time new data comes through the Comments Form.
327327

328-
`routes.py: Update the database`
328+
`routes.py`: Update the database
329329
```python
330330
from flask.helpers import url_for
331331
from werkzeug.utils import redirect
@@ -355,7 +355,7 @@ def index():
355355

356356
`form.validate_on_submit()` is used to validate the form. If the form is valid, the `user` object is created and added to the database. Otherwise, the index page will be displayed. I have added a flash message to notify the user that their comment has been posted. To see the message, we need to update our `base.html` template.
357357

358-
`base.html: Show flash message`
358+
`base.html`: Show flash message
359359
```html
360360
<!-- Contents of all our pages will go here -->
361361
{% block content %}
@@ -384,6 +384,7 @@ Try post a comment. If all goes well, then you should be able to see the flash m
384384

385385
We will display the comments in the index page. So, let us update our `index()` view function to display the comments.
386386

387+
`app/routes.py`: Display comments
387388
```python
388389
from flask.helpers import url_for
389390
from werkzeug.utils import redirect
@@ -418,7 +419,7 @@ def index():
418419

419420
We query our `UserComment` database using the `UserComment.query.all()` function. This function returns a list of all the comments made by users in the database. We can then loop through the list and display the comments in the `index.html` page.
420421

421-
`index.html: Display comments`
422+
`index.html`: Display comments
422423
```html
423424
{% extends 'base.html' %}
424425
{% import 'bootstrap/wtf.html' as wtf %}
@@ -459,7 +460,7 @@ You should be able to see this:
459460

460461
So far so good. The last thing I would like to add to every user is an avatar. This avatar will be displayed in each user's comment. To add an avatar to each user, we will need to update our `UserComment` model.
461462

462-
`models.py: User Avatar`
463+
`models.py`: User Avatar
463464
```python
464465
from hashlib import md5
465466

@@ -500,7 +501,7 @@ Obviously, it is the admin of the website who will have the ability to delete co
500501

501502
We want to collect an admin's username, email address and the password to their accounts. Our model will define these columns and store the relevant data in the database.
502503

503-
`models.py: Admin model`
504+
`models.py`: Admin model
504505

505506
```python
506507
# ...
@@ -538,7 +539,7 @@ Create an admin migration script and apply these changes to our databae.
538539

539540
We can now update our `Admin` model by registering a new admin. We will begin by creating an admin registration form.
540541

541-
`forms.py: Admin Registration Form`
542+
`forms.py`: Admin Registration Form
542543
```python
543544
from flask_wtf import FlaskForm
544545
from wtforms import StringField, PasswordField, SubmitField
@@ -556,7 +557,7 @@ class AdminRegistrationForm(FlaskForm):
556557

557558
With the form created, we will now create a view function which will handle the registration of an admin user.
558559

559-
`routes.py: Admin Registration`
560+
`routes.py`: Admin Registration
560561
```python
561562
# ...
562563

@@ -582,7 +583,7 @@ Let us create the template that will be used to display the registration form. F
582583

583584
We will quickly display our form in the template with the help of flask-wtf.
584585

585-
`register.html: Admin Registration Form`
586+
`register.html`: Admin Registration Form
586587
```html
587588
{% extends 'base.html' %}
588589
{% import 'bootstrap/wtf.html' as wtf %}
@@ -617,7 +618,7 @@ Flask provides the `flask-login` package which we will use to help us manage our
617618

618619
Like other extensions, we will initialize it in the application instance.
619620

620-
`__init__.py: Initialize Flask-Login`
621+
`__init__.py`: Initialize Flask-Login
621622
```python
622623
# ...
623624
from flask_login import LoginManager
@@ -646,7 +647,7 @@ class Admin(UserMixin, db.Model):
646647

647648
Because `Flask-login` literally knows nothing about databases, it will need the application's help to load the admin. We will use a user loader function to load the admin by their ID.
648649

649-
`models.py: User Loader`
650+
`models.py`: User Loader
650651
```python
651652
from app import login
652653

@@ -656,7 +657,7 @@ def load_user(id):
656657
```
657658
With the Admin model fully prepared to handle user sessions, we will now create the login form.
658659

659-
`forms.py: Admin Login Form`
660+
`forms.py`: Admin Login Form
660661
```python
661662
# ...
662663
from wtforms import StringField, SubmitField, PasswordField, BooleanField
@@ -674,7 +675,7 @@ The template that will be used to display the login form will be `login.html`. L
674675
(comment_moderation) $ touch app/templates/login.html # create empty login.html
675676
```
676677

677-
`login.html: Display Admin Login Form`
678+
`login.html`: Display Admin Login Form
678679
```html
679680
{% extends 'base.html' %}
680681
{% import 'bootstrap/wtf.html' as wtf %}
@@ -701,7 +702,7 @@ The template that will be used to display the login form will be `login.html`. L
701702

702703
Finally, we will create the view function which will handle the login of an admin.
703704

704-
`routes.py: Admin Login`
705+
`routes.py`: Admin Login
705706
```python
706707
from flask_login import login_user, current_user, logout_user
707708

@@ -723,7 +724,7 @@ def login():
723724

724725
To make it easier for an admin to log in to their account, we will display a link in the navigation bar. If you noticed, the login page also contains a link to the registration page. So, there is no need to add a registration link beyond that. I have left out the _forgot password_ link because it is beyond the scope of this tutorial. However, you can take it up as a challenge and learn how you can implement it in the application.
725726

726-
`base.html: Add a login link`
727+
`base.html`: Add a login link
727728
```html
728729
{% block navbar %}
729730
<nav class="navbar navbar-default">
@@ -755,7 +756,7 @@ Now, if you click on the Admin link in the navigation bar, you will be redirecte
755756

756757
As an admin, you would want to protect your account by ensuring you log out once you are done moderating user comments. The `logout_user()` method from `flask_login` handles this.
757758

758-
`routes.py: Admin Logout`
759+
`routes.py`: Admin Logout
759760
```python
760761
# ...
761762

@@ -767,7 +768,7 @@ def logout():
767768

768769
We will create a condition in our base template to display the logout link only if the user is logged in.
769770

770-
`base.html: Display logout link`
771+
`base.html`: Display logout link
771772
```html
772773
{% block navbar %}
773774
<nav class="navbar navbar-default">
@@ -811,7 +812,7 @@ For now, we will display all the user comments just as they can be seen in the i
811812
```
812813

813814

814-
`admin_dashboard.html: Display Admin Dashboard`
815+
`admin_dashboard.html`: Display Admin Dashboard
815816
```html
816817
{% extends 'base.html' %}
817818

@@ -842,7 +843,7 @@ For now, we will display all the user comments just as they can be seen in the i
842843

843844
The view function to render the admin dashboard will be `admin_dashboard()`.
844845

845-
`routes.py: Admin Dashboard`
846+
`routes.py`: Admin Dashboard
846847
```python
847848
# ...
848849
from flask_login import login_required
@@ -863,7 +864,7 @@ I have added the `login_required` decorator to protect this page from unauthoriz
863864

864865
We are now ready to implement comment moderation. At the end of this section, our application will only show comments that the admin has approved. To begin, I will add a new field in the `UserComment` model to store the moderation status of a comment.
865866

866-
`models.py: Add comment moderation status`
867+
`models.py`: Add comment moderation status
867868
```python
868869
# ...
869870

@@ -882,7 +883,7 @@ I have set the default value of the `allowed_comment` field to `False`. This wil
882883

883884
The next step is to add two view functions to _allow_ and _delete_ each comment.
884885

885-
`routes.py: Allow and Delete Comment`
886+
`routes.py`: Allow and Delete Comment
886887
```python
887888
@app.route('/admin/delete/<int:id>')
888889
def admin_delete(id):
@@ -907,7 +908,7 @@ I am identifying each comment by its ID. First, I query the database for first i
907908

908909
To make it work, let us update the admin's dashboard links to include the links to the `admin_delete()` and `admin_allow()` view functions.
909910

910-
`dashboard.html: Add links to admin_delete() and admin_allow()`
911+
`dashboard.html`: Add links to update and delete comments
911912
```html
912913
{% extends 'base.html' %}
913914

@@ -942,7 +943,7 @@ To make it work, let us update the admin's dashboard links to include the links
942943

943944
Let us update our `index()` view function to display only approved comments.
944945

945-
`routes.py: Display Approved Comments`
946+
`routes.py`: Display Approved Comments
946947
```python
947948
# ...
948949

@@ -978,7 +979,7 @@ Notice how only one comment appears in the index page whereas the admin dashboar
978979

979980
To ensure that we do not approve a comment twice, I will display an empty link reading _allowed_.
980981

981-
`admin_dashboard.html: Show allowed link`
982+
`admin_dashboard.html`: Show allowed link
982983
```html
983984
<span>
984985
{% if user.allowed_comment == 1 %}

0 commit comments

Comments
 (0)