Skip to content

Commit

Permalink
added dbinsert helper scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
malloc-nbytes committed Dec 6, 2024
1 parent 1a43350 commit 856e828
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 21 deletions.
83 changes: 83 additions & 0 deletions BackEndFlask/dbinsert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/local/bin/python3

from core import app, db
from models.user import create_user
import argparse
import sys


def insert_student(fname, lname, email, password, lms_id):
print("Inserting new student:")
print(f" fname: {fname}")
print(f" lname: {lname}")
print(f" email: {email}")
print(f" password: {password}")
print(f" lms_id: {lms_id}")

with app.app_context():
create_user({
"first_name": fname,
"last_name": lname,
"email": email,
"password": password,
"lms_id": lms_id,
"consent": None,
"owner_id": 2,
"role_id": 5
})


def insert_admin(fname, lname, email, password):
print("Inserting new admin:")
print(f" fname: {fname}")
print(f" lname: {lname}")
print(f" email: {email}")
print(f" password: {password}")

with app.app_context():
create_user({
"first_name": fname,
"last_name": lname,
"email": email,
"password": password,
"lms_id": 1,
"consent": None,
"owner_id": 1,
"role_id": 3
})


def main():
parser = argparse.ArgumentParser(description="Insert user information into the database.")

parser.add_argument("--new-student", action="store_true", help="Indicates that a new student is being added")
parser.add_argument("--fname", type=str, help="First name of the user", required=False)
parser.add_argument("--lname", type=str, help="Last name of the user", required=False)
parser.add_argument("--email", type=str, help="Email of the user", required=False)
parser.add_argument("--password", type=str, help="Password of the user", required=False)
parser.add_argument("--lms", type=str, help="LMS ID of the student", required=False)

parser.add_argument("--new-admin", action="store_true", help="Indicates that a new admin is being added")

args = parser.parse_args()

if not args.new_student and not args.new_admin:
print("Error: You must specify either --new-student or --new-admin.")
sys.exit(1)

if args.new_student:
if not (args.fname and args.lname and args.email and args.password and args.lms):
print("Error: Missing required fields for student (fname, lname, email, password, lms).")
sys.exit(1)
insert_student(args.fname, args.lname, args.email, args.password, args.lms)

if args.new_admin:
if not (args.fname and args.lname and args.email and args.password):
print("Error: Missing required fields for admin (fname, lname, email, password).")
sys.exit(1)
insert_admin(args.fname, args.lname, args.email, args.password)


if __name__ == "__main__":
main()

2 changes: 1 addition & 1 deletion BackEndFlask/models/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,4 @@ def wrapper(*args, **kwargs):
logger.error(f"{e.__traceback__.tb_frame.f_code.co_filename} { e.__traceback__.tb_lineno} Error Type: {type(e).__name__} Message: {e}")
raise e

return wrapper
return wrapper
2 changes: 1 addition & 1 deletion Dockerfile.backend
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ RUN apt-get update
WORKDIR /app

# Copy only requirements and setup scripts first to leverage Docker cache
COPY BackEndFlask/requirements.txt BackEndFlask/setupEnv.py /app/
COPY BackEndFlask/requirements.txt BackEndFlask/setupEnv.py BackEndFlask/dbinsert.py /app/

# Install Python dependencies
RUN pip install --no-cache-dir --upgrade pip \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class StudentViewAssessmentTaskInstructions extends Component {

genericResourceGET(
`/rubric?rubric_id=${state.chosenAssessmentTask["rubric_id"]}`,
"rubrics", this
"rubrics", this, {dest: "rubrics"}
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,24 +97,24 @@ class ViewCompletedAssessmentTasks extends Component {
<div>
<IconButton
onClick={() => {
navbar.setAssessmentTaskInstructions(
assessmentTasks,
atId,
completedAssessments,
{ readOnly: true, skipInstructions: true }
);
// var singluarCompletedAssessment = null;
// if (completedAssessments) {
// singluarCompletedAssessment = completedAssessments.find(completedAssessment => completedAssessment.assessment_task_id === atId) ?? null;
// }
// genericResourcePOST(
// `/rating`,
// this,
// JSON.stringify({
// "user_id" : singluarCompletedAssessment.user_id,
// "completed_assessment_id": singluarCompletedAssessment.completed_assessment_id,
// }),
// );
navbar.setAssessmentTaskInstructions(
assessmentTasks,
atId,
completedAssessments,
{ readOnly: true, skipInstructions: true }
);
var singluarCompletedAssessment = null;
if (completedAssessments) {
singluarCompletedAssessment = completedAssessments.find(completedAssessment => completedAssessment.assessment_task_id === atId) ?? null;
}
genericResourcePOST(
`/rating`,
this,
JSON.stringify({
"user_id" : singluarCompletedAssessment.user_id,
"completed_assessment_id": singluarCompletedAssessment.completed_assessment_id,
}),
);
}}
aria-label="completedAssessmentTasksViewIconButton"
>
Expand Down
19 changes: 19 additions & 0 deletions dbinsert.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

set -e

function help() {
echo "This script is inteded to be ran *while* the backend is running in Docker."
echo -e "It will insert a new user into the DB so you don't have to do it in the website.\n"
echo "dbinsert.sh <OPTION>"
echo "OPTION:"
echo " --new-student - insert a new student into the DB"
echo " --new-admin - insert a new admin into the DB"
exit 1
}

if [ $# -le 1 ]; then
help
fi

docker compose exec backend python dbinsert.py "$@"

0 comments on commit 856e828

Please sign in to comment.