# Path: muffin_rest/sorting.py
# class Sort(Mutate):
#     """Sort a collection."""
# 
#     async def apply(self, collection, desc: bool = False, **_) -> t.Any:
#         """Sort the collection."""
#         return sorted(collection, key=lambda obj: getattr(obj, self.name), reverse=desc)
# 
# class Sorting(Mutator):
# 
#     """Build sorters for handlers."""
# 
#     MUTATE_CLASS = Sort
#     mutations: t.Dict[str, Sort]  # type: ignore
# 
#     def __init__(self, handler: Handler, params: t.Sequence):
#         """Initialize the sorting."""
#         self.default: t.List[Sort] = []
#         super(Sorting, self).__init__(handler, params)
# 
#     async def apply(self, request: Request, collection: TCOLLECTION, **_) -> TCOLLECTION:
#         """Sort the given collection."""
#         data = request.url.query.get(SORT_PARAM)
#         if data:
#             for name, desc in to_sort(data.split(',')):
#                 if name in self.mutations:
#                     collection = await self.mutations[name].apply(collection, desc)
# 
#         elif self.default:
#             return self.sort_default(collection)
# 
#         return collection
# 
#     def convert(self, obj, **meta) -> Sort:
#         """Prepare sorters."""
#         sort: Sort = super(Sorting, self).convert(obj, **meta)  # type: ignore
#         if sort.meta.get('default'):
#             self.default.append(sort)
#         return sort
# 
#     def sort_default(self, collection: TCOLLECTION) -> TCOLLECTION:
#         """Sort by default."""
#         return sorted(collection)  # type: ignore
# 
#     @property
#     def openapi(self):
#         """Prepare OpenAPI params."""
#         sorting = list(self.mutations)
#         return {
#             'name': SORT_PARAM, 'in': 'query', 'style': 'form', 'explode': False,
#             'schema': {'type': 'array', 'items': {'type': 'string', 'enum': sorting}},
#             'description': ",".join(sorting),
#         }


import typing as t
from peewee import Field, Query
from ..sorting import Sort, Sorting
        from . import PWRESTHandler
"""Support sorting for Peewee ORM."""





TCOLLECTION = t.TypeVar('TCOLLECTION', bound=Query)


class PWSort(Sort):
    """Sorter for Peewee."""

    async def apply(self, collection: TCOLLECTION, desc: bool = False, **_) -> TCOLLECTION:
        """Sort the collection."""
        return collection.order_by_extend(self.field if not desc else self.field.desc())  # type: ignore



# Path: muffin_rest/sorting.py
# class Sort(Mutate):
#     """Sort a collection."""
# 
#     async def apply(self, collection, desc: bool = False, **_) -> t.Any:
#         """Sort the collection."""
#         return sorted(collection, key=lambda obj: getattr(obj, self.name), reverse=desc)
# 
# class Sorting(Mutator):
# 
#     """Build sorters for handlers."""
# 
#     MUTATE_CLASS = Sort
#     mutations: t.Dict[str, Sort]  # type: ignore
# 
#     def __init__(self, handler: Handler, params: t.Sequence):
#         """Initialize the sorting."""
#         self.default: t.List[Sort] = []
#         super(Sorting, self).__init__(handler, params)
# 
#     async def apply(self, request: Request, collection: TCOLLECTION, **_) -> TCOLLECTION:
#         """Sort the given collection."""
#         data = request.url.query.get(SORT_PARAM)
#         if data:
#             for name, desc in to_sort(data.split(',')):
#                 if name in self.mutations:
#                     collection = await self.mutations[name].apply(collection, desc)
# 
#         elif self.default:
#             return self.sort_default(collection)
# 
#         return collection
# 
#     def convert(self, obj, **meta) -> Sort:
#         """Prepare sorters."""
#         sort: Sort = super(Sorting, self).convert(obj, **meta)  # type: ignore
#         if sort.meta.get('default'):
#             self.default.append(sort)
#         return sort
# 
#     def sort_default(self, collection: TCOLLECTION) -> TCOLLECTION:
#         """Sort by default."""
#         return sorted(collection)  # type: ignore
# 
#     @property
#     def openapi(self):
#         """Prepare OpenAPI params."""
#         sorting = list(self.mutations)
#         return {
#             'name': SORT_PARAM, 'in': 'query', 'style': 'form', 'explode': False,
#             'schema': {'type': 'array', 'items': {'type': 'string', 'enum': sorting}},
#             'description': ",".join(sorting),
#         }


# Path: muffin_rest/peewee/sorting.py
import typing as t
from peewee import Field, Query
from ..sorting import Sort, Sorting
        from . import PWRESTHandler

"""Support sorting for Peewee ORM."""





TCOLLECTION = t.TypeVar('TCOLLECTION', bound=Query)


class PWSort(Sort):
    """Sorter for Peewee."""

    async def apply(self, collection: TCOLLECTION, desc: bool = False, **_) -> TCOLLECTION:
        """Sort the collection."""
        return collection.order_by_extend(self.field if not desc else self.field.desc())  # type: ignore



class PWSorting(Sorting):