Beispiel #1
0
    def _filter_multi_query(
            self, query: Query,
            filtration: schemas.FilterData) -> tuple[int, list[ModelType]]:
        for col_name, (filter_type, filter_value) in filtration.data.items():
            column = getattr(self.model, col_name)
            if filter_type == schemas.FilterType.VALUE:
                query = query.filter(column == filter_value)
            elif filter_type == schemas.FilterType.DATETIME:
                start_date, end_date = filter_value
                if start_date is not None:
                    query = query.filter(column >= filter_value)
                if end_date is not None:
                    query = query.filter(column <= filter_value)
            elif filter_type == schemas.FilterType.ENUM:
                query = query.filter(column in filter_value)

        query = query.order_by(
            self.model.created if filtration.sort is None else
            getattr(self.model, filtration.sort.column
                    ) if filtration.sort.direction != schemas.SortType.
            ASC else getattr(self.model, filtration.sort.column).desc())

        return (
            query.count(),
            query.offset(filtration.offset).limit(filtration.limit).all(),
        )
Beispiel #2
0
    def __call__(self, scope: SAQuery) -> List[T]:
        total = scope.count()

        results = scope.limit(self.page_size).offset(self.offset).all()

        meta = ListMeta(
            total=total,
            total_pages=self.total_pages(total),
            page_size=self.page_size,
            page=self.page,
            next_page=self.next_page(total),
            prev_page=self.prev_page,
        )

        return List(data=results, meta=meta)
Beispiel #3
0
 def count(self):
     # Without it it works with slow implementation of count(), while
     # we often use a faster one from older version.
     return Query.count(self.private())
Beispiel #4
0
 def count(self):
     # Without it it works with slow implementation of count(), while
     # we often use a faster one from older version.
     return Query.count(self.private())