dahlia / sqlalchemy-imageattach

SQLAlchemy extension for attaching images to entities.
https://sqlalchemy-imageattach.readthedocs.io/
MIT License
116 stars 25 forks source link

find_thumbnail doesn't work on ImageSubset #43

Open picakoch opened 7 years ago

picakoch commented 7 years ago

I might not use the library as expected, but I could not find a way to use the find_thumbnail method on a ImageSubset object (returned by get_image_set). I get the following error : AttributeError: 'ImageSubset' object has no attribute 'filter_by'

Looking in the code, it doesn't seem surprising since ImageSubset is not a descendant of BaseImageQuery, and the method filter_by is called on the self variable.

Could you please let me know how this should be done ? Here is a snippet of my code (Python 3, imageattach version 1.0.0):

class EventPicture(db.Model, Image):
    __tablename__ = 'event_picture'
    event_id = db.Column(db.Integer, db.ForeignKey('event.id'), primary_key=True)
    event = db.relationship("Event")
    order_index = db.Column(db.Integer, primary_key=True)  

    @property
    def object_id(self):
        key = '{0},{1}'.format(self.event_id, self.order_index).encode('ascii')
        return int(sha1(key).hexdigest(), 16)

class Event(db.Model):
    __tablename__ = 'event'
    pictures = image_attachment('EventPicture', uselist=True)

    def get_photo(self, i, width=None, height=None):
    """ get the photo with index i and given width/height """
        with store_context(store):
            image_set = self.pictures.get_image_set(order_index=i)
            image_set.find_thumbnail(width=width,height=height) # This is where the exception occurs
           # find_thumbnail tries to execute filter_by on self, but ImageSubset is not a query, it's query is in it's query attribute.

If I reproduce the code of find_thumbnail but replacing the q variable with image_set.query, then it works smoothly.

Thanks a lot, Best regards, Antoine

rogeriobastos commented 7 years ago

I have the same problem then @picakoch and solution proposed works like a charm.

@picakoch is the method generate_thumbnail() working for you when using Multiple Image Sets ? Here it only generate the first thumbnail, others ImageSets is getting the thumbnail of the first one.