jesustorresdev / slack-badges-bot

2 stars 1 forks source link

Gestiona imagenes de las medallas con clase BadgeImage #8

Closed mbdaso closed 5 years ago

mbdaso commented 5 years ago

He creado una clase BadgeImage en que guarda la ruta, los datos y el tipo (.png, .svg) de la imagen. Tiene un método get_data() que carga la imagen si está en un archivo.

Cuando se guarda una medalla se sustituye el BadgeImage.image por un string que indica la ruta del archivo. Quité lo de file:///... porque una medalla siempre va a tener una ruta de archivo local:

def save_badgeimage(self, entity):
        image_filepath = self._build_filepath(entity.id.hex, entity.image.suffix)
        if entity.image.path is None:
            with image_filepath.open('wb') as f:
                f.write(entity.image.get_data().read())
        entity.image = image_filepath

La función load queda igual que antes.

BadgeService.open_image sustituye el atributo image por un BadgeImage si detecta que es un string, y luego llama a BadgeImage.get_data que devuelve un BufferedBaseIO

 def open_image(self, badge: Badge) -> BufferedIOBase:
        '''
        Método para acceder a la imagen de una medalla
        '''
        if isinstance(badge.image, str):
            if Path(badge.image).exists:
                badge.image = Image(path=badge.image)
            else:
                raise ValueError(f'BadgeService.open_image: path {badge.image} doesn\'t exist')
        if isinstance(badge.image, BadgeImage):
            return badge.image.get_data()
        raise TypeError(f'BadgeService.open_image: Can\'t open image of type {type(badge.image)}')

Intenté hacerlo con @property, haciendo un setter que al pasarle un string o un BytesIO creara la imagen, pero daba problemas por todos lados.

jesustorresdev commented 5 years ago

Aparece en el comentario Image y BadgeImage ¿es un error? ¿no deberian ser ambos BadgeImage?

mbdaso commented 5 years ago

Donde?

jesustorresdev commented 5 years ago

En open_image en el comentario al menos. NO he visto el código

 def open_image(self, badge: Badge) -> BufferedIOBase:
        '''
        Método para acceder a la imagen de una medalla
        '''
        if isinstance(badge.image, str):
            if Path(badge.image).exists:
                badge.image = Image(path=badge.image)
            else:
                raise ValueError(f'BadgeService.open_image: path {badge.image} doesn\'t exist')
        if isinstance(badge.image, BadgeImage):
            return badge.image.get_data()
        raise TypeError(f'BadgeService.open_image: Can\'t open image of type {type(badge.image)}')
mbdaso commented 5 years ago

Si, es que cambié el nombre de la clase y se me olvidó cambiarlo ahí también