Beispiel #1
0
def create_mediation(
    user: User,
    offer: Offer,
    credit: str,
    image_as_bytes: bytes,
    crop_params=None,
) -> Mediation:
    validation.check_mediation_thumb_quality(image_as_bytes)

    mediation = Mediation(
        author=user,
        offer=offer,
        credit=credit,
    )
    # `create_thumb()` requires the object to have an id, so we must save now.
    repository.save(mediation)

    create_thumb(mediation, image_as_bytes, image_index=0, crop_params=crop_params)
    mediation.thumbCount = 1
    repository.save(mediation)

    if feature_queries.is_active(FeatureToggle.SYNCHRONIZE_ALGOLIA):
        redis.add_offer_id(client=app.redis_client, offer_id=offer.id)

    return mediation
Beispiel #2
0
def create_mediation_v2(
    user: User,
    offer: Offer,
    credit: str,
    image_as_bytes: bytes,
    crop_params: tuple = None,
) -> Mediation:
    # checks image type, min dimensions
    validation.check_image(image_as_bytes)

    existing_mediations = mediation_queries.get_mediations_for_offers(
        [offer.id])

    mediation = Mediation(
        author=user,
        offer=offer,
        credit=credit,
    )
    # `create_thumb()` requires the object to have an id, so we must save now.
    repository.save(mediation)

    try:
        # TODO(fseguin): cleanup after image upload v2 launch
        create_thumb(mediation,
                     image_as_bytes,
                     image_index=0,
                     crop_params=crop_params,
                     use_v2=True)

    except Exception as exc:
        app.logger.exception(
            "An unexpected error was encountered during the thumbnail creation: %s",
            exc)
        # I could not use savepoints and rollbacks with SQLA
        repository.delete(mediation)
        raise ThumbnailStorageError

    else:
        mediation.thumbCount = 1
        repository.save(mediation)
        # cleanup former thumbnails and mediations
        for previous_mediation in existing_mediations:
            try:
                for thumb_index in range(0, mediation.thumbCount):
                    remove_thumb(previous_mediation, image_index=thumb_index)
            except Exception as exc:  # pylint: disable=broad-except
                app.logger.exception(
                    "An unexpected error was encountered during the thumbnails deletion for %s: %s",
                    mediation,
                    exc,
                )
            else:
                repository.delete(previous_mediation)

        if feature_queries.is_active(FeatureToggle.SYNCHRONIZE_ALGOLIA):
            redis.add_offer_id(client=app.redis_client, offer_id=offer.id)

        return mediation
Beispiel #3
0
def create_mediation(
    user: User,
    offer: Offer,
    credit: str,
    image_as_bytes: bytes,
    crop_params: tuple = None,
) -> Mediation:
    # checks image type, min dimensions
    validation.check_image(image_as_bytes)

    mediation = Mediation(
        author=user,
        offer=offer,
        credit=credit,
    )
    # `create_thumb()` requires the object to have an id, so we must save now.
    repository.save(mediation)

    try:
        create_thumb(mediation,
                     image_as_bytes,
                     image_index=0,
                     crop_params=crop_params)

    except Exception as exc:
        logger.exception(
            "An unexpected error was encountered during the thumbnail creation: %s",
            exc)
        # I could not use savepoints and rollbacks with SQLA
        repository.delete(mediation)
        raise ThumbnailStorageError

    else:
        mediation.thumbCount = 1
        repository.save(mediation)
        # cleanup former thumbnails and mediations

        previous_mediations = (Mediation.query.filter(
            Mediation.offerId == offer.id).filter(
                Mediation.id != mediation.id).all())
        for previous_mediation in previous_mediations:
            try:
                for thumb_index in range(0, previous_mediation.thumbCount):
                    remove_thumb(previous_mediation, image_index=thumb_index)
            except Exception as exc:  # pylint: disable=broad-except
                logger.exception(
                    "An unexpected error was encountered during the thumbnails deletion for %s: %s",
                    mediation,
                    exc,
                )
            else:
                repository.delete(previous_mediation)

        search.async_index_offer_ids([offer.id])

        return mediation
Beispiel #4
0
def _save_same_thumb_from_thumb_count_to_index(pc_object: Model, thumb_index: int, image_as_bytes: bytes):
    if pc_object.thumbCount is None:  # handle unsaved object
        pc_object.thumbCount = 0
    if thumb_index <= pc_object.thumbCount:
        # replace existing thumb
        create_thumb(pc_object, image_as_bytes, thumb_index)
    else:
        # add new thumb
        for index in range(pc_object.thumbCount, thumb_index):
            create_thumb(pc_object, image_as_bytes, index)
            pc_object.thumbCount += 1
Beispiel #5
0
def store_public_object_from_sandbox_assets(folder, model, subcategoryId):
    mimes_by_folder = {"spreadsheets": "application/CSV", "thumbs": "image/jpeg", "zips": "application/zip"}
    thumb_id = humanize(model.id)
    thumbs_folder_path = Path(pcapi.sandboxes.__path__[0]) / "thumbs"
    picture_path = str(thumbs_folder_path / "mediations" / subcategoryId) + ".jpg"
    with open(picture_path, mode="rb") as thumb_file:
        if folder == "thumbs":
            create_thumb(model, thumb_file.read(), 0)
            model.thumbCount += 1
        else:
            store_public_object(
                folder,
                model.thumb_path_component + "/" + thumb_id,
                thumb_file.read(),
                mimes_by_folder[folder],
            )

    return model
Beispiel #6
0
def store_public_object_from_sandbox_assets(folder, model, offer_type):
    mimes_by_folder = {
        "spreadsheets": "application/CSV",
        "thumbs": "image/jpeg",
        "zips": "application/zip"
    }
    plural_model_name = get_model_plural_name(model)
    thumb_id = humanize(model.id)
    thumb_path = f"{os.path.dirname(os.path.realpath(__file__))}/../../{folder}/{plural_model_name}/{str(offer_type)}"

    with open(thumb_path, mode="rb") as thumb_file:
        if folder == "thumbs":
            create_thumb(model, thumb_file.read(), 0, symlink_path=thumb_path)
            model.thumbCount += 1
        else:
            store_public_object(
                folder,
                plural_model_name + "/" + thumb_id,
                thumb_file.read(),
                mimes_by_folder[folder],
                symlink_path=thumb_path,
            )

    return model