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)¶ -
slug
¶ Returns a slugified version from the category title
-
url
¶ Returns the slugified url for the category
-
delete
(users=None)¶ 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_all
(user)¶ 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, user)¶ 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)¶ -
slug
¶ Returns a slugified version from the forum title
-
url
¶ Returns the slugified url for the forum
-
last_post_url
¶ Returns the url for the last post in the forum
-
update_last_post
(commit=True)¶ Updates the last post in the forum.
-
update_read
(user, forumsread, topicsread)¶ 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=False)¶ 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=None)¶ 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.
-
delete
(users=None)¶ 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)¶ 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, user)¶ 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, user, page=1, per_page=20)¶ 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
-
-
class
flaskbb.forum.models.
Topic
(title=None, user=None)¶ -
second_last_post
¶ Returns the second last post or None.
-
slug
¶ Returns a slugified version of the topic title.
-
url
¶ Returns the slugified url for the topic.
-
first_unread
(topicsread, user, forumsread=None)¶ 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, topicsread)¶ 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 theTRACKER_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, forum, forumsread)¶ 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
()¶ Recalculates the post count in the topic.
-
move
(new_forum)¶ 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=None, forum=None, post=None)¶ 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
(users=None)¶ Deletes a topic with the corresponding posts. If a list with user objects is passed it will also update their post counts
Parameters: users – A list with user objects
-
hide
(user, users=None)¶ Soft deletes a topic from a forum
-
unhide
(users=None)¶ Restores a hidden topic to a forum
-
involved_users
()¶ Returns a query of all users involved in the topic
-
-
class
flaskbb.forum.models.
Post
(content=None, user=None, topic=None)¶ -
url
¶ Returns the url for the post.
-
save
(user=None, topic=None)¶ 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
()¶ Deletes a post and returns self.
-
-
class
flaskbb.forum.models.
TopicsRead
(**kwargs)¶
-
class
flaskbb.forum.models.
ForumsRead
(**kwargs)¶
User Models¶
The user modules contains all related models for the users.
-
class
flaskbb.user.models.
User
(**kwargs)¶ -
is_active
¶ Returns the state of the account. If the
ACTIVATE_ACCOUNT
option has been disabled, it will always returnTrue
. Is the option activated, it will, depending on the state of the account, either returnTrue
orFalse
.
-
last_post
¶ Returns the latest post from the user.
-
url
¶ Returns the url for the user.
-
permissions
¶ Returns the permissions for the user.
-
groups
¶ Returns the user groups.
-
days_registered
¶ Returns the amount of days the user is registered.
-
topic_count
¶ Returns the thread count.
-
posts_per_day
¶ Returns the posts per day count.
-
topics_per_day
¶ Returns the topics per day count.
-
password
¶ Returns the hashed password.
-
check_password
(password)¶ Check passwords. If passwords match it returns true, else false.
-
classmethod
authenticate
(login, password)¶ A classmethod for authenticating users. It returns the user object if the user/password combination is ok. If the user has entered too often a wrong password, he will be locked out of his account for a specified time.
Parameters: - login – This can be either a username or a email address.
- password – The password that is connected to username and email.
-
recalculate
()¶ Recalculates the post count from the user.
-
all_topics
(page, viewer)¶ Returns a paginated result with all topics the user has created.
Parameters: - page – The page which should be displayed.
- viewer – The user who is viewing this user. It will return a list with topics that the viewer has access to and thus it will not display all topics from the requested user.
-
all_posts
(page, viewer)¶ Returns a paginated result with all posts the user has created.
Parameters: - page – The page which should be displayed.
- viewer – The user who is viewing this user. It will return a list with posts that the viewer has access to and thus it will not display all posts from the requested user.
-
track_topic
(topic)¶ Tracks the specified topic.
Parameters: topic – The topic which should be added to the topic tracker.
-
untrack_topic
(topic)¶ Untracks the specified topic.
Parameters: topic – The topic which should be removed from the topic tracker.
-
is_tracking_topic
(topic)¶ Checks if the user is already tracking this topic.
Parameters: topic – The topic which should be checked.
-
add_to_group
(group)¶ 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)¶ 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)¶ Returns True if the user is in the specified group.
Parameters: group – The group which should be checked.
-
get_groups
(**kwargs)¶ Returns all the groups the user is in.
-
get_permissions
(**kwargs)¶ Returns a dictionary with all permissions the user has
-
invalidate_cache
()¶ Invalidates this objects cached metadata.
-
ban
()¶ Bans the user. Returns True upon success.
-
unban
()¶ Unbans the user. Returns True upon success.
-
save
(groups=None)¶ Saves a user. If a list with groups is provided, it will add those to the secondary groups from the user.
Parameters: groups – A list with groups that should be added to the secondary groups from user.
-
delete
()¶ Deletes the User.
-
Management Models¶
The management module contains all related models for the management of FlaskBB.
-
class
flaskbb.management.models.
SettingsGroup
(**kwargs)¶
-
class
flaskbb.management.models.
Setting
(**kwargs)¶ -
classmethod
get_form
(group)¶ Returns a Form for all settings found in
SettingsGroup
.Parameters: group – The settingsgroup name. It is used to get the settings which are in the specified group.
-
classmethod
update
(settings, app=None)¶ Updates the cache and stores the changes in the database.
Parameters: settings – A dictionary with setting items.
-
classmethod
get_settings
(from_group=None)¶ This will return all settings with the key as the key for the dict and the values are packed again in a dict which contains the remaining attributes.
Parameters: from_group – Optionally - Returns only the settings from a group.
-
classmethod
as_dict
(**kwargs)¶ Returns all settings as a dict. This method is cached. If you want to invalidate the cache, simply execute
self.invalidate_cache()
.Parameters: - from_group – Returns only the settings from the group as a dict.
- upper – If upper is
True
, the key will use upper-case letters. Defaults toFalse
.
-
classmethod
invalidate_cache
()¶ Invalidates this objects cached metadata.
-
classmethod