mirror of
https://github.com/creyD/creyPY.git
synced 2026-04-13 03:40:31 +02:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4c25d91e18 | |||
| f24db62781 |
@@ -2,6 +2,10 @@
|
|||||||
|
|
||||||
All notable changes to this project will be documented in this file.
|
All notable changes to this project will be documented in this file.
|
||||||
|
|
||||||
|
## 1.2.X
|
||||||
|
|
||||||
|
- Added order_by method
|
||||||
|
|
||||||
## 1.1.0
|
## 1.1.0
|
||||||
|
|
||||||
- Added headers to testing
|
- Added headers to testing
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ def update_obj_from_data(
|
|||||||
model: Type[T],
|
model: Type[T],
|
||||||
id: UUID | str,
|
id: UUID | str,
|
||||||
db: Session,
|
db: Session,
|
||||||
partial: bool = False,
|
partial: bool = False, # TODO: inverse, because it is currently the wrong way around
|
||||||
ignore_fields=[],
|
ignore_fields=[],
|
||||||
additional_data={},
|
additional_data={},
|
||||||
exclude={},
|
exclude={},
|
||||||
|
|||||||
@@ -1,14 +1,25 @@
|
|||||||
from typing import Callable, Optional
|
from typing import Callable
|
||||||
|
|
||||||
from sqlalchemy import asc, desc
|
from pydantic.json_schema import SkipJsonSchema
|
||||||
|
from sqlalchemy import String, asc, cast, desc
|
||||||
|
from sqlalchemy.dialects.postgresql import UUID
|
||||||
from sqlalchemy.sql.selectable import Select
|
from sqlalchemy.sql.selectable import Select
|
||||||
|
|
||||||
|
|
||||||
def order_by(order_by: Optional[str] = None) -> Callable[[Select], Select]:
|
def order_by(order_by: str | SkipJsonSchema[None] = None) -> Callable[[Select], Select]:
|
||||||
def _order_by(query: Select) -> Select:
|
def _order_by(query: Select) -> Select:
|
||||||
if order_by:
|
if order_by:
|
||||||
direction = desc if order_by.startswith("-") else asc
|
direction = desc if order_by.startswith("-") else asc
|
||||||
query = query.order_by(direction(order_by.lstrip("-")))
|
column_name = order_by.lstrip("-")
|
||||||
|
|
||||||
|
# Get the column from the query
|
||||||
|
for column in query.inner_columns:
|
||||||
|
if column.key == column_name:
|
||||||
|
# If the column is a UUID, cast it to a string
|
||||||
|
if isinstance(column.type, UUID):
|
||||||
|
column = cast(column, String)
|
||||||
|
query = query.order_by(direction(column))
|
||||||
|
break
|
||||||
return query
|
return query
|
||||||
|
|
||||||
return _order_by
|
return _order_by
|
||||||
|
|||||||
Reference in New Issue
Block a user