File: fields.py

package info (click to toggle)
python-beanie 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,484 kB
  • sloc: python: 14,427; makefile: 6; sh: 6
file content (711 lines) | stat: -rw-r--r-- 22,229 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
from __future__ import annotations

import asyncio
from collections import OrderedDict
from dataclasses import dataclass
from enum import Enum
from typing import (
    TYPE_CHECKING,
    Any,
    Dict,
    Generic,
    List,
    Optional,
    Tuple,
    Type,
    TypeVar,
    Union,
)
from typing import OrderedDict as OrderedDictType

from bson import DBRef, ObjectId
from bson.errors import InvalidId
from pydantic import BaseModel
from pymongo import ASCENDING, IndexModel
from typing_extensions import get_args

from beanie.odm.enums import SortDirection
from beanie.odm.operators.find.comparison import (
    GT,
    GTE,
    LT,
    LTE,
    NE,
    Eq,
    In,
)
from beanie.odm.registry import DocsRegistry
from beanie.odm.utils.parsing import parse_obj
from beanie.odm.utils.pydantic import (
    IS_PYDANTIC_V2,
    get_field_type,
    get_model_fields,
    parse_object_as,
)

if IS_PYDANTIC_V2:
    from pydantic import (
        GetCoreSchemaHandler,
        GetJsonSchemaHandler,
        TypeAdapter,
    )
    from pydantic.json_schema import JsonSchemaValue
    from pydantic_core import core_schema
    from pydantic_core.core_schema import CoreSchema, ValidationInfo
else:
    from pydantic.fields import ModelField
    from pydantic.json import ENCODERS_BY_TYPE

if TYPE_CHECKING:
    from beanie.odm.documents import DocType


@dataclass(frozen=True)
class IndexedAnnotation:
    _indexed: Tuple[int, Dict[str, Any]]


def Indexed(typ=None, index_type=ASCENDING, **kwargs: Any):
    """
    If `typ` is defined, returns a subclass of `typ` with an extra attribute
    `_indexed` as a tuple:
    - Index 0: `index_type` such as `pymongo.ASCENDING`
    - Index 1: `kwargs` passed to `IndexModel`
    When instantiated the type of the result will actually be `typ`.

    When `typ` is not defined, returns an `IndexedAnnotation` instance, to be
    used as metadata in `Annotated` fields.

    Example:
    ```py
    # Both fields would have the same behavior
    class MyModel(BaseModel):
        field1: Indexed(str, unique=True)
        field2: Annotated[str, Indexed(unique=True)]
    ```
    """
    if typ is None:
        return IndexedAnnotation(_indexed=(index_type, kwargs))

    class NewType(typ):
        _indexed = (index_type, kwargs)

        def __new__(cls, *args: Any, **kwargs: Any):
            return typ.__new__(typ, *args, **kwargs)

        if IS_PYDANTIC_V2:

            @classmethod
            def __get_pydantic_core_schema__(
                cls, _source_type: Type[Any], _handler: GetCoreSchemaHandler
            ) -> CoreSchema:
                custom_type = getattr(
                    typ, "__get_pydantic_core_schema__", None
                )
                if custom_type is not None:
                    return custom_type(_source_type, _handler)

                return core_schema.no_info_after_validator_function(
                    lambda v: v, core_schema.simple_ser_schema(typ.__name__)
                )

    NewType.__name__ = f"Indexed {typ.__name__}"
    return NewType


class PydanticObjectId(ObjectId):
    """
    Object Id field. Compatible with Pydantic.
    """

    @classmethod
    def _validate(cls, v):
        if isinstance(v, bytes):
            v = v.decode("utf-8")
        try:
            return PydanticObjectId(v)
        except (InvalidId, TypeError):
            raise ValueError("Id must be of type PydanticObjectId")

    if IS_PYDANTIC_V2:

        @classmethod
        def __get_pydantic_core_schema__(
            cls, source_type: Type[Any], handler: GetCoreSchemaHandler
        ) -> CoreSchema:
            definition = core_schema.definition_reference_schema(
                "PydanticObjectId"
            )  # used for deduplication

            return core_schema.definitions_schema(
                definition,
                [
                    core_schema.json_or_python_schema(
                        python_schema=core_schema.no_info_plain_validator_function(
                            cls._validate
                        ),
                        json_schema=core_schema.no_info_after_validator_function(
                            cls._validate,
                            core_schema.str_schema(
                                pattern="^[0-9a-f]{24}$",
                                min_length=24,
                                max_length=24,
                            ),
                        ),
                        serialization=core_schema.plain_serializer_function_ser_schema(
                            lambda instance: str(instance), when_used="json"
                        ),
                        ref=definition["schema_ref"],
                    )
                ],
            )

        @classmethod
        def __get_pydantic_json_schema__(
            cls,
            schema: core_schema.CoreSchema,
            handler: GetJsonSchemaHandler,  # type: ignore
        ) -> JsonSchemaValue:
            """
            Results such schema:
            ```json
            {
              "components": {
                "schemas": {
                  "Item": {
                    "properties": {
                      "id": {
                        "$ref": "#/components/schemas/PydanticObjectId"
                      }
                    },
                    "type": "object",
                    "title": "Item"
                  },
                  "PydanticObjectId": {
                    "type": "string",
                    "maxLength": 24,
                    "minLength": 24,
                    "pattern": "^[0-9a-f]{24}$",
                    "example": "5eb7cf5a86d9755df3a6c593"
                  }
                }
              }
            }
            ```
            """

            json_schema = handler(schema)
            schema_to_update = handler.resolve_ref_schema(json_schema)
            schema_to_update.update(example="5eb7cf5a86d9755df3a6c593")
            return json_schema

    else:

        @classmethod
        def __get_validators__(cls):
            yield cls._validate

        @classmethod
        def __modify_schema__(cls, field_schema: Dict[str, Any]):
            field_schema.update(
                type="string",
                example="5eb7cf5a86d9755df3a6c593",
            )


if not IS_PYDANTIC_V2:
    ENCODERS_BY_TYPE[PydanticObjectId] = (
        str  # it is a workaround to force pydantic make json schema for this field
    )

BeanieObjectId = PydanticObjectId


class ExpressionField(str):
    def __getitem__(self, item):
        """
        Get sub field

        :param item: name of the subfield
        :return: ExpressionField
        """
        return ExpressionField(f"{self}.{item}")

    def __getattr__(self, item):
        """
        Get sub field

        :param item: name of the subfield
        :return: ExpressionField
        """
        return ExpressionField(f"{self}.{item}")

    def __hash__(self):
        return hash(str(self))

    def __eq__(self, other):
        if isinstance(other, ExpressionField):
            return super(ExpressionField, self).__eq__(other)
        return Eq(field=self, other=other)

    def __gt__(self, other):
        return GT(field=self, other=other)

    def __ge__(self, other):
        return GTE(field=self, other=other)

    def __lt__(self, other):
        return LT(field=self, other=other)

    def __le__(self, other):
        return LTE(field=self, other=other)

    def __ne__(self, other):
        return NE(field=self, other=other)

    def __pos__(self):
        return self, SortDirection.ASCENDING

    def __neg__(self):
        return self, SortDirection.DESCENDING

    def __copy__(self):
        return self

    def __deepcopy__(self, memo):
        return self


class DeleteRules(str, Enum):
    DO_NOTHING = "DO_NOTHING"
    DELETE_LINKS = "DELETE_LINKS"


class WriteRules(str, Enum):
    DO_NOTHING = "DO_NOTHING"
    WRITE = "WRITE"


class LinkTypes(str, Enum):
    DIRECT = "DIRECT"
    OPTIONAL_DIRECT = "OPTIONAL_DIRECT"
    LIST = "LIST"
    OPTIONAL_LIST = "OPTIONAL_LIST"

    BACK_DIRECT = "BACK_DIRECT"
    BACK_LIST = "BACK_LIST"
    OPTIONAL_BACK_DIRECT = "OPTIONAL_BACK_DIRECT"
    OPTIONAL_BACK_LIST = "OPTIONAL_BACK_LIST"


class LinkInfo(BaseModel):
    field_name: str
    lookup_field_name: str
    document_class: Type[BaseModel]  # Document class
    link_type: LinkTypes
    nested_links: Optional[Dict] = None
    is_fetchable: bool = True


T = TypeVar("T")


class Link(Generic[T]):
    def __init__(self, ref: DBRef, document_class: Type[T]):
        self.ref = ref
        self.document_class = document_class

    async def fetch(self, fetch_links: bool = False) -> Union[T, Link[T]]:
        result = await self.document_class.get(  # type: ignore
            self.ref.id, with_children=True, fetch_links=fetch_links
        )
        return result or self

    @classmethod
    async def fetch_one(cls, link: Link[T]):
        return await link.fetch()

    @classmethod
    async def fetch_list(
        cls,
        links: List[Union[Link[T], DocType]],
        fetch_links: bool = False,
    ):
        """
        Fetch list that contains links and documents
        :param links:
        :param fetch_links:
        :return:
        """
        data = Link.repack_links(links)  # type: ignore
        ids_to_fetch = []
        document_class = None
        for doc_id, link in data.items():
            if isinstance(link, Link):
                if document_class is None:
                    document_class = link.document_class
                else:
                    if document_class != link.document_class:
                        raise ValueError(
                            "All the links must have the same model class"
                        )
                ids_to_fetch.append(link.ref.id)

        if ids_to_fetch:
            fetched_models = await document_class.find(  # type: ignore
                In("_id", ids_to_fetch),
                with_children=True,
                fetch_links=fetch_links,
            ).to_list()

            for model in fetched_models:
                data[model.id] = model

        return list(data.values())

    @staticmethod
    def repack_links(
        links: List[Union[Link[T], DocType]],
    ) -> OrderedDictType[Any, Any]:
        result = OrderedDict()
        for link in links:
            if isinstance(link, Link):
                result[link.ref.id] = link
            else:
                result[link.id] = link
        return result

    @classmethod
    async def fetch_many(cls, links: List[Link[T]]) -> List[Union[T, Link[T]]]:
        coros = []
        for link in links:
            coros.append(link.fetch())
        return await asyncio.gather(*coros)

    if IS_PYDANTIC_V2:

        @staticmethod
        def serialize(value: Union[Link[T], BaseModel]):
            if isinstance(value, Link):
                return value.to_dict()
            return value.model_dump(mode="json")

        @classmethod
        def wrapped_validate(
            cls, source_type: Type[Any], handler: GetCoreSchemaHandler
        ):
            def validate(
                v: Union[Link[T], T, DBRef, dict[str, Any]],
                validation_info: ValidationInfo,
            ) -> Link[T] | T:
                document_class = DocsRegistry.evaluate_fr(  # type: ignore
                    get_args(source_type)[0]
                )

                if isinstance(v, DBRef):
                    return cls(ref=v, document_class=document_class)
                if isinstance(v, Link):
                    return v
                if isinstance(v, dict) and v.keys() == {"id", "collection"}:
                    return cls(
                        ref=DBRef(
                            collection=v["collection"],
                            id=TypeAdapter(
                                document_class.model_fields["id"].annotation
                            ).validate_python(v["id"]),
                        ),
                        document_class=document_class,
                    )
                if isinstance(v, dict) or isinstance(v, BaseModel):
                    return parse_obj(document_class, v)

                # Default fallback case for unknown type
                new_id = TypeAdapter(
                    document_class.model_fields["id"].annotation
                ).validate_python(v)
                ref = DBRef(
                    collection=document_class.get_collection_name(), id=new_id
                )
                return cls(ref=ref, document_class=document_class)

            return validate

        @classmethod
        def __get_pydantic_core_schema__(
            cls, source_type: Type[Any], handler: GetCoreSchemaHandler
        ) -> CoreSchema:
            return core_schema.json_or_python_schema(
                python_schema=core_schema.with_info_plain_validator_function(
                    cls.wrapped_validate(source_type, handler)
                ),
                json_schema=core_schema.union_schema(
                    [
                        core_schema.typed_dict_schema(
                            {
                                "id": core_schema.typed_dict_field(
                                    core_schema.str_schema()
                                ),
                                "collection": core_schema.typed_dict_field(
                                    core_schema.str_schema()
                                ),
                            }
                        ),
                        core_schema.dict_schema(
                            keys_schema=core_schema.str_schema(),
                            values_schema=core_schema.any_schema(),
                        ),
                    ]
                ),
                serialization=core_schema.plain_serializer_function_ser_schema(
                    function=lambda instance: cls.serialize(instance),
                    when_used="json-unless-none",
                ),
            )

    else:

        @classmethod
        def __get_validators__(cls):
            yield cls._validate

        @classmethod
        def _validate(
            cls,
            v: Union[Link[T], T, DBRef, dict[str, Any]],
            field: ModelField,
        ) -> Link[T] | T:
            document_class = DocsRegistry.evaluate_fr(  # type: ignore
                field.sub_fields[0].type_
            )

            if isinstance(v, DBRef):
                return cls(ref=v, document_class=document_class)
            if isinstance(v, Link):
                return v
            if isinstance(v, dict) or isinstance(v, BaseModel):
                return parse_obj(document_class, v)

            # Default fallback case for unknown type
            new_id = parse_object_as(
                get_field_type(get_model_fields(document_class)["id"]), v
            )
            ref = DBRef(
                collection=document_class.get_collection_name(), id=new_id
            )
            return cls(ref=ref, document_class=document_class)

        @classmethod
        def __modify_schema__(cls, field_schema: Dict[str, Any]):
            field_schema.clear()
            field_schema.update(
                {
                    "anyOf": [
                        {
                            "properties": {
                                "id": {"type": "string", "title": "Id"},
                                "collection": {
                                    "type": "string",
                                    "title": "Collection",
                                },
                            },
                            "type": "object",
                            "required": ["id", "collection"],
                        },
                        {"type": "object"},
                    ],
                }
            )

    def to_ref(self):
        return self.ref

    def to_dict(self):
        return {"id": str(self.ref.id), "collection": self.ref.collection}


if not IS_PYDANTIC_V2:
    ENCODERS_BY_TYPE[Link] = lambda o: o.to_dict()


class BackLink(Generic[T]):
    """Back reference to a document"""

    def __init__(self, document_class: Type[T]):
        self.document_class = document_class

    if IS_PYDANTIC_V2:

        @classmethod
        def wrapped_validate(
            cls, source_type: Type[Any], handler: GetCoreSchemaHandler
        ):
            def validate(
                v: Union[T, dict[str, Any]], validation_info: ValidationInfo
            ) -> BackLink[T] | T:
                document_class = DocsRegistry.evaluate_fr(  # type: ignore
                    get_args(source_type)[0]
                )
                if isinstance(v, dict) or isinstance(v, BaseModel):
                    return parse_obj(document_class, v)
                return cls(document_class=document_class)

            return validate

        @classmethod
        def __get_pydantic_core_schema__(
            cls, source_type: Type[Any], handler: GetCoreSchemaHandler
        ) -> CoreSchema:
            # NOTE: BackLinks are only virtual fields, they shouldn't be serialized nor appear in the schema.
            return core_schema.json_or_python_schema(
                python_schema=core_schema.with_info_plain_validator_function(
                    cls.wrapped_validate(source_type, handler)
                ),
                json_schema=core_schema.dict_schema(
                    keys_schema=core_schema.str_schema(),
                    values_schema=core_schema.any_schema(),
                ),
                serialization=core_schema.plain_serializer_function_ser_schema(
                    lambda instance: cls.to_dict(instance),
                    return_schema=core_schema.dict_schema(),
                    when_used="json-unless-none",
                ),
            )

    else:

        @classmethod
        def __get_validators__(cls):
            yield cls._validate

        @classmethod
        def _validate(
            cls, v: Union[T, dict[str, Any]], field: ModelField
        ) -> BackLink[T] | T:
            document_class = DocsRegistry.evaluate_fr(  # type: ignore
                field.sub_fields[0].type_
            )
            if isinstance(v, dict) or isinstance(v, BaseModel):
                return parse_obj(document_class, v)
            return cls(document_class=document_class)

        @classmethod
        def __modify_schema__(cls, field_schema: Dict[str, Any]):
            field_schema.clear()
            field_schema.update(
                {
                    "anyOf": [
                        {
                            "properties": {
                                "id": {"type": "string", "title": "Id"},
                                "collection": {
                                    "type": "string",
                                    "title": "Collection",
                                },
                            },
                            "type": "object",
                            "required": ["id", "collection"],
                        },
                        {"type": "object"},
                    ],
                }
            )

    def to_dict(self) -> dict[str, str]:
        document_class = DocsRegistry.evaluate_fr(self.document_class)  # type: ignore
        return {"collection": document_class.get_collection_name()}


if not IS_PYDANTIC_V2:
    ENCODERS_BY_TYPE[BackLink] = lambda o: o.to_dict()


class IndexModelField:
    def __init__(self, index: IndexModel):
        self.index = index
        self.name = index.document["name"]

        self.fields = tuple(sorted(self.index.document["key"]))
        self.options = tuple(
            sorted(
                (k, v)
                for k, v in self.index.document.items()
                if k not in ["key", "v"]
            )
        )

    def __eq__(self, other):
        return self.fields == other.fields and self.options == other.options

    def __repr__(self):
        return f"IndexModelField({self.name}, {self.fields}, {self.options})"

    @staticmethod
    def list_difference(
        left: List[IndexModelField], right: List[IndexModelField]
    ):
        result = []
        for index in left:
            if index not in right:
                result.append(index)
        return result

    @staticmethod
    def list_to_index_model(left: List[IndexModelField]):
        return [index.index for index in left]

    @classmethod
    def from_pymongo_index_information(cls, index_info: dict):
        result = []
        for name, details in index_info.items():
            fields = details["key"]
            if ("_id", 1) in fields:
                continue

            options = {k: v for k, v in details.items() if k != "key"}
            index_model = IndexModelField(
                IndexModel(fields, name=name, **options)
            )
            result.append(index_model)
        return result

    def same_fields(self, other: IndexModelField):
        return self.fields == other.fields

    @staticmethod
    def find_index_with_the_same_fields(
        indexes: List[IndexModelField], index: IndexModelField
    ):
        for i in indexes:
            if i.same_fields(index):
                return i
        return None

    @staticmethod
    def merge_indexes(
        left: List[IndexModelField], right: List[IndexModelField]
    ):
        left_dict = {index.fields: index for index in left}
        right_dict = {index.fields: index for index in right}
        left_dict.update(right_dict)
        return list(left_dict.values())

    @classmethod
    def _validate(cls, v: Any) -> "IndexModelField":
        if isinstance(v, IndexModel):
            return IndexModelField(v)
        else:
            return IndexModelField(IndexModel(v))

    if IS_PYDANTIC_V2:

        @classmethod
        def __get_pydantic_core_schema__(
            cls, source_type: Type[Any], handler: GetCoreSchemaHandler
        ) -> CoreSchema:
            return core_schema.no_info_plain_validator_function(cls._validate)

    else:

        @classmethod
        def __get_validators__(cls):
            yield cls._validate