Skip to content

Commit

Permalink
Factor out imaging related helper functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasgraf committed May 27, 2016
1 parent 38f5910 commit abf9ab4
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 80 deletions.
45 changes: 45 additions & 0 deletions src/plone/restapi/imaging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# -*- coding: utf-8 -*-
from Products.CMFCore.interfaces import IPropertiesTool
from zope.component import getUtility

try:
from Products.CMFPlone.factory import _IMREALLYPLONE5 # noqa
except ImportError:
PLONE_5 = False # pragma: no cover
else:
PLONE_5 = True # pragma: no cover


def get_scales(context):
absolute_url = context.absolute_url()
scales = {name: {
u'download': u'{0}/@@images/image/{1}'.format(absolute_url, name),
u'width': width,
u'height': height}
for name, width, height in get_scale_infos()}

return scales


def get_scale_infos():
if PLONE_5:
from plone.registry.interfaces import IRegistry
registry = getUtility(IRegistry)
from Products.CMFPlone.interfaces import IImagingSchema
imaging_settings = registry.forInterface(
IImagingSchema,
prefix='plone'
)
allowed_sizes = imaging_settings.allowed_sizes

else:
ptool = getUtility(IPropertiesTool)
image_properties = ptool.imaging_properties
allowed_sizes = image_properties.getProperty('allowed_sizes')

def split_scale_info(allowed_size):
name, dims = allowed_size.split(' ')
width, height = map(int, dims.split(':'))
return name, width, height

return [split_scale_info(size) for size in allowed_sizes]
38 changes: 2 additions & 36 deletions src/plone/restapi/serializer/atfields.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
from Products.Archetypes.interfaces.field import IImageField
from Products.Archetypes.interfaces.field import IReferenceField
from Products.Archetypes.interfaces.field import ITextField
from Products.CMFCore.interfaces import IPropertiesTool
from plone.app.blob.interfaces import IBlobField
from plone.app.blob.interfaces import IBlobImageField
from plone.restapi.interfaces import IFieldSerializer
from plone.restapi.imaging import get_scales
from plone.restapi.serializer.converters import json_compatible
from zope.component import adapter
from zope.component import getUtility
from zope.interface import Interface
from zope.interface import implementer

Expand Down Expand Up @@ -80,7 +79,7 @@ def __call__(self):

download_url = '/'.join((
self.context.absolute_url(), '@@images', self.field.__name__))
scales = self.get_scales()
scales = get_scales(self.context)
result = {
'filename': self.field.getFilename(self.context),
'content-type': self.field.getContentType(self.context),
Expand All @@ -90,39 +89,6 @@ def __call__(self):
}
return json_compatible(result)

def get_scales(self):
absolute_url = self.context.absolute_url()
scales = {name: {
u'download': u'{0}/@@images/image/{1}'.format(absolute_url, name),
u'width': width,
u'height': height}
for name, width, height in self.get_scale_infos()}

return scales

def get_scale_infos(self):
if PLONE_5:
from plone.registry.interfaces import IRegistry
registry = getUtility(IRegistry)
from Products.CMFPlone.interfaces import IImagingSchema
imaging_settings = registry.forInterface(
IImagingSchema,
prefix='plone'
)
allowed_sizes = imaging_settings.allowed_sizes

else:
ptool = getUtility(IPropertiesTool)
image_properties = ptool.imaging_properties
allowed_sizes = image_properties.getProperty('allowed_sizes')

def split_scale_info(allowed_size):
name, dims = allowed_size.split(' ')
width, height = map(int, dims.split(':'))
return name, width, height

return [split_scale_info(size) for size in allowed_sizes]


@adapter(IBlobField, IBaseObject, Interface)
@implementer(IFieldSerializer)
Expand Down
46 changes: 2 additions & 44 deletions src/plone/restapi/serializer/dxfields.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,15 @@
from plone.dexterity.interfaces import IDexterityContent
from plone.namedfile.interfaces import INamedFileField
from plone.namedfile.interfaces import INamedImageField
from plone.restapi.imaging import get_scales
from plone.restapi.interfaces import IFieldSerializer
from plone.restapi.serializer.converters import json_compatible
from Products.CMFCore.interfaces import IPropertiesTool
from zope.component import adapter
from zope.component import getUtility
from zope.interface import implementer
from zope.interface import Interface
from zope.schema.interfaces import IField


try:
from Products.CMFPlone.factory import _IMREALLYPLONE5 # noqa
except ImportError:
PLONE_5 = False # pragma: no cover
else:
PLONE_5 = True # pragma: no cover


@adapter(IField, IDexterityContent, Interface)
@implementer(IFieldSerializer)
class DefaultFieldSerializer(object):
Expand Down Expand Up @@ -48,7 +39,7 @@ def __call__(self):

download_url = '/'.join((
self.context.absolute_url(), '@@images', self.field.__name__))
scales = self.get_scales()
scales = get_scales(self.context)
result = {
'filename': namedimage.filename,
'content-type': namedimage.contentType,
Expand All @@ -58,39 +49,6 @@ def __call__(self):
}
return json_compatible(result)

def get_scales(self):
absolute_url = self.context.absolute_url()
scales = {name: {
u'download': u'{0}/@@images/image/{1}'.format(absolute_url, name),
u'width': width,
u'height': height}
for name, width, height in self.get_scale_infos()}

return scales

def get_scale_infos(self):
if PLONE_5:
from plone.registry.interfaces import IRegistry
registry = getUtility(IRegistry)
from Products.CMFPlone.interfaces import IImagingSchema
imaging_settings = registry.forInterface(
IImagingSchema,
prefix='plone'
)
allowed_sizes = imaging_settings.allowed_sizes

else:
ptool = getUtility(IPropertiesTool)
image_properties = ptool.imaging_properties
allowed_sizes = image_properties.getProperty('allowed_sizes')

def split_scale_info(allowed_size):
name, dims = allowed_size.split(' ')
width, height = map(int, dims.split(':'))
return name, width, height

return [split_scale_info(size) for size in allowed_sizes]


@adapter(INamedFileField, IDexterityContent, Interface)
class FileFieldSerializer(DefaultFieldSerializer):
Expand Down

0 comments on commit abf9ab4

Please sign in to comment.