Skip to content

Commit

Permalink
Thumbnail field added
Browse files Browse the repository at this point in the history
  • Loading branch information
Szymon Pyżalski committed Sep 5, 2012
1 parent e41125c commit 201e874
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/eplasty/field/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
from eplasty.field.helper import *
from eplasty.field.file import FileField
from eplasty.field.adapter import *
from eplasty.field.image import Image
from eplasty.field.image import Image, Thumb
4 changes: 4 additions & 0 deletions src/eplasty/field/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class Field(object):

def __init__(self, *args, **kwargs):
# self.kwargs = kwargs
# Dependent fields need to be erased when this field is set
self.dependent_fields = []
if 'default' in kwargs:
self._default = kwargs['default']

Expand All @@ -39,6 +41,8 @@ def __set__(self, inst, v):
try:
if self._is_compatible(v):
inst._current[self.name] = v
for field in self.dependent_fields:
field.__set__(inst, None)
inst.touch()
else:
raise TypeError('Type {0} is incompatible with field {1}'.format(
Expand Down
18 changes: 7 additions & 11 deletions src/eplasty/field/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Blob(Field):

def __init__(self, mimetype=None):
self.fixed_mimetype = mimetype
super(Blob, self).__init__()

def bind_class(self, cls, name):
namec = ft.partial(str.format, '{0}_{1}', name)
Expand All @@ -25,12 +26,6 @@ def bind_class(self, cls, name):
super(Blob, self).bind_class(cls, name)
return self

def __get__(self, inst, cls):
if inst and not inst._current.get(self.name):
return BlobData(None, self.fixed_mimetype, None)
else:
return super(Blob, self).__get__(inst, cls)

def hydrate(self, inst, col_vals, dict_, session):
if col_vals[self.blob_column.name] is None:
dict_[self.name] = None
Expand All @@ -39,11 +34,12 @@ def hydrate(self, inst, col_vals, dict_, session):
mimetype = self.fixed_mimetype
else:
mimetype = col_vals[self.mime_column.name]
dict_[self.name] = BlobData(
col_vals[self.blob_column.name].tobytes(),
mimetype,
col_vals[self.filename_column.name],
)
blob_data = col_vals[self.blob_column.name]
dict_[self.name] = BlobData(
blob_data and blob_data.tobytes(),
mimetype,
col_vals[self.filename_column.name],
)

def get_c_vals(self, dict_):
obj = dict_.get(self.name)
Expand Down
27 changes: 25 additions & 2 deletions src/eplasty/field/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def __init__(self, *args, **kwargs):
def hydrate(self, inst, col_vals, dict_, session):
super(Image, self).hydrate(inst, col_vals, dict_, session)
blob = dict_.get(self.name)
if blob.data is not None:
if blob and blob.data is not None:
dict_[self.name] = pystacia.read_blob(blob.data, self.img_format)
dict_[self.name].filename = blob.filename
else:
Expand All @@ -31,4 +31,27 @@ def get_c_vals(self, dict_):
return super(Image, self).get_c_vals(dict_)

def _is_compatible(self, value):
return isinstance(value, pystacia.Image)
return value is None or isinstance(value, pystacia.Image)

class Thumb(Image):
def __init__(self, *args, **kwargs):
origin = kwargs.pop('origin')
self.size = kwargs.pop('size')
origin.dependent_fields.append(self)
self.origin = origin
super(Thumb, self).__init__(*args, **kwargs)

def __get__(self, inst, cls):
result = super(Thumb, self).__get__(inst, cls)
if result is not None:
return result
img = self.origin.__get__(inst, cls)
if img is None:
return img
to_width, to_height = self.size
factor = min(to_width/img.width, to_height/img.height)
if factor < 1:
img = img.copy()
img.rescale(factor=factor)
self.__set__(inst, img)
return img
5 changes: 4 additions & 1 deletion src/test/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class Test(unittest.TestCase):
def setUp(self):
class Image(ep.Object):
image = ep.f.Image()
thumb = ep.f.Thumb(origin=image, size=(100, 100))

self.Image = Image
self.conn = get_test_conn()
ep.set_context(self.conn)
Expand All @@ -37,5 +39,6 @@ def test_read(self):
ep.start_session()
pythons = self.Image.get(1)
img = pythons.image
thumb = pythons.thumb
self.assertEqual(img.width, 640)

self.assertEqual(thumb.width, 100)

0 comments on commit 201e874

Please sign in to comment.