def serialize(self, results):
        """Serialize fields of a list of content type objects.

        :param results: [required] list of objects to be serialized
        :type results: list of catalog brains
        :returns: list of serialized objects
        :rtype: list of dictionaries
        """
        s = []
        for obj in results:
            # initialize a dictionary with the object uri
            # XXX: should we use the UUID?
            fields = dict(uri=obj.absolute_url())
            # continue with the rest of the fields
            if IBaseContent.providedBy(obj):
                obj_fields = obj.Schema().fields()
                obj_fields = [(f.getName(), f.get(obj)) for f in obj_fields]
            else:
                schema = getUtility(IDexterityFTI, name=obj.portal_type).lookupSchema()
                obj_fields = [(f, getattr(obj, f)) for f in schema]
            for name, data in obj_fields:
                if name in SERIALIZABLE_FIELD_NAMES:
                    fields[name] = type_cast(data, name, obj)
            s.append(fields)
        return s
Exemplo n.º 2
0
    def __call__(self):
        request = self.context.REQUEST

        event_uid = request.get('event')

        if event_uid:
            event_uid = event_uid.split('UID_')[1]
        brains = self.context.portal_catalog(UID=event_uid)

        obj = brains[0].getObject()
        dayDelta, minuteDelta = float(request.get('dayDelta')), \
            float(request.get('minuteDelta'))
        if IBaseContent.providedBy(obj):
            startDate, endDate = obj.startDate, obj.endDate
            startDate = startDate + dayDelta + minuteDelta / 1440.0
            endDate = endDate + dayDelta + minuteDelta / 1440.0

            obj.setStartDate(startDate)
            obj.setEndDate(endDate)
        else:
            startDate, endDate = obj.start, obj.end
            startDate = startDate + timedelta(days=dayDelta,
                                              minutes=minuteDelta)
            endDate = endDate + timedelta(days=dayDelta, minutes=minuteDelta)
            obj.start = startDate
            obj.end = endDate
        obj.reindexObject()
        return True
Exemplo n.º 3
0
 def get_image_filename(context, name):
     # Archetypes Support
     if IBaseContent is not None and IBaseContent.providedBy(context):
         field = context.getField(name)
         if field is not None:
             image = field.get(context)
             if image is not None:
                 fname = image.getFilename()
                 if not fname:
                     fname = context.getId()
                 ctype = image.getContentType()
                 ext = CONTENT_TYPE_MAP.get(ctype, '')
                 if ext not in fname.lower():
                     fname += ext
                 normalizer = queryUtility(IFileNameNormalizer)
                 if normalizer is not None:
                     fname = normalizer.normalize(safe_unicode(fname))
                 return fname
         return None
     # Otherwise attribute lookup
     has_image = getattr(aq_base(context), name, None) is not None
     if has_image:
         image = getattr(context, name)
         fname = getattr(image, 'filename', None)
         if not fname:
             fname = context.getId()
         ctype = getattr(image, 'contentType', None)
         ext = CONTENT_TYPE_MAP.get(ctype, '')
         if ext not in fname.lower():
             fname += ext
         normalizer = queryUtility(IFileNameNormalizer)
         if normalizer is not None:
             fname = normalizer.normalize(safe_unicode(fname))
         return fname
Exemplo n.º 4
0
    def serialize(self, results):
        """Serialize fields of a list of content type objects.

        :param results: [required] list of objects to be serialized
        :type results: list of catalog brains
        :returns: list of serialized objects
        :rtype: list of dictionaries
        """
        s = []
        for obj in results:
            # initialize a dictionary with the object uri
            # XXX: should we use the UUID?
            fields = dict(uri=obj.absolute_url())
            # continue with the rest of the fields
            if IBaseContent.providedBy(obj):
                obj_fields = obj.Schema().fields()
                obj_fields = [(f.getName(), f.get(obj)) for f in obj_fields]
            else:
                schema = getUtility(IDexterityFTI,
                                    name=obj.portal_type).lookupSchema()
                obj_fields = [(f, getattr(obj, f)) for f in schema]
            for name, data in obj_fields:
                if name in SERIALIZABLE_FIELD_NAMES:
                    fields[name] = type_cast(data, name, obj)
            s.append(fields)
        return s
Exemplo n.º 5
0
 def _fix_at_fields(self, obj, data):
     # unlike dexterity, AT text fields use same serializer as rich text,
     # which wrecks our index mapping
     if IBaseContent.providedBy(obj):
         for field in AT_SIMPLE_TEXT_FIELDS:
             if field in data and isinstance(data[field], dict):
                 data[field] = data[field]['data']
Exemplo n.º 6
0
 def _getParentDocument(self, context):
     """Return the parent document (in case of comments)"""
     while True:
         if IBaseContent.providedBy(context):
             return context
         if IApplication.providedBy(context):
             return None
         context = aq_parent(context)
Exemplo n.º 7
0
 def _root_child_is_content(self, container, child):
     if not IFinderRoot.providedBy(container):
         return False
     if AT_INSTALLED and IBaseContent.providedBy(child):
         return True
     if DX_INSTALLED and IDexterityContent.providedBy(child):
         return True
     return False
Exemplo n.º 8
0
 def get_primary_caption(self):
     context = self.context
     if IBaseContent is not None and IBaseContent.providedBy(context):
         field = context.getField('imageCaption')
         if field is not None:
             return safe_unicode(field.get(context))
         return None
     if getattr(aq_base(context), 'image_caption', None):
         return getattr(context, 'image_caption')
Exemplo n.º 9
0
def get_language(context):
    ps = context.restrictedTraverse('plone_portal_state')
    default_language = ps.default_language()
    content = aq_base(context)
    if IBaseContent.providedBy(content):
        language = content.Language()
    else:
        language = content.language if safe_hasattr(content, 'language') else ''
    return language if language else default_language
Exemplo n.º 10
0
def get_language(context):
    ps = context.restrictedTraverse("plone_portal_state")
    default_language = ps.default_language()
    content = aq_base(context)
    if IBaseContent.providedBy(content):
        language = content.Language()
    else:
        language = content.language if hasattr(content, "language") else ""
    return language if language else default_language
Exemplo n.º 11
0
def get_language(context):
    ps = context.restrictedTraverse('plone_portal_state')
    default_language = ps.default_language()
    content = aq_base(context)
    if IBaseContent.providedBy(content):
        language = content.Language()
    else:
        language = content.language if safe_hasattr(content,
                                                    'language') else ''
    return language if language else default_language
Exemplo n.º 12
0
    def _expand_binary_data(self, obj, data):
        max_size = es_config.max_blobsize
        is_archetype = False
        if HAS_ARCHETYPES and IBaseContent.providedBy(obj):
            is_archetype = True
            schema = obj.Schema()
        for fieldname in self._iterate_binary_fields(obj, data):
            if fieldname not in data:
                data[fieldname] = None
                continue
            if is_archetype:
                field = schema[fieldname]
                value = field.get(obj)
                if value is None:
                    data[fieldname] = None
                    continue
                data[fieldname + '_meta'] = data[fieldname]
                if IBlobWrapper.providedBy(value):
                    if max_size and value.get_size() > max_size:
                        data[fieldname] = None
                        del data[fieldname + '_meta']
                        msg = 'File too big for ElasticSearch Indexing: {0}'
                        logger.info(msg.format(obj.absolute_url(), ), )
                        continue

                    with value.getBlob().open() as fh:
                        data[fieldname] = base64.b64encode(fh.read())
                elif ITextField.providedBy(field):
                    data[fieldname] = base64.b64encode(
                        data[fieldname + '_meta']['data'].encode('utf8'))
            else:
                field = getattr(obj, fieldname, None)
                if field is None:
                    data[fieldname] = None
                    continue
                data[fieldname + '_meta'] = data[fieldname]
                if IBlobby.providedBy(field):
                    if max_size and field.getSize() > max_size:
                        data[fieldname] = None
                        del data[fieldname + '_meta']
                        msg = 'File too big for ElasticSearch Indexing: {0}'
                        logger.info(msg.format(obj.absolute_url(), ), )
                        continue
                    with field.open() as fh:
                        data[fieldname] = base64.b64encode(fh.read())
                elif IRichTextValue.providedBy(field):
                    data[fieldname] = base64.b64encode(
                        data[fieldname + '_meta']['data'].encode('utf8'), )
            if max_size and len(data[fieldname]) > max_size:
                data[fieldname] = None
                del data[fieldname + '_meta']
                logger.info(
                    'File too big for ElasticSearch Indexing: {0}'.format(
                        obj.absolute_url(), ), )
Exemplo n.º 13
0
    def get_body_parts(self):
        text = None
        context = self.context
        text_field = self.primary_text
        if IBaseContent is not None and IBaseContent.providedBy(context):
            field = context.getField(text_field)
            if field is not None:
                text = safe_unicode(field.get(context))
        elif getattr(aq_base(context), text_field, None):
            text = getattr(context, text_field).raw

        if text:
            return self.html_to_components(text)
Exemplo n.º 14
0
    def getPayload(self):
        """
        Get mimetype and data for associated file
        """
        if IBaseContent.providedBy(self.context):
            primary_field = self.context.getPrimaryField()
            mimetype = self.context.getContentType()
            data = primary_field.get(self.context)
        else:
            primary_field = IPrimaryFieldInfo(self.context).value
            mimetype = primary_field.contentType
            data = primary_field.data

        return mimetype, str(data)
Exemplo n.º 15
0
def get_images_view(context):
    view = context.unrestrictedTraverse('@@images', None)
    field = 'image'
    if view:
        fields = ['image', 'leadImage', 'portrait']
        if IBaseContent.providedBy(context):
            schema = context.Schema()
            field = [f for f in schema.keys() if f in fields]
            if field:
                field = field[0]
                # if a content has an image field that isn't an ImageField
                # (for example a relation field), set field='' to avoid errors
                if schema[field].type not in ['image', 'blob']:
                    field = ''
    return (view, field) if (view and field) else (None, None)
Exemplo n.º 16
0
def get_images_view(context):
    view = context.unrestrictedTraverse('@@images', None)
    field = 'image'
    if view:
        fields = ['image', 'leadImage', 'portrait']
        if IBaseContent.providedBy(context):
            schema = context.Schema()
            field = [f for f in schema.keys() if f in fields]
            if field:
                field = field[0]
                # if a content has an image field that isn't an ImageField
                # (for example a relation field), set field='' to avoid errors
                if schema[field].type not in ['image', 'blob']:
                    field = ''
    return (view, field) if (view and field) else (None, None)
Exemplo n.º 17
0
def indexTree(obj, portal, vars=None):
    if not IBaseContent.providedBy(obj):
        # No archetypes, no support
        raise AttributeError
    for field in obj.schema.fields():
        if not IVocabulary.providedBy(field.vocabulary):
            # Wrong vocabulary type
            continue
        termUIDs = field.getRaw(obj)
        if not isinstance(termUIDs, list):
            termUIDs = [termUIDs]
        all_dicts = field.vocabulary.getVocabularyDict(obj)
        path = []
        for term in termUIDs:
            path.extend(getPath(term, ('', all_dicts)))
        return path
Exemplo n.º 18
0
def check_at_blobs(context):
    """ Archetypes content checker for damaged blob fields
    """

    if IBaseContent.providedBy(context):
        schema = context.Schema()
        for field in schema.fields():
            id = field.getName()
            if isinstance(field, FileField):
                try:
                    field.get_size(context)
                except POSKeyError:
                    logger.warn("Found damaged AT FileField %s on %s" %
                                (id, context.absolute_url()))
                    return True
    return False
Exemplo n.º 19
0
def get_images_view(context):
    request = getRequest()
    key = "cache-view-%s" % (context)
    cache = IAnnotations(request)
    value = cache.get(key, None)
    if not value:
        view = context.unrestrictedTraverse('@@images', None)
        field = 'image'
        if view:
            fields = ['image', 'leadImage', 'portrait']
            if IBaseContent.providedBy(context):
                schema = context.Schema()
                field = [f for f in schema.keys() if f in fields]
                if field:
                    field = field[0]
        value = (view, field) if (view and field) else (None, None)
        cache[key] = value
    return value
Exemplo n.º 20
0
def get_images_view(context):
    request = getRequest()
    key = "cache-view-%s" % (context)
    cache = IAnnotations(request)
    value = cache.get(key, None)
    if not value:
        view = context.unrestrictedTraverse('@@images', None)
        field = 'image'
        if view:
            fields = ['image', 'leadImage', 'portrait']
            if IBaseContent.providedBy(context):
                schema = context.Schema()
                field = [field for field in schema.keys() if field in fields]
                if field:
                    field = field[0]
        value = (view, field) if (view and field) else (None, None)
        cache[key] = value
    return value
Exemplo n.º 21
0
    def get_recipients(self):
        '''
        The recipients of this mail
        '''
        # Try to load data from the target object
        fieldName = str(self.element.fieldName)
        obj = self.get_target_obj()

        # 1: object attribute
        try:
            # BBB don't have time to investigate difference between original __getattribute__
            # and this getattr... _getattribute__ remove the possibility to use objects chain
            attr = getattr(obj, fieldName)
            # 3: object method
            if hasattr(attr, '__call__'):
                recipients = attr()
                logger.debug('getting e-mail from %s method' % fieldName)
            else:
                recipients = attr
                logger.debug('getting e-mail from %s attribute' % fieldName)
        except AttributeError:
            # 2: try with AT field
            if IBaseContent.providedBy(obj):
                field = obj.getField(fieldName)
                if field:
                    recipients = field.get(obj)
                else:
                    recipients = False
            else:
                recipients = False
            if not recipients:
                recipients = obj.getProperty(fieldName, [])
                if recipients:
                    logger.debug('getting e-mail from %s CMF property' %
                                 fieldName)
            else:
                logger.debug('getting e-mail from %s AT field' % fieldName)

        # now transform recipients in a iterator, if needed
        if type(recipients) == str or type(recipients) == unicode:
            recipients = [
                str(recipients),
            ]
        return filter(bool, recipients)
Exemplo n.º 22
0
def check_at_blobs(context):
    """ Archetypes content checker.
        Return True if purge needed
    """

    if IBaseContent.providedBy(context):

        schema = context.Schema()
        for field in schema.fields():
            id = field.getName()
            if isinstance(field, FileField):
                try:
                    field.get_size(context)
                except POSKeyError:
                    print "Found damaged AT FileField %s on %s" % (
                            id, context.absolute_url())
                    return True

    return False
Exemplo n.º 23
0
def check_at_blobs(context, report_file):
    """ Archetypes content checker.

    Return True if purge needed
    """
    if IBaseContent.providedBy(context):
        schema = context.Schema()
        for field in schema.fields():
            id = field.getName()
            if isinstance(field, FileField):
                try:
                    field.get_size(context)
                except POSKeyError:
		    msg = "Found damaged AT FileField %s on %s" % (id, context.absolute_url())
		    report_file.write(msg)
                    print msg
                    return True

    return False
Exemplo n.º 24
0
    def get_recipients(self):
        '''
        The recipients of this mail
        '''
        # Try to load data from the target object
        fieldName = str(self.element.fieldName)
        obj = self.get_target_obj()

        # 1: object attribute
        try:
            # BBB don't have time to investigate difference between original __getattribute__
            # and this getattr... _getattribute__ remove the possibility to use objects chain
            attr = getattr(obj, fieldName)
            # 3: object method
            if hasattr(attr, '__call__'):
                recipients = attr()
                logger.debug('getting e-mail from %s method' % fieldName)
            else:
                recipients = attr
                logger.debug('getting e-mail from %s attribute' % fieldName)
        except AttributeError:
            # 2: try with AT field
            if IBaseContent.providedBy(obj):
                field = obj.getField(fieldName)
                if field:
                    recipients = field.get(obj)
                else:
                    recipients = False
            else:
                recipients = False
            if not recipients:
                recipients = obj.getProperty(fieldName, [])
                if recipients:
                    logger.debug('getting e-mail from %s CMF property'
                                 % fieldName)
            else:
                logger.debug('getting e-mail from %s AT field' % fieldName)

        # now transform recipients in a iterator, if needed
        if type(recipients) == str or type(recipients) == unicode:
            recipients = [str(recipients), ]
        return filter(bool, recipients)
Exemplo n.º 25
0
def get_images_view(context):
    request = getRequest()
    key = 'cache-view-%s' % (context)
    cache = IAnnotations(request)
    value = cache.get(key, None)
    if not value:
        view = context.unrestrictedTraverse('@@images', None)
        field = 'image'
        if view:
            fields = ['image', 'leadImage', 'portrait']
            if IBaseContent.providedBy(context):
                schema = context.Schema()
                field = [f for f in schema.keys() if f in fields]
                if field:
                    field = field[0]
                    # if a content has an image field that isn't an ImageField
                    # (for example a relation field), set field='' to avoid errors
                    if schema[field].type not in ['image', 'blob']:
                        field = ''
        value = (view, field) if (view and field) else (None, None)
        cache[key] = value
    return value
Exemplo n.º 26
0
    def replace_text(self, item, mode, fields, source, destination):
        changed = False

        def get_new_value(current_value):
            if type(current_value) == str:
                s = safe_encode(source)
                d = safe_encode(destination)
            else:
                s = safe_unicode(source)
                d = safe_unicode(destination)

            if mode == 'plain':
                new_value = current_value.replace(s, d)
            elif mode == 'regexp':
                new_value = re.sub(s, d, current_value)
            elif mode == 'empty':
                new_value = current_value if current_value else d
            else:
                raise ValueError("Unhandled option for text_replace_mode: %s" % mode)
            return new_value

        for field in fields:
            if field == 'short-name':
                continue

            # validation
            if field == 'pdf_url':
                if not IBibliographicItem.providedBy(item):
                    continue

                current = item.pdf_url
                new = get_new_value(current)
                if current != new:
                    item.pdf_url = new
                    changed = True
            elif field == 'publication_url':
                if not IBibliographicItem.providedBy(item):
                    continue

                current = item.publication_url
                new = get_new_value(current)
                if current != new:
                    item.publication_url = new
                    changed = True
            elif field == 'text':
                if IBaseContent.providedBy(item):
                    at_field = item.getField('text')
                    if not at_field:
                        IStatusMessage(self.request).add("%s has no field: %s" % (item.absolute_url(), field), 'error')
                        continue

                    current = at_field.getAccessor(item)()
                    new = get_new_value(current)
                    if current != new:
                        at_field.getMutator(item)(new)
                        changed = True
                elif base_hasattr(item, 'text'):
                    if isinstance(item.text, RichTextValue):
                        current = item.text.raw
                    else:
                        current = item.text

                    new = get_new_value(current)
                    if current != new:
                        if isinstance(item.text, RichTextValue):
                            rtv = item.text
                            rtv._raw_holder.value = new
                            item.text = rtv
                        else:
                            item.text = new
                        changed = True
            elif field == 'title':
                current = item.Title()
                new = get_new_value(current)
                if current != new:
                    item.setTitle(new)
                    changed = True
            elif field == 'description':
                current = item.Description()
                new = get_new_value(current)
                if current != new:
                    item.setDescription(new)
                    changed = True

        if 'short-name' in fields:
            if IFolderish.providedBy(item):
                IStatusMessage(self.request).add('Mass change of short name is forbidden for folder : %s' % item.absolute_url())
            else:
                current = item.id
                new = str(get_new_value(current))
                if current != new:
                    try:
                        api.content.rename(item, new_id=new)
                        changed = True
                    except CopyError:
                        IStatusMessage(self.request).add('Rename failed : %s' % item.absolute_url())
        elif changed:
                item.reindexObject(idxs=['SearchableText'])

        return changed