Modules
Database
- class modules.database.DB[source]
Bases:
objectManage the app database.
- Parameters:
MONGO_URI (str) – Uniform Resource Identifier.
__client (MongoClient) – The MongoDB client.
__db (Database) – The app database.
__users_collection – The collections where are all the users.
__posts_collection – The collections where are all the posts.
- MONGO_URI: str = 'mongodb://admin:password@mongodb:27017/mi_base_de_datos?authSource=admin'
- classmethod add_comment(post_id: ObjectId, comment: Dict[str, Any]) bool[source]
Add a comment in the post.
- Parameters:
post_id (ObjectId) – The post’s ID which the comment will be added.
comment (Dict[str, Any]) – The comment data.
- Raises:
RuntimeError – Error adding the comment.
- Returns:
True if was added.
- Return type:
bool
- classmethod add_post(post: Dict[str, Any], user_id: ObjectId) bool[source]
Add a post in the database.
- Parameters:
post (Dict[str, Any]) – Post info.
user_id (ObjectId) – Post Creator ID.
- Raises:
RuntimeError – Error adding the post.
- Returns:
True if the post was added.
- Return type:
bool
- classmethod add_user(user: Dict[str, Any]) bool[source]
Add a user in the database.
- Parameters:
user (Dict[str, Any]) – User info.
- Raises:
RuntimeError – Error adding the user.
- Returns:
True if the user was added.
- Return type:
bool
- classmethod delete_comment(post_id: ObjectId, comment_id: ObjectId) bool[source]
Delete a comment.
- Parameters:
post_id (ObjectId) – The post’s ID which the comment will be deleted.
comment_id (ObjectId) – The comment’s ID that will be deleted.
- Raises:
RuntimeError – Error deleting the comment.
- Returns:
True if was deteled.
- Return type:
bool
- classmethod delete_post(post_id: ObjectId, user_id: ObjectId) bool[source]
Delete a post in the database.
- Parameters:
post_id (ObjectId) – Post’s ID.
user_id (ObjectId) – Post creator ID.
- Raises:
RuntimeError – Error deleting the post.
- Returns:
True if the post was deleted.
- Return type:
bool
- classmethod edit_post(post_id: ObjectId, data: Dict[str, Any]) bool[source]
Edit a post in the database.
- Parameters:
post_id (ObjectId) – Post’s ID.
data (Dict[str, Any]) – New data.
- Raises:
RuntimeError – Error editing the post.
- Returns:
True if the post was edited.
- Return type:
bool
- classmethod follow(follower_id: ObjectId, following_id: ObjectId) bool[source]
Make a follow.
- Parameters:
follower_id (ObjectId) – User that wants to follow.
following_id (ObjectId) – User that will be followed.
- Returns:
True if the operation was successful.
- Return type:
bool
- classmethod get_comment(post_id: ObjectId, comment_id: ObjectId) Dict[str, Any][source]
Fetch one comment in the post.
- Parameters:
post_id (ObjectId) – The post’s ID which the comment will be fetched.
comment_id (ObjectId) – The comment’s ID.
- Raises:
RuntimeError – Error fetching a comment.
- Returns:
The comment or {} if not found.
- Return type:
Dict[str, Any]
- classmethod get_post(value: Any, field: str = '_id') Dict[str, Any][source]
Fetch an specific post.
- Parameters:
value (Any) – Fetch an specific post.
field (str, optional) – Field to search. Defaults to “_id”.
- Raises:
RuntimeError – Error fetching the post.
- Returns:
Post if is found, else {}.
- Return type:
Dict[str, Any]
- classmethod get_posts() List[Dict[str, Any]][source]
Fetch all the posts in the database.
- Raises:
RuntimeError – Error fetching the posts.
- Returns:
All posts.
- Return type:
List[Dict[str, Any]]
- classmethod get_user(value: Any, field: str = '_id') Dict[str, Any][source]
Fetch a user.
- Parameters:
value (Any) – The value of the field to search.
field (str, optional) – The field to search. Defaults to “_id”.
- Raises:
RuntimeError – Error fetching the user.
- Returns:
The user if is founded, else {}.
- Return type:
Dict[str, Any]
Posts
- class modules.posts.Comment(data: Dict[str, Any] | None = None)[source]
Bases:
objectInitialize a comment.
- filter(request_user: ~modules.users.User_lite = <modules.users.User_lite object>) Dict[str, Any][source]
Filter a comment with this params: - Changes “_id” for “ID”. - Only returns the creator username. - If the request user is the creator, returns “editable” as True, else False.
- Parameters:
request_user (User_lite, optional) – The user that fetch the comment. Defaults to User_lite().
- Raises:
RuntimeError – Error filtering the comment.
- Returns:
The comment filtered.
- Return type:
Dict[str, Any]
- class modules.posts.Post(data: Dict[str, Any] | None = None)[source]
Bases:
objectInitialize a post.
- edit(new_info: Dict[str, Any] = {}) Dict[str, Any][source]
Edit a post, only in the valid fields: “name”, “location”, “review”, “rating”, “imageUrl”.
- Parameters:
new_info (Dict[str, Any], optional) – New post’s data. Defaults to {}.
- Raises:
RuntimeError – Error editting the post.
- Returns:
The edited post.
- Return type:
Dict[str, Any]
- filter(request_user: ~modules.users.User_lite = <modules.users.User_lite object>) Dict[str, Any][source]
Filter a post with this params: - Changes “_id” for “ID”. - Only returns the creator username. - If the request user is the creator, returns “editable” as True, else False. - Filter every comment in the comments.
- Parameters:
request_user (User_lite, optional) – The user that fetch the post. Defaults to User_lite().
- Raises:
RuntimeError – Error filtering the post.
- Returns:
The post filtered.
- Return type:
Dict[str, Any]
- class modules.posts.Posts[source]
Bases:
objectIt manages the posts an comments.
- classmethod create_post(name: str, location: str, review: str, rating: float, imageUrl: str, creator: User_lite) Post[source]
Create a new post.
- Parameters:
name (str) – Post’s name.
location (str) – Post’s location.
review (str) – Post’s review.
rating (float) – Post’s rating.
imageUrl (str) – Post’s image url.
creator (User_lite) – Post’s creator.
- Raises:
RuntimeError – Error creating the post.
- Returns:
New post data if was created.
- Return type:
- classmethod get_comment(post_id: ObjectId, comment_id: ObjectId) Comment[source]
Fetch one comment in the post.
- Parameters:
post_id (ObjectId) – Post’ID where is the comment.
comment_id (ObjectId) – Comment’s ID to fetch.
- Raises:
RuntimeError – Error fetching a comment.
- Returns:
The comment fetched if it exist.
- Return type:
- classmethod get_post(value: Any, field: str = '_id') Post[source]
Fetch an specific post.
- Parameters:
value (Any) – Fetch an specific post.
field (str, optional) – Field to search. Defaults to “_id”.
- Raises:
RuntimeError – Error fetching the post.
- Returns:
The post founded.
- Return type:
- classmethod get_posts() List[Dict[str, Any]][source]
Fetch all the posts in the database.
- Raises:
RuntimeError – Error fetching the posts.
- Returns:
All posts.
- Return type:
List[Dict[str, Any]]
- classmethod get_user_posts(user_posts: list = []) List[Dict[str, Any]][source]
Fetch all the posts for one user.
- Parameters:
user_posts (list, optional) – The ID of every post to fetch. Defaults to [].
- Raises:
RuntimeError – Error fetching the user posts.
- Returns:
All user’s post or [] if is empty.
- Return type:
List[Dict[str, Any]]
Users
- class modules.users.User(data: Dict[str, Any] | None = None)[source]
Bases:
User_liteInitialize an user.
- filter(request_user: ~modules.users.User_lite = <modules.users.User_lite object>) Dict[str, Any][source]
Filter a user with this params: - Returns the “username”. - Returns the posts. - Returns the count of followers. - Returns “isFollowing” as True of False, if is self don’t have this key.
- Parameters:
request_user (User_lite, optional) – The user that fetch the post. Defaults to User_lite().
- Raises:
RuntimeError – Error filtering the user.
- Returns:
The user filtered.
- Return type:
Dict[str, Any]
- class modules.users.User_lite(data: Dict[str, Any] | None = None)[source]
Bases:
objectInitialize a lite user’s version.
- class modules.users.Users[source]
Bases:
objectManage all users in the app.
- classmethod create_user(username: str, email: str, password: str) User[source]
Creates a new user.
- Parameters:
username (str) – The username.
email (str) – The email.
password (str) – The password
- Raises:
RuntimeError – Error creating the user.
- Returns:
The new user if was created.
- Return type:
- classmethod get_user(value: Any, field: str = '_id') User[source]
Fetch a user.
- Parameters:
value (Any) – The value of the field to search.
field (str, optional) – The field to search. Defaults to “_id”.
- Raises:
RuntimeError – Error fetching the user.
- Returns:
The user.
- Return type:
Dict[str, Any]