Skip to content

Commit

Permalink
Merge pull request #39 from ooemperor/dev
Browse files Browse the repository at this point in the history
Updating installation scripts, fixing bugs and adding new run scripts for frontend
  • Loading branch information
ooemperor authored Feb 18, 2024
2 parents 4ff5de8 + dbcb53e commit cd438ab
Show file tree
Hide file tree
Showing 22 changed files with 266 additions and 19 deletions.
4 changes: 2 additions & 2 deletions codeGrader/backend/api/handlers/Score.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def get_scores(self, view: str, arguments: dict = {}):
db_object = self.sql_session.get_object(self.dbClass, object_id)
print(db_object.id)
score = db_object.user_score(user_id)
user = self.sql_session.get(User, user_id)
user = self.sql_session.get_object(User, user_id)

user_data_dict = {"user_id": user_id, "username": user.username, "score": score}
output_data_list.append(user_data_dict)
Expand All @@ -95,7 +95,7 @@ def get_scores(self, view: str, arguments: dict = {}):
else:
# the object_id is not given, so we need to query all objects
db_objects = self.sql_session.get_all(self.dbClass)
user = self.sql_session.get(User, user_id)
user = self.sql_session.get_object(User, user_id)
for db_object in db_objects:
score = db_object.user_score(user_id)
print(score)
Expand Down
2 changes: 1 addition & 1 deletion codeGrader/frontend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@

from . import admin
from . import user
from . import config
from . import config
1 change: 1 addition & 0 deletions codeGrader/frontend/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
Init file for the frontend grader
"""
from .app import admin_frontend

13 changes: 11 additions & 2 deletions codeGrader/frontend/admin/handlers/AdminUser.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ def get(self, id_: int) -> Union[str, Response]:
editable = self.admin.check_permission('w')
admin["editable"] = editable

if self.admin.check_permission('r', admin["profile"]["id"]): # when admin is allowed to view this admin
prof_id = 0
if admin["profile"] is not None:
prof_id = admin["profile"]["id"]

if self.admin.check_permission('r', prof_id): # when admin is allowed to view this admin
return render_template("adminUser.html", **admin)

else: # admin is not allowed to view this user
Expand All @@ -98,7 +102,12 @@ def post(self, id_: int) -> Response:
assert self.request.form is not None

admin_before = self.api.get(f"/admin/{id_}") # get the admin data
if self.admin.check_permission('w', admin_before["profile"]["id"]):

prof_id = 0
if admin_before["profile"] is not None:
prof_id = admin_before["profile"]["id"]

if self.admin.check_permission('w', prof_id):
admin_data = dict()

# getting the data from the form provided in the request
Expand Down
1 change: 1 addition & 0 deletions codeGrader/frontend/admin/handlers/Exercise.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ def post(self) -> Response:

exercise_data["name"] = self.get_value("name")
exercise_data["tag"] = self.get_value("tag")
exercise_data["description"] = self.get_value("description")

exercise_data["subject_id"] = self.get_value("subject")

Expand Down
3 changes: 3 additions & 0 deletions codeGrader/frontend/admin/handlers/SessionAdmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,16 @@ def check_permission(self, operation: str, profile_id: str = None, create_object
return False

elif operation == 'w':
print(create_object)
print(self._admin_type_name)
if self._admin_type_name == config.admin_rw_partial and profile_id == self.profile_id:
return True

elif self._admin_type_name == config.admin_rw_partial and create_object not in [None, "profile", "admin"]:
return True

else:
print(1)
return False

else:
Expand Down
1 change: 1 addition & 0 deletions codeGrader/frontend/admin/handlers/Task.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ def post(self) -> Response:

task_data["name"] = self.get_value("name")
task_data["tag"] = self.get_value("tag")
task_data["description"] = self.get_value("description")
task_data["exercise_id"] = self.get_value("exercise")

self.api.post("/task/add", body=task_data)
Expand Down
2 changes: 1 addition & 1 deletion codeGrader/frontend/admin/handlers/User.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def get(self) -> Union[str, Response]:
Get and render the site to create an admin user
@return: The rendered page.
"""
if self.admin.check_permission('w', 'user'):
if self.admin.check_permission('w', create_object='user'):

user_data = dict()

Expand Down
8 changes: 8 additions & 0 deletions codeGrader/frontend/admin/templates/addExercise.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ <h1 class="header1">Exercise</h1>
<input class="input" type="text" id="input_name" name="name" placeholder="Exercise Name"">
</td>
</tr>
<tr>
<td>Description</td>
<td>
<textarea class="input" type="text"
id="input_description" name="description" rows="3"
cols="100">{{ description }}</textarea>
</td>
</tr>
<tr>
<td>Tag
</td>
Expand Down
8 changes: 8 additions & 0 deletions codeGrader/frontend/admin/templates/addTask.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ <h1 class="header1">Task</h1>
<input class="input" type="text" id="input_name" name="name" placeholder="Task Name">
</td>
</tr>
<tr>
<td>Description</td>
<td>
<textarea class="input" type="text"
id="input_description" name="description" rows="3"
cols="100">{{ description }}</textarea>
</td>
</tr>
<tr>
<td class=" data">Tag</td>
<td>
Expand Down
3 changes: 2 additions & 1 deletion codeGrader/frontend/user/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@
"""
init file for the user Frontend
@author: mkaiser
"""
"""
from .app import user_frontend
3 changes: 2 additions & 1 deletion codeGrader/frontend/user/handlers/Exercise.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ def get(self) -> str:
@return: The rendered template
"""
exercises = self.api.get("/exercises")
temp_exercises = exercises["exercise"].copy()

for ex in exercises["exercise"]:
for ex in temp_exercises:
# filtering only the exercises that are allowed by the memberships
if not self.user.check_permission(subject_id=ex["subject_id"]):
exercises["exercise"].remove(ex)
Expand Down
4 changes: 2 additions & 2 deletions codeGrader/frontend/user/handlers/Subject.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ def get(self) -> str:
@return: The rendered template
"""
subjects = self.api.get("/subjects")
temp_subjects = subjects["subject"].copy()


for sub in subjects["subject"]:
for sub in temp_subjects:
# filtering only the subjects that are allowed by the memberships
if not self.user.check_permission(subject_id=sub["id"]):
subjects["subject"].remove(sub)
Expand Down
3 changes: 0 additions & 3 deletions codeGrader/scripts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,3 @@
@author: mkaiser
"""

from . import deployDB, cgEvaluationService, cgExecutionService, cgApiBackend

__all__ = ["deployDB", "cgEvaluationService", "cgExecutionService", "cgAdminFrontend", "cgApiBackend"]
87 changes: 87 additions & 0 deletions codeGrader/scripts/cgAddAdmin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# CodeGrader - https://github.com/ooemperor/CodeGrader
# Copyright © 2023, 2024 Michael Kaiser <[email protected]>
#
# This file is part of CodeGrader.
#
# CodeGrader is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# CodeGrader is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with CodeGrader. If not, see <http://www.gnu.org/licenses/>.

"""
Script to create an admin user object in the Database for login in the admin frontend
Script will be generated and added to the server path.
@author: mkaiser
"""

from codeGrader.backend.db import Session, AdminUser
import argparse
import sys
import hashlib


def addAdmin(username, first_name, last_name, email, password):
"""
Generate an admin user that will be added to the database for the initial setups
@param username: The username for the new admin
@type username: str
@param first_name: The first name of the new admin
@type first_name: str
@param last_name: The lastname of the new admin
@type last_name: str
@param email: The email of the new admin
@type email: str
@param password: The password of the new admin
@type password: str
@return: Print statement about the user
@rtype: str
"""
sql = Session() # open session
password = password.encode('utf-8') # Convert the password to bytes
hash_object = hashlib.sha256(password) # Choose a hashing algorithm (e.g., SHA-256)
hex_dig = hash_object.hexdigest()
admin_dict = {
"username": username,
"first_name": first_name,
"last_name": last_name,
"email": email,
"password": hex_dig,
"tag": "",
"admin_type": 1
}
admin_user = AdminUser(**admin_dict) # create the object
sql.create(admin_user) # write token to sql

print(f"User {username} has been created with password {password}")


def main():
"""
The main method adding an admin to the backend.
@return:
"""

parser = argparse.ArgumentParser(
description="Add Admin User to the Database"
)
parser.add_argument('-u', '--username')
parser.add_argument('-fn', '--first_name')
parser.add_argument('-ln', '--last_name')
parser.add_argument('-e', '--email')
parser.add_argument('-p', '--password')

args = parser.parse_args()

addAdmin(args.username, args.first_name, args.last_name, args.email, args.password)


if __name__ == '__main__':
sys.exit(main())
14 changes: 13 additions & 1 deletion codeGrader/scripts/cgAddApiToken.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
"""

from codeGrader.backend.db import Session, APIToken
import argparse
import sys


def main(description):
Expand All @@ -44,4 +46,14 @@ def main(description):


if __name__ == '__main__':
main("")
"""
Parsing the description argument via a ArgumentParser
"""
parser = argparse.ArgumentParser(
description="Add API Key to the Database"
)
parser.add_argument('-d', 'description')

args = parser.parse_args()

sys.exit(main(args.description))
2 changes: 2 additions & 0 deletions codeGrader/scripts/cgAdminFrontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@

import sys
from codeGrader.frontend.admin import admin_frontend
import os


def main():
"""
Starting a new instance of the EvaluationService
@return:
"""
os.chdir("/usr/local/lib/python3.11/dist-packages/codeGrader/frontend/admin")
admin_frontend()


Expand Down
39 changes: 39 additions & 0 deletions codeGrader/scripts/cgUserFrontend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# CodeGrader - https://github.com/ooemperor/CodeGrader
# Copyright © 2023, 2024 Michael Kaiser <[email protected]>
#
# This file is part of CodeGrader.
#
# CodeGrader is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# CodeGrader is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with CodeGrader. If not, see <http://www.gnu.org/licenses/>.

"""
Script File for starting the User Frontend
@author: mkaiser
"""

import sys
from codeGrader.frontend.user import user_frontend
import os


def main():
"""
Starting a new instance of the EvaluationService
@return:
"""
os.chdir("/usr/local/lib/python3.11/dist-packages/codeGrader/frontend/user")
user_frontend()


if __name__ == '__main__':
sys.exit(main())
Loading

0 comments on commit cd438ab

Please sign in to comment.