Пример #1
0
def get_dict(document: "Document"):
    exclude = set()
    if document.id is None:
        exclude.add("id")
    if not document.get_settings().model_settings.use_revision:
        exclude.add("revision_id")
    return bson_encoder.encode(document, by_alias=True, exclude=exclude)
Пример #2
0
async def test_custom_filed_types():
    custom1 = DocumentWithCustomFiledsTypes(
        color="#753c38",
        decimal=500,
        secret_bytes=b"secret_bytes",
        secret_string="super_secret_password",
        ipv4address="127.0.0.1",
        ipv4interface="192.0.2.5/24",
        ipv4network="192.0.2.0/24",
        ipv6address="::abc:7:def",
        ipv6interface="2001:db00::2/24",
        ipv6network="2001:db00::0/24",
        date="2000-12-24",
        time="12:24:12.000333",
        timedelta=4782453,
        set_type={"one", "two", "three"},
        tuple_type=tuple([3, "string"]),
        path="/etc/hosts",
    )
    custom2 = DocumentWithCustomFiledsTypes(
        color="magenta",
        decimal=500.213,
        secret_bytes=b"secret_bytes",
        secret_string="super_secret_password",
        ipv4address="127.0.0.1",
        ipv4interface="192.0.2.5/24",
        ipv4network="192.0.2.0/24",
        ipv6address="::abc:7:def",
        ipv6interface="2001:db00::2/24",
        ipv6network="2001:db00::0/24",
        date=1627498153,
        time="12:35",
        timedelta=4782453,
        set_type=["one", "two", "three"],
        tuple_type=[3, "three"],
        path=Path("C:\\Windows"),
    )
    c1 = await custom1.insert()
    c2 = await custom2.insert()
    c1_fromdb = await DocumentWithCustomFiledsTypes.get(c1.id)
    c2_fromdb = await DocumentWithCustomFiledsTypes.get(c2.id)
    assert bson_encoder.encode(c1_fromdb) == bson_encoder.encode(c1)
    assert bson_encoder.encode(c2_fromdb) == bson_encoder.encode(c2)
Пример #3
0
 def update_query(self) -> Dict[str, Any]:
     query: Dict[str, Any] = {}
     for expression in self.update_expressions:
         if isinstance(expression, BaseUpdateOperator):
             query.update(expression.query)
         elif isinstance(expression, dict):
             query.update(expression)
         else:
             raise TypeError("Wrong expression type")
     return bson_encoder.encode(query, custom_encoder=self.encoders)
Пример #4
0
    async def replace_one(
        self,
        document: "DocType",
        session: Optional[ClientSession] = None,
    ) -> UpdateResult:
        """
        Replace found document by provided
        :param document: Document - document, which will replace the found one
        :param session: Optional[ClientSession] - PyMongo session
        :return: UpdateResult
        """
        self.set_session(session=session)
        result: UpdateResult = (
            await self.document_model.get_motor_collection().replace_one(
                self.get_filter_query(),
                bson_encoder.encode(document, by_alias=True, exclude={"id"}),
                session=self.session,
            ))

        if not result.raw_result["updatedExisting"]:
            raise DocumentNotFound
        return result
Пример #5
0
 def get_filter_query(self) -> Mapping[str, Any]:
     if self.find_expressions:
         return bson_encoder.encode(And(*self.find_expressions).query,
                                    custom_encoder=self.encoders)
     else:
         return {}