Skip to content

Commit

Permalink
fixing performance issue on loading multiple joins, adding support fo…
Browse files Browse the repository at this point in the history
…r query parameter in url in api, working on better python doc.
  • Loading branch information
ooemperor committed Dec 4, 2023
1 parent 706c504 commit 51ecc2b
Show file tree
Hide file tree
Showing 45 changed files with 227 additions and 187 deletions.
18 changes: 9 additions & 9 deletions codeGrader/backend/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def users():
@return: Custom Representation of all the user objects
@rtype: dict
"""
return UserHandler().get_all()
return UserHandler().get_all(request.args)


@app.route("/user/add", methods=['POST'])
Expand Down Expand Up @@ -126,7 +126,7 @@ def admins():
@return: Custom Representation of all the objects
@rtype: dict
"""
return AdminUserHandler().get_all()
return AdminUserHandler().get_all(request.args)


@app.route("/admin/add", methods=['POST'])
Expand Down Expand Up @@ -175,7 +175,7 @@ def profiles():
@return: Custom Representation of all the objects
@rtype: dict
"""
return ProfileHandler().get_all()
return ProfileHandler().get_all(request.args)


@app.route("/subject/add", methods=['POST'])
Expand Down Expand Up @@ -214,7 +214,7 @@ def subjects():
@return: Custom Representation of all the objects
@rtype: dict
"""
return SubjectHandler().get_all()
return SubjectHandler().get_all(request.args)


# Task
Expand Down Expand Up @@ -254,7 +254,7 @@ def tasks():
@return: Custom Representation of all the objects
@rtype: dict
"""
return TaskHandler().get_all()
return TaskHandler().get_all(request.args)


@app.route("/exercise/add", methods=['POST'])
Expand Down Expand Up @@ -293,7 +293,7 @@ def exercises():
@return: Custom Representation of all the objects
@rtype: dict
"""
return ExerciseHandler().get_all()
return ExerciseHandler().get_all(request.args)


@app.route("/uploadFile", methods=["POST"])
Expand Down Expand Up @@ -364,7 +364,7 @@ def submissions():
@return: Custom Representation of all the objects
@rtype: dict
"""
return SubmissionHandler().get_all()
return SubmissionHandler().get_all(request.args)


@app.route("/testcase/add", methods=["POST"])
Expand Down Expand Up @@ -398,7 +398,7 @@ def testcases():
@return: Custom Representation of all the objects
@rtype: dict
"""
return TestCaseHandler().get_all()
return TestCaseHandler().get_all(request.args)


@app.route("/adminTypes")
Expand All @@ -408,7 +408,7 @@ def admin_types():
@@return: Custom Representation of all the objects
@rtype: dict
"""
return AdminTypeHandler().get_all()
return AdminTypeHandler().get_all(request.args)


# starting the web application
Expand Down
4 changes: 2 additions & 2 deletions codeGrader/backend/api/handlers/Admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ class AdminUserHandler(BaseHandler):
Handler for the AdminUser
"""

def __init__(self):
def __init__(self) -> None:
"""
Constructor for the UserHandlerClass
"""
super().__init__()
self.dbClass = AdminUser

def _preprocess_data_dict(self, dict_: dict):
def _preprocess_data_dict(self, dict_: dict) -> dict:
"""
Preprocessing of the data dictionary for the Admin UserHandler
Overwrites the method from the parent class
Expand Down
2 changes: 1 addition & 1 deletion codeGrader/backend/api/handlers/AdminType.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class AdminTypeHandler(BaseHandler):
Handler for the AdminType
"""

def __init__(self):
def __init__(self) -> None:
"""
Constructor for the UserHandlerClass
"""
Expand Down
2 changes: 1 addition & 1 deletion codeGrader/backend/api/handlers/Authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from flask import request


def _check_authentication(api_token: str):
def _check_authentication(api_token: str) -> None:
"""
Checking if the provided api_token is valid
@param api_token: The Bearer Token used in the authentication.
Expand Down
24 changes: 22 additions & 2 deletions codeGrader/backend/api/handlers/Base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Base Handler for the backend API. Setting basic properties like a session.
@author: mkaiser
"""
import sqlalchemy.orm.decl_api

from codeGrader.backend.db import Session
from .ResponseGenerator import ErrorResponseHandler, GenericResponseHandler

Expand All @@ -11,6 +13,9 @@ class BaseHandler:
The Class for the Basic Handler of the backend API.
This Handler will be the parent class for all the other handlers.
"""
# defintion of non present instance variable in the parent class
# these variables will be defined in the child classes
dbClass: sqlalchemy.orm.decl_api.DeclarativeMeta

def __init__(self):
"""
Expand Down Expand Up @@ -136,20 +141,35 @@ def delete(self, id_: int):
except Exception as err:
return self.create_generic_error_response('DELETE', err, id_)

def get_all(self):
def get_all(self, arguments={}):
"""
Get all objects of a Class.
@param arguments: The arguments provided in the API Call as URL arguments
@type arguments: str
@return: List of all Objects
@rtype: list
"""
if arguments is None:
arguments = dict()
try:
objects = self.sql_session.get_all(self.dbClass)
if len(objects) == 0:
return self.create_generic_response('GET', f"No Objects entries to display")

else:
object_list = []

for object_ in objects:
object_list.append(object_.toJson())
object_dict = object_.toJson()

if len(arguments.keys()) == 0:
object_list.append(object_dict)
continue

for key in arguments.keys():
if object_dict[key] == arguments.get(key):
object_list.append(object_dict)

output = dict()
output[str(self.dbClass.__table__)] = object_list
return output
Expand Down
2 changes: 1 addition & 1 deletion codeGrader/backend/api/handlers/Exercise.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ExerciseHandler(BaseHandler):
Using the default get, post, delete and put methods defined in the BaseHandler
@see: BaseHandler
"""
def __init__(self):
def __init__(self) -> None:
"""
Constructor for the ExerciseHandler
"""
Expand Down
2 changes: 1 addition & 1 deletion codeGrader/backend/api/handlers/File.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class FileHandler(BaseHandler):
"""
Handler for the File class.
"""
def __init__(self):
def __init__(self) -> None:
"""
Constructor for the FileHandler
"""
Expand Down
6 changes: 3 additions & 3 deletions codeGrader/backend/api/handlers/LoginHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ class LoginHandler(BaseHandler):

# instance variables that are not set in this parent class

def __init__(self):
def __init__(self) -> None:
"""
Constructor of the LoginHandler Partent Class.
Uses the Basehandler
"""
super().__init__()

def login(self, username: str, password: str):
def login(self, username: str, password: str) -> dict:
"""
Verifying if a user provided the correct login credentials
@param username: The username of the user
Expand Down Expand Up @@ -62,7 +62,7 @@ class AdminUserLoginHandler(LoginHandler):
Used by the frontend to check the login status of a AdminUser.
"""

def __init__(self):
def __init__(self) -> None:
"""
Constructor of the AdminUserLoginHandler
"""
Expand Down
2 changes: 1 addition & 1 deletion codeGrader/backend/api/handlers/Profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ProfileHandler(BaseHandler):
Using the default get, post, delete and put methods defined in the BaseHandler
@see: BaseHandler
"""
def __init__(self):
def __init__(self) -> None:
"""
Constructor for the ProfileHandler
"""
Expand Down
26 changes: 20 additions & 6 deletions codeGrader/backend/api/handlers/ResponseGenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,22 @@ def __init__(self):
"""

@staticmethod
def _get_response_code(method: str):
def _get_response_code(method: str) -> int:
"""
Calculating the resonse code for a specific method
@param method: The method used on the API Call
@type method: str
@return: The response code
@rtype: int
"""
if method == 'POST':
return 201
elif method == 'DELETE':
return 204
else:
return 200

def generate_response(self, method: str, response: str, id_: int = None):
def generate_response(self, method: str, response: str, id_: int = None) -> (dict, int):
"""
Generates a generic method for a response Body
@param method: the request method
Expand All @@ -36,7 +43,7 @@ def generate_response(self, method: str, response: str, id_: int = None):
@param response: The response message that should be added
@type: response: String
@return: The response JSON
@rtype: dict
@rtype: (dict, int)
"""
out = dict()
_response = dict()
Expand All @@ -60,7 +67,14 @@ def __init__(self):
"""

@staticmethod
def _get_response_code(exception: Exception):
def _get_response_code(exception: Exception) -> int:
"""
Calculating the response code for an exception
@param exception: The exception that has been raised
@type exception: str
@return: the response code according to the error
@rtype: int
"""
if exception in [sqlalchemy.orm.exc.UnmappedInstanceError]:
# object was not found in the database
return 404
Expand All @@ -70,7 +84,7 @@ def _get_response_code(exception: Exception):
# return general server error
return 500

def generate_response(self, method: str, exception: Exception, id_: int = None):
def generate_response(self, method: str, exception: Exception, id_: int = None) -> (dict, int):
"""
The Response Generator
@param method: The method of the request
Expand All @@ -80,7 +94,7 @@ def generate_response(self, method: str, exception: Exception, id_: int = None):
@param exception: The exception that has been raised while handling the request.
@type exception: Exception
@return: The properly rendered repsonse
@rtype: dict
@rtype: (dict, int)
"""

out = dict()
Expand Down
2 changes: 1 addition & 1 deletion codeGrader/backend/api/handlers/Subject.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class SubjectHandler(BaseHandler):
Using the default get, post, delete and put methods defined in the BaseHandler
@see: BaseHandler
"""
def __init__(self):
def __init__(self) -> None:
"""
Constructor for the ProfileHandler
"""
Expand Down
2 changes: 1 addition & 1 deletion codeGrader/backend/api/handlers/Submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class SubmissionHandler(BaseHandler):
Using the default get, post, delete and put methods defined in the BaseHandler
@see: BaseHandler
"""
def __init__(self):
def __init__(self) -> None:
"""
Constructor for the ProfileHandler
"""
Expand Down
2 changes: 1 addition & 1 deletion codeGrader/backend/api/handlers/Task.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class TaskHandler(BaseHandler):
# TODO: integrate the files to be added.
@see: BaseHandler
"""
def __init__(self):
def __init__(self) -> None:
"""
Constructor for the ProfileHandler
"""
Expand Down
2 changes: 1 addition & 1 deletion codeGrader/backend/api/handlers/TestCase.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TestCaseHandler(BaseHandler):
Using the default get, post, delete and put methods defined in the
@see: BaseHandler
"""
def __init__(self):
def __init__(self) -> None:
"""
Constructor for the ProfileHandler
"""
Expand Down
2 changes: 1 addition & 1 deletion codeGrader/backend/api/handlers/User.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __init__(self):
super().__init__()
self.dbClass = User

def _preprocess_data_dict(self, dict_: dict):
def _preprocess_data_dict(self, dict_: dict) -> dict:
"""
Preprocessing of the data dictionary for the User Handler
Overwrites the method from the parent class
Expand Down
2 changes: 1 addition & 1 deletion codeGrader/backend/db/Admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class AdminUser(Base):
lazy="joined"
)

def toJson(self):
def toJson(self) -> dict:
"""
Render the json representation of a admin user
@return: JSON representation of a admin user
Expand Down
2 changes: 1 addition & 1 deletion codeGrader/backend/db/AdminType.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class AdminType(Base):
String, nullable=True, unique=False
)

def toJson(self):
def toJson(self) -> dict:
"""
Render the representation of the AdminType
@return: JSON representation of a user
Expand Down
4 changes: 2 additions & 2 deletions codeGrader/backend/db/Attachments.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Instruction(Base):
index=True
)

def toJson(self):
def toJson(self) -> dict:
"""
Render the json representation of a Instruction
This is a relation table, so we get just the values of the related tables.
Expand Down Expand Up @@ -92,7 +92,7 @@ class Attachment(Base):
index=True
)

def toJson(self):
def toJson(self) -> dict:
"""
Render the json representation of a Attachment
This is a relation table, so we get just the values of the related tables.
Expand Down
2 changes: 1 addition & 1 deletion codeGrader/backend/db/Authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class APIToken(Base):
String, nullable=False, index=False
)

def __init__(self, description: str):
def __init__(self, description: str) -> None:
"""
API Token Constructor of an Object in the database
Overwrites the Base Constructor.
Expand Down
Loading

0 comments on commit 51ecc2b

Please sign in to comment.