Skip to content

Commit

Permalink
Merge pull request #15 from Police-Data-Accessibility-Project/mc_repl…
Browse files Browse the repository at this point in the history
…ace_with_abort

Move from flask_restful to flask_restx. Replace error dictionaries with abort
  • Loading branch information
maxachis authored Jun 17, 2024
2 parents 36bff2c + 92c6f91 commit 2fae1ef
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from flask import Flask
from flask_restful import Api
from flask_restx import Api
from flask_cors import CORS
from resources.User import User
from resources.Login import Login
Expand Down
4 changes: 4 additions & 0 deletions middleware/security.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import functools
from flask import request, jsonify
from flask_restx import abort


from collections import namedtuple

from http import HTTPStatus
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ exceptiongroup==1.1.3
Flask==2.3.2
Flask-Cors==4.0.0
Flask-RESTful==0.3.10
flask-restx==1.3.0
Flask-SQLAlchemy~=3.1.1
gotrue==1.0.3
gunicorn==21.2.0
Expand Down
3 changes: 2 additions & 1 deletion resources/Login.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from flask_restx import abort
from werkzeug.security import check_password_hash
from flask import request
from middleware.login_queries import login_results, create_session_token
Expand Down Expand Up @@ -35,4 +36,4 @@ def post(self):
"data": token,
}

return {"message": "Invalid email or password"}, 401
abort(code=401, message="Invalid email or password")
8 changes: 4 additions & 4 deletions resources/PsycopgResource.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import functools
from typing import Callable, Any, Union, Tuple, Dict

from flask import make_response
from flask_restful import Resource
from flask_restx import abort, Resource


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

return wrapper


class PsycopgResource(Resource):
def __init__(self, **kwargs):
def __init__(self, *args, **kwargs):
"""
Initializes the resource with a database connection.
- kwargs (dict): Keyword arguments containing 'psycopg2_connection' for database connection.
"""
super().__init__(*args, **kwargs)
self.psycopg2_connection = kwargs["psycopg2_connection"]
4 changes: 4 additions & 0 deletions resources/RefreshSession.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from flask import request

from flask_restx import abort

from middleware.custom_exceptions import TokenNotFoundError
from middleware.login_queries import (
get_session_token_user_data,
Expand Down Expand Up @@ -40,3 +42,5 @@ def post(self) -> Dict[str, Any]:
"message": "Successfully refreshed session token",
"data": token,
}

abort(code=403, message="Invalid session token")
5 changes: 3 additions & 2 deletions resources/ResetPassword.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from flask_restx import abort
from werkzeug.security import generate_password_hash
from flask import request
from middleware.reset_token_queries import (
Expand Down Expand Up @@ -32,13 +33,13 @@ def post(self) -> Dict[str, Any]:
token_data = check_reset_token(cursor, token)
email = token_data.get("email")
if "create_date" not in token_data:
return {"message": "The submitted token is invalid"}, 400
abort(code=400, message="The submitted token is invalid")

token_create_date = token_data["create_date"]
token_expired = (dt.utcnow() - token_create_date).total_seconds() > 900
delete_reset_token(cursor, token_data["email"], token)
if token_expired:
return {"message": "The submitted token is invalid"}, 400
abort(code=400, message="The submitted token is invalid")

password_digest = generate_password_hash(password)
cursor = self.psycopg2_connection.cursor()
Expand Down
4 changes: 3 additions & 1 deletion resources/ResetTokenValidation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from flask import request
from flask_restx import abort

from middleware.reset_token_queries import (
check_reset_token,
)
Expand All @@ -22,6 +24,6 @@ def post(self):
token_expired = (dt.utcnow() - token_create_date).total_seconds() > 900

if token_expired:
return {"message": "The submitted token is invalid"}, 400
abort(code=400, message="The submitted token is invalid")

return {"message": "Token is valid"}
4 changes: 1 addition & 3 deletions tests/resources/test_RefreshSession.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,7 @@ def test_post_refresh_session_unexpected_error(
)

check_response_status(response, 500)
assert response.json == {
"message": "An unexpected error occurred",
}
assert response.json["message"] == "An unexpected error occurred"
mock_get_session_token_user_data.assert_called_once_with(
mock_cursor, "old_test_session_token"
)
Expand Down

0 comments on commit 2fae1ef

Please sign in to comment.