-
Notifications
You must be signed in to change notification settings - Fork 63
Workspaces API - order by schema fields instead of database fields #565
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
harminius
wants to merge
3
commits into
develop
Choose a base branch
from
api_to_db_fields_map
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+49
−9
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,16 @@ | ||
| # Copyright (C) Lutra Consulting Limited | ||
| # | ||
| # SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-MerginMaps-Commercial | ||
| import logging | ||
|
|
||
| import math | ||
| from collections import namedtuple | ||
| from datetime import datetime, timedelta, timezone | ||
| from enum import Enum | ||
| import os | ||
| from flask import current_app | ||
| from flask import current_app, abort | ||
| from flask_sqlalchemy import Model | ||
| from marshmallow import Schema | ||
| from pathvalidate import sanitize_filename | ||
| from sqlalchemy import Column, JSON | ||
| from sqlalchemy.sql.elements import UnaryExpression | ||
|
|
@@ -33,7 +36,7 @@ def split_order_param(order_param: str) -> Optional[OrderParam]: | |
|
|
||
|
|
||
| def get_order_param( | ||
| cls: Model, order_param: OrderParam, json_sort: dict = None | ||
| cls: Model, order_param: OrderParam, json_sort: dict = None, field_map: dict = None | ||
| ) -> Optional[UnaryExpression]: | ||
| """Return order by clause parameter for SQL query | ||
|
|
||
|
|
@@ -43,15 +46,22 @@ def get_order_param( | |
| :type order_param: OrderParam | ||
| :param json_sort: type mapping for sort by json field, e.g. '{"storage": "int"}', defaults to None | ||
| :type json_sort: dict | ||
| :param field_map: mapping for translating public field names to internal DB columns, e.g. '{"size": "disk_usage"}' | ||
| :type field_map: dict | ||
| """ | ||
| # translate field name to column name | ||
| db_column_name = order_param.name | ||
| if field_map and order_param.name in field_map: | ||
| db_column_name = field_map[order_param.name] | ||
| # find candidate for nested json sort | ||
| if "." in order_param.name: | ||
| col, attr = order_param.name.split(".") | ||
| if "." in db_column_name: | ||
| col, attr = db_column_name.split(".") | ||
| else: | ||
| col = order_param.name | ||
| col = db_column_name | ||
| attr = None | ||
| order_attr = cls.__table__.c.get(col, None) | ||
| if not isinstance(order_attr, Column): | ||
| logging.warning("Ignoring invalid order parameter.") | ||
| return | ||
| # sort by key in JSON field | ||
| if attr: | ||
|
|
@@ -80,7 +90,9 @@ def get_order_param( | |
| return order_attr.desc() | ||
|
|
||
|
|
||
| def parse_order_params(cls: Model, order_params: str, json_sort: dict = None): | ||
| def parse_order_params( | ||
| cls: Model, order_params: str, json_sort: dict = None, field_map: dict = None | ||
| ) -> list[UnaryExpression]: | ||
| """Convert order parameters in query string to list of order by clauses. | ||
|
|
||
| :param cls: Db model class | ||
|
|
@@ -89,6 +101,8 @@ def parse_order_params(cls: Model, order_params: str, json_sort: dict = None): | |
| :type order_params: str | ||
| :param json_sort: type mapping for sort by json field, e.g. '{"storage": "int"}', defaults to None | ||
| :type json_sort: dict | ||
| :param field_map: mapping response fields to database column names, e.g. '{"size": "disk_usage"}' | ||
| :type field_map: dict | ||
|
|
||
| :rtype: List[Column] | ||
| """ | ||
|
|
@@ -97,7 +111,7 @@ def parse_order_params(cls: Model, order_params: str, json_sort: dict = None): | |
| order_param = split_order_param(p) | ||
| if not order_param: | ||
| continue | ||
| order_attr = get_order_param(cls, order_param, json_sort) | ||
| order_attr = get_order_param(cls, order_param, json_sort, field_map) | ||
| if order_attr is not None: | ||
| order_by_params.append(order_attr) | ||
| return order_by_params | ||
|
|
@@ -135,3 +149,16 @@ def save_diagnostic_log_file(app: str, username: str, body: bytes) -> str: | |
| f.write(content) | ||
|
|
||
| return file_name | ||
|
|
||
|
|
||
| def get_schema_fields_map(schema: Schema) -> dict: | ||
| """ | ||
| Creates a mapping of schema field names to corresponding DB columns. | ||
| This allows sorting by the API field name (e.g. 'size') while | ||
| actually sorting by the database column (e.g. 'disk_usage'). | ||
| """ | ||
| mapping = {} | ||
| for name, field in schema._declared_fields.items(): | ||
| if field and field.attribute: | ||
| mapping[name] = field.attribute | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about fallback here? |
||
| return mapping | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is necessary? Its not better to have function where this schema will go as param?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be (I had it in my first version). But it seemed cleaner to me to have it here. Moreover, the map is created once and then the "cached" version is used for all API calls.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this field_map is like standard or just patched value? What about add it to some method or something instead of here? I think we do not need to cache that.