Exemple #1
0
    def test_deser(self):
        class C(ComplexModel):
            a = Unicode
            b = Decimal

        ret = get_object_as_json(C(a='burak', b=D(30)), C)
        assert ret == '["burak", "30"]'
        ret = get_object_as_json(C(a='burak', b=D(30)), C, complex_as=dict)
        assert ret == '{"a": "burak", "b": "30"}'
Exemple #2
0
    def test_deser(self):
        class C(ComplexModel):
            _type_info = [
                ('a', Unicode),
                ('b', Decimal),
            ]

        ret = get_object_as_json(C(a='burak', b=D(30)), C)
        assert ret == b'["burak", "30"]'
        ret = get_object_as_json(C(a='burak', b=D(30)), C, complex_as=dict)
        assert json.loads(ret.decode('utf8')) == \
                                        json.loads(u'{"a": "burak", "b": "30"}')
Exemple #3
0
    def test_deser(self):
        class C(ComplexModel):
            _type_info = [
                ('a', Unicode),
                ('b', Decimal),
            ]

        ret = get_object_as_json(C(a='burak', b=D(30)), C)
        assert ret == b'["burak", "30"]'
        ret = get_object_as_json(C(a='burak', b=D(30)), C, complex_as=dict)
        assert json.loads(ret.decode('utf8')) == \
                                        json.loads(u'{"a": "burak", "b": "30"}')
Exemple #4
0
        def process(value):
            if value is not None:
                if value.data is not None:
                    value.path = uuid1().get_hex()
                    fp = join(self.store, value.path)
                    if not abspath(fp).startswith(self.store):
                        raise ValidationError(
                            value.path, "Path %r contains "
                            "relative path operators (e.g. '..')")

                    with open(fp, 'wb') as file:
                        for d in value.data:
                            file.write(d)

                elif value.handle is not None:
                    value.path = uuid1().hex
                    fp = join(self.store, value.path)
                    if not abspath(fp).startswith(self.store):
                        raise ValidationError(
                            value.path, "Path %r contains "
                            "relative path operators (e.g. '..')")

                    data = mmap(value.handle.fileno(), 0)  # 0 = whole file
                    with open(fp, 'wb') as out_file:
                        out_file.write(data)

                elif value.path is not None:
                    in_file_path = value.path

                    if not isfile(in_file_path):
                        logger.error("File path in %r not found" % value)

                    if dirname(abspath(in_file_path)) != self.store:
                        dest = join(self.store, uuid1().get_hex())

                        if value.move:
                            shutil.move(in_file_path, dest)
                            print("move", in_file_path, dest)
                        else:
                            shutil.copy(in_file_path, dest)

                        value.path = basename(dest)
                        value.abspath = dest

                else:
                    raise ValueError("Invalid file object passed in. All of "
                                     ".data .handle and .path are None.")

                value.store = self.store
                value.abspath = join(self.store, value.path)

                retval = get_object_as_json(
                    value,
                    self.cls,
                    ignore_wrappers=self.ignore_wrappers,
                    complex_as=self.complex_as,
                )

                return retval
Exemple #5
0
 def process(value):
     if value is not None:
         return get_object_as_json(
             value,
             self.cls,
             ignore_wrappers=self.ignore_wrappers,
             complex_as=self.complex_as,
         )
Exemple #6
0
        def process(value):
            if value is not None:
                if value.data is not None:
                    value.path = uuid1().get_hex()
                    fp = join(self.store, value.path)
                    if not abspath(fp).startswith(self.store):
                        raise ValidationError(value.path, "Path %r contains "
                                          "relative path operators (e.g. '..')")

                    with open(fp, 'wb') as file:
                        for d in value.data:
                            file.write(d)

                elif value.handle is not None:
                    value.path = uuid1().hex
                    fp = join(self.store, value.path)
                    if not abspath(fp).startswith(self.store):
                        raise ValidationError(value.path, "Path %r contains "
                                          "relative path operators (e.g. '..')")

                    data = mmap(value.handle.fileno(), 0) # 0 = whole file
                    with open(fp, 'wb') as out_file:
                        out_file.write(data)

                elif value.path is not None:
                    in_file_path = value.path

                    if not isfile(in_file_path):
                        logger.error("File path in %r not found" % value)

                    if dirname(abspath(in_file_path)) != self.store:
                        dest = join(self.store, uuid1().get_hex())

                        if value.move:
                            shutil.move(in_file_path, dest)
                            print("move", in_file_path, dest)
                        else:
                            shutil.copy(in_file_path, dest)

                        value.path = basename(dest)
                        value.abspath = dest

                else:
                    raise ValueError("Invalid file object passed in. All of "
                                            ".data .handle and .path are None.")

                value.store = self.store
                value.abspath = join(self.store, value.path)

                retval = get_object_as_json(value, self.cls,
                        ignore_wrappers=self.ignore_wrappers,
                        complex_as=self.complex_as,
                    )

                return retval
Exemple #7
0
 def process(value):
     if value is not None:
         return get_object_as_json(value, self.cls,
                 ignore_wrappers=self.ignore_wrappers,
                 complex_as=self.complex_as,
             )