Models

FlaskBB uses SQLAlchemy as it’s ORM. The models are split in three modules which are covered below.

Forum Models

This module contains all related models for the forums.

The hierarchy looks like this: Category > Forum > Topic > Post. In the Report model are stored the reports and the TopicsRead and ForumsRead models are used to store the status if the user has read a specific forum or not.

class flaskbb.forum.models.Category(**kwargs)[source]
property slug

Returns a slugified version from the category title

property url

Returns the slugified url for the category

delete(users: list[User] | None = None)[source]

Deletes a category. If a list with involved user objects is passed, it will also update their post counts

Parameters:

users – A list with user objects

classmethod get_categories(user: User)[source]

Get all categories with all associated forums. It returns a list with tuples. Those tuples are containing the category and their associated forums (whose are stored in a list).

For example:

[(<Category 1>, [(<Forum 2>, <ForumsRead>), (<Forum 1>, None)]),
 (<Category 2>, [(<Forum 3>, None), (<Forum 4>, None)])]
Parameters:

user – The user object is needed to check if we also need their forumsread object.

classmethod get_forums(category_id: int, user: User)[source]

Get the forums for the category. It returns a tuple with the category and the forums with their forumsread object are stored in a list.

A return value can look like this for a category with two forums:

(<Category 1>, [(<Forum 1>, None), (<Forum 2>, None)])
Parameters:
  • category_id – The category id

  • user – The user object is needed to check if we also need their forumsread object.

class flaskbb.forum.models.Forum(**kwargs)[source]
property slug

Returns a slugified version from the forum title

property url

Returns the slugified url for the forum

property last_post_url

Returns the url for the last post in the forum

update_last_post(commit: bool = True)[source]

Updates the last post in the forum.

update_read(user: User, forumsread: ForumsRead | None, topicsread: TopicsRead | None)[source]

Updates the ForumsRead status for the user. In order to work correctly, be sure that topicsread is **not** `None.

Parameters:
  • user – The user for whom we should check if he has read the forum.

  • forumsread – The forumsread object. It is needed to check if if the forum is unread. If forumsread is None and the forum is unread, it will create a new entry in the ForumsRead relation, else (and the forum is still unread) we are just going to update the entry in the ForumsRead relation.

  • topicsread – The topicsread object is used in combination with the forumsread object to check if the forumsread relation should be updated and therefore is unread.

recalculate(last_post: bool = False)[source]

Recalculates the post_count and topic_count in the forum. Returns the forum with the recounted stats.

Parameters:

last_post – If set to True it will also try to update the last post columns in the forum.

save(groups: list[Group] | None = None)[source]

Saves a forum

Parameters:
  • moderators – If given, it will update the moderators in this forum with the given iterable of user objects.

  • groups – A list with group objects to restrict this (new) forum to. If omitted, the forum is made visible to every group.

delete(users: list[User] | None = None)[source]

Deletes forum. If a list with involved user objects is passed, it will also update their post counts

Parameters:

users – A list with user objects

move_topics_to(topics: list[Topic])[source]

Moves a bunch a topics to the forum. Returns True if all topics were moved successfully to the forum.

Parameters:

topics – A iterable with topic objects.

classmethod get_forum(forum_id: int, user: User)[source]

Returns the forum and forumsread object as a tuple for the user.

Parameters:
  • forum_id – The forum id

  • user – The user object is needed to check if we also need their forumsread object.

classmethod get_topics(forum_id: int, user: User, page: int = 1, per_page: int = 20, forumsread: ForumsRead | None = None)[source]

Get the topics for the forum. If the user is logged in, it will perform an outerjoin for the topics with the topicsread and forumsread relation to check if it is read or unread.

Parameters:
  • forum_id – The forum id

  • user – The user object

  • page – The page whom should be loaded

  • per_page – How many topics per page should be shown

  • forumsread – The forumsread object for the forum, used to determine unread state together with topicsread

classmethod get_accessible(user: User) → Sequence[Forum][source]

Returns the forums that user is allowed to see.

Parameters:

user – The user object.

class flaskbb.forum.models.Topic(title=None, user=None, content=None)[source]
property second_last_post

Returns the second last post id or None.

property slug

Returns a slugified version of the topic title.

property url

Returns the slugified url for the topic.

is_first_post(post: Post)[source]

Checks if the post is the first post in the topic.

Parameters:

post – The post object.

first_unread(topicsread: TopicsRead | None, user: User, forumsread: ForumsRead | None = None)[source]

Returns the url to the first unread post. If no unread posts exist it will return the url to the topic.

Parameters:
  • topicsread – The topicsread object for the topic

  • user – The user who should be checked if he has read the last post in the topic

  • forumsread – The forumsread object in which the topic is. If you also want to check if the user has marked the forum as read, than you will also need to pass an forumsread object.

tracker_needs_update(forumsread: ForumsRead | None, topicsread: TopicsRead | None)[source]

Returns True if the topicsread tracker needs an update. Also, if the TRACKER_LENGTH is configured, it will just recognize topics that are newer than the TRACKER_LENGTH (in days) as unread.

Parameters:
  • forumsread – The ForumsRead object is needed because we also need to check if the forum has been cleared sometime ago.

  • topicsread – The topicsread object is used to check if there is a new post in the topic.

update_read(user: User, forum: Forum, forumsread: ForumsRead | None)[source]

Updates the topicsread and forumsread tracker for a specified user, if the topic contains new posts or the user hasn’t read the topic. Returns True if the tracker has been updated.

Parameters:
  • user – The user for whom the readstracker should be updated.

  • forum – The forum in which the topic is.

  • forumsread – The forumsread object. It is used to check if there is a new post since the forum has been marked as read.

recalculate()[source]

Recalculates the post count in the topic.

move(new_forum: Forum)[source]

Moves a topic to the given forum. Returns True if it could successfully move the topic to forum.

Parameters:

new_forum – The new forum for the topic

save(user: User | None = None, forum: Forum | None = None, post: Post | None = None)[source]

Saves a topic and returns the topic object. If no parameters are given, it will only update the topic.

Parameters:
  • user – The user who has created the topic

  • forum – The forum where the topic is stored

  • post – The post object which is connected to the topic

delete()[source]

Deletes a topic with the corresponding posts.

hide(user: User)[source]

Soft deletes a topic from a forum

Parameters:

user – The user who hid the topic.

unhide()[source]

Restores a hidden topic to a forum

involved_users() → list[User][source]

Returns all users involved in the topic

hidden
hidden_at
hidden_by
hidden_by_id
class flaskbb.forum.models.Post(content=None, user=None, topic=None)[source]
property url

Returns the url for the post.

is_first_post()[source]

Checks whether this post is the first post in the topic or not.

save(user: User | None = None, topic: Topic | None = None)[source]

Saves a new post. If no parameters are passed we assume that you will just update an existing post. It returns the object after the operation was successful.

Parameters:
  • user – The user who has created the post

  • topic – The topic in which the post was created

delete()[source]

Deletes a post and returns self.

hidden
hidden_at
hidden_by
hidden_by_id
class flaskbb.forum.models.TopicsRead(**kwargs)[source]
class flaskbb.forum.models.ForumsRead(**kwargs)[source]
class flaskbb.forum.models.Report(**kwargs)[source]
save(post: Post | None = None, user: User | None = None)[source]

Saves a report.

Parameters:
  • post – The post that should be reported

  • user – The user who has reported the post

  • reason – The reason why the user has reported the post

User Models

The user modules contains all related models for the users.

class flaskbb.user.models.User(**kwargs)[source]
property is_active

Returns the state of the account. If the ACTIVATE_ACCOUNT option has been disabled, it will always return True. Is the option activated, it will, depending on the state of the account, either return True or False.

property last_post

Returns the latest post from the user.

property url

Returns the url for the user.

property days_registered: int

Returns the amount of days the user is registered.

property topic_count

Returns the thread count.

property posts_per_day

Returns the posts per day count.

property topics_per_day

Returns the topics per day count.

password

Returns the hashed password.

check_password(password: str)[source]

Check passwords. If passwords match it returns true, else false.

recalculate()[source]

Recalculates the post count from the user.

all_topics(page: int, viewer: User)[source]

Topics made by a given user, most recent first.

Parameters:
  • page – The page which should be displayed.

  • viewer – The user who is viewing the page. Only posts accessible to the viewer will be returned.

Return type:

flask_sqlalchemy.Pagination

all_posts(page: int, viewer: User)[source]

Posts made by a given user, most recent first.

Parameters:
  • page – The page which should be displayed.

  • viewer – The user who is viewing the page. Only posts accessible to the viewer will be returned.

Return type:

flask_sqlalchemy.Pagination

track_topic(topic: Topic)[source]

Tracks the specified topic.

Parameters:

topic – The topic which should be added to the topic tracker.

untrack_topic(topic: Topic)[source]

Untracks the specified topic.

Parameters:

topic – The topic which should be removed from the topic tracker.

is_tracking_topic(topic: Topic)[source]

Checks if the user is already tracking this topic.

Parameters:

topic – The topic which should be checked.

add_to_group(group: Group)[source]

Adds the user to the group if he isn’t in it.

Parameters:

group – The group which should be added to the user.

remove_from_group(group: Group)[source]

Removes the user from the group if he is in it.

Parameters:

group – The group which should be removed from the user.

in_group(group: Group)[source]

Returns True if the user is in the specified group.

Parameters:

group – The group which should be checked.

get_groups() → list[Group][source]

The groups the user acts with. A ban suspends every other group, whether the banned group is the primary or a secondary one.

get_memberships() → list[Group][source]

Every group the user belongs to, including those a ban suspends.

ban()[source]

Bans the user. Returns True upon success.

unban()[source]

Unbans the user. Returns True upon success.

save(groups: list[Group] | None = None) → User[source]

Saves a user. If groups are provided, they replace the user’s secondary groups, excluding the primary group.

Parameters:

groups – A list with groups that should be added to the secondary groups from user.

delete() → User[source]

Deletes the User.

class flaskbb.user.models.Group(**kwargs)[source]
property permission_levels: dict[str, PermissionLevel]

The level of every registered permission. One the group has not decided on shows as allow or deny after the permission’s default.

property permissions: dict[str, bool]

Whether the group on its own grants each registered permission.

save()[source]

Saves the object to the database.

delete()[source]

Delete the object from the database.

classmethod get_member_group() → Group[source]

Returns the first member group.

Settings Models

This module owns storage and caching for setting values. See the Settings chapter for how setting groups and definitions are registered.

class flaskbb.settings.models.Setting(key, value, group_key)[source]
static as_dict() → dict[str, Any][source]

Load and deserialize every setting value from the DB.

Core settings stay unprefixed (settings.PROJECT_TITLE), plugin settings are exposed prefixed with their group_key (settings.PORTAL_FORUM_IDS).

classmethod invalidate_cache()[source]

Invalidates this objects cached metadata.

classmethod update(group_key: str, settings: dict[str, Any], *, exclude: Iterable[str] = frozenset({'csrf_token'})) → None[source]

Save a settings group’s form data to the DB and invalidate the cache in one step.

Parameters:
  • group_key – the SettingGroup.key whose settings are being saved (e.g. “general”, “portal”). Required because (group_key, key) together identify a setting.

  • settings – dict of {setting_key: new_value}, typically form.data from the WTForm built via build_form(group)

  • exclude – keys to skip. Defaults to just “csrf_token” because forms usually contain this key.

classmethod diff_group(group_key: str) → SettingsDiff[source]

Compares a group’s currently registered SettingDefinitions against what actually has DB rows tagged with this group_key.

Returns:

SettingsDiff listing which definition keys have no DB row yet and which DB rows no longer match any definition .

classmethod prune_group(group_key: str) → None[source]

Removes settings from a group that are no longer used. For example, removed from the SettingDefinitions file.

classmethod install_group(group_key: str) → None[source]

Insert DB rows (using each definition’s default value) for any setting in this group that doesn’t have one yet.

Used when a plugin is installed for the first time, or when a new setting is added to an existing group via a fixture/plugin update.

classmethod remove_group(group_key: str) → None[source]

Delete every DB row belonging to a settings group and invalidate the cache in one step.

Used when uninstalling a plugin - deletes only rows tagged with this group_key, so other groups settings are untouched.

class flaskbb.settings.models.SettingsDiff(missing: tuple[str, ...], obsolete: tuple[str, ...])[source]

A settings diff of a group’s registered SettingDefinitions against what actually has rows in the DB. Used to check whether a plugin needs a settings upgrade before actually performing one.