def convert(identifier):
     try:
         content =  get_content_from_id(int(identifier))
     except ValueError:
         self.raise_error('invalid_value', field)
     if ISilvaObject.providedBy(content):
         return content
     return None
def get_subscribable_content(identifier):
    """Fetch a subscribable content out of an identifier.
    """
    if not identifier:
        # Identifier is missing.
        raise errors.NotSubscribableError()
    try:
        identifier = int(identifier)
    except (ValueError, TypeError):
        # Identifier is an invalid format.
        raise errors.NotSubscribableError()
    content = get_content_from_id(identifier)
    if content is None:
        # Idnetifier did not resolve.
        raise errors.NotSubscribableError()
    return content
Example #3
0
 def validate(self, data):
     """Validate ghost target before setting it.
     """
     # This is not beauty, but it works.
     content_id = data.get(field_name)
     if content_id is silvaforms.NO_VALUE:
         # If there value is required it is already checked
         return []
     getManager = getComponent((wanted,), IGhostManager)
     if adding:
         manager = getManager(container=self.form.context)
     else:
         manager = getManager(ghost=self.form.context)
     error = manager.validate(get_content_from_id(content_id), adding)
     if error is not None:
         identifier = widgetId(self.form, self.fields[field_name])
         return [Error(error.doc(), identifier)]
     return []
Example #4
0
 def validate(self, data):
     """Validate ghost target before setting it.
     """
     # This is not beauty, but it works.
     content_id = data.get(field_name)
     if content_id is silvaforms.NO_VALUE:
         # If there value is required it is already checked
         return []
     getManager = getComponent((wanted, ), IGhostManager)
     if adding:
         manager = getManager(container=self.form.context)
     else:
         manager = getManager(ghost=self.form.context)
     error = manager.validate(get_content_from_id(content_id), adding)
     if error is not None:
         identifier = widgetId(self.form, self.fields[field_name])
         return [Error(error.doc(), identifier)]
     return []
Example #5
0
 def update_reference_for(self, attributes):
     name, reference = self.get_reference(
         attributes['data-silva-reference'])
     if reference is not None:
         target_id = attributes.get('data-silva-target', '0')
         try:
             target_id = int(str(target_id))
             assert get_content_from_id(target_id) is not None
         except (ValueError, AssertionError):
             # Invalid target id, set it as zero (broken)
             target_id = 0
     else:
         # Invalid reference. We create a new one and mark the
         # target as broken
         name, reference = self.get_reference('new')
         target_id = 0
     if target_id != reference.target_id:
         reference.set_target_id(target_id)
     attributes['reference'] = name
    def add(self, value_id=_marker, value=_marker, sub_widget=_marker):
        if sub_widget is _marker:
            sub_widget = self.widget
        sub_widget.value_id = None
        sub_widget.value_url = None
        sub_widget.value_title = None
        sub_widget.value_icon = None
        sub_widget.value_path = None

        if value_id is not _marker:
            try:
                value_id = int(value_id)
            except (ValueError, TypeError):
                value_id = 0

            if value is _marker:
                if value_id:
                    value = get_content_from_id(value_id)
                else:
                    value = None
            sub_widget.value_id = value_id
        elif value is not _marker:
            sub_widget.value_id = get_content_id(value)
        else:
            if not self.multiple:
                self.set_lookup_url(self.context)
            return

        # None as a icon, it is missing
        sub_widget.value_icon = self.get_icon_tag(value)
        if value is not None:
            sub_widget.value_title = value.get_title_or_id()
            sub_widget.value_url = absoluteURL(value, self.request)
            sub_widget.value_path = self.get_content_path(value)
            if not self.multiple:
                self.set_lookup_url(value)
        else:
            sub_widget.value_title = self.message
            if not self.multiple:
                self.set_lookup_url(self.context)
    def convert(self, context):
        title = self.getattr('title', default='')
        name = self.getattr('name', default=None)
        href = self.getattr('href', default='#')
        anchor = self.getattr('_silva_anchor', default=None)
        window_target = self.getattr('target', default='')

        link_attributes = {'target': window_target,
                           'title': title,
                           'anchor': anchor}

        if name is not None and href == '#':
            # Case one, we are an anchor
            if self.getattr('class', None) == 'index':
                # index item
                text = ''.join([t.convert(context).asBytes('UTF-8') for
                                t in extract_texts(self, context)])
                textnode = Frag()
                if text and (text[0] != '[' or text[-1] != ']'):
                    textnode = Text(text)
                return Frag(
                    textnode,
                    silva.index(name=name, title=title))
            else:
                # named anchor, probably pasted from some other page
                return Frag(self.content.convert(context))
        elif self.hasattr('_silva_reference'):
            # Case of a Silva reference used
            reference_name = str(self.getattr('_silva_reference'))
            reference_name, reference = context.get_reference(reference_name)
            if reference is not None:
                target_id = self.getattr('_silva_target', '0')
                try:
                    target_id = int(str(target_id))
                    assert get_content_from_id(target_id) is not None
                except (ValueError, AssertionError):
                    # Invalid target id, set it as zero (broken)
                    target_id = 0
            else:
                # Invalid reference. We create a new one and mark the
                # target as broken
                reference_name, reference = context.get_reference('new')
                target_id = 0

            # If the target changed, update it
            if target_id != reference.target_id:
                reference.set_target_id(target_id)
            link_attributes['reference'] = reference_name
            return silva.link(
                self.content.convert(context),
                **link_attributes)
        elif self.hasattr('href'):
            # External links
            url = self.getattr('_silva_href', None)
            if url is None:
                url = self.getattr('href', '')
            if unicode(url).startswith('/'):
                # convert to physical path before storing
                url = Text(IPath(context.request).urlToPath(unicode(url)))
            if url:
                link_attributes['url'] = url
            return silva.link(
                self.content.convert(context),
                **link_attributes)
        elif anchor is not None:
            # Link to an anchor on the same page
            return silva.link(
                self.content.convert(context),
                **link_attributes)
        else:
            return Frag()
Example #8
0
 def set_target_id(self, target_id):
     self._check_image(get_content_from_id(target_id))
     super(ImageDeleteSourceReferenceValue, self).set_target_id(target_id)
Example #9
0
 def set_target_id(self, target_id):
     self._check_image(get_content_from_id(target_id))
     super(ImageDeleteSourceReferenceValue, self).set_target_id(target_id)