Esempio n. 1
0
    class Test(Model):
        a = Integer()
        b = Integer()
        best = Integer()

        @a.setter
        def a(self, value):
            self.best = min(self.b, value)
            return value

        @b.setter
        def b(self, value):
            self.best = min(self.a, value)
            return value
Esempio n. 2
0
 class Flag(Model):
     uuid = UUID()
     name = Keyword()
     fans = List(Integer(), default=[])
Esempio n. 3
0
 class A(Model):
     fast = Integer(default=1)
     slow = Keyword(default='abc')
     flags = List(Keyword(), default=['cat-snack'])
Esempio n. 4
0
 class BannedTest(Model):
     ALL = Integer()
Esempio n. 5
0
 class BannedTest(Model):
     id = Integer()
Esempio n. 6
0
 class BannedTest(Model):
     _1 = Integer()
Esempio n. 7
0
 class Test(Model):
     a = Compound(Inner)
     b = Integer()
Esempio n. 8
0
 class Test(Model):
     a = Compound(InnerA)
     b = Compound(InnerB)
     c = Compound(InnerB, default={'number': 99, 'value': 'yellow'})
     x = Integer()
     y = Integer(default=-1)
Esempio n. 9
0
 class Test(Model):
     a = Integer()
     b = Integer()
Esempio n. 10
0
 class InnerA(Model):
     number = Integer(default=10)
     value = Keyword()
Esempio n. 11
0
 class InnerB(Model):
     number = Integer()
     value = Keyword()
Esempio n. 12
0
 class Entry(Model):
     value = Integer()
     key = Keyword()
Esempio n. 13
0
 class Test(Model):
     values = List(Integer())
Esempio n. 14
0
    class Test(Model):
        first = Integer()

        @first.getter
        def first(self, value):
            return value if value >= 1 else 100
Esempio n. 15
0
 class A(Model):
     fast = Integer(default=1)
     slow = Keyword(default='abc')
     count = List(Integer())
Esempio n. 16
0
 class Test(Model):
     a = Mapping(Integer(), default={}, index=True, store=True)
Esempio n. 17
0
 class Inner(Model):
     a = Integer()
     b = Integer()
Esempio n. 18
0
 class Test(Model):
     a = Mapping(Integer(), default={}, index=False, store=False)
Esempio n. 19
0
 class Test(Model):
     first = Keyword()
     second = Integer()
Esempio n. 20
0
    def _validate_operations(self, operations):
        """
        Validate the different operations received for a partial update

        TODO: When the field is of type Mapping, the validation/check only works for depth 1. A full recursive
              solution is needed to support multi-depth cases.

        :param operations: list of operation tuples
        :raises: DatastoreException if operation not valid
        """
        if self.model_class:
            fields = self.model_class.flat_fields(show_compound=True)
            if 'classification in fields':
                fields.update({
                    "__access_lvl__": Integer(),
                    "__access_req__": List(Keyword()),
                    "__access_grp1__": List(Keyword()),
                    "__access_grp2__": List(Keyword())
                })
        else:
            fields = None

        ret_ops = []
        for op, doc_key, value in operations:
            if op not in self.UPDATE_OPERATIONS:
                raise DataStoreException(f"Not a valid Update Operation: {op}")

            if fields is not None:
                prev_key = None
                if doc_key not in fields:
                    if '.' in doc_key:
                        prev_key = doc_key[:doc_key.rindex('.')]
                        if prev_key in fields and not isinstance(
                                fields[prev_key], Mapping):
                            raise DataStoreException(
                                f"Invalid field for model: {prev_key}")
                    else:
                        raise DataStoreException(
                            f"Invalid field for model: {doc_key}")

                if prev_key:
                    field = fields[prev_key].child_type
                else:
                    field = fields[doc_key]

                if op in [self.UPDATE_APPEND, self.UPDATE_REMOVE]:
                    try:
                        value = field.check(value)
                    except (ValueError, TypeError, AttributeError):
                        raise DataStoreException(
                            f"Invalid value for field {doc_key}: {value}")

                elif op in [self.UPDATE_SET, self.UPDATE_DEC, self.UPDATE_INC]:
                    try:
                        value = field.check(value)
                    except (ValueError, TypeError):
                        raise DataStoreException(
                            f"Invalid value for field {doc_key}: {value}")

                if isinstance(value, Model):
                    value = value.as_primitives()
                elif isinstance(value, datetime):
                    value = value.isoformat()
                elif isinstance(value, ClassificationObject):
                    value = str(value)

            ret_ops.append((op, doc_key, value))

        return ret_ops