Serverside sort and limit for vendor and filaments
This commit is contained in:
@@ -2,7 +2,33 @@
|
||||
|
||||
from enum import Enum
|
||||
|
||||
import sqlalchemy
|
||||
|
||||
from spoolman.database import models
|
||||
|
||||
|
||||
class SortOrder(Enum):
|
||||
ASC = 1
|
||||
DESC = 2
|
||||
|
||||
|
||||
def parse_nested_field(base_obj: type[models.Base], field: str) -> sqlalchemy.Column:
|
||||
"""Parse a nested field string into a sqlalchemy field object."""
|
||||
fields = field.split(".")
|
||||
if not hasattr(base_obj, fields[0]):
|
||||
raise ValueError(f"Invalid field name '{field}'")
|
||||
|
||||
if fields[0] == "filament" and len(fields) == 1:
|
||||
raise ValueError("No field specified for filament")
|
||||
if fields[0] == "filament":
|
||||
return parse_nested_field(models.Filament, ".".join(fields[1:]))
|
||||
|
||||
if fields[0] == "vendor" and len(fields) == 1:
|
||||
raise ValueError("No field specified for vendor")
|
||||
if fields[0] == "vendor":
|
||||
return parse_nested_field(models.Vendor, ".".join(fields[1:]))
|
||||
|
||||
if len(fields) > 1:
|
||||
raise ValueError(f"Field '{fields[0]}' does not have any nested fields")
|
||||
|
||||
return getattr(base_obj, fields[0])
|
||||
|
||||
Reference in New Issue
Block a user