Skip to content

Commit

Permalink
Merge pull request #13 from Police-Data-Accessibility-Project/mc_issu…
Browse files Browse the repository at this point in the history
…e_285_refactor_search_tokens

Mc issue 285 refactor search tokens
  • Loading branch information
maxachis authored Jun 2, 2024
2 parents 3cfa69b + 01454af commit adfe6b4
Show file tree
Hide file tree
Showing 13 changed files with 310 additions and 176 deletions.
11 changes: 11 additions & 0 deletions middleware/access_token_logic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import datetime
import uuid


def insert_access_token(cursor):
token = uuid.uuid4().hex
expiration = datetime.datetime.now() + datetime.timedelta(minutes=5)
cursor.execute(
f"insert into access_tokens (token, expiration_date) values (%s, %s)",
(token, expiration),
)
36 changes: 36 additions & 0 deletions middleware/data_source_queries.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from typing import List, Dict, Any, Optional, Tuple, Union

from flask import make_response
from sqlalchemy.dialects.postgresql import psycopg2

from utilities.common import convert_dates_to_strings, format_arrays
from psycopg2.extensions import connection as PgConnection

Expand Down Expand Up @@ -88,6 +92,38 @@
]


def get_approved_data_sources_wrapper(conn: PgConnection):
data_source_matches = get_approved_data_sources(conn)

return make_response(
{
"count": len(data_source_matches),
"data": data_source_matches,
},
200,
)


def data_source_by_id_wrapper(arg, conn: PgConnection):
data_source_details = data_source_by_id_query(arg, conn=conn)
if data_source_details:
return make_response(data_source_details, 200)

else:
return make_response({"message": "Data source not found."}, 404)


def get_data_sources_for_map_wrapper(conn: PgConnection):
data_source_details = get_data_sources_for_map(conn)
return make_response(
{
"count": len(data_source_details),
"data": data_source_details,
},
200,
)


def data_source_by_id_results(
conn: PgConnection, data_source_id: str
) -> Union[tuple[Any, ...], None]:
Expand Down
27 changes: 27 additions & 0 deletions middleware/quick_search_query.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import spacy
import json
import datetime

from flask import make_response
from sqlalchemy.dialects.postgresql import psycopg2

from middleware.webhook_logic import post_to_webhook
from utilities.common import convert_dates_to_strings, format_arrays
from typing import List, Dict, Any, Optional
from psycopg2.extensions import connection as PgConnection, cursor as PgCursor
Expand Down Expand Up @@ -159,3 +164,25 @@ def quick_search_query(
cursor.close()

return data_sources


def quick_search_query_wrapper(arg1, arg2, conn: PgConnection):
try:
data_sources = quick_search_query(arg1, arg2, conn=conn)

return make_response(data_sources, 200)

except Exception as e:
conn.rollback()
user_message = "There was an error during the search operation"
message = {
"content": user_message
+ ": "
+ str(e)
+ "\n"
+ f"Search term: {arg1}\n"
+ f"Location: {arg2}"
}
post_to_webhook(json.dumps(message))

return {"count": 0, "message": user_message}, 500
12 changes: 12 additions & 0 deletions middleware/token_management.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import datetime
import uuid


def insert_new_access_token(cursor):
token = uuid.uuid4().hex
expiration = datetime.datetime.now() + datetime.timedelta(minutes=5)
cursor.execute(
f"insert into access_tokens (token, expiration_date) values (%s, %s)",
(token, expiration),
)
return token
14 changes: 14 additions & 0 deletions middleware/webhook_logic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import json
import os

import requests


def post_to_webhook(data: str):
webhook_url = os.getenv("WEBHOOK_URL")

requests.post(
webhook_url,
data=data,
headers={"Content-Type": "application/json"},
)
36 changes: 6 additions & 30 deletions resources/DataSources.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from flask import request
from middleware.security import api_required
from middleware.data_source_queries import (
data_source_by_id_query,
get_data_sources_for_map,
get_approved_data_sources,
needs_identification_data_sources,
get_approved_data_sources_wrapper,
data_source_by_id_wrapper,
get_data_sources_for_map_wrapper,
)
from datetime import datetime

Expand Down Expand Up @@ -32,17 +32,7 @@ def get(self, data_source_id: str) -> Tuple[Dict[str, Any], int]:
Returns:
- Tuple containing the response message with data source details if found, and the HTTP status code.
"""
data_source_details = data_source_by_id_query(
conn=self.psycopg2_connection, data_source_id=data_source_id
)
if data_source_details:
return {
"message": "Successfully found data source",
"data": data_source_details,
}

else:
return {"message": "Data source not found."}, 200
return data_source_by_id_wrapper(data_source_id, self.psycopg2_connection)

@handle_exceptions
@api_required
Expand Down Expand Up @@ -105,14 +95,7 @@ def get(self) -> Dict[str, Any]:
Returns:
- A dictionary containing the count of data sources and their details.
"""
data_source_matches = get_approved_data_sources(self.psycopg2_connection)

data_sources = {
"count": len(data_source_matches),
"data": data_source_matches,
}

return data_sources
return get_approved_data_sources_wrapper(self.psycopg2_connection)

@handle_exceptions
@api_required
Expand Down Expand Up @@ -190,11 +173,4 @@ def get(self) -> Dict[str, Any]:
Returns:
- A dictionary containing the count of data sources and their details.
"""
data_source_matches = get_data_sources_for_map(self.psycopg2_connection)

data_sources = {
"count": len(data_source_matches),
"data": data_source_matches,
}

return data_sources
return get_data_sources_for_map_wrapper(self.psycopg2_connection)
3 changes: 2 additions & 1 deletion resources/PsycopgResource.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import functools
from typing import Callable, Any, Union, Tuple, Dict

from flask import make_response
from flask_restful import Resource


Expand Down Expand Up @@ -36,7 +37,7 @@ def wrapper(
except Exception as e:
self.psycopg2_connection.rollback()
print(str(e))
return {"message": str(e)}, 500
return make_response({"message": str(e)}, 500)

return wrapper

Expand Down
48 changes: 3 additions & 45 deletions resources/QuickSearch.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
from middleware.security import api_required
from middleware.quick_search_query import quick_search_query
import requests
import json
import os
from middleware.initialize_psycopg2_connection import (
initialize_psycopg2_connection,
DatabaseInitializationError,
)
from flask import request
from middleware.quick_search_query import quick_search_query_wrapper

from typing import Dict, Any

from resources.PsycopgResource import PsycopgResource
Expand Down Expand Up @@ -35,39 +28,4 @@ def get(self, search: str, location: str) -> Dict[str, Any]:
Returns:
- A dictionary containing a message about the search results and the data found, if any.
"""
try:
data_sources = quick_search_query(
search, location, self.psycopg2_connection
)

if data_sources["count"] == 0:
return {
"count": 0,
"message": "No results found. Please considering requesting a new data source.",
}, 404

return {
"message": "Results for search successfully retrieved",
"data": data_sources,
}

except DatabaseInitializationError as e:
self.psycopg2_connection.rollback()
print(str(e))
webhook_url = os.getenv("WEBHOOK_URL")
user_message = "There was an error during the search operation"
message = {
"content": user_message
+ ": "
+ str(e)
+ "\n"
+ f"Search term: {search}\n"
+ f"Location: {location}"
}
requests.post(
webhook_url,
data=json.dumps(message),
headers={"Content-Type": "application/json"},
)

return {"count": 0, "message": user_message}, 500
return quick_search_query_wrapper(search, location, self.psycopg2_connection)
Loading

0 comments on commit adfe6b4

Please sign in to comment.