Skip to content

Commit

Permalink
Remove image cache
Browse files Browse the repository at this point in the history
  • Loading branch information
rodfersou committed Jul 18, 2017
1 parent fa1c8c3 commit 886bb35
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 54 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ There's a frood who really knows where his towel is.
2.11 (unreleased)
^^^^^^^^^^^^^^^^^

- Remove image cache (closes `#109`_).
[rodfersou]

- Add ``canonical_domain`` field record to the registry when upgrading;
this fixes an issue in the upgrade step to profile version 3045 (fixes `#114 <https://github.com/collective/sc.social.like/issues/114>`_).
[hvelarde]
Expand Down Expand Up @@ -296,3 +299,4 @@ Previous entries can be found in the HISTORY.rst file.
.. _`#83`: https://github.com/collective/sc.social.like/issues/83
.. _`#91`: https://github.com/collective/sc.social.like/issues/91
.. _`#100`: https://github.com/collective/sc.social.like/issues/100
.. _`#109`: https://github.com/collective/sc.social.like/issues/109
92 changes: 38 additions & 54 deletions sc/social/like/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,71 +3,55 @@
from Products.Archetypes.interfaces import IBaseContent
from Products.CMFPlone.utils import safe_hasattr
from urlparse import urlparse
from zope.annotation.interfaces import IAnnotations
from zope.globalrequest import getRequest
from zope.interface import Invalid


def get_images_view(context):
request = getRequest()
key = 'cache-view-' + str(context)
cache = IAnnotations(request)
value = cache.get(key, None)
if not value:
view = context.unrestrictedTraverse('@@images', None)
field = 'image'
if view:
fields = ['image', 'leadImage', 'portrait']
if IBaseContent.providedBy(context):
schema = context.Schema()
field = [f for f in schema.keys() if f in fields]
if field:
field = field[0]
# if a content has an image field that isn't an ImageField
# (for example a relation field), set field='' to avoid errors
if schema[field].type not in ['image', 'blob']:
field = ''
value = (view, field) if (view and field) else (None, None)
cache[key] = value
return value
view = context.unrestrictedTraverse('@@images', None)
field = 'image'
if view:
fields = ['image', 'leadImage', 'portrait']
if IBaseContent.providedBy(context):
schema = context.Schema()
field = [f for f in schema.keys() if f in fields]
if field:
field = field[0]
# if a content has an image field that isn't an ImageField
# (for example a relation field), set field='' to avoid errors
if schema[field].type not in ['image', 'blob']:
field = ''
return (view, field) if (view and field) else (None, None)


def get_content_image(context,
scale='large',
width=None,
height=None):
request = getRequest()
modification = context.ModificationDate()
key = 'cache-{0}-{1}-{2}-{3}-{4}'.format(
context, modification, scale, width, height)
cache = IAnnotations(request)
img = cache.get(key, None)
if not img:
view, field = get_images_view(context)
if view:
view, field = get_images_view(context)
img = None
if view:
try:
sizes = view.getImageSize(field)
except AttributeError:
sizes = img = None
if sizes == (0, 0) or sizes == ('', ''):
# this avoid strange cases where we can't get size infos.
# for example if the loaded image in a news is a bmp or a tiff
return None
if sizes:
kwargs = {}
if not (width or height):
kwargs['scale'] = scale
else:
new = (width, height)
width, height = _image_size(sizes, new)
kwargs['width'] = width
kwargs['height'] = height
kwargs['direction'] = 'down'
try:
sizes = view.getImageSize(field)
except AttributeError:
sizes = img = None
if sizes == (0, 0) or sizes == ('', ''):
# this avoid strange cases where we can't get size infos.
# for example if the loaded image in a news is a bmp or a tiff
return None
if sizes:
kwargs = {}
if not (width or height):
kwargs['scale'] = scale
else:
new = (width, height)
width, height = _image_size(sizes, new)
kwargs['width'] = width
kwargs['height'] = height
kwargs['direction'] = 'down'
try:
img = view.scale(fieldname=field, **kwargs)
except (AttributeError, TypeError):
img = None
cache[key] = img
img = view.scale(fieldname=field, **kwargs)
except (AttributeError, TypeError):
img = None
return img


Expand Down

0 comments on commit 886bb35

Please sign in to comment.