def as_dict(self, keys_as_camel_case: bool = True, persisting=False):
        """
        Returns a dictionary representation of this object
        :raises TypeError: If the current object does not conform to the constraints declared in an engine.Document
        :return:
        """
        from odm.type import MongoObject, MongoForeignKey, MongoList
        d = {}

        for attr, mongo_type_inst in self._get_declared_class_mongo_attrs():
            if mongo_type_inst._serialize or persisting:
                # set default if value is None and we are persisting, and of course only if there is a _default
                if mongo_type_inst._default and getattr(self, attr) is None:
                    setattr(self, attr, mongo_type_inst._default)
                self.validate(attr)

                curr_val_for_attr = getattr(self, attr)

                if mongo_type_inst._serialize_as:
                    if isinstance(curr_val_for_attr, MongoObject):
                        d[mongo_type_inst.
                          _serialize_as] = curr_val_for_attr.as_dict()
                    elif isinstance(mongo_type_inst,
                                    MongoForeignKey) and persisting:
                        d[mongo_type_inst._serialize_as] = ObjectId(
                            curr_val_for_attr)
                    elif isinstance(mongo_type_inst, MongoList):
                        l = []
                        for item in curr_val_for_attr:
                            if isinstance(item, MongoObject):
                                l.append(item.as_dict())
                            else:
                                l.append(item)
                        d[mongo_type_inst._serialize_as] = l
                    else:
                        d[mongo_type_inst._serialize_as] = curr_val_for_attr
                elif keys_as_camel_case:
                    if isinstance(curr_val_for_attr, MongoObject):
                        d[snake_to_camel(attr)] = curr_val_for_attr.as_dict()
                    elif isinstance(mongo_type_inst,
                                    MongoForeignKey) and persisting:
                        d[snake_to_camel(attr)] = ObjectId(curr_val_for_attr)
                    elif isinstance(mongo_type_inst, MongoList):
                        l = []
                        for item in curr_val_for_attr:
                            if isinstance(item, MongoObject):
                                l.append(item.as_dict())
                            else:
                                l.append(item)
                        d[snake_to_camel(attr)] = l
                    else:
                        d[snake_to_camel(attr)] = curr_val_for_attr
                else:
                    d[attr] = curr_val_for_attr

        return d
    def _get_serialized_fields(cls) -> List[Tuple[str, str]]:
        """

        :return: Tuple where first value is the attribute of the python field and the second value is the
        potential serialized JSON key/attribute
        """
        serialized_fields = []
        for attr, mongo_inst in cls._get_declared_class_mongo_attrs():
            if not mongo_inst._serialize:
                continue
            if mongo_inst._serialize_as:
                serialized_fields.append((attr, mongo_inst._serialize_as))
            serialized_fields.append((attr, snake_to_camel(attr)))
            serialized_fields.append((attr, attr))

        return serialized_fields
Beispiel #3
0
 def test_snake_to_camel_with_already_camel(self):
     self.assertEqual(snake_to_camel('camelCase'), 'camelCase')
Beispiel #4
0
 def test_snake_to_camel_for_id(self):
     self.assertEqual(snake_to_camel('_id'), '_id')
Beispiel #5
0
 def test_snake_to_camel(self):
     self.assertEqual(snake_to_camel('snake_case'), 'snakeCase')
     self.assertEqual(snake_to_camel(''), '')
     self.assertEqual(snake_to_camel('c'), 'c')