コード例 #1
0
 async def order_by_pagination(cls, queryset: QuerySet, order_by: 'OrderByType', pagination: 'PaginationType') -> \
 List[Any]:
     if order_by is not None:
         order_by.field = to_camel_case(order_by.field)
         if order_by.desk:
             queryset = queryset.order_by(f'-{order_by.field}')
         else:
             queryset = queryset.order_by(order_by.field)
     if pagination is not None:
         queryset = queryset.offset(pagination.limit *
                                    (pagination.page - 1)).limit(
                                        pagination.limit)
     return await queryset
コード例 #2
0
    def annotate(cls: Type[MODEL], **kwargs: Function) -> QuerySet[MODEL]:
        """
        Annotates the result set with extra Functions/Aggregations.

        :param kwargs: Parameter name and the Function/Aggregation to annotate with.
        """
        return QuerySet(cls).annotate(**kwargs)
コード例 #3
0
async def create_or_update_object_from_mapping(queryset_object: QuerySet,
                                               queryset_base_fields: dict,
                                               object_to_map: dict,
                                               data_fields: dict) -> None:

    item_type_model_fields = queryset_base_fields['item_type'].model_fields

    items = return_field_mapping(item_type_model_fields, object_to_map,
                                 data_fields)

    queryset = queryset_object.filter(**queryset_base_fields)
    queryset_exists = await queryset.exists()

    fields = {}
    fields.update(queryset_base_fields)

    create_or_update = False

    ## the Call of Duty API in all of it's infinite wisdom returns 'None' and 0 respectively
    ## for all fields for any given object, even if the player hasn't played that object.
    ## thuis, if this is the case we're wasting space int he DB. Just don't continue if this is the case.
    if not (all(value == 0 or value == None for value in items.values())):
        fields.update(items)

        if not queryset_exists:

            thisobject = await queryset_object.create(**fields)

        else:

            await queryset.update(**fields)
            thisobject = await queryset.first()

        return thisobject
コード例 #4
0
ファイル: models.py プロジェクト: Ravillatypov/tortoise-orm
    def filter(cls: Type[MODEL], *args: Q, **kwargs: Any) -> QuerySet[MODEL]:
        """
        Generates a QuerySet with the filter applied.

        :param args:
        :param kwargs:
        """
        return QuerySet(cls).filter(*args, **kwargs)
コード例 #5
0
    def exclude(cls: Type[MODEL], *args: Q, **kwargs: Any) -> QuerySet[MODEL]:
        """
        Generates a QuerySet with the exclude applied.

        :param args: Q funtions containing constraints. Will be AND'ed.
        :param kwargs: Simple filter constraints.
        """
        return QuerySet(cls).exclude(*args, **kwargs)
コード例 #6
0
def prepare_default_ordering(meta: "Model.Meta") -> Tuple[Tuple[str, Order], ...]:
    ordering_list = getattr(meta, "ordering", ())

    parsed_ordering = tuple(
        QuerySet._resolve_ordering_string(ordering) for ordering in ordering_list
    )

    return parsed_ordering
コード例 #7
0
ファイル: models.py プロジェクト: Ravillatypov/tortoise-orm
    def exclude(cls: Type[MODEL], *args: Q, **kwargs: Any) -> QuerySet[MODEL]:
        """
        Generates a QuerySet with the exclude applied.

        :param args:
        :param kwargs:
        """
        return QuerySet(cls).exclude(*args, **kwargs)
コード例 #8
0
    def get_or_none(cls: Type[MODEL], *args,
                    **kwargs) -> QuerySetSingle[Optional[MODEL]]:
        """
        Fetches a single record for a Model type using the provided filter parameters or None.

        .. code-block:: python3

            user = await User.get(username="******")
        """
        return QuerySet(cls).filter(*args, **kwargs).first()
コード例 #9
0
async def read_valid_paths(background_tasks: BackgroundTasks):
    """
    # To get info on all valid paths for posts and comments ordered by fetched utc
    """
    data = get_from_cache("/api/v2/display")
    if data is not None:
        return data
    posts = await PostsList_Pydantic.from_queryset(
        QuerySet(Posts).only("fetched_utc").distinct()
    )
    comments = await CommentsList_Pydantic.from_queryset(
        QuerySet(Comments).only("fetched_utc").distinct()
    )
    data = posts.dict()["__root__"]
    data.extend(comments.dict()["__root__"])
    data.sort(key=lambda item: item["fetched_utc"], reverse=True)
    for item in data:
        item.update({"fetched_utc": item["fetched_utc"].isoformat()})
    background_tasks.add_task(add_to_cache, url_path="/api/v2/display", data=data)
    return data
コード例 #10
0
    def get_or_none(cls: Type[MODEL], *args: Q, **kwargs: Any) -> QuerySetSingle[Optional[MODEL]]:
        """
        Fetches a single record for a Model type using the provided filter parameters or None.

        .. code-block:: python3

            user = await User.get(username="******")

        :param args: Q funtions containing constraints. Will be AND'ed.
        :param kwargs: Simple filter constraints.
        """
        return QuerySet(cls).get_or_none(*args, **kwargs)
コード例 #11
0
    def get(cls: Type[MODEL], *args, **kwargs) -> QuerySetSingle[MODEL]:
        """
        Fetches a single record for a Model type using the provided filter parameters.

        .. code-block:: python3

            user = await User.get(username="******")

        :raises MultipleObjectsReturned: If provided search returned more than one object.
        :raises DoesNotExist: If object can not be found.
        """
        return QuerySet(cls).get(*args, **kwargs)
コード例 #12
0
    def exists(cls: Type[MODEL], *args: Q, **kwargs: Any) -> ExistsQuery:
        """
        Return True/False whether record exists with the provided filter parameters.

        .. code-block:: python3

            result = await User.exists(username="******")

        :param args: Q funtions containing constraints. Will be AND'ed.
        :param kwargs: Simple filter constraints.
        """
        return QuerySet(cls).filter(*args, **kwargs).exists()
コード例 #13
0
    async def refresh_from_db(
        self, fields: Optional[Iterable[str]] = None, using_db: Optional[BaseDBAsyncClient] = None
    ) -> None:
        """
        Refresh latest data from db.

        .. code-block:: python3

            user.refresh_from_db(fields=['name'])

        :param fields: The special fields that to be refreshed.
        :param using_db: Specific DB connection to use instead of default bound.

        :raises OperationalError: If object has never been persisted.
        """
        if not self._saved_in_db:
            raise OperationalError("Can't refresh unpersisted record")
        qs = QuerySet(self.__class__).only(*(fields or []))
        using_db and qs.using_db(using_db)
        obj = await qs.get(pk=self.pk)
        for field in fields or self._meta.fields_map:
            setattr(self, field, getattr(obj, field, None))
コード例 #14
0
    def get(cls: Type[MODEL], *args: Q, **kwargs: Any) -> QuerySetSingle[MODEL]:
        """
        Fetches a single record for a Model type using the provided filter parameters.

        .. code-block:: python3

            user = await User.get(username="******")

        :param args: Q funtions containing constraints. Will be AND'ed.
        :param kwargs: Simple filter constraints.

        :raises MultipleObjectsReturned: If provided search returned more than one object.
        :raises DoesNotExist: If object can not be found.
        """
        return QuerySet(cls).get(*args, **kwargs)
コード例 #15
0
 def filter(cls: Type[MODEL], *args, **kwargs) -> QuerySet[MODEL]:
     """
     Generates a QuerySet with the filter applied.
     """
     return QuerySet(cls).filter(*args, **kwargs)
コード例 #16
0
 def first(cls: Type[MODEL]) -> QuerySetSingle[Optional[MODEL]]:
     """
     Generates a QuerySet that returns the first record.
     """
     return QuerySet(cls).first()
コード例 #17
0
ファイル: models.py プロジェクト: gridl/tortoise-orm
 def get(cls, *args, **kwargs) -> QuerySet:
     return QuerySet(cls).get(*args, **kwargs)
コード例 #18
0
ファイル: models.py プロジェクト: gridl/tortoise-orm
 def all(cls) -> QuerySet:
     return QuerySet(cls)
コード例 #19
0
ファイル: models.py プロジェクト: gridl/tortoise-orm
 def filter(cls, *args, **kwargs) -> QuerySet:
     return QuerySet(cls).filter(*args, **kwargs)
コード例 #20
0
 def all(cls: Type[MODEL]) -> QuerySet[MODEL]:
     """
     Returns the complete QuerySet.
     """
     return QuerySet(cls)
コード例 #21
0
ファイル: models.py プロジェクト: zoliszeredi/tortoise-orm
 def all(cls) -> QuerySet:
     """
     Returns the complete QuerySet.
     """
     return QuerySet(cls)
コード例 #22
0
ファイル: models.py プロジェクト: zoliszeredi/tortoise-orm
 def annotate(cls, **kwargs) -> QuerySet:
     return QuerySet(cls).annotate(**kwargs)
コード例 #23
0
ファイル: models.py プロジェクト: zoliszeredi/tortoise-orm
 def exclude(cls, *args, **kwargs) -> QuerySet:
     """
     Generates a QuerySet with the exclude applied.
     """
     return QuerySet(cls).exclude(*args, **kwargs)
コード例 #24
0
ファイル: manager.py プロジェクト: vadim-shadrin/tortoise-orm
 def get_queryset(self) -> QuerySet:
     return QuerySet(self._model)
コード例 #25
0
ファイル: models.py プロジェクト: zoliszeredi/tortoise-orm
 def first(cls) -> QuerySet:
     """
     Generates a QuerySet that returns the first record.
     """
     return QuerySet(cls).first()
コード例 #26
0
 def exclude(cls: Type[MODEL], *args, **kwargs) -> QuerySet[MODEL]:
     """
     Generates a QuerySet with the exclude applied.
     """
     return QuerySet(cls).exclude(*args, **kwargs)
コード例 #27
0
 def annotate(cls: Type[MODEL], **kwargs) -> QuerySet[MODEL]:
     return QuerySet(cls).annotate(**kwargs)
コード例 #28
0
ファイル: models.py プロジェクト: gridl/tortoise-orm
 def first(cls) -> QuerySet:
     return QuerySet(cls).first()
コード例 #29
0
ファイル: models.py プロジェクト: zoliszeredi/tortoise-orm
 def filter(cls, *args, **kwargs) -> QuerySet:
     """
     Generates a QuerySet with the filter applied.
     """
     return QuerySet(cls).filter(*args, **kwargs)
コード例 #30
0
ファイル: models.py プロジェクト: dheshun/tortoise-orm
 def exclude(cls, *args, **kwargs) -> QuerySet:
     return QuerySet(cls).exclude(*args, **kwargs)