Exemplo n.º 1
0
def copy_analysis_field_values(source, analysis, **kwargs):
    src_schema = source.Schema()
    dst_schema = analysis.Schema()
    # Some fields should not be copied from source!
    # BUT, if these fieldnames are present in kwargs, the value will
    # be set accordingly.
    IGNORE_FIELDNAMES = [
        'UID', 'id', 'allowDiscussion', 'subject', 'location', 'contributors',
        'creators', 'effectiveDate', 'expirationDate', 'language', 'rights',
        'creation_date', 'modification_date', 'IsReflexAnalysis',
        'OriginalReflexedAnalysis', 'ReflexAnalysisOf', 'ReflexRuleAction',
        'ReflexRuleLocalID', 'ReflexRuleActionsTriggered', 'Hidden']
    for field in src_schema.fields():
        fieldname = field.getName()
        if fieldname in IGNORE_FIELDNAMES and fieldname not in kwargs:
            continue
        if fieldname not in dst_schema:
            continue
        value = kwargs.get(fieldname, field.get(source))
        if value:
            # Campbell's mental note:never ever use '.set()' directly to a
            # field. If you can't use the setter, then use the mutator in order
            # to give the value. We have realized that in some cases using
            # 'set' when the value is a string, it saves the value
            # as unicode instead of plain string.
            field = analysis.getField(fieldname)
            if IExtensionField.providedBy(field):
                # SchemaExtender fields don't auto-generate the accessor/mutator
                field.set(analysis, value)
            else:
                mutator_name = field.mutator
                mutator = getattr(analysis, mutator_name)
                mutator(value)
Exemplo n.º 2
0
    def get_archetypes_fields(self):
        """If Archetypes is used then dump schema.
        """
        try:
            from Products.Archetypes.interfaces import IBaseObject
            if not self.providedBy(IBaseObject, self.context):
                return
        except:
            return

        try:
            from archetypes.schemaextender.interfaces import IExtensionField
        except:
            IExtensionField = None

        import base64

        fields = []
        for schemata in self.context.Schemata().values():
            fields.extend(schemata.fields())

        for field in fields:
            fieldname = unicode(field.__name__)
            type_ = field.__class__.__name__

            try:
                if self.providedBy(IExtensionField, field):
                    # archetypes.schemaextender case:
                    # Try to get the base class of the schemaexter-field, which
                    # is not an extension field.
                    type_ = [
                        it.__name__ for it in field.__class__.__bases__
                        if not IExtensionField.implementedBy(it)
                    ][0]
            except:
                pass

            fieldnames = [
                'BooleanField',
                'ComputedField',
                'DataGridField',
                'EmailField',
                'FixedPointField',
                'FloatField',
                'IntegerField',
                'LinesField',
                'SimpleDataGridField',
                'StringField',
                'TALESLines',
                'TALESString',
                'TextField',
                'ZPTField',
            ]

            if type_ in fieldnames:
                try:
                    value = field.getRaw(self.context)
                except AttributeError:
                    value = self._get_at_field_value(field)

                if callable(value):
                    value = value()

                if value and type_ in ['ComputedField']:
                    if isinstance(value, str):
                        value = self.decode(value)

                if value and type_ in ['StringField', 'TextField']:
                    try:
                        value = self.decode(value)
                    except AttributeError:
                        # maybe an int?
                        value = unicode(value)
                    except Exception, e:
                        raise Exception('problems with %s: %s' % (
                            self.context.absolute_url(), str(e))
                        )
                elif value and type_ == 'DataGridField':
                    for i, row in enumerate(value):
                        for col_key in row.keys():
                            col_value = row[col_key]
                            if type(col_value) in (unicode, str):
                                value[i][col_key] = self.decode(col_value)

                self[unicode(fieldname)] = value

                if value and type_ in ['StringField', 'TextField']:
                    try:
                        ct = field.getContentType(self.context)
                        self[unicode('_content_type_') + fieldname] = ct
                    except AttributeError:
                        pass

            elif type_ in ['DateTimeField', ]:
                value = str(self._get_at_field_value(field))
                if value:
                    self[unicode(fieldname)] = value
Exemplo n.º 3
0
    def get_archetypes_fields(self):
        """If Archetypes is used then dump schema.
        """
        try:
            from Products.Archetypes.interfaces import IBaseObject
            if not self.providedBy(IBaseObject, self.context):
                return
        except:
            return

        try:
            from archetypes.schemaextender.interfaces import IExtensionField
        except:
            IExtensionField = None

        import base64

        fields = []
        for schemata in self.context.Schemata().values():
            fields.extend(schemata.fields())

        for field in fields:
            fieldname = unicode(field.__name__)
            type_ = field.__class__.__name__

            try:
                if self.providedBy(IExtensionField, field):
                    # archetypes.schemaextender case:
                    # Try to get the base class of the schemaexter-field, which
                    # is not an extension field.
                    type_ = [
                        it.__name__ for it in field.__class__.__bases__
                        if not IExtensionField.implementedBy(it)
                    ][0]
            except:
                pass

            fieldnames = [
                'BooleanField',
                'ComputedField',
                'DataGridField',
                'EmailField',
                'FixedPointField',
                'FloatField',
                'IntegerField',
                'LinesField',
                'SimpleDataGridField',
                'StringField',
                'TALESLines',
                'TALESString',
                'TextField',
                'ZPTField',
            ]

            if type_ in fieldnames:
                try:
                    value = field.getRaw(self.context)
                except AttributeError:
                    value = self._get_at_field_value(field)

                if callable(value):
                    value = value()

                if value and type_ in ['ComputedField']:
                    if isinstance(value, str):
                        value = self.decode(value)

                if value and type_ in ['StringField', 'TextField']:
                    try:
                        value = self.decode(value)
                    except AttributeError:
                        # maybe an int?
                        value = unicode(value)
                    except Exception, e:
                        raise Exception('problems with %s: %s' %
                                        (self.context.absolute_url(), str(e)))
                elif value and type_ == 'DataGridField':
                    for i, row in enumerate(value):
                        for col_key in row.keys():
                            col_value = row[col_key]
                            if type(col_value) in (unicode, str):
                                value[i][col_key] = self.decode(col_value)

                self[unicode(fieldname)] = value

                if value and type_ in ['StringField', 'TextField']:
                    try:
                        ct = field.getContentType(self.context)
                        self[unicode('_content_type_') + fieldname] = ct
                    except AttributeError:
                        pass

            elif type_ in [
                    'DateTimeField',
            ]:
                value = str(self._get_at_field_value(field))
                if value:
                    self[unicode(fieldname)] = value
Exemplo n.º 4
0
 def fields(self):
     return [field for field in self.context.Schema().values()
                   if IExtensionField.providedBy(field)]
Exemplo n.º 5
0
    def get_archetypes_fields(self):
        """If Archetypes is used then dump schema.
        """
        try:
            from Products.Archetypes.interfaces import IBaseObject

            if not self.providedBy(IBaseObject, self.context):
                return
        except Exception:
            return

        try:
            from archetypes.schemaextender.interfaces import IExtensionField
        except Exception:
            IExtensionField = None

        import base64

        fields = []
        for schemata in self.context.Schemata().values():
            fields.extend(schemata.fields())

        for field in fields:
            fieldname = six.text_type(field.__name__)
            type_ = field.__class__.__name__

            try:
                if self.providedBy(IExtensionField, field):
                    # archetypes.schemaextender case:
                    # Try to get the base class of the schemaexter-field, which
                    # is not an extension field.
                    type_ = [
                        it.__name__
                        for it in field.__class__.__bases__
                        if not IExtensionField.implementedBy(it)
                    ][0]
            except Exception:
                pass

            fieldnames = [
                "BooleanField",
                "ComputedField",
                "DataGridField",
                "EmailField",
                "FixedPointField",
                "FloatField",
                "IntegerField",
                "LinesField",
                "SimpleDataGridField",
                "StringField",
                "TALESLines",
                "TALESString",
                "TextField",
                "ZPTField",
            ]

            if type_ in fieldnames:
                try:
                    value = field.getRaw(self.context)
                except AttributeError:
                    value = self._get_at_field_value(field)

                if callable(value):
                    value = value()

                if value and type_ in ["ComputedField"]:
                    if isinstance(value, str):
                        value = self.decode(value)

                if value and type_ in ["StringField", "TextField"]:
                    widget_class = field.widget.__class__.__name__
                    if type_ == "TextField" and widget_class == "RichWidget":
                        value = self.fix_links(html=value)
                    try:
                        value = self.decode(value)
                    except AttributeError:
                        # maybe an int?
                        value = six.text_type(value)
                    except Exception as e:
                        raise Exception(
                            "problems with %s: %s"
                            % (self.context.absolute_url(), str(e))
                        )

                elif value and type_ == "DataGridField":
                    for i, row in enumerate(value):
                        for col_key in row.keys():
                            col_value = row[col_key]
                            if type(col_value) in (six.text_type, str):
                                value[i][col_key] = self.decode(col_value)

                self[six.text_type(fieldname)] = value

                if value and type_ in ["StringField", "TextField"]:
                    try:
                        ct = field.getContentType(self.context)
                        self[six.text_type("_content_type_") + fieldname] = ct
                    except AttributeError:
                        pass

            elif type_ in ["DateTimeField"]:
                value = str(self._get_at_field_value(field))
                if value:
                    self[six.text_type(fieldname)] = value

            elif type_ in [
                "ImageField",
                "FileField",
                "BlobField",
                "AttachmentField",
                "ExtensionBlobField",
            ]:
                fieldname = six.text_type("_datafield_" + fieldname)
                value = self._get_at_field_value(field)
                value2 = value

                if value and not isinstance(value, str):
                    if isinstance(getattr(value, "data", None), str):
                        value = base64.b64encode(value.data)
                    else:
                        data = value.data
                        value = ""
                        while data is not None:
                            value += data.data
                            data = data.next
                        value = base64.b64encode(value)

                try:
                    max_filesize = int(
                        os.environ.get("JSONIFY_MAX_FILESIZE", 20000000)
                    )
                except ValueError:
                    max_filesize = 20000000

                if value and len(value) < max_filesize:
                    size = value2.getSize()
                    try:
                        fname = field.getFilename(self.context)
                    except AttributeError:
                        fname = value2.getFilename()

                    try:
                        fname = self.decode(fname)
                    except AttributeError:
                        # maybe an int?
                        fname = six.text_type(fname)
                    except Exception as e:
                        raise Exception(
                            "problems with %s: %s"
                            % (self.context.absolute_url(), str(e))
                        )

                    try:
                        ctype = field.getContentType(self.context)
                    except AttributeError:
                        ctype = value2.getContentType()

                    self[fieldname] = {
                        "data": value,
                        "size": size,
                        "filename": fname or "",
                        "content_type": ctype,
                        "encoding": "base64",
                    }

            elif type_ in ["ReferenceField"]:
                # If there are references, add the UIDs to the referenced
                # contents
                value = field.getRaw(self.context)
                if value:
                    self[fieldname] = value

            elif type_ in ["QueryField"]:
                value = field.getRaw(self.context)
                value = [dict(q) for q in value]
                self[fieldname] = self._fix_collection_query(value)

            elif type_ in [
                "RecordsField",  # from Products.ATExtensions
                "RecordField",
                "FormattableNamesField",
                "FormattableNameField",
            ]:
                # ATExtensions fields
                # convert items to real dicts
                # value = [dict(it) for it in field.get(self.context)]

                def _enc(val):
                    if type(val) in (six.text_type, str):
                        val = self.decode(val)
                    return val

                value = []
                for it in field.get(self.context):
                    it = dict(it)
                    val_ = {}
                    for k_, v_ in it.items():
                        val_[_enc(k_)] = _enc(v_)
                    value.append(val_)

                self[six.text_type(fieldname)] = value

            else:
                # Just try to stringify value
                try:
                    value = field.getRaw(self.context)
                except AttributeError:
                    value = self._get_at_field_value(field)
                self[six.text_type(fieldname)] = self.decode(str(value))

        if self.context.portal_type == "Topic":
            mt = TopicMigrator()
            criterions_list = mt.__call__(self.context)
            # check format in case of date values
            for crit_dict in criterions_list:
                values = crit_dict.get("v")
                if not values:
                    continue
                if isinstance(values, int):
                    continue
                if not any(
                    [True for x in values if isinstance(x, DateTime)]
                ):  # noqa
                    continue

                new_values = []

                for val in values:
                    new_values.append(val.asdatetime().isoformat())
                if isinstance(values, tuple):
                    new_values = tuple(new_values)
                crit_dict.update({"v": new_values})

            sort_on = mt._collection_sort_on
            sort_reversed = mt._collection_sort_reversed

            self["query"] = criterions_list
            self["sort_on"] = sort_on
            self["sort_reversed"] = sort_reversed

            if not self.get("itemCount"):
                self["item_count"] = "30"
            else:
                self["item_count"] = data.get("itemCount")
Exemplo n.º 6
0
 def fields(self):
     return [
         field for field in self.context.Schema().values()
         if IExtensionField.providedBy(field)
     ]