Skip to content

Commit

Permalink
Merge pull request #213 from Police-Data-Accessibility-Project/mc_512…
Browse files Browse the repository at this point in the history
…_csv_export

Mc 512 csv export
  • Loading branch information
maxachis authored Dec 9, 2024
2 parents 9db28af + 9f91477 commit 13ca852
Show file tree
Hide file tree
Showing 13 changed files with 276 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@

@dataclass
class GetRelatedResourcesParameters:
db_client: DatabaseClient
dto: GetByIDBaseDTO
db_client_method: callable
primary_relation: Relations
related_relation: Relations
linking_column: str
metadata_count_name: str
db_client: DatabaseClient = DatabaseClient()
resource_name: str = "resource"


Expand Down
5 changes: 5 additions & 0 deletions middleware/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class AccessTypeEnum(Enum):
NO_AUTH = auto()


class OutputFormatEnum(Enum):
JSON = "json"
CSV = "csv"


class Relations(Enum):
"""
A list of valid relations for the database
Expand Down
7 changes: 3 additions & 4 deletions middleware/location_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ def get_location_id(

# In the case of a nonexistent locality, this can be added,
# provided the rest of the location is valid
county_id = _get_county_id(db_client, location_info_dict)

county_id = _get_county_id(location_info_dict)
# If this exists, locality does not yet exist in database and should be added. Add and return location id
db_client.create_locality(
column_value_mappings={
Expand All @@ -47,13 +46,13 @@ def _raise_if_not_locality(location_info, location_info_dict):
raise InvalidLocationError(f"{location_info_dict} is not a valid location")


def _get_county_id(db_client, location_info_dict) -> int:
def _get_county_id(location_info_dict: dict) -> int:
county_dict = {
"county_fips": location_info_dict["county_fips"],
"state_iso": location_info_dict["state_iso"],
"type": LocationType.COUNTY,
}
results = db_client._select_from_relation(
results = DatabaseClient()._select_from_relation(
relation_name=Relations.LOCATIONS_EXPANDED.value,
columns=["county_id"],
where_mappings=WhereMapping.from_dict(county_dict),
Expand Down
10 changes: 1 addition & 9 deletions middleware/primary_resource_logic/agencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ def get_agencies(
"""
return get_many(
middleware_parameters=MiddlewareParameters(
db_client=db_client,
access_info=access_info,
entry_name="agencies",
relation=Relations.AGENCIES_EXPANDED.value,
Expand All @@ -69,7 +68,6 @@ def get_agency_by_id(
) -> Response:
return get_by_id(
middleware_parameters=MiddlewareParameters(
db_client=db_client,
access_info=access_info,
entry_name="agency",
relation=Relations.AGENCIES_EXPANDED.value,
Expand Down Expand Up @@ -151,11 +149,7 @@ def create_agency(
)

return post_entry(
middleware_parameters=MiddlewareParameters(
entry_name="agency",
relation=Relations.AGENCIES.value,
db_client_method=DatabaseClient.create_agency,
),
middleware_parameters=AGENCY_POST_MIDDLEWARE_PARAMETERS,
entry=entry_data,
pre_insertion_function_with_parameters=pre_insertion_function,
check_for_permission=False,
Expand Down Expand Up @@ -209,7 +203,6 @@ def update_agency(

return put_entry(
middleware_parameters=MiddlewareParameters(
db_client=db_client,
access_info=access_info,
entry_name="agency",
relation=Relations.AGENCIES.value,
Expand All @@ -226,7 +219,6 @@ def delete_agency(
) -> Response:
return delete_entry(
middleware_parameters=MiddlewareParameters(
db_client=db_client,
access_info=access_info,
entry_name="agency",
relation=Relations.AGENCIES.value,
Expand Down
14 changes: 6 additions & 8 deletions middleware/primary_resource_logic/batch_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from io import BytesIO

from marshmallow import Schema, ValidationError
from werkzeug.datastructures import FileStorage

from database_client.database_client import DatabaseClient
from middleware.dynamic_request_logic.supporting_classes import (
Expand All @@ -25,14 +26,14 @@
from middleware.schema_and_dto_logic.dynamic_logic.dynamic_schema_request_content_population import (
setup_dto_class,
)
from middleware.schema_and_dto_logic.primary_resource_dtos.agencies_dtos import (
AgenciesPostDTO,
)

from middleware.schema_and_dto_logic.primary_resource_dtos.batch_dtos import (
BatchRequestDTO,
)
from csv import DictReader

from middleware.util import bytes_to_text_iter, read_from_csv


def replace_empty_strings_with_none(row: dict):
for key, value in row.items():
Expand All @@ -48,12 +49,9 @@ def _get_raw_rows_from_csv(
return raw_rows


def _get_raw_rows(file: BytesIO):
def _get_raw_rows(file: FileStorage):
try:
text_file = (line.decode("utf-8") for line in file)
reader = DictReader(text_file)
rows = list(reader)
return rows
return read_from_csv(file)
except Exception as e:
FlaskResponseManager.abort(
code=HTTPStatus.BAD_REQUEST, message=f"Error reading csv file: {e}"
Expand Down
9 changes: 0 additions & 9 deletions middleware/primary_resource_logic/data_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ def get_data_requests_wrapper(
}
return get_many(
middleware_parameters=MiddlewareParameters(
db_client=db_client,
access_info=access_info,
entry_name="data requests",
relation=Relations.DATA_REQUESTS_EXPANDED.value,
Expand Down Expand Up @@ -251,7 +250,6 @@ def delete_data_request_wrapper(
"""
return delete_entry(
middleware_parameters=MiddlewareParameters(
db_client=db_client,
access_info=access_info,
entry_name="Data request",
relation=RELATION,
Expand Down Expand Up @@ -299,7 +297,6 @@ def update_data_request_wrapper(
entry_dict = created_filtered_entry_dict(dto)
return put_entry(
middleware_parameters=MiddlewareParameters(
db_client=db_client,
access_info=access_info,
entry_name="Data request",
relation=RELATION,
Expand Down Expand Up @@ -366,7 +363,6 @@ def get_data_request_related_sources(db_client: DatabaseClient, dto: GetByIDBase

return get_related_resource(
get_related_resources_parameters=GetRelatedResourcesParameters(
db_client=db_client,
dto=dto,
db_client_method=DatabaseClient.get_data_requests,
primary_relation=Relations.DATA_REQUESTS,
Expand All @@ -383,7 +379,6 @@ def get_data_request_related_locations(
) -> Response:
return get_related_resource(
get_related_resources_parameters=GetRelatedResourcesParameters(
db_client=db_client,
dto=dto,
db_client_method=DatabaseClient.get_data_requests,
primary_relation=Relations.DATA_REQUESTS,
Expand Down Expand Up @@ -435,7 +430,6 @@ def create_data_request_related_source(
):
post_logic = CreateDataRequestRelatedSourceLogic(
middleware_parameters=MiddlewareParameters(
db_client=db_client,
access_info=access_info,
entry_name="Request-Source association",
relation=RELATED_SOURCES_RELATION,
Expand All @@ -458,7 +452,6 @@ def delete_data_request_related_source(
):
return delete_entry(
middleware_parameters=MiddlewareParameters(
db_client=db_client,
access_info=access_info,
entry_name="Request-Source association",
relation=RELATED_SOURCES_RELATION,
Expand All @@ -483,7 +476,6 @@ def create_data_request_related_location(
):
post_logic = CreateDataRequestRelatedLocationLogic(
middleware_parameters=MiddlewareParameters(
db_client=db_client,
access_info=access_info,
entry_name="Request-Location association",
relation=Relations.LINK_LOCATIONS_DATA_REQUESTS.value,
Expand All @@ -508,7 +500,6 @@ def delete_data_request_related_location(
):
return delete_entry(
middleware_parameters=MiddlewareParameters(
db_client=db_client,
access_info=access_info,
entry_name="Request-Location association",
relation=Relations.LINK_LOCATIONS_DATA_REQUESTS.value,
Expand Down
5 changes: 0 additions & 5 deletions middleware/primary_resource_logic/data_sources_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ def data_source_by_id_wrapper(
access_info=access_info,
relation=Relations.DATA_SOURCES_EXPANDED.value,
db_client_method=DatabaseClient.get_data_sources,
db_client=db_client,
entry_name="data source",
subquery_parameters=SUBQUERY_PARAMS,
),
Expand Down Expand Up @@ -132,7 +131,6 @@ def delete_data_source_wrapper(
access_info=access_info,
relation=RELATION,
db_client_method=DatabaseClient.delete_data_source,
db_client=db_client,
entry_name="data source",
),
id_info=IDInfo(
Expand Down Expand Up @@ -160,7 +158,6 @@ def update_data_source_wrapper(
optionally_add_last_approval_editor(entry_data, access_info)
return put_entry(
middleware_parameters=MiddlewareParameters(
db_client=db_client,
entry_name="Data source",
relation=RELATION,
db_client_method=DatabaseClient.update_data_source,
Expand Down Expand Up @@ -305,7 +302,6 @@ def create_data_source_related_agency(
) -> Response:
post_logic = CreateDataSourceRelatedAgenciesLogic(
middleware_parameters=MiddlewareParameters(
db_client=db_client,
access_info=access_info,
entry_name="Data source-agency association",
relation=RELATION,
Expand All @@ -324,7 +320,6 @@ def delete_data_source_related_agency(
) -> Response:
return delete_entry(
middleware_parameters=MiddlewareParameters(
db_client=db_client,
access_info=access_info,
entry_name="Data source-agency association",
relation=Relations.LINK_AGENCIES_DATA_SOURCES.value,
Expand Down
Loading

0 comments on commit 13ca852

Please sign in to comment.