diff --git a/.flake8 b/.flake8 index 396adf5..031d058 100644 --- a/.flake8 +++ b/.flake8 @@ -7,3 +7,6 @@ extend-ignore = BLK100, # See https://github.com/PyCQA/pycodestyle/issues/373 E203, + E402, +exclude = + venv diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4654e9b..3ca35da 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -17,6 +17,7 @@ repos: language_version: python3.9 - repo: https://github.com/pycqa/flake8.git rev: 6.0.0 + args: ["--ignore=E501,BLK100,E203,E402"] hooks: - id: flake8 additional_dependencies: diff --git a/odata_server/__init__.py b/odata_server/__init__.py index f33a800..3cbe5f1 100644 --- a/odata_server/__init__.py +++ b/odata_server/__init__.py @@ -1 +1 @@ -__version__ = "1.0.0a23" +__version__ = "1.0.0a24" diff --git a/odata_server/flask.py b/odata_server/flask.py index 70e30f7..255d73f 100644 --- a/odata_server/flask.py +++ b/odata_server/flask.py @@ -8,10 +8,11 @@ import abnf import pymongo +import pymongo.database import werkzeug from flask import Blueprint, Response, abort, request, url_for -from odata_server import edm +from odata_server import edm, settings from odata_server.utils import ( build_response_headers, expand_result, @@ -79,7 +80,7 @@ def make_setup_state(self, app, options, first_registration=False): and entity_set.custom_insert_business is None ): logger.error( - "EntitySet {} is managing an entity type that contains computed properties. The logic for initializaing those computed properties has to be configured".format( + "EntitySet {} is managing an entity type that contains computed properties. The logic for initializing those computed properties has to be configured".format( entity_set.Name ) ) @@ -126,9 +127,9 @@ def make_setup_state(self, app, options, first_registration=False): } ) ) - entity_set.annotations[ - "Org.OData.Core.V1.ResourcePath" - ] = entity_set.Annotations[-1] + entity_set.annotations["Org.OData.Core.V1.ResourcePath"] = ( + entity_set.Annotations[-1] + ) resource_path = edm.get_annotation( entity_set, "Org.OData.Core.V1.ResourcePath" @@ -273,7 +274,14 @@ def parse_prefer_header(value, version="4.0"): return data -def get(mongo, RootEntitySet, subject, id_value, prefers, session=None): +def get( + mongo: pymongo.database.Database, + RootEntitySet, + subject, + id_value, + prefers, + session=None, +): anonymous = not isinstance(subject, edm.EntitySet) qs = parse_qs(request.query_string) EntityType = subject.entity_type @@ -324,7 +332,11 @@ def get(mongo, RootEntitySet, subject, id_value, prefers, session=None): pipeline.append({"$unwind": f"${prefix}"}) pipeline.append({"$limit": 1}) - results = tuple(mongo_collection.aggregate(pipeline, session=session)) + results = tuple( + mongo_collection.aggregate( + pipeline, session=session, maxTimeMs=settings.MONGO_SEARCH_MAX_TIME_MS + ) + ) if len(results) == 0: abort(404) data = results[0] diff --git a/odata_server/settings.py b/odata_server/settings.py new file mode 100644 index 0000000..366a978 --- /dev/null +++ b/odata_server/settings.py @@ -0,0 +1,3 @@ +import os + +MONGO_SEARCH_MAX_TIME_MS = int(os.getenv("MONGO_SEARCH_MAX_TIME_MS", "30000")) diff --git a/odata_server/utils/flask.py b/odata_server/utils/flask.py index d0d421b..0a2ea16 100644 --- a/odata_server/utils/flask.py +++ b/odata_server/utils/flask.py @@ -7,10 +7,9 @@ def add_odata_annotations(data, entity_set): key_predicate = format_key_predicate(extract_id_value(entity_set.entity_type, data)) - data["@odata.id"] = "{}({})".format( - url_for("odata.{}".format(entity_set.Name), _external=True), key_predicate - ) - data["@odata.etag"] = 'W/"{}"'.format(data["uuid"]) + base_url = url_for("odata.{}".format(entity_set.Name), _external=True) + data["@odata.id"] = f"{base_url}({key_predicate})" + data["@odata.etag"] = f'W/"{data["uuid"]}"' del data["uuid"] return data