Skip to content
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

Mc 477 resource mass imports #212

Merged
merged 9 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from flask_cors import CORS

from middleware.util import get_env_variable
from resources.Batch import namespace_batch
from resources.Callback import namespace_auth
from resources.DataRequests import namespace_data_requests
from resources.GithubDataRequests import namespace_github
Expand Down Expand Up @@ -64,6 +65,7 @@
namespace_notifications,
namespace_map,
namespace_signup,
namespace_batch,
]

MY_PREFIX = "/api"
Expand Down
14 changes: 8 additions & 6 deletions middleware/access_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,15 @@ def get_identity():

@staticmethod
def get_access_info(token: str):
simple_jwt = SimpleJWT.decode(token, purpose=JWTPurpose.STANDARD_ACCESS_TOKEN)
identity = JWTService.get_identity()
if identity:
return get_jwt_access_info_with_permissions(
user_email=identity["user_email"], user_id=identity["id"]
try:
simple_jwt = SimpleJWT.decode(
token, purpose=JWTPurpose.STANDARD_ACCESS_TOKEN
)
return None
except Exception:
return None
return get_jwt_access_info_with_permissions(
user_email=simple_jwt.sub["user_email"], user_id=simple_jwt.sub["id"]
)


def get_token_from_request_header(scheme: AuthScheme):
Expand Down
34 changes: 32 additions & 2 deletions middleware/dynamic_request_logic/post_logic.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from http import HTTPStatus
from typing import Optional, Callable, Type
from typing import Optional, Callable, Type, Any

Check warning on line 2 in middleware/dynamic_request_logic/post_logic.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/dynamic_request_logic/post_logic.py#L2 <401>

'typing.Callable' imported but unused
Raw output
./middleware/dynamic_request_logic/post_logic.py:2:1: F401 'typing.Callable' imported but unused

import psycopg.errors
import sqlalchemy
Expand All @@ -13,11 +13,25 @@
from middleware.dynamic_request_logic.supporting_classes import (
MiddlewareParameters,
PutPostBase,
PostPutHandler,
PutPostRequestInfo,
)
from middleware.flask_response_manager import FlaskResponseManager
from middleware.util_dynamic import execute_if_not_none


class PostHandler(PostPutHandler):

Check warning on line 23 in middleware/dynamic_request_logic/post_logic.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/dynamic_request_logic/post_logic.py#L23 <101>

Missing docstring in public class
Raw output
./middleware/dynamic_request_logic/post_logic.py:23:1: D101 Missing docstring in public class

def call_database_client_method(self, request: PutPostRequestInfo):
"""

Check warning on line 26 in middleware/dynamic_request_logic/post_logic.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/dynamic_request_logic/post_logic.py#L26 <205>

1 blank line required between summary line and description
Raw output
./middleware/dynamic_request_logic/post_logic.py:26:1: D205 1 blank line required between summary line and description

Check warning on line 26 in middleware/dynamic_request_logic/post_logic.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/dynamic_request_logic/post_logic.py#L26 <400>

First line should end with a period
Raw output
./middleware/dynamic_request_logic/post_logic.py:26:1: D400 First line should end with a period

Check warning on line 26 in middleware/dynamic_request_logic/post_logic.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/dynamic_request_logic/post_logic.py#L26 <401>

First line should be in imperative mood
Raw output
./middleware/dynamic_request_logic/post_logic.py:26:1: D401 First line should be in imperative mood
Runs the database client method
and sets the request entry id in-place with the result
"""
request.entry_id = self.mp.db_client_method(
self.mp.db_client, column_value_mappings=request.entry
)


class PostLogic(PutPostBase):

def __init__(
Expand Down Expand Up @@ -54,13 +68,29 @@
)


def post_entry_with_handler(

Check warning on line 71 in middleware/dynamic_request_logic/post_logic.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/dynamic_request_logic/post_logic.py#L71 <103>

Missing docstring in public function
Raw output
./middleware/dynamic_request_logic/post_logic.py:71:1: D103 Missing docstring in public function
handler: PostHandler,
dto: Any,
):
request = PutPostRequestInfo(entry=dict(dto), dto=dto)
handler.mass_execute([request])
if request.error_message is not None:
FlaskResponseManager.abort(
code=HTTPStatus.BAD_REQUEST, message=request.error_message
)
return created_id_response(
new_id=str(request.entry_id), message=f"{handler.mp.entry_name} created."
)


def post_entry(
middleware_parameters: MiddlewareParameters,
entry: dict,
pre_insertion_function_with_parameters: Optional[DeferredFunction] = None,
relation_role_parameters: RelationRoleParameters = RelationRoleParameters(),
post_logic_class: Optional[Type[PostLogic]] = PostLogic,
check_for_permission: bool = True,
make_response: bool = True,
) -> Response:

post_logic = post_logic_class(
Expand All @@ -70,7 +100,7 @@
relation_role_parameters=relation_role_parameters,
check_for_permission=check_for_permission,
)
return post_logic.execute()
return post_logic.execute(make_response=make_response)


def try_to_add_entry(middleware_parameters: MiddlewareParameters, entry: dict) -> str:
Expand Down
12 changes: 12 additions & 0 deletions middleware/dynamic_request_logic/put_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,21 @@
from middleware.dynamic_request_logic.supporting_classes import (
MiddlewareParameters,
PutPostBase,
PostPutHandler,
PutPostRequestInfo,
)


class PutHandler(PostPutHandler):

Check warning on line 19 in middleware/dynamic_request_logic/put_logic.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/dynamic_request_logic/put_logic.py#L19 <101>

Missing docstring in public class
Raw output
./middleware/dynamic_request_logic/put_logic.py:19:1: D101 Missing docstring in public class

def call_database_client_method(self, request: PutPostRequestInfo):

Check warning on line 21 in middleware/dynamic_request_logic/put_logic.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/dynamic_request_logic/put_logic.py#L21 <102>

Missing docstring in public method
Raw output
./middleware/dynamic_request_logic/put_logic.py:21:1: D102 Missing docstring in public method
self.mp.db_client_method(
self.mp.db_client,
column_edit_mappings=request.entry,
entry_id=request.entry_id,
)


class PutLogic(PutPostBase):

def __init__(
Expand Down
63 changes: 59 additions & 4 deletions middleware/dynamic_request_logic/supporting_classes.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from abc import ABC, abstractmethod
from dataclasses import dataclass, field
from typing import Optional, Union
from enum import Enum, auto

Check warning on line 3 in middleware/dynamic_request_logic/supporting_classes.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/dynamic_request_logic/supporting_classes.py#L3 <401>

'enum.Enum' imported but unused
Raw output
./middleware/dynamic_request_logic/supporting_classes.py:3:1: F401 'enum.Enum' imported but unused

Check warning on line 3 in middleware/dynamic_request_logic/supporting_classes.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/dynamic_request_logic/supporting_classes.py#L3 <401>

'enum.auto' imported but unused
Raw output
./middleware/dynamic_request_logic/supporting_classes.py:3:1: F401 'enum.auto' imported but unused
from typing import Optional, Union, Any

from flask import Response
from pydantic import BaseModel

from database_client.database_client import DatabaseClient
from database_client.enums import RelationRoleEnum
Expand All @@ -22,9 +24,9 @@
Contains parameters for the middleware functions
"""

access_info: AccessInfoPrimary
relation: str
db_client_method: callable
access_info: Optional[AccessInfoPrimary] = None
db_client: DatabaseClient = DatabaseClient()
# Additional arguments for the Database Client method beyond those provided in the given method
db_client_additional_args: dict = field(default_factory=dict)
Expand All @@ -46,6 +48,58 @@
self.where_mappings[id_column_name] = id_column_value


class PutPostRequestInfo(BaseModel):
"""

Check warning on line 52 in middleware/dynamic_request_logic/supporting_classes.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/dynamic_request_logic/supporting_classes.py#L52 <400>

First line should end with a period
Raw output
./middleware/dynamic_request_logic/supporting_classes.py:52:1: D400 First line should end with a period
A DTO for the post/put request

:param request_id: the id of the request
:param entry: the entry information for the request
:param entry_id: the id of the entry to be updated, OR the id of the entry created
:param error_message: the error message, if any
"""

request_id: int = 1
entry: dict
dto: Optional[Any] = None
entry_id: Optional[int] = None
error_message: Optional[str] = None


class PostPutHandler(ABC):

Check warning on line 68 in middleware/dynamic_request_logic/supporting_classes.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/dynamic_request_logic/supporting_classes.py#L68 <101>

Missing docstring in public class
Raw output
./middleware/dynamic_request_logic/supporting_classes.py:68:1: D101 Missing docstring in public class

def __init__(

Check warning on line 70 in middleware/dynamic_request_logic/supporting_classes.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/dynamic_request_logic/supporting_classes.py#L70 <107>

Missing docstring in __init__
Raw output
./middleware/dynamic_request_logic/supporting_classes.py:70:1: D107 Missing docstring in __init__
self,
middleware_parameters: MiddlewareParameters,
):
self.mp = middleware_parameters

def pre_execute(self, request: PutPostRequestInfo):

Check warning on line 76 in middleware/dynamic_request_logic/supporting_classes.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/dynamic_request_logic/supporting_classes.py#L76 <102>

Missing docstring in public method
Raw output
./middleware/dynamic_request_logic/supporting_classes.py:76:1: D102 Missing docstring in public method

Check warning on line 76 in middleware/dynamic_request_logic/supporting_classes.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/dynamic_request_logic/supporting_classes.py#L76 <100>

Unused argument 'request'
Raw output
./middleware/dynamic_request_logic/supporting_classes.py:76:27: U100 Unused argument 'request'
return

@abstractmethod
def call_database_client_method(self, request: PutPostRequestInfo):

Check warning on line 80 in middleware/dynamic_request_logic/supporting_classes.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/dynamic_request_logic/supporting_classes.py#L80 <102>

Missing docstring in public method
Raw output
./middleware/dynamic_request_logic/supporting_classes.py:80:1: D102 Missing docstring in public method

Check warning on line 80 in middleware/dynamic_request_logic/supporting_classes.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/dynamic_request_logic/supporting_classes.py#L80 <100>

Unused argument 'request'
Raw output
./middleware/dynamic_request_logic/supporting_classes.py:80:43: U100 Unused argument 'request'
raise NotImplementedError

def execute(

Check warning on line 83 in middleware/dynamic_request_logic/supporting_classes.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/dynamic_request_logic/supporting_classes.py#L83 <102>

Missing docstring in public method
Raw output
./middleware/dynamic_request_logic/supporting_classes.py:83:1: D102 Missing docstring in public method
self,
request: PutPostRequestInfo,
):
self.pre_execute(request=request)
result = self.call_database_client_method(request=request)
self.post_execute(request=request)
return result

def post_execute(self, request: PutPostRequestInfo):

Check warning on line 92 in middleware/dynamic_request_logic/supporting_classes.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/dynamic_request_logic/supporting_classes.py#L92 <102>

Missing docstring in public method
Raw output
./middleware/dynamic_request_logic/supporting_classes.py:92:1: D102 Missing docstring in public method

Check warning on line 92 in middleware/dynamic_request_logic/supporting_classes.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/dynamic_request_logic/supporting_classes.py#L92 <100>

Unused argument 'request'
Raw output
./middleware/dynamic_request_logic/supporting_classes.py:92:28: U100 Unused argument 'request'
return

def mass_execute(self, requests: list[PutPostRequestInfo]):

Check warning on line 95 in middleware/dynamic_request_logic/supporting_classes.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/dynamic_request_logic/supporting_classes.py#L95 <102>

Missing docstring in public method
Raw output
./middleware/dynamic_request_logic/supporting_classes.py:95:1: D102 Missing docstring in public method
for request in requests:
try:
self.execute(request=request)
except Exception as e:
request.error_message = str(e)


class PutPostBase(ABC):

def __init__(
Expand Down Expand Up @@ -83,7 +137,7 @@
def make_response(self) -> Response:
raise NotImplementedError

def execute(self) -> Response:
def execute(self, make_response: bool = True) -> Response:

Check warning on line 140 in middleware/dynamic_request_logic/supporting_classes.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/dynamic_request_logic/supporting_classes.py#L140 <102>

Missing docstring in public method
Raw output
./middleware/dynamic_request_logic/supporting_classes.py:140:1: D102 Missing docstring in public method
if self.check_for_permission:
relation_role = (
self.relation_role_parameters.get_relation_role_from_parameters(
Expand All @@ -94,7 +148,8 @@
self.pre_database_client_method_logic()
self.call_database_client_method()
self.post_database_client_method_logic()
return self.make_response()
if make_response:
return self.make_response()

def post_database_client_method_logic(self):
return
10 changes: 0 additions & 10 deletions middleware/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,6 @@ class DuplicateUserError(Exception):
pass


class TokenNotFoundError(Exception):
"""Raised when the token is not found in the database."""

pass


class AccessTokenNotFoundError(Exception):
pass


class InvalidAPIKeyException(Exception):
pass

Expand Down
67 changes: 60 additions & 7 deletions middleware/primary_resource_logic/agencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
from middleware.dynamic_request_logic.delete_logic import delete_entry
from middleware.dynamic_request_logic.get_by_id_logic import get_by_id
from middleware.dynamic_request_logic.get_many_logic import get_many
from middleware.dynamic_request_logic.post_logic import post_entry
from middleware.dynamic_request_logic.put_logic import put_entry
from middleware.dynamic_request_logic.post_logic import post_entry, PostHandler
from middleware.dynamic_request_logic.put_logic import put_entry, PutHandler
from middleware.dynamic_request_logic.supporting_classes import (
MiddlewareParameters,
IDInfo,
PutPostRequestInfo,
)
from middleware.location_logic import get_location_id
from middleware.schema_and_dto_logic.primary_resource_schemas.agencies_advanced_schemas import (
Expand All @@ -29,6 +30,7 @@
LocationInfoDTO,
)
from middleware.enums import Relations, JurisdictionType
from middleware.util import dict_enums_to_values

SUBQUERY_PARAMS = [SubqueryParameterManager.data_sources()]

Expand Down Expand Up @@ -93,11 +95,55 @@
entry_data["location_id"] = location_id


class AgencyPostRequestInfo(PutPostRequestInfo):

Check warning on line 98 in middleware/primary_resource_logic/agencies.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/primary_resource_logic/agencies.py#L98 <101>

Missing docstring in public class
Raw output
./middleware/primary_resource_logic/agencies.py:98:1: D101 Missing docstring in public class
dto: AgenciesPostDTO


class AgencyPostHandler(PostHandler):

Check warning on line 102 in middleware/primary_resource_logic/agencies.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/primary_resource_logic/agencies.py#L102 <101>

Missing docstring in public class
Raw output
./middleware/primary_resource_logic/agencies.py:102:1: D101 Missing docstring in public class

def __init__(self):

Check warning on line 104 in middleware/primary_resource_logic/agencies.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/primary_resource_logic/agencies.py#L104 <107>

Missing docstring in __init__
Raw output
./middleware/primary_resource_logic/agencies.py:104:1: D107 Missing docstring in __init__
super().__init__(middleware_parameters=AGENCY_POST_MIDDLEWARE_PARAMETERS)

def pre_execute(self, request: AgencyPostRequestInfo):

Check warning on line 107 in middleware/primary_resource_logic/agencies.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/primary_resource_logic/agencies.py#L107 <102>

Missing docstring in public method
Raw output
./middleware/primary_resource_logic/agencies.py:107:1: D102 Missing docstring in public method
validate_and_add_location_info(
db_client=DatabaseClient(),
entry_data=request.entry,
location_info=request.dto.location_info,
)


class AgencyPutHandler(PutHandler):

Check warning on line 115 in middleware/primary_resource_logic/agencies.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/primary_resource_logic/agencies.py#L115 <101>

Missing docstring in public class
Raw output
./middleware/primary_resource_logic/agencies.py:115:1: D101 Missing docstring in public class

def __init__(self):

Check warning on line 117 in middleware/primary_resource_logic/agencies.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/primary_resource_logic/agencies.py#L117 <107>

Missing docstring in __init__
Raw output
./middleware/primary_resource_logic/agencies.py:117:1: D107 Missing docstring in __init__
super().__init__(middleware_parameters=AGENCY_PUT_MIDDLEWARE_PARAMETERS)

def pre_execute(self, request: PutPostRequestInfo):

Check warning on line 120 in middleware/primary_resource_logic/agencies.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/primary_resource_logic/agencies.py#L120 <102>

Missing docstring in public method
Raw output
./middleware/primary_resource_logic/agencies.py:120:1: D102 Missing docstring in public method
# The below values are probably incorrect, but serve as a placeholder
entry_data = dict(request.dto.agency_info)
request.entry = dict_enums_to_values(entry_data)
location_info = request.dto.location_info
if location_info is not None:
validate_and_add_location_info(
db_client=DatabaseClient(),
entry_data=entry_data,
location_info=location_info,
)


AGENCY_POST_MIDDLEWARE_PARAMETERS = MiddlewareParameters(
entry_name="agency",
relation=Relations.AGENCIES.value,
db_client_method=DatabaseClient.create_agency,
)


def create_agency(
db_client: DatabaseClient, dto: AgenciesPostDTO, access_info: AccessInfoPrimary
db_client: DatabaseClient,
dto: AgenciesPostDTO,
make_response: bool = True,
) -> Response:
entry_data = dict(dto.agency_info)
deferred_function = optionally_get_location_info_deferred_function(
pre_insertion_function = optionally_get_location_info_deferred_function(
db_client=db_client,
jurisdiction_type=dto.agency_info.jurisdiction_type,
entry_data=entry_data,
Expand All @@ -106,14 +152,14 @@

return post_entry(
middleware_parameters=MiddlewareParameters(
db_client=db_client,
access_info=access_info,
entry_name="agency",
relation=Relations.AGENCIES.value,
db_client_method=DatabaseClient.create_agency,
),
entry=entry_data,
pre_insertion_function_with_parameters=deferred_function,
pre_insertion_function_with_parameters=pre_insertion_function,
check_for_permission=False,
make_response=make_response,
)


Expand All @@ -135,6 +181,13 @@
return deferred_function


AGENCY_PUT_MIDDLEWARE_PARAMETERS = MiddlewareParameters(
entry_name="agency",
relation=Relations.AGENCIES.value,
db_client_method=DatabaseClient.update_agency,
)


def update_agency(
db_client: DatabaseClient,
access_info: AccessInfoPrimary,
Expand Down
Loading
Loading