Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add types to functions and improve docstring in proofs/utils.py #540

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions open_prices/proofs/utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import random
import string
from mimetypes import guess_extension
from pathlib import Path

from django.conf import settings
from django.core.files.uploadedfile import InMemoryUploadedFile, TemporaryUploadedFile
from PIL import Image, ImageOps


def get_file_extension_and_mimetype(file) -> tuple[str, str]:
def get_file_extension_and_mimetype(
file: InMemoryUploadedFile | TemporaryUploadedFile,
) -> tuple[str, str]:
"""Get the extension and mimetype of the file.

Defaults to '.bin', 'application/octet-stream'.
Expand All @@ -28,15 +32,17 @@ def get_file_extension_and_mimetype(file) -> tuple[str, str]:
return extension, mimetype


def generate_full_path(current_dir, file_stem, extension):
def generate_full_path(current_dir: Path, file_stem: str, extension: str) -> Path:
"""
Generate the full path of the file.
Example: /path/to/img/0001/dWQ5Hjm1H6.png
"""
return current_dir / f"{file_stem}{extension}"


def generate_relative_path(current_dir_id_str, file_stem, extension):
def generate_relative_path(
current_dir_id_str: str, file_stem: str, extension: str
) -> str:
"""
Generate the relative path of the file.
Example: 0001/dWQ5Hjm1H6.png
Expand All @@ -45,13 +51,13 @@ def generate_relative_path(current_dir_id_str, file_stem, extension):


def generate_thumbnail(
current_dir,
current_dir_id_str,
file_stem,
extension,
mimetype,
thumbnail_size=settings.THUMBNAIL_SIZE,
):
current_dir: Path,
current_dir_id_str: str,
file_stem: str,
extension: str,
mimetype: str,
thumbnail_size: tuple[int, int] = settings.THUMBNAIL_SIZE,
) -> str | None:
"""Generate a thumbnail for the image at the given path."""
image_thumb_path = None
if mimetype.startswith("image"):
Expand All @@ -78,13 +84,15 @@ def generate_thumbnail(
return image_thumb_path


def store_file(file):
def store_file(
file: InMemoryUploadedFile | TemporaryUploadedFile,
) -> tuple[str, str, str | None]:
"""
Create a file in the images directory with a random name and the
correct extension.

:param file: the file to save
:return: the file path and the mimetype
:return: the file path, the mimetype and the thumbnail path
"""
# Generate a random name for the file
# This name will be used to display the image to the client, so it shouldn't be discoverable # noqa
Expand Down