Welcome to PhotoBank by Drujba’s documentation!¶
PhotoBank main¶
- async main.deny_get_redis()¶
The deny_get_redis function.
- Returns:
None
- async main.get_limit()¶
The get_limit function.
- Returns:
None
- main.lifespan(app: FastAPI)¶
The lifespan function is a context manager that runs during the lifespan of the application.
- Parameters:
app – FastAPI: Pass the FastAPI app
- Returns:
A lifespan function
- main.open_browser()¶
The open_browser function opens the web browser in a separate thread.
- Returns:
None
- async main.startup()¶
The startup function is a coroutine that runs during the startup of the application.
- Returns:
A coroutine
PhotoBank repository Comments¶
- async src.repository.comments.create_comment(image_id: int, body: CommentSchema, db: AsyncSession, current_user: User) Comment | None ¶
The create_comment function creates a new comment for an image.
- Parameters:
image_id – int: Specify the image that the comment is being created for
body – CommentSchema: Validate the request body
db – AsyncSession: Pass the database connection to the function
current_user – User: Get the user_id of the current user
- Returns:
A comment object
- async src.repository.comments.delete_comment(comment_id: int, db: AsyncSession, current_user: User)¶
The delete_comment function deletes a comment from the database.
- Parameters:
comment_id – int: Identify the comment to be deleted
db – AsyncSession: Access the database
current_user – User: Get the current user’s id
- Returns:
The deleted comment
- async src.repository.comments.edit_comment(comment_id: int, body: CommentSchema, db: AsyncSession, current_user: User) Comment | None ¶
- The edit_comment function allows a user to edit their own comment.
The function takes in the comment_id, body, db and current_user as parameters. It then checks if the user is authorized to edit this particular comment by checking if they are either an admin or moderator or if they are the owner of that specific comment. If not it raises a 403 error with a message saying permission denied. If so it updates the database with new information from body and returns that updated information.
- Parameters:
comment_id – int: Get the comment from the database
body – CommentSchema: Validate the request body
db – AsyncSession: Access the database
current_user – User: Get the current user’s id
- Returns:
The edited comment
- async src.repository.comments.get_comment(comment_id: int, db: AsyncSession)¶
The get_comment function returns a comment object from the database.
- Parameters:
comment_id – int: Specify the id of the comment to be retrieved
db – AsyncSession: Pass the database session to the function
- Returns:
A comment object
- async src.repository.comments.get_comments_by_image(image_id: int, db: AsyncSession)¶
The get_comments_by_image function takes in an image_id and a database connection, and returns all comments associated with that image.
- Parameters:
image_id – int: Specify the image_id of the comments you want to retrieve
db – AsyncSession: Pass in the database connection
- Returns:
A list of comments for a given image
PhotoBank repository Images¶
- async src.repository.images.create_image(body: ImageSchema, base_url: str, db: AsyncSession, current_user: User)¶
The create_image function creates an image in the database.
- Parameters:
body – ImageSchema: Get the data from the request body
base_url – str: Pass the base url of the image
db – AsyncSession: Pass the database session
current_user – User: Get the current user
- Returns:
A created image object
- async src.repository.images.delete_image(image_id: int, db: AsyncSession, current_user: User)¶
The delete_image function deletes an image from the database.
- Parameters:
image_id – int: Specify the image you want to delete
db – AsyncSession: Pass the database session
current_user – User: Get the current user
- Returns:
The deleted image
- async src.repository.images.get_all_images(db: AsyncSession)¶
The get_all_images function returns all images from the database.
- Parameters:
db – AsyncSession: Pass the database session
- Returns:
A list of all images in the database
- async src.repository.images.get_base_url(image_id: int, db: AsyncSession, current_user: User)¶
Get base url of image
- Parameters:
image_id – int: Specify the image you want to get the base url
db – AsyncSession: Pass the database session
current_user – User: Get the current user
- Returns:
The base url of the image
- async src.repository.images.get_image(image_id: int, db: AsyncSession, mode=1)¶
The get_image function takes in an image_id and a database session, and returns an image object if it exists.
- Parameters:
image_id – int: Specify the image you want to retrieve
db – AsyncSession: Pass the database session
- Returns:
An image with the owner of a dictionary
- async src.repository.images.get_transform_url(image_id: int, db: AsyncSession, current_user: User)¶
The get_transform_url function returns the transform url of an image.
- Parameters:
image_id – int: Specify the image you want to get the transform url for
db – AsyncSession: Pass the database session
current_user – User: Get the current user
- Returns:
The transform url of the image
- async src.repository.images.qr_code(image_id: int, qr_url: str, db: AsyncSession, current_user: User)¶
The qr_code function generates a QR code for an image.
- Parameters:
image_id – int: Specify the image you want to generate the QR code for
qr_url – str: Specify the url of the QR code
db – AsyncSession: Pass the database session
current_user – User: Get the current user
- Returns:
An image with the qr_url of the image
- async src.repository.images.search_images(keyword: str, db: AsyncSession)¶
The search_images function searches for images in the database.
- Parameters:
keyword – str: Search for images with a specific keyword
db – AsyncSession: Pass the database session to the function
- Returns:
A list of image objects
- async src.repository.images.transform_image(image_id: int, tr_url: str, db: AsyncSession, current_user: User)¶
The transform_image function transforms an image.
- Parameters:
image_id – int: Specify the image you want to transform
tr_url – str: Specify the transformed url
db – AsyncSession: Pass the database session
current_user – User: Get the current user
- Returns:
The transformed image
- async src.repository.images.update_image(image_id: int, body: ImageSchema, db: AsyncSession, current_user: User)¶
The update_image function updates an image in the database.
- Parameters:
image_id – int: Specify the image you want to update
body – ImageSchema: Pass the new image data
db – AsyncSession: Pass the database session
current_user – User: Get the current user
- Returns:
The updated image
PhotoBank repository Likes¶
- async src.repository.likes.add_grade_to_image(image_id: int, body: LikeSchema, db: AsyncSession, current_user: User) ImageLike ¶
Add grade to image if it exists.
- Parameters:
image_id – image id
body – like schema
db – database session
current_user – current user
- Returns:
image like
- async src.repository.likes.get_current_rating(image_id: int, db: AsyncSession) float ¶
Get current rating.
- Parameters:
image_id – image id
db – database session
- Returns:
current rating
PhotoBank repository Users¶
- async src.repository.users.confirmed_email(email: str, db: AsyncSession) None ¶
Confirmed email.
- Parameters:
email – str
db – AsyncSession
- Returns:
None
- async src.repository.users.create_user(body: UserSchema, db: AsyncSession = Depends(get_db))¶
Create new user.
- Parameters:
body – UserSchema
db – AsyncSession
- Returns:
User
- async src.repository.users.get_admin_exist(db: AsyncSession = Depends(get_db))¶
Check if admin exists.
- Parameters:
db – AsyncSession
- Returns:
True if admin exists, False otherwise
- async src.repository.users.get_user_by_email(email: str, db: AsyncSession = Depends(get_db))¶
Get user by email.
- Parameters:
email – str
db – AsyncSession
- Returns:
User
- async src.repository.users.update_token(user: User, token: str | None, db: AsyncSession)¶
Update user token.
- Parameters:
user – User
token – str
db – AsyncSession
- Returns:
None
PhotoBank routes Auth¶
- async src.routes.auth.confirmed_email(token: str, db: AsyncSession = Depends(get_db))¶
- The confirmed_email function is used to confirm a user’s email address.
It takes in the token that was sent to the user’s email and uses it to get their email address. Then, it gets the user from our database using their email address and checks if they exist. If not, an error is thrown. Next, we check if they have already confirmed their account or not by checking if confirmed == True for them in our database (if so, we return a message saying as much). Finally, we update our database with this information.
- Parameters:
token – str: Get the token from the url
db – AsyncSession: Pass the database session to the function
- Returns:
A dict with the message
- async src.routes.auth.login(body: OAuth2PasswordRequestForm = Depends(NoneType), db: AsyncSession = Depends(get_db))¶
The login function is used to authenticate a user.
- Parameters:
body – OAuth2PasswordRequestForm: Get the username and password from the request body
db – AsyncSession: Pass the database session to the function
- Returns:
A dictionary with access_token, refresh_token and token_type
- async src.routes.auth.logout(user: User = Depends(get_current_user), db: AsyncSession = Depends(get_db))¶
The logout function is used to logout a user.
- Parameters:
user – User: Get the current user
db – AsyncSession: Get the database session
:param : Get the current user from the database and then update their token to an empty string :return: A 204 response
- async src.routes.auth.refresh_token(credentials: HTTPAuthorizationCredentials = Depends(HTTPBearer), db: AsyncSession = Depends(get_db))¶
Refresh token.
- Parameters:
credentials – HTTPAuthorizationCredentials
db – AsyncSession
- Returns:
Token
- async src.routes.auth.request_email(body: RequestEmail, background_tasks: BackgroundTasks, request: Request, db: AsyncSession = Depends(get_db))¶
Request email.
- Parameters:
body – RequestEmail
background_tasks – BackgroundTasks
request – Request
db – AsyncSession
- Returns:
None
- async src.routes.auth.signup(body: UserSchema, bt: BackgroundTasks, request: Request, db: AsyncSession = Depends(get_db))¶
The signup function creates a new user in the database.
- Parameters:
body – UserSchema: Validate the request body, and to deserialize it into a userschema object
bt – BackgroundTasks: Add a task to the background tasks queue
request – Request: Get the base_url of the request
db – AsyncSession: Get the database session
- Returns:
The new user, but the send_email function is asynchronous
PhotoBank routes Comments¶
- async src.routes.comments.create_comment_route(image_id: int, body: CommentSchema, db: AsyncSession = Depends(get_db), current_user: User = Depends(get_current_user))¶
The create_comment_route function creates a comment for an image.
- Parameters:
image_id – int: Get the image id from the url
body – CommentSchema: Validate the request body
db – AsyncSession: Pass the database session to the function
current_user – User: Get the user who is currently logged in
:param : Get the image id from the url
- Returns:
A comment object
- async src.routes.comments.delete_comment_route(comment_id: int, db: AsyncSession = Depends(get_db), current_user: User = Depends(get_current_user))¶
The delete_comment_route function deletes a comment from the database.
- Parameters:
comment_id – int: Get the comment_id from the url
db – AsyncSession: Pass the database session to the function
current_user – User: Get the current user from the auth_service
:param : Get the id of the comment to be deleted
- Returns:
The deleted comment
- async src.routes.comments.edit_comment_route(comment_id: int, body: CommentSchema, db: AsyncSession = Depends(get_db), current_user: User = Depends(get_current_user))¶
The edit_comment_route function allows a user to edit an existing comment.
- Parameters:
comment_id – int: Get the comment id from the url
body – CommentSchema: Validate the data in the body of a request
db – AsyncSession: Get the database session
current_user – User: Get the current user
:param : Get the comment_id from the url
- Returns:
A comment object
- async src.routes.comments.get_comment_route(comment_id: int, db: AsyncSession = Depends(get_db))¶
The get_comment_route function is a route that returns the comment with the given id.
- Parameters:
comment_id – int: Get the comment id from the url path
db – AsyncSession: Get a database session, which is used when executing sqlalchemy commands
:param : Get the comment id from the url
- Returns:
The comment object
PhotoBank routes Frontend¶
- async src.routes.frontend.home(request: Request)¶
Route to render the home page. This route renders the index.html template as the home page.
- Params:
request: the request object
- Returns:
HTMLResponse: the rendered HTML response
PhotoBank routes Healthchecker¶
- async src.routes.healthchecker_db.healthchecker(db: AsyncSession = Depends(get_db))¶
The healthchecker function is a simple function that checks the health of the database. It does this by executing a SQL query and checking if it returns any results. If it doesn’t, then we know something is wrong with our database connection.
- Parameters:
db – AsyncSession: Inject the database session into the function
- Returns:
A dictionary with a message
PhotoBank routes Images¶
- async src.routes.images.create_qr_code(image_id: int = Path(PydanticUndefined), db: AsyncSession = Depends(get_db), current_user: User = Depends(get_current_user))¶
The create_qr_code function creates a qr code for an image.
- Parameters:
image_id – int: Specify the image id
db – AsyncSession: Pass the database session
current_user – User: Get the current user
- Returns:
An image JSON response with qr code
- async src.routes.images.create_transform_image(image_id: int = Path(PydanticUndefined), db: AsyncSession = Depends(get_db), current_user: User = Depends(get_current_user))¶
The create_transform_image function transforms an image to watermark.
- Parameters:
image_id – int: Specify the image id
db – AsyncSession: Pass the database session
current_user – User: Get the current user
- Returns:
An image JSON response with transformed url
- async src.routes.images.delete_image(image_id: int = Path(PydanticUndefined), db: AsyncSession = Depends(get_db), current_user: User = Depends(get_current_user))¶
The delete_image function deletes an image from the database.
- Parameters:
image_id – int: Specify the image id
db – AsyncSession: Pass the database session
current_user – User: Get the current user
- Returns:
An image JSON response which was deleted
- async src.routes.images.get_all_images(page: int | None = Query(None), per_page: int | None = Query(None), db: AsyncSession = Depends(get_db))¶
The get_all_images function returns a list of all images in the database.
- Parameters:
page – Optional[int]: Specify the page number
per_page – Optional[int]: Limit the number of images returned
db – AsyncSession: Pass the database session
- Returns:
A list of images
- async src.routes.images.get_image(image_id: int = Path(PydanticUndefined), db: AsyncSession = Depends(get_db))¶
The get_image function returns the image with the specified id.
- Parameters:
image_id – int: Get the image id from the url
db – AsyncSession: Pass the database session
- Returns:
A single image object
- async src.routes.images.load_image(body: ImageSchema = Depends(NoneType), file: UploadFile = File(PydanticUndefined), db: AsyncSession = Depends(get_db), current_user: User = Depends(get_current_user))¶
The load_image function creates a new image in the database.
- Parameters:
body – ImageSchema: Get the data from the request body
file – UploadFile: Upload the image
db – AsyncSession: Pass the database session
current_user – User: Get the current user
- Returns:
A JSON response with the image data
- async src.routes.images.search_images(keyword: str, db: AsyncSession = Depends(get_db))¶
The search_images function searches for images in the database.
- Parameters:
keyword – str: Search for images in the database
db – AsyncSession: Pass the database session
- Returns:
A list of images
- async src.routes.images.update_image(image_id: int = Path(PydanticUndefined), body: ImageSchema = Depends(NoneType), db: AsyncSession = Depends(get_db), current_user: User = Depends(get_current_user))¶
The update_image function updates an image in the database.
- Parameters:
image_id – int: Specify the image id
body – ImageSchema: Get the data from the request body
db – AsyncSession: Pass the database session
current_user – User: Get the current user
- Returns:
An image JSON response
PhotoBank routes Likes¶
- async src.routes.likes.add_grade_to_image_route(image_id: int, body: LikeSchema, db: AsyncSession = Depends(get_db), current_user: User = Depends(get_current_user))¶
Add grade to image.
- Parameters:
image_id – id of image
body – schema with grade
db – database session
current_user – current user
- Returns:
grade of image
- async src.routes.likes.get_image_rating(image_id: int, db: AsyncSession = Depends(get_db))¶
Get rating of image grade
- Parameters:
image_id – id of image
db – database session
- Returns:
rating of image in JSON format
PhotoBank services Auth¶
- class src.services.auth.Auth¶
Bases:
object
- ALGORITHM = 'HS256'¶
- SECRET_KEY = 'secret_key'¶
- cache = Redis<ConnectionPool<Connection<host=redis-14476.c293.eu-central-1-1.ec2.redns.redis-cloud.com,port=14476,db=0>>>¶
- async create_access_token(data: dict, expires_delta: float | None = None)¶
- The create_access_token function creates a new access token.
- Args:
data (dict): The data to be encoded in the JWT. expires_delta (Optional[float]): A timedelta object representing how long the token should last for. Defaults to 15 minutes if not specified.
- Parameters:
self – Represent the instance of the class
data – dict: Pass the data that will be encoded into the token
expires_delta – Optional[float]: Set the expiration time for the access token
- Returns:
A token that is encoded with the data you pass to it
- create_email_token(data: dict)¶
Create email token.
- Parameters:
data – data
- Returns:
email token
- async create_refresh_token(data: dict, expires_delta: float | None = None)¶
Create refresh token.
- Parameters:
data – data
expires_delta – expiration time
- Returns:
refresh token
- async decode_refresh_token(refresh_token: str)¶
- The decode_refresh_token function is used to decode the refresh token.
The function will raise an HTTPException if the token is invalid or has expired. If the token is valid, it will return a string with the email address of user who owns that refresh_token.
- Parameters:
self – Represent the instance of a class
refresh_token – str: Pass in the refresh token that is sent to the server
- Returns:
The email address of the user who requested a new access token
- async get_current_user(token: str = Depends(OAuth2PasswordBearer), db: AsyncSession = Depends(get_db))¶
- The get_current_user function is a dependency that will be used in the
protected endpoints. It takes a token and returns the user object if it’s valid, otherwise raises an HTTPException with status code 401 (Unauthorized).
- Parameters:
self – Denote that the get_current_user function is a member of the user class
token – str: Get the token from the request header
db – AsyncSession: Get the database session
- Returns:
A user object, which is used in the following function:
- async get_email_from_token(token: str)¶
The get_email_from_token function takes a token as an argument and returns the email address associated with that token. The function uses the jwt library to decode the token, which is then used to retrieve the email address from within it.
- Parameters:
self – Represent the instance of the class
token – str: Pass the token from the user to this function
- Returns:
The email associated with the token
- get_password_hash(password: str)¶
- The get_password_hash function takes a password as input and returns the hash of that password.
The function uses the pwd_context object to generate a hash from the given password.
- Parameters:
self – Represent the instance of the class
password – str: Pass in the password that you want to hash
- Returns:
A string that is the hashed version of the password
- oauth2_scheme = <fastapi.security.oauth2.OAuth2PasswordBearer object>¶
- pwd_context = <CryptContext>¶
- verify_password(plain_password, hashed_password)¶
- The verify_password function takes a plain-text password and the hashed version of that password,
and returns True if they match, False otherwise. This is used to verify that the user’s login credentials are correct.
- Parameters:
self – Represent the instance of the class
plain_password – Store the password that is entered by the user
hashed_password – Compare the hashed password in the database with the plain text password entered by user
- Returns:
A boolean value
PhotoBank services Email¶
- async src.services.email.send_email(email: EmailStr, nickname: str, host: str)¶
Sends verification email to user.
- Parameters:
email – user email
nickname – user nickname
host – server host
- Returns:
None