Skip to content

Commit

Permalink
Create filter for multiple statuses for /data-requests GET
Browse files Browse the repository at this point in the history
  • Loading branch information
maxachis committed Dec 14, 2024
1 parent 84a6df7 commit a02be45
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 33 deletions.
2 changes: 1 addition & 1 deletion middleware/primary_resource_logic/data_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def get_data_requests_wrapper(
}
if dto.request_statuses is not None:
db_client_additional_args["where_mappings"] = {
"request_statuses": [rs.value for rs in dto.request_statuses]
"request_status": [rs.value for rs in dto.request_statuses]
}
return get_many(
middleware_parameters=MiddlewareParameters(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,46 @@ def add_description_info_from_enum(
return description


class DescriptionBuilder:
"""

Check warning on line 94 in middleware/schema_and_dto_logic/dynamic_logic/dynamic_schema_documentation_construction.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/schema_and_dto_logic/dynamic_logic/dynamic_schema_documentation_construction.py#L94 <205>

1 blank line required between summary line and description
Raw output
./middleware/schema_and_dto_logic/dynamic_logic/dynamic_schema_documentation_construction.py:94:1: D205 1 blank line required between summary line and description

Check warning on line 94 in middleware/schema_and_dto_logic/dynamic_logic/dynamic_schema_documentation_construction.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/schema_and_dto_logic/dynamic_logic/dynamic_schema_documentation_construction.py#L94 <400>

First line should end with a period
Raw output
./middleware/schema_and_dto_logic/dynamic_logic/dynamic_schema_documentation_construction.py:94:1: D400 First line should end with a period
Appends information to description based on data
in field
"""

def __init__(self, field: marshmallow_fields, description: str):

Check warning on line 99 in middleware/schema_and_dto_logic/dynamic_logic/dynamic_schema_documentation_construction.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/schema_and_dto_logic/dynamic_logic/dynamic_schema_documentation_construction.py#L99 <107>

Missing docstring in __init__
Raw output
./middleware/schema_and_dto_logic/dynamic_logic/dynamic_schema_documentation_construction.py:99:1: D107 Missing docstring in __init__
self.field = field
self.description = description
self.metadata = field.metadata

def __str__(self):

Check warning on line 104 in middleware/schema_and_dto_logic/dynamic_logic/dynamic_schema_documentation_construction.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/schema_and_dto_logic/dynamic_logic/dynamic_schema_documentation_construction.py#L104 <105>

Missing docstring in magic method
Raw output
./middleware/schema_and_dto_logic/dynamic_logic/dynamic_schema_documentation_construction.py:104:1: D105 Missing docstring in magic method
return self.description

def build_description(self):

Check warning on line 107 in middleware/schema_and_dto_logic/dynamic_logic/dynamic_schema_documentation_construction.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/schema_and_dto_logic/dynamic_logic/dynamic_schema_documentation_construction.py#L107 <102>

Missing docstring in public method
Raw output
./middleware/schema_and_dto_logic/dynamic_logic/dynamic_schema_documentation_construction.py:107:1: D102 Missing docstring in public method
self.enum_case()
self.validator_case()
self.list_query_case()

def enum_case(self):

Check warning on line 112 in middleware/schema_and_dto_logic/dynamic_logic/dynamic_schema_documentation_construction.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/schema_and_dto_logic/dynamic_logic/dynamic_schema_documentation_construction.py#L112 <102>

Missing docstring in public method
Raw output
./middleware/schema_and_dto_logic/dynamic_logic/dynamic_schema_documentation_construction.py:112:1: D102 Missing docstring in public method
if isinstance(self.field, marshmallow_fields.Enum):
self.description = add_description_info_from_enum(
field_value=self.field, description=self.description
)

def validator_case(self):

Check warning on line 118 in middleware/schema_and_dto_logic/dynamic_logic/dynamic_schema_documentation_construction.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/schema_and_dto_logic/dynamic_logic/dynamic_schema_documentation_construction.py#L118 <102>

Missing docstring in public method
Raw output
./middleware/schema_and_dto_logic/dynamic_logic/dynamic_schema_documentation_construction.py:118:1: D102 Missing docstring in public method
for validator in self.field.validators:
if isinstance(validator, OneOf):
self.description = add_description_info_from_validators(
field=self.field, description=self.description
)

def list_query_case(self):

Check warning on line 125 in middleware/schema_and_dto_logic/dynamic_logic/dynamic_schema_documentation_construction.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/schema_and_dto_logic/dynamic_logic/dynamic_schema_documentation_construction.py#L125 <102>

Missing docstring in public method
Raw output
./middleware/schema_and_dto_logic/dynamic_logic/dynamic_schema_documentation_construction.py:125:1: D102 Missing docstring in public method
if not isinstance(self.field, marshmallow_fields.List):
return
if not self.metadata.get("source") == SourceMappingEnum.QUERY_ARGS:
return
self.description += " \n(Comma-delimited list)"


class FieldInfo:

def __init__(
Expand Down Expand Up @@ -125,9 +165,9 @@ def _get_description(
description = _get_required_argument(
"description", metadata, schema_class, field_name
)
if isinstance(field_value, marshmallow_fields.Enum):
description = add_description_info_from_enum(field_value, description)
return add_description_info_from_validators(field_value, description)
desc_builder = DescriptionBuilder(field=field_value, description=description)
desc_builder.build_description()
return str(desc_builder)

def _map_field_type(self, field_type: type[MarshmallowFields]) -> Type[RestxFields]:
try:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from marshmallow import fields, Schema, post_load
from marshmallow import fields, Schema, post_load, pre_load

Check warning on line 1 in middleware/schema_and_dto_logic/primary_resource_schemas/data_requests_advanced_schemas.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/schema_and_dto_logic/primary_resource_schemas/data_requests_advanced_schemas.py#L1 <100>

Missing docstring in public module
Raw output
./middleware/schema_and_dto_logic/primary_resource_schemas/data_requests_advanced_schemas.py:1:1: D100 Missing docstring in public module

from database_client.enums import RequestStatus
from middleware.schema_and_dto_logic.primary_resource_schemas.data_requests_base_schema import (
Expand Down Expand Up @@ -139,15 +139,24 @@ def location_infos_convert_empty_list_to_none(self, in_data, **kwargs):

class GetManyDataRequestsRequestsSchema(GetManyRequestsBaseSchema):
request_statuses = fields.List(
fields.Enum(
enum=RequestStatus,
by_value=fields.Str,
allow_none=True,
metadata=get_query_metadata("The status of the requests to return."),
),
fields.Enum(
enum=RequestStatus,
by_value=fields.Str,
allow_none=True,
metadata=get_query_metadata("The status of the requests to return."),
),
metadata=get_query_metadata("The status of the requests to return."),
)

@pre_load
def listify_request_statuses(self, in_data, **kwargs):

Check warning on line 152 in middleware/schema_and_dto_logic/primary_resource_schemas/data_requests_advanced_schemas.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/schema_and_dto_logic/primary_resource_schemas/data_requests_advanced_schemas.py#L152 <102>

Missing docstring in public method
Raw output
./middleware/schema_and_dto_logic/primary_resource_schemas/data_requests_advanced_schemas.py:152:1: D102 Missing docstring in public method

Check warning on line 152 in middleware/schema_and_dto_logic/primary_resource_schemas/data_requests_advanced_schemas.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/schema_and_dto_logic/primary_resource_schemas/data_requests_advanced_schemas.py#L152 <100>

Unused argument 'kwargs'
Raw output
./middleware/schema_and_dto_logic/primary_resource_schemas/data_requests_advanced_schemas.py:152:51: U100 Unused argument 'kwargs'
request_statuses = in_data.get("request_statuses", None)
if request_statuses is None:
return in_data
in_data["request_statuses"] = request_statuses.split(",")

return in_data


GetManyDataRequestsResponseSchema = create_get_many_schema(
data_list_schema=DataRequestsGetSchemaBase,
Expand Down
16 changes: 16 additions & 0 deletions middleware/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,19 @@ def get_temp_directory() -> str:
# Go to root directory

return os.path.join(os.getcwd(), "temp")


def stringify_list_of_ints(l: list[int]):

Check warning on line 147 in middleware/util.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/util.py#L147 <103>

Missing docstring in public function
Raw output
./middleware/util.py:147:1: D103 Missing docstring in public function

Check failure on line 147 in middleware/util.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/util.py#L147 <741>

ambiguous variable name 'l'
Raw output
./middleware/util.py:147:28: E741 ambiguous variable name 'l'
for i in range(len(l)):
l[i] = str(l[i])
return l


def stringify_lists(d: dict):

Check warning on line 153 in middleware/util.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/util.py#L153 <103>

Missing docstring in public function
Raw output
./middleware/util.py:153:1: D103 Missing docstring in public function
for k, v in d.items():
if isinstance(v, dict):
stringify_lists(v)
if isinstance(v, list):
v = stringify_list_of_ints(v)
d[k] = ",".join(v)
return d
5 changes: 3 additions & 2 deletions tests/helper_scripts/helper_classes/RequestValidator.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,6 @@ def update_data_request(
def get_data_requests(
self,
headers: dict,
request_status: Optional[RequestStatus] = None,
sort_by: Optional[str] = None,
sort_order: Optional[SortOrder] = None,
request_statuses: Optional[list[RequestStatus]] = None,
Expand All @@ -366,7 +365,9 @@ def get_data_requests(
"sort_by": sort_by,
"sort_order": sort_order.value if sort_order is not None else None,
"request_statuses": (
[rs.value for rs in request_statuses] if request_statuses is not None else None
[rs.value for rs in request_statuses]
if request_statuses is not None
else None
),
},
)
Expand Down
4 changes: 4 additions & 0 deletions tests/helper_scripts/helper_functions_simple.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from datetime import datetime, timezone, timedelta
from urllib.parse import urlparse, parse_qs, urlencode, urlunparse

from middleware.util import stringify_lists

"""
`Simple` in this case refers to helper functions
which rely on no external dependencies
Expand All @@ -22,6 +24,8 @@ def add_query_params(url, params: dict):
:return:
"""

stringify_lists(d=params)

# Parse the original URL into components
url_parts = list(urlparse(url))

Expand Down
17 changes: 1 addition & 16 deletions tests/integration/test_bulk.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
AgenciesPutBatchRequestSchema,
DataSourcesPutBatchRequestSchema,
)
from middleware.util import stringify_lists
from tests.helper_scripts.common_test_data import get_test_name
from tests.helper_scripts.common_asserts import assert_contains_key_value_pairs
from tests.helper_scripts.helper_classes.RequestValidator import RequestValidator
Expand All @@ -34,22 +35,6 @@
)


def stringify_list_of_ints(l: list[int]):
for i in range(len(l)):
l[i] = str(l[i])
return l


def stringify_lists(d: dict):
for k, v in d.items():
if isinstance(v, dict):
stringify_lists(v)
if isinstance(v, list):
v = stringify_list_of_ints(v)
d[k] = ",".join(v)
return d


@dataclass
class BatchTestRunner:
tdc: TestDataCreatorFlask
Expand Down
6 changes: 2 additions & 4 deletions tests/integration/test_data_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,13 @@ def test_data_requests_get(
headers=tus_creator.jwt_authorization_header,
)[DATA_KEY]



# Assert admin columns are greater than user columns
assert len(admin_data[0]) > len(data[0])

# Run get again, this time filtering the request status to be active
data = tdc.request_validator.get_data_requests(
headers=tus_creator.jwt_authorization_header,
request_status=RequestStatus.ACTIVE,
request_statuses=[RequestStatus.ACTIVE],
)[DATA_KEY]

# The more recent data request should be returned, but the old one should be filtered out
Expand All @@ -121,7 +119,7 @@ def test_data_requests_get(
def get_sorted_data_requests(sort_order: SortOrder):
return tdc.request_validator.get_data_requests(
headers=tus_creator.jwt_authorization_header,
request_status=RequestStatus.INTAKE,
request_statuses=[RequestStatus.INTAKE],
sort_by="id",
sort_order=sort_order,
)
Expand Down

0 comments on commit a02be45

Please sign in to comment.