コード例 #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
コード例 #2
0
 class Flag(Model):
     uuid = UUID()
     name = Keyword()
     fans = List(Integer(), default=[])
コード例 #3
0
 class A(Model):
     fast = Integer(default=1)
     slow = Keyword(default='abc')
     flags = List(Keyword(), default=['cat-snack'])
コード例 #4
0
 class BannedTest(Model):
     ALL = Integer()
コード例 #5
0
 class BannedTest(Model):
     id = Integer()
コード例 #6
0
 class BannedTest(Model):
     _1 = Integer()
コード例 #7
0
 class Test(Model):
     a = Compound(Inner)
     b = Integer()
コード例 #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)
コード例 #9
0
 class Test(Model):
     a = Integer()
     b = Integer()
コード例 #10
0
 class InnerA(Model):
     number = Integer(default=10)
     value = Keyword()
コード例 #11
0
 class InnerB(Model):
     number = Integer()
     value = Keyword()
コード例 #12
0
 class Entry(Model):
     value = Integer()
     key = Keyword()
コード例 #13
0
 class Test(Model):
     values = List(Integer())
コード例 #14
0
    class Test(Model):
        first = Integer()

        @first.getter
        def first(self, value):
            return value if value >= 1 else 100
コード例 #15
0
 class A(Model):
     fast = Integer(default=1)
     slow = Keyword(default='abc')
     count = List(Integer())
コード例 #16
0
 class Test(Model):
     a = Mapping(Integer(), default={}, index=True, store=True)
コード例 #17
0
 class Inner(Model):
     a = Integer()
     b = Integer()
コード例 #18
0
 class Test(Model):
     a = Mapping(Integer(), default={}, index=False, store=False)
コード例 #19
0
 class Test(Model):
     first = Keyword()
     second = Integer()
コード例 #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