Aller au contenu

Schemas

NonEmptyStr = Annotated[str, MinLen(1)] module-attribute

SimpleUserSchema

Bases: ModelSchema

A schema with the minimum amount of information to represent a user.

UserProfileSchema

Bases: ModelSchema

The necessary information to show a user profile

Album

Bases: SasFile

NAME_MAX_LENGTH = 50 class-attribute

Maximum length of an album's name.

SithFile have a maximum length of 256 characters. However, this limit is too high for albums. Names longer than 50 characters are harder to read and harder to display on the SAS page.

It is to be noted, though, that this does not add or modify any db behaviour. It's just a constant to be used in views and forms.

Picture

Bases: SasFile

generate_thumbnails(*, img=None, save=False)

Generate the thumbnail and the compressed version of this picture.

Parameters:

Name Type Description Default
img Image | None

if given, this will be used to generate

None
save bool

if True, save the instance in database.

False
Source code in sas/models.py
def generate_thumbnails(
    self, *, img: Image.Image | None = None, save: bool = False
):
    """Generate the thumbnail and the compressed version of this picture.

    Args:
        img: if given, this will be used to generate
        all three images (file, compressed, thumbnail).
        Else, `self.file` will be used
        save: if True, save the instance in database.
    """
    img = img or Image.open(self.file)
    extension = self.mime_type.split("/")[-1]
    previous_files = [
        f.name for f in (self.file, self.thumbnail, self.compressed) if f
    ]
    # convert the compressed image and the thumbnail into webp
    # The original image keeps its original type, because it's not
    # meant to be shown on the website, but rather to keep the real image
    # for less frequent cases (like downloading the pictures of a user)
    # the HD version of the image doesn't need to be optimized, because :
    # - it isn't frequently queried
    # - optimizing large images takes a lot of time, which greatly hinders the UX
    # - photographers usually already optimize their images
    new_extension_name = str(Path(self.name).with_suffix(".webp"))
    file = resize_image(img, max(img.size), extension, optimize=False)
    self.file.save(self.name, file, save=False)
    thumbnail = resize_image(img, 200, "webp")
    self.thumbnail.save(new_extension_name, thumbnail, save=False)
    compressed = resize_image(img, 1200, "webp")
    self.compressed.save(new_extension_name, compressed, save=save)
    # once the new images have been saved, delete the previous ones.
    # The deletion of old files is done after, so that if anything goes
    # during the whole process, no data will be lost.
    for filename in previous_files:
        self.file.storage.delete(filename)

rotate(degree)

Rotate this picture and update its thumbnails accordingly.

Parameters:

Name Type Description Default
degree int | float

the rotation angle, in degree, counter-clockwise

required
Source code in sas/models.py
def rotate(self, degree: int | float):
    """Rotate this picture and update its thumbnails accordingly.

    Args:
        degree: the rotation angle, in degree, counter-clockwise
    """
    img = Image.open(self.file).rotate(degree)
    self.generate_thumbnails(img=img, save=True)

PictureModerationRequest

Bases: Model

A request to remove a Picture from the SAS.

AlbumFilterSchema

Bases: FilterSchema

SimpleAlbumSchema

Bases: ModelSchema

AlbumSchema

Bases: ModelSchema

AlbumAutocompleteSchema

Bases: ModelSchema

Schema to use on album autocomplete input field.

PictureFilterSchema

Bases: FilterSchema

PictureSchema

Bases: ModelSchema

PictureRelationCreationSchema

Bases: Schema

IdentifiedUserSchema

Bases: Schema

ModerationRequestSchema

Bases: ModelSchema