liveBook · Manning

Comment

As explained in The Well-Grounded Python Developer: How the pros use Python and Flask

A comment is a user-authored response associated with a blog post, other content, or another comment in a threaded discussion.

A comment is a user-authored response attached to a blog post or to another comment. Replies turn a post into a small discussion tree: the post is the root, and each comment can have children of its own. Registered users can add comments, while administrators can mark comments active or inactive. section 11.7 section 6.0

A comment resembles a content post in storage: both have an author, text, and creation and update timestamps. Comments lack titles and display as plain text rather than rendered Markdown. Keeping posts and comments in one self-referential Post table avoids separate tables for each reply level; a comment’s parent identifies what it answers. section 11.7

Figure 11.6 The self-referential Post table creates a hierarchal content/comment structure.

The self-referential Post-table diagram shows how content and its replies form a hierarchy without a separate table for every comment level. section 11.7

Creating and displaying a comment are two sides of the same parent-child relationship:

  • Create: validate the submitted comment form, save a Post whose parent_uid points to the post or comment being answered, then redirect to the root post. section 11.8 section 11.8.3
  • Retrieve: start at the root post and use a recursive common table expression to obtain its comment hierarchy. section 11.7.2
  • Render: iterate through the returned comments, indenting replies by their place in the hierarchy. Bootstrap modals keep create and edit forms on the post page. section 11.7.3

A post with replies

One content post can have two direct comments. If the first has two replies and the second has one, all five comments still belong to the same discussion tree. The example interface limits nesting to two comment levels, but the self-referential storage design does not require a new table for each level. section 11.7

How The Well-Grounded Python Developer: How the pros use Python and Flask covers it

How deep it goes
Works through storage, recursive retrieval, rendering, and creation; limits the example interface to two reply levels.
Examples
Uses a two-level post-and-reply tree and a form handler that saves a child post before redirecting to its root.
What it assumes
Assumes an existing application with a database, users, roles, and Flask Blueprints; explains recursive CTEs for hierarchy retrieval.
What you can do after
Apply it via SQLAlchemy and Flask to retrieve threaded comments and save new replies beneath their parent posts.
How it connects
Connects comments to self-referential tables, parent-child relationships, recursive CTEs, and template rendering.

Also explained in

Compare how 2 books explain comment →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Python Developer: How the pros use Python and Flask. The section links point to the parts of the book it draws on.