Skip to content

Commit

Permalink
Allow % on width and height properties
Browse files Browse the repository at this point in the history
  • Loading branch information
rodfersou committed Aug 26, 2015
1 parent af0203b commit 8e8cd0f
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Expand Up @@ -6,6 +6,9 @@ There's a frood who really knows where his towel is.
1.0b4 (unreleased)
^^^^^^^^^^^^^^^^^^

- Allow % on width properties (closes `#6`_).
[rodfersou]

- Add Embedder tile for collective.cover (closes `#32`_).
[rodfersou]

Expand Down Expand Up @@ -90,6 +93,7 @@ There's a frood who really knows where his towel is.
- Initial release.

.. _`#3`: https://github.com/simplesconsultoria/sc.embedder/issues/3
.. _`#6`: https://github.com/simplesconsultoria/sc.embedder/issues/6
.. _`#14`: https://github.com/simplesconsultoria/sc.embedder/issues/14
.. _`#20`: https://github.com/simplesconsultoria/sc.embedder/issues/20
.. _`#32`: https://github.com/simplesconsultoria/sc.embedder/issues/32
32 changes: 29 additions & 3 deletions src/sc/embedder/content/embedder.py
Expand Up @@ -30,6 +30,7 @@
from zope.event import notify
from zope.interface import implementer
from zope.interface import Interface
from zope.interface import Invalid

import requests
import urllib2
Expand Down Expand Up @@ -93,15 +94,15 @@ class IEmbedder(form.Schema):
required=False,
)

width = schema.Int(
width = schema.TextLine(
title=_(u'Width'),
description=_(u''),
description=_(u'The width of the video, can be expressed as a decimal number or a percentage. ex.: 270 or 50%'),
required=True,
)

height = schema.Int(
title=_(u'Height'),
description=_(u''),
description=_(u'The height of the video, can be expressed as a decimal number. ex.: 480'),
required=True,
)

Expand Down Expand Up @@ -162,6 +163,27 @@ def tag(self, scale='thumb', css_class='tileImage', **kw):
**kw)


@form.validator(field=IEmbedder['width'])
def validate_size(size):
"""Validate if size inserted is a float number or a %.
:param size: size to be validated
:type size: unicode
:return: True if size is valid
:rtype: bool
:raises:
:class:`~zope.interface.Invalid` if the size is not valid
"""

if size:
size = size.replace(' ', '')
size = size.rstrip('%')
try:
size = int(size, 10)
except ValueError:
raise Invalid(_(u'Size should be a int number or a percent.'))


class BaseForm(DexterityExtensibleForm):
"""
"""
Expand Down Expand Up @@ -313,6 +335,8 @@ def handleAdd(self, action):
if json_data.get(k):
self.request[v] = unicode(json_data[k])
data, errors = self.extractData()
if 'width' in data:
data['width'] = data['width'].replace(' ', '')
self.handle_image(data)
self.set_custom_embed_code(data)
if errors:
Expand Down Expand Up @@ -362,6 +386,8 @@ def handleLoad(self, action):
@button.buttonAndHandler(_(u'Save'), name='save')
def handleApply(self, action):
data, errors = self.extractData()
if 'width' in data:
data['width'] = data['width'].replace(' ', '')
self.handle_image(data)
self.set_custom_embed_code(data)
if errors:
Expand Down
38 changes: 38 additions & 0 deletions src/sc/embedder/tests/test_content.py
Expand Up @@ -7,7 +7,9 @@
from Products.statusmessages.interfaces import IStatusMessage
from sc.embedder.content.embedder import Embedder
from sc.embedder.content.embedder import IEmbedder
from sc.embedder.content.embedder import validate_size
from sc.embedder.testing import INTEGRATION_TESTING
from zope.interface import Invalid
from zope.interface.verify import verifyClass
from zope.interface.verify import verifyObject

Expand Down Expand Up @@ -403,3 +405,39 @@ def test_image_tag_no_image(self):
# set an empty image file
content.image = NamedBlobImage('', 'image/jpeg', u'picture.jpg')
self.assertEqual(content.tag(), expected)


class SizeValidationTestCase(unittest.TestCase):

layer = INTEGRATION_TESTING

def setUp(self):
self.portal = self.layer['portal']

def test_validate_size(self):
# Normal size
size = '420'
self.assertEqual(validate_size(size), None)

# Size with percent
size = '100%'
self.assertEqual(validate_size(size), None)

# Spaces allowed
size = '100 %'
self.assertEqual(validate_size(size), None)
size = ' 100%'
self.assertEqual(validate_size(size), None)
size = '100% '
self.assertEqual(validate_size(size), None)

# Size number should be a valid int
size = 'a123'
with self.assertRaises(Invalid):
validate_size(size)
size = '123b'
with self.assertRaises(Invalid):
validate_size(size)
size = '12c3'
with self.assertRaises(Invalid):
validate_size(size)

0 comments on commit 8e8cd0f

Please sign in to comment.