Modules

Database

class modules.database.DB[source]

Bases: object

Manage 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]

classmethod unfollow(follower_id: ObjectId, following_id: ObjectId) bool[source]

Make a unfollow.

Parameters:
  • follower_id (ObjectId) – User that wants to unfollow.

  • following_id (ObjectId) – User that will be unfollowed.

Returns:

True if the operation was successful.

Return type:

bool

Posts

class modules.posts.Comment(data: Dict[str, Any] | None = None)[source]

Bases: object

Initialize 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]

json() Dict[str, Any][source]

Parse the comment to JSON format.

Returns:

Comment data in JSON.

Return type:

Dict[str, Any]

class modules.posts.Post(data: Dict[str, Any] | None = None)[source]

Bases: object

Initialize 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]

json() Dict[str, Any][source]

Parse the post to JSON format.

Returns:

Post data in JSON.

Return type:

Dict[str, Any]

lite() Dict[str, Any][source]

Make a lite post version, only returns: - ID. - Name. - Location. - Rating. - ImageUrl.

Returns:

The lite post.

Return type:

Dict[str, Any]

class modules.posts.Posts[source]

Bases: object

It 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:

Post

classmethod delete_comment(post: Post, comment: Comment) Comment[source]

Delete a comment.

Parameters:
  • post (Post) – Post where is the comment.

  • comment (Comment) – Comment to delete.

Raises:

RuntimeError – Error deleting the comment.

Returns:

Deleted comment data.

Return type:

Comment

classmethod delete_post(post: Post) Post[source]

Delete a post

Parameters:

post (Post) – The post to delete.

Raises:

RuntimeError – Error deleting the post.

Returns:

Deleted post data.

Return type:

Post

classmethod edit_post(post: Post, new_data: Dict[str, Any] = {}) Post[source]

Edit a post.

Parameters:
  • post (Post) – The post to edit.

  • new_data (Dict[str, Any], optional) – New data. Defaults to {}.

Raises:

RuntimeError – Error editing the post.

Returns:

Edited post if was edited.

Return type:

Post

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:

Comment

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:

Post

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]]

classmethod new_comment(post: Post, content: str, rating: float, creator: User_lite) Comment[source]

Create a new comment.

Parameters:
  • post (Post) – Post where will be the comment.

  • content (str) – Comment’s content.

  • rating (float) – Comment’s rating.

  • creator (User_lite) – Comment’s creator.

Raises:

RuntimeError – Error creating the comment

Returns:

New comment data if was added.

Return type:

Comment

Users

class modules.users.User(data: Dict[str, Any] | None = None)[source]

Bases: User_lite

Initialize 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]

json() Dict[str, Any][source]

Parse the user to JSON format.

Returns:

User data in JSON.

Return type:

Dict[str, Any]

lite() User_lite[source]

Make a lite user’s version.

Raises:

RuntimeError – Error liting de user.

Returns:

The lite user’s version.

Return type:

User_lite

class modules.users.User_lite(data: Dict[str, Any] | None = None)[source]

Bases: object

Initialize a lite user’s version.

json() Dict[str, Any][source]

Parse the user to JSON format.

Returns:

User data in JSON.

Return type:

Dict[str, Any]

token() str[source]

Creates a JWT with the user data.

Raises:

RuntimeError – Error creating the token.

Returns:

The JWT created.

Return type:

str

class modules.users.Users[source]

Bases: object

Manage 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:

User

classmethod follow(follower: User_lite, following: User_lite) bool[source]

Make a follow.

Parameters:
  • follower (User_lite) – User that wants to follow.

  • following (User_lite) – User that will be followed.

Raises:

RuntimeError – Error following.

Returns:

True if the operation was successfull.

Return type:

bool

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]

classmethod unfollow(follower: User_lite, following: User_lite) bool[source]

Make a unfollow.

Parameters:
  • follower (User_lite) – User that wants to unfollow.

  • following (User_lite) – User that will be unfollowed.

Raises:

RuntimeError – Error unfollowing.

Returns:

True if the operation was successfull.

Return type:

bool

Module contents