Skip to content

Interpreting FlaskBB Source Code

Info

Author: Void, published on October 29, 2021, Reading time: About 6 minutes, WeChat public account article link:

1 Introduction

Last time, we talked about FlaskBB being a powerful open-source forum and successfully ran it locally. This time, we will interpret the source code of FlaskBB with you.

2 Directory Structure

First, let's take a look at its directory structure.

In the root directory, you can see common dependency-related files (such as requirements.txt, setup.py, etc.), as well as the flaskbb.cfg generated by flaskbb makeconfig. The tests folder stores code related to automated testing. For a product-level application, automatic testing is essential. Finally, there is the flaskbb main folder.

3 Source Code Interpretation

Let's take a look at the create_app function in the main program app.py of FlaskBB:

def create_app(config=None, instance_path=None):

    app = Flask(
        "flaskbb", instance_path=instance_path, instance_relative_config=True
    )

    # instance folders are not automatically created by flask
    if not os.path.exists(app.instance_path):
        os.makedirs(app.instance_path)

    configure_app(app, config)
    configure_celery_app(app, celery)
    configure_extensions(app)
    load_plugins(app)
    configure_blueprints(app)
    configure_template_filters(app)
    configure_context_processors(app)
    configure_before_handlers(app)
    configure_errorhandlers(app)
    configure_migrations(app)
    configure_translations(app)
    app.pluggy.hook.flaskbb_additional_setup(app=app, pluggy=app.pluggy)

    return app

In configure_app, FlaskBB first loads the configuration information. Then, configure_extensions initializes the relevant Flask extensions used. configure_blueprints initializes the Blueprint, including user, forum, auth, and management. These four categories are mounted on different URLs, they are the four most important sections of the entire structure. We also have configure_errorhandlers, which deals with errors, configure_template_filters which adds filters to Jinja2 templates, and other modules.

After looking at the most important app.py, we can see that there are four folders, user, forum, auth, and management, all in the same directory. Let's see how these four major sections are constructed.

We open the models.py file in the user folder. This file defines the relationship between users and groups, etc. Here, we need to mention the concept of Object-Relational Mapping (ORM). Simply put, ORM refers to using a programming language to define the database.

class User(db.Model, UserMixin, CRUDMixin):
    __tablename__ = "users"

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(200), unique=True, nullable=False)
    email = db.Column(db.String(200), unique=True, nullable=False)
    _password = db.Column('password', db.String(120), nullable=False)

The definition of fields seems straightforward, but how do we define relationships? For example, the one-to-many relationship between users and topics. A user may start multiple topics. Under the User class, we can see the following code.

topics = db.relationship(
    "Topic",
    backref="user",
    primaryjoin="User.id == Topic.user_id",
    lazy="dynamic"
)

This definition will create the topics field in User and the user field in Topic. The condition for the join query is User.id == Topic.user_id. The code also defines the usual operations, such as deletion and saving.

The four major modules all have three sub-modules: models, views, and forms. The overall structure is very clear. Similar to most Flask projects, html templates are still placed under the templates folder, and js, css, images, etc. are placed under the static folder. The tests folder mentioned earlier stores modules related to automated testing.

4 Conclusion

Through the FlaskBB source code, we learned the structure of a mature, product-level project and the content of different modules. Overall, its code is very neat, standardized, and readable. Based on this, we can modify the content according to our needs to make it a truly useful forum product.


Viewed times

Comments