Reputation: 3781
I am writing ImgProperty in which I would like to give a user a way to define default height and width, like this:
class MyModel(ndb.Model):
img = ImgProperty(height=32, width=32)
and then:
m = MyModel(img='https://example.com/my-photo')
the image then will be saved to the db resized to given height, width values.
The ImgProperty itself subclasses BlobProperty. Can I add those two attribute to Property._attributes and thus make it work? Or better not to mess with it? The other way I see to do it, is to create an intermediate model with fields height and width and add __init__
method to ImgProperty.
Something like this:
class ImgModel(ndb.Model):
height = ndb.IntegerProperty()
width = ndb.IntegerProperty()
class ImgProperty(ndb.BlobProperty):
def __init__(self, **kwds):
super(ImgProperty, self).__init__(ImgModel, **kwds)
I'm not sure if this way will allow definition like img = ImgProperty(height=32, width=32)
.
Upvotes: 1
Views: 590
Reputation: 11706
You can also save the get_serving_url including the size. In this way you make use of the Google High Performance Image serving API, where Google serves the Image.
From the doc: This URL format allows dynamic resizing and cropping, so you don't need to store different image sizes on the server. Images are served with low latency from a highly optimized, cookieless infrastructure.
Here is an example: https://lh6.ggpht.com/lOghqU2JrYk8M-Aoio8WjMM6mstgZcTP0VzJk79HteVLhnwZy0kqbgVGQZYP8YsoqVNzsu0EBysX16qMJe7H2BsOAr4j=s70
For more information see this blog post: http://googleappengine.blogspot.nl/2010/08/multi-tenancy-support-high-performance_17.html
Upvotes: 0
Reputation: 16890
I can answer your question about the constructor signature. You should be able to do it as follows:
class ImgProperty(ndb.BlobProperty):
def __init__(self, height=32, width=32, **kwds):
self.height = height
self.width = width
super(ImgProperty, self).__init__(**kwds)
For the rest I suppose you are following the docs: https://developers.google.com/appengine/docs/python/ndb/subclassprop
I can't really help you with the resizing (use the image api); however I wonder if you aren't better off making the resize call by hand before you store the data into the property value, just to avoid unnecessary extra resize calls to the image api; the automatic property transforms are very powerful but may occasionally make redundant calls.
Good luck!
Upvotes: 3