Ejemplo n.º 1
0
class IEmbedderTile(IPersistentCoverTile):
    """
    """

    title = schema.TextLine(
        title=_(u'Title'),
        required=False,
    )

    embed_code = schema.TextLine(
        title=_(u'Videos'),
        required=False,
        readonly=True,
    )

    url = schema.TextLine(
        title=_(u'Videos'),
        required=False,
        readonly=True,
    )

    uuids = schema.List(
        title=_(u'Videos'),
        value_type=schema.TextLine(),
        required=False,
        readonly=True,
    )
Ejemplo n.º 2
0
class AddForm(BaseForm, DefaultAddForm):
    template = ViewPageTemplateFile('templates/sc.embedder.pt')

    @button.buttonAndHandler(_('Save'), name='save')
    def handleAdd(self, action):
        if self.request.get('form.widgets.url') and \
           not self.request.get('form.widgets.embed_html'):
            fields = {'width': 'form.widgets.width',
                      'height': 'form.widgets.height',
                      'description': 'form.widgets.IDublinCore.description',
                      'title': 'form.widgets.IDublinCore.title',
                      'html': 'form.widgets.embed_html'}
            consumer = component.getUtility(IConsumer)
            json_data = consumer.get_data(self.request['form.widgets.url'],
                                          maxwidth=None,
                                          maxheight=None,
                                          format='json')
            for k, v in fields.iteritems():
                if json_data.get(k):
                    self.request[v] = unicode(json_data[k])
        data, errors = self.extractData()
        self.handle_image(data)
        self.set_custom_embed_code(data)
        if errors:
            self.status = self.formErrorsMessage
            return
        obj = self.createAndAdd(data)
        if obj is not None:
            # mark only as finished if we get the new object
            self._finishedAdd = True
            api.portal.show_message(
                _(u'Item created'), self.request, type='info')

    @button.buttonAndHandler(_(u'Cancel'), name='cancel')
    def handleCancel(self, action):
        api.portal.show_message(
            _(u'Add New Item operation cancelled.'), self.request, type='info')
        self.request.response.redirect(self.nextURL())
        notify(AddCancelledEvent(self.context))

    @button.buttonAndHandler(_('Load'), name='load')
    def handleLoad(self, action):
        self.load_oembed(action)

    def get_url_widget(self):
        widget = [key for key in self.widgets.values()
                  if key.id == 'form-widgets-url']
        if widget != []:
            url_w = widget[0]
            return url_w

    def get_load_action(self):
        action = [key for key in self.actions.values()
                  if key.id == 'form-buttons-load']
        if action != []:
            load = action[0]
            return load
Ejemplo n.º 3
0
class EditForm(dexterity.EditForm, BaseForm):
    grok.context(IEmbedder)
    template = ViewPageTemplateFile('templates/edit.pt')

    @button.buttonAndHandler(_('Load'), name='load')
    def handleLoad(self, action):
        self.load_oembed(action)

    @button.buttonAndHandler(_(u'Save'), name='save')
    def handleApply(self, action):
        data, errors = self.extractData()
        self.handle_image(data)
        self.set_custom_embed_code(data)
        if errors:
            self.status = self.formErrorsMessage
            return
        self.applyChanges(data)
        api.portal.show_message(_(u'Changes saved.'),
                                self.request,
                                type='info')
        self.request.response.redirect(self.nextURL())
        notify(EditFinishedEvent(self.context))

    @button.buttonAndHandler(_(u'Cancel'), name='cancel')
    def handleCancel(self, action):
        api.portal.show_message(_(u'Edit cancelled.'),
                                self.request,
                                type='info')
        self.request.response.redirect(self.nextURL())
        notify(EditCancelledEvent(self.context))

    def get_url_widget(self):
        widget = [
            key for key in self.widgets.values()
            if key.id == 'form-widgets-url'
        ]
        if widget != []:
            url_w = widget[0]
            return url_w

    def get_load_action(self):
        action = [
            key for key in self.actions.values()
            if key.id == 'form-buttons-load'
        ]
        if action != []:
            load = action[0]
            return load
Ejemplo n.º 4
0
 def handleAdd(self, action):
     if self.request.get('form.widgets.url') and \
        not self.request.get('form.widgets.embed_html'):
         fields = {
             'width': 'form.widgets.width',
             'height': 'form.widgets.height',
             'description': 'form.widgets.IDublinCore.description',
             'title': 'form.widgets.IDublinCore.title',
             'html': 'form.widgets.embed_html'
         }
         consumer = component.getUtility(IConsumer)
         json_data = consumer.get_data(self.request['form.widgets.url'],
                                       maxwidth=None,
                                       maxheight=None,
                                       format='json')
         for k, v in fields.iteritems():
             if json_data.get(k):
                 self.request[v] = unicode(json_data[k])
     data, errors = self.extractData()
     self.handle_image(data)
     self.set_custom_embed_code(data)
     if errors:
         self.status = self.formErrorsMessage
         return
     obj = self.createAndAdd(data)
     if obj is not None:
         # mark only as finished if we get the new object
         self._finishedAdd = True
         api.portal.show_message(_(u'Item created'),
                                 self.request,
                                 type='info')
Ejemplo n.º 5
0
    def load_oembed(self, action):
        url = self.widgets['url'].value

        if not self._validate_url(url):
            api.portal.show_message(_(u'Invalid URL'),
                                    request=self.request,
                                    type='error')
            return

        json_data = self.get_data(url,
                                  maxwidth=None,
                                  maxheight=None,
                                  format='json')

        if json_data is None:
            json_data = self.get_fallback(url)
            if json_data is None:
                return
        # html parameter not always required:
        # https://github.com/abarmat/python-oembed/blob/master/oembed/__init__.py#L157-L167
        # https://github.com/abarmat/python-oembed/blob/master/oembed/__init__.py#L181-L187
        if 'html' in json_data:
            json_data['html'] = sanitize_iframe_tag(json_data['html'])
        for k, v in self.tr_fields.iteritems():
            if json_data.get(k):
                self.widgets[v].value = unicode(json_data[k])
        if json_data.get('thumbnail_url'):
            self.set_image(json_data.get('thumbnail_url'))
 def handleAdd(self, action):
     if self.request.get('form.widgets.url') and \
        not self.request.get('form.widgets.embed_html'):
         fields = {'width': 'form.widgets.width',
                   'height': 'form.widgets.height',
                   'description': 'form.widgets.IDublinCore.description',
                   'title': 'form.widgets.IDublinCore.title',
                   'html': 'form.widgets.embed_html'}
         consumer = component.getUtility(IConsumer)
         json_data = consumer.get_data(self.request['form.widgets.url'],
                                       maxwidth=None,
                                       maxheight=None,
                                       format='json')
         for k, v in fields.iteritems():
             if json_data.get(k):
                 self.request[v] = unicode(json_data[k])
     data, errors = self.extractData()
     self.handle_image(data)
     self.set_custom_embed_code(data)
     if errors:
         self.status = self.formErrorsMessage
         return
     obj = self.createAndAdd(data)
     if obj is not None:
         # mark only as finished if we get the new object
         self._finishedAdd = True
         api.portal.show_message(
             _(u'Item created'), self.request, type='info')
Ejemplo n.º 7
0
    def load_oembed(self, action):
        url = self.widgets['url'].value

        if not self._validate_url(url):
            api.portal.show_message(
                _(u'Invalid URL'), request=self.request, type='error')
            return

        json_data = self.get_data(
            url, maxwidth=None, maxheight=None, format='json')

        if json_data is None:
            json_data = self.get_fallback(url)
            if json_data is None:
                return
        # html parameter not always required:
        # https://github.com/abarmat/python-oembed/blob/master/oembed/__init__.py#L157-L167
        # https://github.com/abarmat/python-oembed/blob/master/oembed/__init__.py#L181-L187
        if 'html' in json_data:
            json_data['html'] = sanitize_iframe_tag(json_data['html'])
        for k, v in self.tr_fields.iteritems():
            if json_data.get(k):
                self.widgets[v].value = unicode(json_data[k])
        if json_data.get('thumbnail_url'):
            self.set_image(json_data.get('thumbnail_url'))
    def get_data(self, url, maxwidth=None, maxheight=None, format='json'):
        """Return the data provided by the endpoint."""

        consumer = component.getUtility(IConsumer)
        json_data = None
        try:
            json_data = consumer.get_data(
                url, maxwidth=None, maxheight=None, format='json')
        except HTTPError, e:
            if e.code == 401:
                api.portal.show_message(
                    _(u'Unauthorized request'), request=self.request, type='error')
            elif e.code == 404:
                api.portal.show_message(
                    _(u'URL not found'), request=self.request, type='error')
            else:
                logger.warn(e)
Ejemplo n.º 9
0
    def get_data(self, url, maxwidth=None, maxheight=None, format='json'):
        """Return the data provided by the endpoint."""

        consumer = component.getUtility(IConsumer)
        json_data = None
        try:
            json_data = consumer.get_data(
                url, maxwidth=None, maxheight=None, format='json')
        except HTTPError, e:
            if e.code == 401:
                api.portal.show_message(
                    _(u'Unauthorized request'), request=self.request, type='error')
            elif e.code == 404:
                api.portal.show_message(
                    _(u'URL not found'), request=self.request, type='error')
            else:
                logger.warn(e)
Ejemplo n.º 10
0
 def handleApply(self, action):
     data, errors = self.extractData()
     self.handle_image(data)
     self.set_custom_embed_code(data)
     if errors:
         self.status = self.formErrorsMessage
         return
     self.applyChanges(data)
     api.portal.show_message(
         _(u'Changes saved.'), self.request, type='info')
     self.request.response.redirect(self.nextURL())
     notify(EditFinishedEvent(self.context))
Ejemplo n.º 11
0
 def handleApply(self, action):
     data, errors = self.extractData()
     self.handle_image(data)
     self.set_custom_embed_code(data)
     if errors:
         self.status = self.formErrorsMessage
         return
     self.applyChanges(data)
     IStatusMessage(self.request).addStatusMessage(
         _(u"Changes saved."), "info")
     self.request.response.redirect(self.nextURL())
     notify(EditFinishedEvent(self.context))
Ejemplo n.º 12
0
 def handleApply(self, action):
     data, errors = self.extractData()
     self.handle_image(data)
     self.set_custom_embed_code(data)
     if errors:
         self.status = self.formErrorsMessage
         return
     self.applyChanges(data)
     api.portal.show_message(
         _(u'Changes saved.'), self.request, type='info')
     self.request.response.redirect(self.nextURL())
     notify(EditFinishedEvent(self.context))
Ejemplo n.º 13
0
def validate_int_or_percentage(value):
    """Check if size is an positive integer (less than 9999) or a
    percetage.

    :param value: number to be validated
    :type value: string
    :raises:
        :class:`~zope.interface.Invalid` if the value is not valid
    """
    if not value:
        return

    p = re.compile(r'^\d{1,4}%?$')
    if p.match(value) is None:
        raise Invalid(_(u'Value should be an integer or a percentage.'))
Ejemplo n.º 14
0
def validate_int_or_percentage(value):
    """Check if size is an positive integer (less than 9999) or a
    percetage.

    :param value: number to be validated
    :type value: string
    :raises:
        :class:`~zope.interface.Invalid` if the value is not valid
    """
    if not value:
        return

    p = re.compile(r'^\d{1,4}%?$')
    if p.match(value) is None:
        raise Invalid(_(u'Value should be an integer or a percentage.'))
Ejemplo n.º 15
0
    def load_oembed(self, action):
        url = self.widgets['url'].value

        if not self._validate_url(url):
            api.portal.show_message(
                _(u'Invalid URL'), request=self.request, type='error')
            return

        json_data = self.get_data(
            url, maxwidth=None, maxheight=None, format='json')

        if json_data is None:
            json_data = self.get_fallback(url)
            if json_data is None:
                return
        for k, v in self.tr_fields.iteritems():
            if json_data.get(k):
                self.widgets[v].value = unicode(json_data[k])
        if json_data.get('thumbnail_url'):
            self.set_image(json_data.get('thumbnail_url'))
Ejemplo n.º 16
0
 def handleCancel(self, action):
     api.portal.show_message(
         _(u'Edit cancelled.'), self.request, type='info')
     self.request.response.redirect(self.nextURL())
     notify(EditCancelledEvent(self.context))
Ejemplo n.º 17
0
 def handleCancel(self, action):
     IStatusMessage(self.request).addStatusMessage(
         _(u"Edit cancelled."), "info")
     self.request.response.redirect(self.nextURL())
     notify(EditCancelledEvent(self.context))
Ejemplo n.º 18
0
 def handleCancel(self, action):
     api.portal.show_message(_(u'Edit cancelled.'),
                             self.request,
                             type='info')
     self.request.response.redirect(self.nextURL())
     notify(EditCancelledEvent(self.context))
Ejemplo n.º 19
0
class IEmbedder(form.Schema):
    """ A representation of a content embedder content type
    """

    dexteritytextindexer.searchable('text', 'alternate_content')

    form.order_before(**{'url': '*'})

    url = schema.ASCIILine(
        title=_(u'Multimedia URL'),
        description=_(u'The URL for your multimedia file. Can be a URL ' +
                      u'from YouTube, Vimeo, SlideShare, SoundCloud or ' +
                      u'other main multimedia websites.'),
        required=False,
    )

    width = schema.ASCIILine(
        title=_(u'Width'),
        description=
        _(u'Can be expressed as a decimal number or a percentage, e.g., 270 or 50%.'
          ),
        required=True,
    )

    height = schema.Int(
        title=_(u'Height'),
        description=_(u'Can be expressed as a decimal number, e.g., 480.'),
        required=True,
        min=1,
        max=9999,
    )

    embed_html = schema.Text(
        title=_(u'Embedded HTML code'),
        description=_(u'HTML code to render the embedded media.'),
        required=True,
    )

    player_position = schema.Choice(
        title=_(u'Player position'),
        description=_(u''),
        default=u'Top',
        required=True,
        values=[u'Top', u'Bottom', u'Left', u'Right'],
    )

    form.widget(image=EmbedderImageFieldWidget)
    image = NamedImage(
        title=_(u'Preview image'),
        description=_(u'Image to be used when listing content.'),
        required=False,
    )

    text = RichText(
        title=_(u'Body text'),
        required=False,
    )

    alternate_content = RichText(
        title=_(u'Alternate content'),
        description=_(
            u'Description or transcription for accessibility (WCAG) users.'),
        required=False,
    )