From f04da2c7c7dc8035836bba86083243fcf0e9e83d Mon Sep 17 00:00:00 2001 From: pdmurray Date: Thu, 14 Nov 2024 16:27:57 -0800 Subject: [PATCH] Continue working --- .../_internal/server/views/api.py | 27 ++++++++++--------- .../_internal/server/views/pagination.py | 27 ++++++++++++------- conda-store-server/conda_store_server/api.py | 9 +++---- 3 files changed, 37 insertions(+), 26 deletions(-) diff --git a/conda-store-server/conda_store_server/_internal/server/views/api.py b/conda-store-server/conda_store_server/_internal/server/views/api.py index 644ca52e9..43d0a213d 100644 --- a/conda-store-server/conda_store_server/_internal/server/views/api.py +++ b/conda-store-server/conda_store_server/_internal/server/views/api.py @@ -23,8 +23,8 @@ from conda_store_server.server.auth import Authentication -def get_cursor(cursor: Optional[str] = None) -> Cursor: - return Cursor.load(cursor) +def get_cursor(encoded_cursor: Optional[str] = None) -> Cursor: + return Cursor.load(encoded_cursor) class PaginatedArgs(TypedDict): @@ -716,16 +716,18 @@ async def api_list_environments( role_bindings=auth.entity_bindings(entity), ) - sorts = get_sorts( - order=paginated_args["order"], - sort_by=paginated_args["sort_by"], - allowed_sort_bys={ - "namespace": orm.Namespace.name, - "name": orm.Environment.name, - }, - default_sort_by=["namespace", "name"], - default_order="asc", - ) + valid_sort_by = { + "namespace": orm.Namespace.name, + "name": orm.Environment.name, + } + + # sorts = get_sorts( + # order=paginated_args["order"], + # sort_by=paginated_args["sort_by"], + # allowed_sort_bys=valid_sort_by, + # default_sort_by=["namespace", "name"], + # default_order="asc", + # ) # query = ( # query @@ -745,6 +747,7 @@ async def api_list_environments( query=query, cursor=cursor, sort_by=paginated_args["sort_by"], + valid_sort_by=valid_sort_by, ) return paginated_api_response( diff --git a/conda-store-server/conda_store_server/_internal/server/views/pagination.py b/conda-store-server/conda_store_server/_internal/server/views/pagination.py index 076cda870..ae3e914df 100644 --- a/conda-store-server/conda_store_server/_internal/server/views/pagination.py +++ b/conda-store-server/conda_store_server/_internal/server/views/pagination.py @@ -1,5 +1,4 @@ import base64 -from typing import Dict, List, Optional import pydantic from sqlalchemy import tuple_ @@ -7,28 +6,30 @@ class Cursor(pydantic.BaseModel): - last_id: Optional[int] = 1 + last_id: int | None = 1 # List of names of attributes to order by, and the last value of the ordered attribute # { # 'namespace': 'foo', # 'environment': 'bar', # } - last_value: Optional[Dict[str, str]] = {} + last_value: dict[str, str] | None = {} def dump(self): return base64.b64encode(self.model_dump_json()) @classmethod - def load(cls, data: str): + def load(cls, data: str | None = None): + if data is None: + return cls() return cls.from_json(base64.b64decode(data)) def paginate( query: SqlQuery, cursor: Cursor, - sort_by: List[str], - valid_sort_by: Dict[str, object], + sort_by: list[str] | None = None, + valid_sort_by: dict[str, object] | None = None, ) -> SqlQuery: """Paginate the query using the cursor and the requested sort_bys. @@ -44,11 +45,19 @@ def paginate( cursor : Cursor Cursor object containing information about the last item on the previous page - sort_by : List[str] + sort_by : list[str] | None List of sort_by query parameters - valid_sort_by : Dict[str, object] + valid_sort_by : dict[str, object] | None Mapping between query parameter names and the orm object they apply to """ + breakpoint() + + if sort_by is None: + sort_by = [] + + if valid_sort_by is None: + valid_sort_by = {} + objects = [] last_values = [] for obj in sort_by: @@ -57,4 +66,4 @@ def paginate( return query.filter( tuple_(*objects, object.id) > (*last_values, cursor.last_id) - ).order_by(sorts) + ) # .order_by(sorts) diff --git a/conda-store-server/conda_store_server/api.py b/conda-store-server/conda_store_server/api.py index 16df0043a..e086d5c3c 100644 --- a/conda-store-server/conda_store_server/api.py +++ b/conda-store-server/conda_store_server/api.py @@ -323,11 +323,7 @@ def list_environments( Query Sqlalchemy query containing the requested environments """ - query = ( - db.query(orm.Environment) - .join(orm.Environment.namespace) - .join(orm.Environment.current_build) - ) + query = db.query(orm.Environment).join(orm.Environment.namespace) if namespace: query = query.filter(orm.Namespace.name == namespace) @@ -346,6 +342,9 @@ def list_environments( if not show_soft_deleted: query = query.filter(orm.Environment.deleted_on == null()) + if status or artifact or packages: + query = query.join(orm.Environment.current_build) + if status: query = query.filter(orm.Build.status == status)