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 17, 2015
1 parent c745245 commit 243e860
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 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.
1.0b4 (unreleased)
^^^^^^^^^^^^^^^^^^

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

- Brazilian Portuguese and Spanish translations were updated.
[rodfersou, hvelarde]

Expand Down Expand Up @@ -87,5 +90,6 @@ 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
29 changes: 27 additions & 2 deletions src/sc/embedder/content/embedder.py
Original file line number Diff line number Diff line change
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 @@ -77,6 +78,28 @@ def EmbedderImageFieldWidget(field, request):
return FieldWidget(field, EmbedderImageWidget(request))


def valid_size_constraint(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:
if ' ' in size:
raise Invalid(_(u'Size should not have spaces.'))
size = size.rstrip('%')
try:
size = float(size)
except ValueError:
raise Invalid(_(u'Size should be a float number or a percent.'))
return True


class IEmbedder(form.Schema):
""" A representation of a content embedder content type
"""
Expand All @@ -93,16 +116,18 @@ class IEmbedder(form.Schema):
required=False,
)

width = schema.Int(
width = schema.TextLine(
title=_(u'Width'),
description=_(u''),
required=True,
constraint=valid_size_constraint,
)

height = schema.Int(
height = schema.TextLine(
title=_(u'Height'),
description=_(u''),
required=True,
constraint=valid_size_constraint,
)

embed_html = schema.Text(
Expand Down

0 comments on commit 243e860

Please sign in to comment.