Skip to content

Commit

Permalink
πŸ› πŸš§ fix reqparse.add_argument with location
Browse files Browse the repository at this point in the history
- reference issue: flask-restful/flask-restful#963
- todo work on user management system class
  • Loading branch information
ZenithClown committed Apr 6, 2024
1 parent d4d9b3a commit 90a8434
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
8 changes: 7 additions & 1 deletion finfolio/main/config.py
Expand Up @@ -113,7 +113,7 @@ def get_small_message(self, code : str) -> int:
}.get(code, 502) # 502 > Bad Gateway


def get(self, data : list, err : str = None, code : int = 200, msg : str = None) -> dict:
def get(self, data : list | dict, err : str = None, code : int = 200, msg : str = None) -> dict:
"""Format O/P of all GET Response"""

return {
Expand Down Expand Up @@ -199,3 +199,9 @@ def args(self) -> object:
"""

return self.req_parser.parse_args()


class BaseInterface(object):
def __init__(self) -> None:
self.code = None
self.message = None
11 changes: 7 additions & 4 deletions finfolio/main/controller/ums.py
Expand Up @@ -22,7 +22,7 @@ def __init__(self) -> None:
super().__init__()

# adding list of arguments available for the controller
# self.req_parser.add_argument("username", type = str, required = False)
self.req_parser.add_argument("username", type = str, required = False, location=("values",))

# initialize the interfaces associated with the controller
self.ums_interface = UMSInterface()
Expand All @@ -31,11 +31,14 @@ def __init__(self) -> None:
def get(self):
"""GET Request for UMS Module"""

data, err = None, None # no response, safe-keeping
data, err, msg = None, None, None

if request.endpoint == "ums/root-user":
data = self.ums_interface.get_root()
elif request.endpoint == "ums/default":
data = self.ums_interface.get_all()
if self.args["username"]:
data, err, msg = self.ums_interface.get_user(self.args["username"])
else:
data = self.ums_interface.get_all()

return self.formatter.get(data)
return self.formatter.get(data, err = err, code = 200 if data else 204)
21 changes: 16 additions & 5 deletions finfolio/main/interface/ums.py
Expand Up @@ -10,9 +10,10 @@
Copywright Β© [2023] pOrgz <https://github.com/pOrgz-dev>
"""

from finfolio.main.config import BaseInterface
from finfolio.main.models.ums import * # noqa: F401, F403 # pyright: ignore[reportMissingImports]

class UMSInterface:
class UMSInterface(BaseInterface):

get_all = lambda self : [row.__to_dict__() for row in UserAccounts.query.order_by(UserAccounts.username.asc()).all()]
get_root = lambda self : UserAccounts.query.filter(UserAccounts.roles == 1).first().__to_dict__() # get root user details
Expand All @@ -29,7 +30,17 @@ def get_user(self, username : str) -> dict:
database, and returns only one single record.
"""

try:
return UserAccounts.query.filter(UserAccounts.username == username).first().__to_dict__(), None
except AttributeError as err:
return dict(), err # ? probably no data
content = UserAccounts.query.filter(UserAccounts.username == username).first()

if content:
try:
content = content.__to_dict__()
except Exception as err:
content = None
self.error = err
self.message = "InternalError: Something Went Wrong!"
else:
self.error = 204
self.message = f"{username} is not available."

return content, self.error, self.message
1 change: 1 addition & 0 deletions manage.py
Expand Up @@ -7,6 +7,7 @@

if __name__ == "__main__":
app.run(
debug = True, # run the application in debug mode
port = os.getenv("port", 5000), # run the application on default 5000 Port
# localhost is required to run the code from m/c
# else, 0.0.0.0 can be used for docker container
Expand Down

0 comments on commit 90a8434

Please sign in to comment.