コード例 #1
0
    def updateWidgets(self):
        config = _load_config(self.request.get("form.widgets.config"))
        section = None
        for section_id in config.keys():
            tmp = config[section_id]
            if tmp.get("blueprint", "") in SOURCE_SECTIONS:
                section = tmp
                break
        if not section:
            raise Exception("Source section not found.")

        # Omit some fields depending on the selected source
        if section["blueprint"] == "collective.jsonmigrator.catalogsource":
            self.fields = self.fields.omit("remote_path", "remote_crawl_depth", "remote_skip_path")
        elif section["blueprint"] == "collective.jsonmigrator.remotesource":
            self.fields = self.fields.omit("catalog_path", "catalog_query")

        # Fill in default values from the transmogrifier config file
        for option, value in section.items():
            field = self.fields.get(option.replace("-", "_"))
            if field:
                field = field.field
                value = value.decode("utf8")
                if IFromUnicode.providedBy(field):
                    field.default = field.fromUnicode(value)
                elif IList.providedBy(field):
                    field.default = [field.value_type.fromUnicode(v) for v in value.split()]

        super(JSONMigratorRun, self).updateWidgets()
        self.widgets["config"].mode = interfaces.HIDDEN_MODE
コード例 #2
0
    def map_with_vocab(self, behavior, fieldname, value):
        """Look in the schema for a vocab and return the mapped value
        """

        if type(value) == int:
            return str(value)

        portal = self.layer['portal']
        fields = getFieldsInOrder(behavior)
        for name, field in fields:
            if name == fieldname:

                # We have different types of fields, so we have to check,
                # that we become the vocabulary
                value_type = field

                if IList.providedBy(field) or ITuple.providedBy(field):
                    value_type = field.value_type

                if IChoice.providedBy(value_type):
                    if value_type.vocabulary:
                        vocab = value_type.vocabulary(portal)

                    else:
                        vocab = getVocabularyRegistry().get(
                            portal, value_type.vocabularyName)

                    value = vocab.getTerm(value).title

        return value
コード例 #3
0
    def verify_registry_key(self):
        registry = queryUtility(IRegistry)

        if registry is None:
            logger.warning('Plone registry is not available, doing nothing.')
            return False

        record = registry.records.get(self.name)

        if record is None:
            logger.warning(
                'The registry key for the utility registry `%s` is not '
                'registered.', self.name)
            return False

        if not (IList.providedBy(record.field) and
                IChoice.providedBy(record.field.value_type) and
                record.field.value_type.vocabularyName == self.available_vocab
                ):
            logger.warning(
                'The registry key for the utility registry `%s` is registered '
                'incorrectly.', self.name)
            return False

        return True
コード例 #4
0
    def __call__(self):
        context = aq_inner(self.context)
        fieldname = self.request.get('fieldname')
        portal_type = self.request.get('portal_type')

        fti = zope.component.getUtility(IDexterityFTI, name=portal_type)
        schema = fti.lookupSchema()
        field = schema.get(fieldname)
        if field is None:
            for behaviorname in fti.behaviors:
                behavior = resolve(behaviorname)
                field = behavior.get(fieldname.split('.')[1])
                if field is not None:
                    break
        if IList.providedBy(field) or ISet.providedBy(field):
            vname = field.value_type.vocabularyName
        else:
            vname = field.vocabularyName
        factory = zope.component.getUtility(IVocabularyFactory, vname)
        tree = factory(context)
        leafsOnly = getattr(tree, 'leafsOnly', True)
        
        tree = dict2dynatree(
            self.context,
            tree,
            [],  # selected not needed here, this is done at js side
            leafsOnly,
            False
        )
        return json.dumps(tree)
コード例 #5
0
def vocabulary_mailattributes(context):
    terms = list()
    for field in grok.AutoFields(interfaces.IMail):
        if IList.providedBy(field.field):
            name = '%s %s' % (field.field.title, _('(List)'),)
        else:
            name = field.field.title
        terms.append(vocabulary.SimpleTerm(value=field.field.getName(), title=name))
    return vocabulary.SimpleVocabulary(terms)
コード例 #6
0
    def get_fieldtype_by_schema(self, field):
        type_field = ""
        if IRelationList.providedBy(field):
            type_field = "relation"
        elif "ListField" in str(field):
            type_field = "datagridfield"
            self.datagrids[field.__name__] = False
        elif IChoice.providedBy(field):
            type_field = "choice"
        elif ITextLine.providedBy(field):
            type_field = "text"
        elif IList.providedBy(field):
            type_field = "list"
        elif IText.providedBy(field):
            type_field = "text"
        elif IRichText.providedBy(field):
            type_field = "text"
        else:
            type_field = "unknown"

        return type_field
コード例 #7
0
    def get_default_value_by_schema(self, field):
        type_field = " "
        if IRelationList.providedBy(field):
            type_field = []
        elif "ListField" in str(field):
            type_field = []
            self.datagrids[field.__name__] = False
        elif IChoice.providedBy(field):
            type_field = " "
        elif ITextLine.providedBy(field):
            type_field = " "
        elif IList.providedBy(field):
            type_field = []
        elif IText.providedBy(field):
            type_field = " "
        elif IRichText.providedBy(field):
            type_field = " "
        else:
            type_field = " "

        return type_field
コード例 #8
0
def vocabulary_propose(context):
    index = 0
    terms = list()
    for field in grok.AutoFields(interfaces.IMail):
        if IList.providedBy(field.field):
            continue
        name = '(Mail) %s' % field.field.title
        index += 1
        value = 'placeholder_field_%s' % index
        terms.append(vocabulary.SimpleTerm(value=value,
                                           token=field.field.getName(),
                                           title=name))
    terms.append(vocabulary.SimpleTerm(value=datetime.now().strftime('%Y-%m-%d%T%H:%M:%S'),
                                       token='datetime_now',
                                       title=translate(_('Date Time'), context=utils.getRequest())))
    terms.append(vocabulary.SimpleTerm(value=datetime.now().strftime('%Y-%m-%d'),
                                       token='date_now',
                                       title=translate(_('Date'), context=utils.getRequest())))
    terms.append(vocabulary.SimpleTerm(value=datetime.now().strftime('%H:%M:%S'),
                                       token='time_now',
                                       title=translate(_('Time'), context=utils.getRequest())))
    return vocabulary.SimpleVocabulary(terms)
コード例 #9
0
    def __getattribute__(self, name):
        if name.startswith('_') or name.startswith(
                'portal_') or name.startswith('@@') or name == 'sql_id':
            return super(SQLDexterityItem, self).__getattribute__(name)
        connection = queryUtility(ISQLConnectionsUtility,
                                  name=self.portal_type,
                                  default=None)
        if connection == None and self.portal_type:
            fti = queryUtility(IDexterityFTI,
                               name=self.portal_type,
                               default=None)
            if not fti:
                return None
            updateConnectionsForFti(fti)
            connection = queryUtility(ISQLConnectionsUtility,
                                      name=self.portal_type,
                                      default=None)
        if name == 'view':
            #be sure session and sqlitem are up to date
            self._v_sql_item = None
            connection.session.close()
        if not connection:
            return super(SQLDexterityItem, self).__getattribute__(name)
        if name == 'UID' and self.sql_virtual:
            return self.portal_type + '-' + connection.sql_table + '-' + str(
                self.sql_id)
        if name == 'id' and 'id' not in connection.fieldnames.keys():
            if not self.sql_virtual:
                return super(SQLDexterityItem, self).__getattribute__(name)
            fti = ISQLTypeSettings(
                getUtility(IDexterityFTI, name=self.portal_type))
            nameFromTitle = INameFromTitle(self, None)
            if nameFromTitle is not None and nameFromTitle.title:
                sql_folder_id = getattr(fti, 'sql_folder_id',
                                        'data-' + self.portal_type)
                title = nameFromTitle.title
                folder = None
                if IRelationValue.providedBy(sql_folder_id):
                    folder = sql_folder_id.to_object
                elif sql_folder_id and sql_folder_id.startswith('/'):
                    portal = getToolByName(getSite(),
                                           'portal_url').getPortalObject()
                    folder = portal.restrictedTraverse(sql_folder_id)
                if folder:
                    name = INameChooser(folder).chooseName(title, self)
                    return name
#                return INameChooser(getSite()).chooseName(title, self)
#                return getUtility(IURLNormalizer).normalize(title)
                return self.sql_id
        if name in connection.fieldnames.keys():
            sql_column = connection.fieldnames[name]
            sql_item = self.getSQLItem()
            try:
                sql_id = getattr(sql_item, connection.sql_id_column, None)
            except orm_exc.DetachedInstanceError:
                self._v_sql_item = None
                sql_item = self.getSQLItem()
                sql_id = getattr(sql_item, connection.sql_id_column, None)
            fieldname = 'name'
            if sql_item and sql_column:
                while '.' in sql_column:
                    sql_key = sql_column.split('.')[0]
                    sql_item = getattr(sql_item, sql_key, None)
                    if isinstance(sql_item, list):
                        value = sql_item
                        fieldname = sql_column.split('.')[-1]
                        break
                    sql_column = '.'.join(sql_column.split('.')[1:])
                else:
                    if not isinstance(sql_item, list):
                        value = getattr(sql_item, sql_column, None)
                if not value and (isinstance(value, list)
                                  or hasattr(value, '_sa_instance_state')):
                    value = ''
                elif (isinstance(value, list)
                      or hasattr(value, '_sa_instance_state')):
                    sqlftis = [
                        a for a in getAllUtilitiesRegisteredFor(IDexterityFTI)
                        if
                        'collective.behavior.sql.behavior.behaviors.ISQLContent'
                        in a.behaviors and getattr(a, 'sql_table', None)
                    ]
                    if name == 'subject':
                        return tuple(
                            [getattr(a, fieldname, '') for a in value])
                    tableftis = []
                    for iface in iterSchemataForType(self.portal_type):
                        if name in iface.names():
                            field = iface[name]
                            if IRelationChoice.providedBy(
                                    field) or IRelationList.providedBy(field):
                                if IRelationChoice.providedBy(field):
                                    allowed_types = field.source.query.get(
                                        'portal_type', [])
                                else:
                                    allowed_types = field.value_type.source.query.get(
                                        'portal_type', [])
                                tableftis = []
                                for sqlfti in sqlftis:
                                    adapted = ISQLTypeSettings(sqlfti, None)
                                    if isinstance(value, list):
                                        classname = value[0].__class__.__name__
                                    else:
                                        classname = value.__class__.__name__
                                    if adapted and getattr(
                                            adapted, 'sql_table',
                                            None) == classname:
                                        if not allowed_types or sqlfti.id in allowed_types:
                                            tableftis.append(adapted)
                                catalog = getToolByName(
                                    getSite(), 'portal_catalog')
                                relations = []
                                for tablefti in tableftis:
                                    sql_id_column = getattr(
                                        tablefti, 'sql_id_column', 'id')
                                    valueids = []
                                    if isinstance(value, list):
                                        valueids = [
                                            getattr(a, sql_id_column, None)
                                            for a in value
                                            if getattr(a, sql_id_column, None)
                                        ]
                                    else:
                                        valueids = getattr(
                                            value, sql_id_column, None)
                                    valueids = [str(a) for a in valueids]
                                    brains = catalog.unrestrictedSearchResults(
                                        portal_type=tablefti.id,
                                        sql_id=valueids)
                                    for brain in brains:
                                        relations.append(
                                            SQLRelationValue(
                                                brain.portal_type, brain.UID,
                                                self))
                                if IRelationChoice.providedBy(
                                        field) and relations:
                                    return relations[0]
                                elif IRelationList.providedBy(
                                        field) and relations:
                                    return relations
                            elif ITuple.providedBy(field):
                                return tuple(
                                    [getattr(a, fieldname, '') for a in value])
                            elif IList.providedBy(field):
                                return [
                                    getattr(a, fieldname, '') for a in value
                                ]
                            elif value and isinstance(value, list):
                                value = getattr(value[0], fieldname, '')
                for iface in iterSchemataForType(self.portal_type):
                    if name == 'subject':
                        try:
                            return tuple([
                                a.decode('utf-8') for a in literal_eval(value)
                            ])
                        except:
                            return tuple([a.strip() for a in value.split(',')])
                    if name in iface.names():
                        field = iface[name]
                        if IRichText.providedBy(field):
                            if not value:
                                return ''
                            if not '<p' in value or not '<br' in value:
                                value = '<p>' + '</p><p>'.join([
                                    a for a in value.split('\n') if a.strip()
                                ]) + '</p>'


#                            try:
#                                value = str(value)
#                            except:
#                                try:
#                                    value = value.decode('utf-8')
#                                except:
#                                    try:
#                                        value = value.encode('utf-8')
#                                    except:
#                                        pass
                            return RichTextValue(unidecode(value))
                        elif INamedBlobImage.providedBy(field):
                            return NamedBlobImage(
                                base64.b64decode(value),
                                filename=unicode(self.portal_type + self.id +
                                                 ".jpg"))
                        elif ITuple.providedBy(field):
                            if not value:
                                return tuple([])
                            try:
                                return tuple([
                                    a.decode('utf-8')
                                    for a in literal_eval(value)
                                ])
                            except:
                                return tuple(
                                    [a.strip() for a in value.split(',')])
                        elif IList.providedBy(field):
                            if not value:
                                return []
                            try:
                                return [
                                    a.decode('utf-8')
                                    for a in literal_eval(value)
                                ]
                            except:
                                return [a.strip() for a in value.split(',')]
                        elif IDatetime.providedBy(field) and hasattr(
                                value, 'day') and not hasattr(value, 'hour'):
                            value = datetime.datetime.combine(
                                value, datetime.datetime.min.time())
                if name in [
                        'expiration_date', 'effective_date', 'effective',
                        'expires'
                ] and hasattr(value, 'day') and not hasattr(value, 'hour'):
                    value = datetime.datetime.combine(
                        value, datetime.datetime.min.time())
                if isinstance(value, unicode) or name == 'id':
                    try:
                        value = str(value)
                    except:
                        pass
                return value
        return super(SQLDexterityItem, self).__getattribute__(name)
コード例 #10
0
 def testImplements(self):
     field = List()
     self.failUnless(IList.providedBy(field))
     self.failUnless(ISequence.providedBy(field))
     self.failUnless(ICollection.providedBy(field))
コード例 #11
0
 def __getattribute__(self, name):
     if name.startswith('_') or name.startswith('portal_') or name.startswith('@@'):
         return super(SQLDexterityItem, self).__getattribute__(name)
     if name == 'id' and not self.sql_virtual:
         return super(SQLDexterityItem, self).__getattribute__(name)
     connection = queryUtility(ISQLConnectionsUtility, name=self.portal_type, default=None)
     if connection == None and self.portal_type:
         fti = queryUtility(IDexterityFTI, name=self.portal_type, default=None)
         if not fti:
             return None
         updateConnectionsForFti(fti)
         connection = queryUtility(ISQLConnectionsUtility, name=self.portal_type, default=None)
     if not connection:
         return super(SQLDexterityItem, self).__getattribute__(name)
     if name == 'UID':
         return self.portal_type+'-'+connection.sql_table+'-'+str(self.sql_id)
     if name == 'id' and 'id' not in connection.fieldnames.keys():
         fti = ISQLTypeSettings(getUtility(IDexterityFTI, name=self.portal_type))
         nameFromTitle = INameFromTitle(self, None)
         if nameFromTitle is not None and nameFromTitle.title:
             sql_folder_id = getattr(fti, 'sql_folder_id', 'data-'+self.portal_type)
             title = nameFromTitle.title
             if IRelationValue.providedBy(name):
                 folder = sql_folder_id.to_object
                 if folder:
                     return INameChooser(folder).chooseName(title, self)
             return INameChooser(getSite()).chooseName(title, self)
     if name in connection.fieldnames.keys():
         sql_column = connection.fieldnames[name]
         sql_item = self.getSQLItem()
         fieldname = 'name'
         if sql_item and sql_column:
             while '.' in sql_column:
                 sql_item = getattr(sql_item, sql_column.split('.')[0], None)
                 if sql_item and ((isinstance(sql_item, list) and len(sql_item)>1) or hasattr(sql_item, '_sa_instance_state')):
                     value = sql_item
                     fieldname = sql_column.split('.')[-1]
                     break
                 sql_column = '.'.join(sql_column.split('.')[1:])
             else:
                 value = getattr(sql_item, sql_column, None)
             #this is a relation
             if value and (isinstance(value, list) or hasattr(value, '_sa_instance_state')):
                 sqlftis = [a for a in getAllUtilitiesRegisteredFor(IDexterityFTI) if 'collective.behavior.sql.behavior.behaviors.ISQLContent' in a.behaviors and getattr(a, 'sql_table', None)]
                 if name == 'subject':
                     return tuple([getattr(a, fieldname, '') for a in value])
                 tableftis = []
                 for iface in iterSchemataForType(self.portal_type):
                     if name in iface.names():
                         field = iface[name]
                         if IList.providedBy(field):
                             return [getattr(a, fieldname, '') for a in value] # hope it has name!
                         elif ITuple.providedBy(field):
                             return tuple([getattr(a, fieldname, '') for a in value])
                         if IRelationChoice.providedBy(field) or IRelationList.providedBy(field):
                             if IRelationChoice.providedBy(field):
                                 allowed_types = field.source.query.get('portal_type', [])
                             else:
                                 allowed_types = field.value_type.source.query.get('portal_type', [])
                             tableftis = []
                             for sqlfti in sqlftis:
                                 adapted = ISQLTypeSettings(sqlfti, None)
                                 if isinstance(value, list):
                                     classname = value[0].__class__.__name__
                                 else:
                                     classname = value.__class__.__name__
                                 if adapted and getattr(adapted, 'sql_table', None) == classname:
                                     if not allowed_types or sqlfti.id in allowed_types:
                                         tableftis.append(adapted)
                     
                             catalog = getToolByName(getSite(), 'portal_catalog')
                             relations = []
                             for tablefti in tableftis:
                                 sql_id_column = getattr(tablefti, 'sql_id_column', 'id')
                                 valueids = []
                                 if isinstance(value, list):
                                     valueids = [getattr(a, sql_id_column, None) for a in value if getattr(a, sql_id_column, None)]
                                 else:
                                     valueids = getattr(value, sql_id_column, None)
                                 brains = catalog.searchResults(portal_type=tablefti.id, sql_id=valueids)
                                 for brain in brains:
                                     relations.append(SQLRelationValue(brain.portal_type, brain.sql_id))
                             if IRelationChoice.providedBy(field) and relations:
                                 return relations[0]
                             elif IRelationList.providedBy(field) and relations:
                                 return relations
             for iface in iterSchemataForType(self.portal_type):
                 if name == 'subject':
                     try:
                         return tuple([a.decode('utf-8') for a in literal_eval(value)])
                     except:
                         return tuple([a.strip() for a in value.split(',')])
                 if name in iface.names():
                     field = iface[name]
                     if IRichText.providedBy(field):
                         return RichTextValue(value)
                     if INamedBlobImage.providedBy(field):
                         return NamedBlobImage(base64.b64decode(value), filename=unicode(self.portal_type+self.id+".jpg"))
                     if IList.providedBy(field):
                         try:
                             return [a.decode('utf-8') for a in literal_eval(value)]
                         except:
                             return [a.strip() for a in value.split(',')]
                     if ITuple.providedBy(field):
                         try:
                             return tuple([a.decode('utf-8') for a in literal_eval(value)])
                         except:
                             return tuple([a.strip() for a in value.split(',')])
                         
             if isinstance(value, unicode) or name == 'id':
                 try:
                     return str(value)
                 except:
                     pass
             return value
     return super(SQLDexterityItem, self).__getattribute__(name)
コード例 #12
0
    def __getattribute__(self, name):
        if name.startswith('_') or name.startswith('portal_') or name.startswith('@@') or name == 'sql_id':
            return super(SQLDexterityItem, self).__getattribute__(name)
        connection = queryUtility(ISQLConnectionsUtility, name=self.portal_type, default=None)
        if connection == None and self.portal_type:
            fti = queryUtility(IDexterityFTI, name=self.portal_type, default=None)
            if not fti:
                return None
            updateConnectionsForFti(fti)
            connection = queryUtility(ISQLConnectionsUtility, name=self.portal_type, default=None)
        if name == 'view':
            #be sure session and sqlitem are up to date
            self._v_sql_item = None
            connection.session.close()
        if not connection:
            return super(SQLDexterityItem, self).__getattribute__(name)
        if name == 'UID' and self.sql_virtual:
            return self.portal_type+'-'+connection.sql_table+'-'+str(self.sql_id)
        if name == 'id' and 'id' not in connection.fieldnames.keys():
            if not self.sql_virtual:
                return super(SQLDexterityItem, self).__getattribute__(name)
            fti = ISQLTypeSettings(getUtility(IDexterityFTI, name=self.portal_type))
            nameFromTitle = INameFromTitle(self, None)
            if nameFromTitle is not None and nameFromTitle.title:
                sql_folder_id = getattr(fti, 'sql_folder_id', 'data-'+self.portal_type)
                title = nameFromTitle.title
                folder = None
                if IRelationValue.providedBy(sql_folder_id):
                    folder = sql_folder_id.to_object
                elif sql_folder_id and sql_folder_id.startswith('/'):
                    portal = getToolByName(getSite(), 'portal_url').getPortalObject()
                    folder = portal.restrictedTraverse(sql_folder_id)
                if folder:
                    name = INameChooser(folder).chooseName(title, self)
                    return name
#                return INameChooser(getSite()).chooseName(title, self)
#                return getUtility(IURLNormalizer).normalize(title)
                return self.sql_id
        if name in connection.fieldnames.keys():
            sql_column = connection.fieldnames[name]
            sql_item = self.getSQLItem()
            try:
                sql_id = getattr(sql_item, connection.sql_id_column, None)
            except orm_exc.DetachedInstanceError:
                self._v_sql_item = None
                sql_item = self.getSQLItem()
                sql_id = getattr(sql_item, connection.sql_id_column, None)
            fieldname = 'name'
            if sql_item and sql_column:
                while '.' in sql_column:
                    sql_key = sql_column.split('.')[0]
                    sql_item = getattr(sql_item, sql_key, None)
                    if isinstance(sql_item, list):
                        value = sql_item
                        fieldname = sql_column.split('.')[-1]
                        break
                    sql_column = '.'.join(sql_column.split('.')[1:])
                else:
                    if not isinstance(sql_item, list):
                        value = getattr(sql_item, sql_column, None)
                if not value and (isinstance(value, list) or hasattr(value, '_sa_instance_state')):
                    value = ''
                elif (isinstance(value, list) or hasattr(value, '_sa_instance_state')):
                    sqlftis = [a for a in getAllUtilitiesRegisteredFor(IDexterityFTI) if 'collective.behavior.sql.behavior.behaviors.ISQLContent' in a.behaviors and getattr(a, 'sql_table', None)]
                    if name == 'subject':
                        return tuple([getattr(a, fieldname, '') for a in value])
                    tableftis = []
                    for iface in iterSchemataForType(self.portal_type):
                        if name in iface.names():
                            field = iface[name]
                            if IRelationChoice.providedBy(field) or IRelationList.providedBy(field):
                                if IRelationChoice.providedBy(field):
                                    allowed_types = field.source.query.get('portal_type', [])
                                else:
                                    allowed_types = field.value_type.source.query.get('portal_type', [])
                                tableftis = []
                                for sqlfti in sqlftis:
                                    adapted = ISQLTypeSettings(sqlfti, None)
                                    if isinstance(value, list):
                                        classname = value[0].__class__.__name__
                                    else:
                                        classname = value.__class__.__name__
                                    if adapted and getattr(adapted, 'sql_table', None) == classname:
                                        if not allowed_types or sqlfti.id in allowed_types:
                                            tableftis.append(adapted)
                                catalog = getToolByName(getSite(), 'portal_catalog')
                                relations = []
                                for tablefti in tableftis:
                                    sql_id_column = getattr(tablefti, 'sql_id_column', 'id')
                                    valueids = []
                                    if isinstance(value, list):
                                        valueids = [getattr(a, sql_id_column, None) for a in value if getattr(a, sql_id_column, None)]
                                    else:
                                        valueids = getattr(value, sql_id_column, None)
                                    valueids = [str(a) for a in valueids]
                                    brains = catalog.unrestrictedSearchResults(portal_type=tablefti.id, sql_id=valueids)
                                    for brain in brains:
                                        relations.append(SQLRelationValue(brain.portal_type, brain.UID, self))
                                if IRelationChoice.providedBy(field) and relations:
                                    return relations[0]
                                elif IRelationList.providedBy(field) and relations:
                                    return relations
                            elif ITuple.providedBy(field):
                                return tuple([getattr(a, fieldname, '') for a in value])
                            elif IList.providedBy(field):
                                return [getattr(a, fieldname, '') for a in value]
                            elif value and isinstance(value, list):
                                value = getattr(value[0], fieldname, '')
                for iface in iterSchemataForType(self.portal_type):
                    if name == 'subject':
                        try:
                            return tuple([a.decode('utf-8') for a in literal_eval(value)])
                        except:
                            return tuple([a.strip() for a in value.split(',')])
                    if name in iface.names():
                        field = iface[name]
                        if IRichText.providedBy(field):
                            if not value:
                                return ''
                            if not '<p' in value or not '<br' in value:
                                value = '<p>'+'</p><p>'.join([a for a in value.split('\n') if a.strip()])+'</p>'
#                            try:
#                                value = str(value)
#                            except:
#                                try:
#                                    value = value.decode('utf-8')
#                                except:
#                                    try:
#                                        value = value.encode('utf-8')
#                                    except:
#                                        pass
                            return RichTextValue(unidecode(value))
                        elif INamedBlobImage.providedBy(field):
                            return NamedBlobImage(base64.b64decode(value), filename=unicode(self.portal_type+self.id+".jpg"))
                        elif ITuple.providedBy(field):
                            if not value:
                                return tuple([])
                            try:
                                return tuple([a.decode('utf-8') for a in literal_eval(value)])
                            except:
                                return tuple([a.strip() for a in value.split(',')])
                        elif IList.providedBy(field):
                            if not value:
                                return []
                            try:
                                return [a.decode('utf-8') for a in literal_eval(value)]
                            except:
                                return [a.strip() for a in value.split(',')]
                        elif IDatetime.providedBy(field) and hasattr(value, 'day') and not hasattr(value, 'hour'):
                            value = datetime.datetime.combine(value, datetime.datetime.min.time())
                if name in ['expiration_date','effective_date', 'effective', 'expires'] and hasattr(value, 'day') and not hasattr(value, 'hour'):
                    value = datetime.datetime.combine(value, datetime.datetime.min.time())
                if isinstance(value, unicode) or name == 'id':
                    try:
                        value = str(value)
                    except:
                        pass
                return value
        return super(SQLDexterityItem, self).__getattribute__(name)
コード例 #13
0
 def testImplements(self):
     field = List()
     self.assertTrue(IList.providedBy(field))
     self.assertTrue(ISequence.providedBy(field))
     self.assertTrue(ICollection.providedBy(field))
コード例 #14
0
 def testImplements(self):
     field = List()
     self.failUnless(IList.providedBy(field))
     self.failUnless(ISequence.providedBy(field))
     self.failUnless(ICollection.providedBy(field))
コード例 #15
0
def export_file(result, header_mapping, request=None):
    if not result:
        return None

    if request is None:
        request = getRequest()

    transforms = api.portal.get_tool('portal_transforms')
    catalog = api.portal.get_tool("portal_catalog")

    data = []
    for row in result:
        items_dict = dict()
        if getattr(row, 'getObject', None):
            obj = row.getObject()
        else:
            obj = row
        for d in header_mapping:
            fieldid = d['field']
            if obj is None:
                items_dict[d['header']] = row(fieldid)
                continue
            if fieldid == '_path':
                path = obj.getPhysicalPath()
                virtual_path = request.physicalPathToVirtualPath(path)
                items_dict[d['header']] = (
                    '/'.join(virtual_path)).decode('utf-8')
                continue
            elif fieldid == '_url':
                items_dict[d['header']] = (obj.absolute_url()).decode('utf-8')
                continue

            value = ""
            for schemata in iterSchemata(obj):
                if fieldid not in schemata:
                    continue
                field = schemata[fieldid]

                try:
                    value = field.get(schemata(obj))
                except AttributeError:
                    continue
                if value is field.missing_value:
                    continue
                if IRichTextValue.providedBy(value):
                    # Convert to plain text
                    value = transforms.convertTo(target_mimetype='text/plain',
                                                 orig=value.raw_encoded,
                                                 encoding=value.encoding,
                                                 mimetype=value.mimeType)
                    value = safe_unicode(value.getData())
                    break

                if IList.providedBy(field):
                    # Check if items in list point to objects and use titles
                    # instead
                    names = []
                    for item_id in value:
                        # Loop through choices
                        query = dict(id=item_id)
                        results = catalog(**query)
                        if results:
                            names.append(results[0].Title)
                    if names:
                        value = names
                    value = ', '.join(value)
                    break

                if INamed.providedBy(value):
                    # Image, file field
                    value = u'{0}/@@download/{1}'.format(row.getURL(), fieldid)
                    break

                serializer = ISerializer(field)
                value = serializer(value, {})
                break

            # Added due to an ascii error related to csv export. We convert to
            # unicode as unicodecsv requires unicode
            value = safe_unicode(value)

            items_dict[d['header']] = value

        data.append(items_dict)

    dataset = tablib.Dataset()
    dataset.dict = data

    return dataset
コード例 #16
0
 def is_list(self):
     if IList.providedBy(self.field):
         return 'checkbox'
     else:
         return 'radio'
コード例 #17
0
    def __getattribute__(self, name):
        if name.startswith('_') or name.startswith(
                'portal_') or name.startswith('@@'):
            return super(SQLDexterityItem, self).__getattribute__(name)
        if name == 'id' and not self.sql_virtual:
            return super(SQLDexterityItem, self).__getattribute__(name)
        connection = queryUtility(ISQLConnectionsUtility,
                                  name=self.portal_type,
                                  default=None)
        if connection == None and self.portal_type:
            fti = queryUtility(IDexterityFTI,
                               name=self.portal_type,
                               default=None)
            if not fti:
                return None
            updateConnectionsForFti(fti)
            connection = queryUtility(ISQLConnectionsUtility,
                                      name=self.portal_type,
                                      default=None)
        if not connection:
            return super(SQLDexterityItem, self).__getattribute__(name)
        if name == 'UID':
            return self.portal_type + '-' + connection.sql_table + '-' + str(
                self.sql_id)
        if name == 'id' and 'id' not in connection.fieldnames.keys():
            fti = ISQLTypeSettings(
                getUtility(IDexterityFTI, name=self.portal_type))
            nameFromTitle = INameFromTitle(self, None)
            if nameFromTitle is not None and nameFromTitle.title:
                sql_folder_id = getattr(fti, 'sql_folder_id',
                                        'data-' + self.portal_type)
                title = nameFromTitle.title
                if IRelationValue.providedBy(name):
                    folder = sql_folder_id.to_object
                    if folder:
                        return INameChooser(folder).chooseName(title, self)
                return INameChooser(getSite()).chooseName(title, self)
        if name in connection.fieldnames.keys():
            sql_column = connection.fieldnames[name]
            sql_item = self.getSQLItem()
            fieldname = 'name'
            if sql_item and sql_column:
                while '.' in sql_column:
                    sql_item = getattr(sql_item,
                                       sql_column.split('.')[0], None)
                    if sql_item and (
                        (isinstance(sql_item, list) and len(sql_item) > 1)
                            or hasattr(sql_item, '_sa_instance_state')):
                        value = sql_item
                        fieldname = sql_column.split('.')[-1]
                        break
                    sql_column = '.'.join(sql_column.split('.')[1:])
                else:
                    value = getattr(sql_item, sql_column, None)
                #this is a relation
                if value and (isinstance(value, list)
                              or hasattr(value, '_sa_instance_state')):
                    sqlftis = [
                        a for a in getAllUtilitiesRegisteredFor(IDexterityFTI)
                        if
                        'collective.behavior.sql.behavior.behaviors.ISQLContent'
                        in a.behaviors and getattr(a, 'sql_table', None)
                    ]
                    if name == 'subject':
                        return tuple(
                            [getattr(a, fieldname, '') for a in value])
                    tableftis = []
                    for iface in iterSchemataForType(self.portal_type):
                        if name in iface.names():
                            field = iface[name]
                            if IList.providedBy(field):
                                return [
                                    getattr(a, fieldname, '') for a in value
                                ]  # hope it has name!
                            elif ITuple.providedBy(field):
                                return tuple(
                                    [getattr(a, fieldname, '') for a in value])
                            if IRelationChoice.providedBy(
                                    field) or IRelationList.providedBy(field):
                                if IRelationChoice.providedBy(field):
                                    allowed_types = field.source.query.get(
                                        'portal_type', [])
                                else:
                                    allowed_types = field.value_type.source.query.get(
                                        'portal_type', [])
                                tableftis = []
                                for sqlfti in sqlftis:
                                    adapted = ISQLTypeSettings(sqlfti, None)
                                    if isinstance(value, list):
                                        classname = value[0].__class__.__name__
                                    else:
                                        classname = value.__class__.__name__
                                    if adapted and getattr(
                                            adapted, 'sql_table',
                                            None) == classname:
                                        if not allowed_types or sqlfti.id in allowed_types:
                                            tableftis.append(adapted)

                                catalog = getToolByName(
                                    getSite(), 'portal_catalog')
                                relations = []
                                for tablefti in tableftis:
                                    sql_id_column = getattr(
                                        tablefti, 'sql_id_column', 'id')
                                    valueids = []
                                    if isinstance(value, list):
                                        valueids = [
                                            getattr(a, sql_id_column, None)
                                            for a in value
                                            if getattr(a, sql_id_column, None)
                                        ]
                                    else:
                                        valueids = getattr(
                                            value, sql_id_column, None)
                                    brains = catalog.searchResults(
                                        portal_type=tablefti.id,
                                        sql_id=valueids)
                                    for brain in brains:
                                        relations.append(
                                            SQLRelationValue(
                                                brain.portal_type,
                                                brain.sql_id))
                                if IRelationChoice.providedBy(
                                        field) and relations:
                                    return relations[0]
                                elif IRelationList.providedBy(
                                        field) and relations:
                                    return relations
                for iface in iterSchemataForType(self.portal_type):
                    if name == 'subject':
                        try:
                            return tuple([
                                a.decode('utf-8') for a in literal_eval(value)
                            ])
                        except:
                            return tuple([a.strip() for a in value.split(',')])
                    if name in iface.names():
                        field = iface[name]
                        if IRichText.providedBy(field):
                            return RichTextValue(value)
                        if INamedBlobImage.providedBy(field):
                            return NamedBlobImage(
                                base64.b64decode(value),
                                filename=unicode(self.portal_type + self.id +
                                                 ".jpg"))
                        if IList.providedBy(field):
                            try:
                                return [
                                    a.decode('utf-8')
                                    for a in literal_eval(value)
                                ]
                            except:
                                return [a.strip() for a in value.split(',')]
                        if ITuple.providedBy(field):
                            try:
                                return tuple([
                                    a.decode('utf-8')
                                    for a in literal_eval(value)
                                ])
                            except:
                                return tuple(
                                    [a.strip() for a in value.split(',')])

                if isinstance(value, unicode) or name == 'id':
                    try:
                        return str(value)
                    except:
                        pass
                return value
        return super(SQLDexterityItem, self).__getattribute__(name)
コード例 #18
0
def get_attribute_values(request, record, attribute_map):
    values = {}
    vocabularies = get_vocabularies(request, attribute_map)

    for header, field in attribute_map.items():

        downloaded = download_field_from_url(field, record[header])
        if downloaded is not False:
            values[field.__name__] = downloaded
            continue

        if IDate.providedBy(field):
            if not record[header]:
                values[field.__name__] = None
            else:
                values[field.__name__] = parse_date(record[header])
            continue

        if IDatetime.providedBy(field):
            if not record[header]:
                values[field.__name__] = None
            else:
                values[field.__name__] = parse_datetime(record[header])
            continue

        if IURI.providedBy(field):
            if not record[header].strip():
                values[field.__name__] = None
                continue

        if IList.providedBy(field):
            if ITextLine.providedBy(field.value_type):
                values[field.__name__] = convert_to_list(record[header])
                continue

        if ISet.providedBy(field):
            if IChoice.providedBy(field.value_type):
                values[field.__name__] = set(convert_to_list(record[header]))
                continue

        if IChoice.providedBy(field):
            if not record[header].strip():
                values[field.__name__] = None
            else:
                vocabulary = vocabularies[header]
                if record[header].lower() not in vocabulary:
                    raise ContentImportError(
                        _(
                            u'The ${name} column contains the '
                            u'unknown value ${value}',
                            mapping=dict(name=header, value=record[header])
                        )
                    )

                values[field.__name__] = vocabulary[record[header].lower()]

            continue

        assert IFromUnicode.providedBy(field), """
            {} does not support fromUnicode
        """.format(field)

        try:
            values[field.__name__] = field.fromUnicode(record[header])

            if isinstance(values[field.__name__], basestring):
                values[field.__name__] = values[field.__name__].strip()
            if isinstance(field, Text):
                values[field.__name__] = values[field.__name__].replace(
                    '<br />', '\n'
                )

        except ValidationError, e:
            raise ContentImportError(e.doc(), colname=header)
        except ValueError, e:
            raise ContentImportError(e.message, colname=header)