How to use the odoorpc.fields.BaseField function in OdooRPC

To help you get started, we’ve selected a few OdooRPC examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github OCA / odoorpc / odoorpc / fields.py View on Github external
):
                raise ValueError(
                    "The value supplied has to be a list, a recordset "
                    "or 'False'"
                )
        return super(Many2many, self).check_value(value)

    def store(self, record, value):
        """Store the value in the record."""
        if record._values[self.name].get(record.id):
            tuples2ids(value, record._values[self.name][record.id])
        else:
            record._values[self.name][record.id] = tuples2ids(value, [])


class Many2one(BaseField):
    """Represent the OpenObject 'fields.many2one'"""

    def __init__(self, name, data):
        super(Many2one, self).__init__(name, data)
        self.relation = 'relation' in data and data['relation'] or False
        self.context = 'context' in data and data['context'] or {}
        self.domain = 'domain' in data and data['domain'] or False

    def __get__(self, instance, owner):
        id_ = instance._values[self.name].get(instance.id)
        if instance.id in instance._values_to_write[self.name]:
            id_ = instance._values_to_write[self.name][instance.id]
        # None value => get the value on the fly
        if id_ is None:
            args = [[instance.id], [self.name]]
            kwargs = {'context': self.context, 'load': '_classic_write'}
github osiell / odoorpc / odoorpc / fields.py View on Github external
if not is_string(value):
                raise ValueError("Value supplied has to be a string")
            if len(value) > self.size:
                raise ValueError(
                    "Lenght of the '{0}' is limited to {1}".format(
                        self.name, self.size))
        if not value and self.required:
            raise ValueError("'{0}' field is required".format(self.name))
        return value

    def store(self, record, value):
        """Store the value in the record."""
        record._values[self.name][record.id] = value


class Binary(BaseField):
    """Equivalent of the `fields.Binary` class."""
    def __init__(self, name, data):
        super(Binary, self).__init__(name, data)

    def __get__(self, instance, owner):
        value = instance._values[self.name][instance.id]
        if instance.id in instance._values_to_write[self.name]:
            value = instance._values_to_write[self.name][instance.id]
        return value

    def __set__(self, instance, value):
        if value is None:
            value = False
        value = self.check_value(value)
        instance._values_to_write[self.name][instance.id] = value
        super(Binary, self).__set__(instance, value)
github osiell / odoorpc / odoorpc / fields.py View on Github external
instance._values_to_write[self.name][instance.id] = value
        super(Selection, self).__set__(instance, value)

    def check_value(self, value):
        super(Selection, self).check_value(value)
        selection = [val[0] for val in self.selection]
        if value and value not in selection:
            raise ValueError(
                "The value '{0}' supplied doesn't match with the possible "
                "values '{1}' for the '{2}' field".format(
                    value, selection, self.name,
                ))
        return value


class Many2many(BaseField):
    """Represent the OpenObject 'fields.many2many'"""
    def __init__(self, name, data):
        super(Many2many, self).__init__(name, data)
        self.relation = 'relation' in data and data['relation'] or False
        self.context = 'context' in data and data['context'] or {}
        self.domain = 'domain' in data and data['domain'] or False

    def __get__(self, instance, owner):
        """Return a recordset."""
        ids = None
        if instance._values[self.name].get(instance.id):
            ids = instance._values[self.name][instance.id][:]
        # None value => get the value on the fly
        if ids is None:
            args = [[instance.id], [self.name]]
            kwargs = {'context': self.context, 'load': '_classic_write'}
github OCA / odoorpc / odoorpc / fields.py View on Github external
super(Many2one, self).check_value(value)
        if value and value._name != self.relation:
            raise ValueError(
                (
                    "Instance of '{model}' supplied doesn't match with the "
                    + "relation '{relation}' of the '{field_name}' field."
                ).format(
                    model=value._name,
                    relation=self.relation,
                    field_name=self.name,
                )
            )
        return value


class One2many(BaseField):
    """Represent the OpenObject 'fields.one2many'"""

    def __init__(self, name, data):
        super(One2many, self).__init__(name, data)
        self.relation = 'relation' in data and data['relation'] or False
        self.context = 'context' in data and data['context'] or {}
        self.domain = 'domain' in data and data['domain'] or False

    def __get__(self, instance, owner):
        """Return a recordset."""
        ids = None
        if instance._values[self.name].get(instance.id):
            ids = instance._values[self.name][instance.id][:]
        # None value => get the value on the fly
        if ids is None:
            args = [[instance.id], [self.name]]
github OCA / odoorpc / odoorpc / fields.py View on Github external
super(Selection, self).__set__(instance, value)

    def check_value(self, value):
        super(Selection, self).check_value(value)
        selection = [val[0] for val in self.selection]
        if value and value not in selection:
            raise ValueError(
                "The value '{}' supplied doesn't match with the possible "
                "values '{}' for the '{}' field".format(
                    value, selection, self.name
                )
            )
        return value


class Many2many(BaseField):
    """Represent the OpenObject 'fields.many2many'"""

    def __init__(self, name, data):
        super(Many2many, self).__init__(name, data)
        self.relation = 'relation' in data and data['relation'] or False
        self.context = 'context' in data and data['context'] or {}
        self.domain = 'domain' in data and data['domain'] or False

    def __get__(self, instance, owner):
        """Return a recordset."""
        ids = None
        if instance._values[self.name].get(instance.id):
            ids = instance._values[self.name][instance.id][:]
        # None value => get the value on the fly
        if ids is None:
            args = [[instance.id], [self.name]]
github osiell / odoorpc / odoorpc / fields.py View on Github external
elif is_string(value):
            try:
                datetime.datetime.strptime(value, self.pattern)
            except:
                raise ValueError(
                    "String not well formatted, expecting '{0}' format".format(
                        self.pattern))
        elif isinstance(value, bool) or value is None:
            return value
        else:
            raise ValueError(
                "Expecting a datetime.date object or string")
        return value


class Datetime(BaseField):
    """Represent the OpenObject 'fields.datetime'"""
    pattern = "%Y-%m-%d %H:%M:%S"

    def __init__(self, name, data):
        super(Datetime, self).__init__(name, data)

    def __get__(self, instance, owner):
        value = instance._values[self.name].get(instance.id)
        if instance.id in instance._values_to_write[self.name]:
            value = instance._values_to_write[self.name][instance.id]
        try:
            res = datetime.datetime.strptime(value, self.pattern)
        except (ValueError, TypeError):
            res = value
        return res
github osiell / odoorpc / odoorpc / fields.py View on Github external
elif is_string(value):
            try:
                datetime.datetime.strptime(value, self.pattern)
            except:
                raise ValueError(
                    "Value not well formatted, expecting '{0}' format".format(
                        self.pattern))
        elif isinstance(value, bool):
            return value
        else:
            raise ValueError(
                "Expecting a datetime.datetime object or string")
        return value


class Float(BaseField):
    """Equivalent of the `fields.Float` class."""
    def __init__(self, name, data):
        super(Float, self).__init__(name, data)

    def __get__(self, instance, owner):
        value = instance._values[self.name].get(instance.id)
        if instance.id in instance._values_to_write[self.name]:
            value = instance._values_to_write[self.name][instance.id]
        if value in [None, False]:
            value = 0.0
        return value

    def __set__(self, instance, value):
        if value is None:
            value = False
        value = self.check_value(value)
github osiell / odoorpc / odoorpc / fields.py View on Github external
value = instance._values[self.name].get(instance.id)
        if instance.id in instance._values_to_write[self.name]:
            value = instance._values_to_write[self.name][instance.id]
        if value in [None, False]:
            value = 0.0
        return value

    def __set__(self, instance, value):
        if value is None:
            value = False
        value = self.check_value(value)
        instance._values_to_write[self.name][instance.id] = value
        super(Float, self).__set__(instance, value)


class Integer(BaseField):
    """Equivalent of the `fields.Integer` class."""

    def __init__(self, name, data):
        super(Integer, self).__init__(name, data)

    def __get__(self, instance, owner):
        value = instance._values[self.name].get(instance.id)
        if instance.id in instance._values_to_write[self.name]:
            value = instance._values_to_write[self.name][instance.id]
        if value in [None, False]:
            value = 0
        return value

    def __set__(self, instance, value):
        if value is None:
            value = False
github OCA / odoorpc / odoorpc / fields.py View on Github external
value = instance._values[self.name].get(instance.id)
        if instance.id in instance._values_to_write[self.name]:
            value = instance._values_to_write[self.name][instance.id]
        if value in [None, False]:
            value = 0.0
        return value

    def __set__(self, instance, value):
        if value is None:
            value = False
        value = self.check_value(value)
        instance._values_to_write[self.name][instance.id] = value
        super(Float, self).__set__(instance, value)


class Integer(BaseField):
    """Equivalent of the `fields.Integer` class."""

    def __init__(self, name, data):
        super(Integer, self).__init__(name, data)

    def __get__(self, instance, owner):
        value = instance._values[self.name].get(instance.id)
        if instance.id in instance._values_to_write[self.name]:
            value = instance._values_to_write[self.name][instance.id]
        if value in [None, False]:
            value = 0
        return value

    def __set__(self, instance, value):
        if value is None:
            value = False
github osiell / odoorpc / odoorpc / fields.py View on Github external
def __set__(self, instance, value):
        if value is None:
            value = False
        value = self.check_value(value)
        instance._values_to_write[self.name][instance.id] = value
        super(Text, self).__set__(instance, value)


class Html(Text):
    """Equivalent of the `fields.Html` class."""
    def __init__(self, name, data):
        super(Html, self).__init__(name, data)


class Unknown(BaseField):
    """Represent an unknown field. This should not happen but this kind of
    field only exists to avoid a blocking situation from a RPC point of view.
    """
    def __init__(self, name, data):
        super(Unknown, self).__init__(name, data)

    def __get__(self, instance, owner):
        value = instance._values[self.name][instance.id]
        if instance.id in instance._values_to_write[self.name]:
            value = instance._values_to_write[self.name][instance.id]
        return value

    def __set__(self, instance, value):
        value = self.check_value(value)
        instance._values_to_write[self.name][instance.id] = value
        super(Unknown, self).__set__(instance, value)