class GoogleNewsSettingsEditForm(base.RegistryEditForm):
    """Google News control panel settings form."""

    schema = GoogleNewsSettings
    label = _(u'Google News Settings')
    description = _(
        u'controlpanel_desc',
        default=
        u'You can configure the settings of collective.googlenews add-on',
    )

    def updateFields(self):
        """Update logo widget."""
        super(GoogleNewsSettingsEditForm, self).updateFields()
        self.fields['logo'].widgetFactory = NamedImageFieldWidget
 def validate_standout_journalism(data):
     context = data.__context__  # this will be None when adding
     if not _valid_as_standout_journalism(context):
         raise Invalid(_(
             u"Can't mark this news article as standout. "
             u'There are already seven marked in the past calendar week.'
         ))
Example #3
0
    def validate_standout_journalism(data):
        """Check there are less than 7 published news articles marked
        as standout journalism in the past calendar week.

        This invariant only takes care of content being edited; content
        being published is checked within a workflow guard.
        """
        context = data.__context__
        if context is None:
            return  # adding an item, not editing it

        if not data.standout_journalism:
            return  # this is not standout journalism

        if api.content.get_state(context) != 'published':
            return  # item not published yet

        results = get_current_standout_journalism()
        # there should never be more than 7 items marked as
        # standout journalism at any given time
        assert len(results) <= 7
        # ignore current item if already marked as standout
        results = [o for o in results if o != context]
        if len(results) == 7:
            raise Invalid(
                _(u"Can't mark this item as standout. "
                  u'There are already 7 items marked in the past calendar week.'
                  ))
def validate_logo(value):
    """Validate the image to be used in the feed.

    The image must follow these specifications:
    * Image should be .png format
    * Image dimensions must match one of two options:
      * height between 20 and 40px, width of 250px
      * height of 40px, width between 125 and 250px
    * Image background should be transparent

    :param value: Image encoded into base64 to be validated
    :type value: string
    :raises:
        :class:`~zope.interface.Invalid` if the image is not valid
    """
    if not value:
        return True

    filename, data = b64decode_file(value)

    img = Image.open(StringIO(data))

    # check format
    if img.format != 'PNG':
        raise Invalid(_(u'Image should be in PNG format.'))

    # Check image size
    width, height = img.size
    if not((20 <= height <= 40 and width == 250) or
       (height == 40 and 125 <= width <= 250)):
        raise Invalid(_(
            u'Image should have height beetween 20px and 40px and width equal 250px '
            u'or height equal 40px and width beetween 125px and 250px.'
        ))

    # Check image transparency.
    if not((img.mode in ('RGBA', 'LA')) or
       (img.mode == 'P' and 'transparency' in img.info)):
        raise Invalid(_(u'Image should have transparency layer.'))

    return True
def validate_logo(value):
    """Validate the image to be used in the feed.

    The image must follow these specifications:
    * Image should be .png format
    * Image dimensions must match one of two options:
      * height between 20 and 40px, width of 250px
      * height of 40px, width between 125 and 250px
    * Image background should be transparent

    :param value: Image encoded into base64 to be validated
    :type value: string
    :raises:
        :class:`~zope.interface.Invalid` if the image is not valid
    """
    if not value:
        return True

    filename, data = b64decode_file(value)

    img = Image.open(StringIO(data))

    # check format
    if img.format != 'PNG':
        raise Invalid(_(u'Image should be in PNG format.'))

    # Check image size
    width, height = img.size
    if not ((20 <= height <= 40 and width == 250) or
            (height == 40 and 125 <= width <= 250)):
        raise Invalid(
            _(u'Image should have height beetween 20px and 40px and width equal 250px '
              u'or height equal 40px and width beetween 125px and 250px.'))

    # Check image transparency.
    if not ((img.mode in ('RGBA', 'LA')) or
            (img.mode == 'P' and 'transparency' in img.info)):
        raise Invalid(_(u'Image should have transparency layer.'))

    return True
Example #6
0
class GoogleNewsSettings(model.Schema):

    """Main settings of this addon"""

    # XXX: default value is declared at profiles/default/registry.xml
    portal_types = schema.List(
        title=_(u'Portal types'),
        description=_(u'Select portal types that will be considered news articles.'),
        value_type=schema.Choice(
            vocabulary=u'plone.app.vocabularies.ReallyUserFriendlyTypes'),
    )

    logo = schema.ASCII(
        title=_(u'Logo image'),
        description=_(
            u"Google News requires Editors' Picks feed logos to follow some "
            u'<a href="{0}">general guidelines</a>. This image will replace '
            u'the logo in all Atom feeds on the site.'.format(EDITORS_PICKS_LINK)
        ),
        required=False,
        constraint=validate_logo,
    )
Example #7
0
class IGoogleNews(model.Schema):
    """Behavior interface to add some Google News features."""

    model.fieldset(
        'google-news',
        label=_(u'Google News'),
        fields=['standout_journalism', 'news_keywords'],
    )

    # https://support.google.com/news/publisher/answer/191283
    standout_journalism = schema.Bool(
        title=_(u'Standout Journalism'),
        description=_(
            u'help_standout_journalism',
            default=
            u'Used to indicate this is a big story, or an extraordinary work of journalism. '
            u'You can mark as standout no more than seven news articles in the past calendar week. '
            u'Implements Google News <code>standout</code> metatag.',
        ),
        required=False,
    )

    # https://support.google.com/news/publisher/answer/68297
    news_keywords = schema.Tuple(
        title=_(u'Keywords'),
        description=_(
            u'help_news_keywords',
            default=
            u'Used to specify keywords that are relevant to this news article. '
            u'Add one phrase or keyword on each line. '
            u'Implements Google News <code>news_keywords</code> metatag.',
        ),
        value_type=schema.TextLine(),
        required=False,
    )

    @invariant
    def validate_standout_journalism(data):
        """Check there are less than 7 published news articles marked
        as standout journalism in the past calendar week.

        This invariant only takes care of content being edited; content
        being published is checked within a workflow guard.
        """
        context = data.__context__
        if context is None:
            return  # adding an item, not editing it

        if not data.standout_journalism:
            return  # this is not standout journalism

        if api.content.get_state(context) != 'published':
            return  # item not published yet

        results = get_current_standout_journalism()
        # there should never be more than 7 items marked as
        # standout journalism at any given time
        assert len(results) <= 7
        # ignore current item if already marked as standout
        results = [o for o in results if o != context]
        if len(results) == 7:
            raise Invalid(
                _(u"Can't mark this item as standout. "
                  u'There are already 7 items marked in the past calendar week.'
                  ))