From bae8b39c758deef34328a058dbd51aa7a683b603 Mon Sep 17 00:00:00 2001 From: Nicholas Couillard Date: Mon, 16 Oct 2023 17:34:35 -0400 Subject: [PATCH] added second table, now they communicate --- CourseFunctions.py | 145 ++++++++++++++++++++++++++------------------ StudentFunctions.py | 22 +++++++ 2 files changed, 108 insertions(+), 59 deletions(-) create mode 100644 StudentFunctions.py diff --git a/CourseFunctions.py b/CourseFunctions.py index 408ff49..ba72f0d 100644 --- a/CourseFunctions.py +++ b/CourseFunctions.py @@ -4,9 +4,10 @@ import random course_table = resource('dynamodb').Table('Courses') +student_table = resource('dynamodb').Table('Students') # NOTE: another table for users and professors may be necessary. In order to do this we might need functionality to introduce salt and hashing for security -def courseInsert(courseId, courseSection, courseLevel, courseDepartment, courseProfessor, courseEnrolledStudents, courseMaxStudents, courseIsOpen, courseFormat, prerequisites='None', courseStartTime=0, courseEndTime=0, courseScheduledDays='None', courseLocation='None'): +def courseInsert(courseId, courseSection, courseLevel, courseDepartment, courseProfessor, courseEnrolledStudents, courseMaxStudents, courseIsOpen, courseFormat, prerequisites='None', courseStartTime=0, courseEndTime=0, courseScheduledDays='None', courseLocation='None', enrolledStudentList = []): courseInsertion = course_table.put_item( Item = { 'CourseId' : courseId, # partition key @@ -22,7 +23,8 @@ def courseInsert(courseId, courseSection, courseLevel, courseDepartment, courseP 'StartTime' : courseStartTime, # is storing times in the form of 24-hour time easier? it's simple to convert when necessary 'EndTime' : courseEndTime, 'CourseScheduledDays' : courseScheduledDays, # this will likely be a list, but im lazy so for now it's a string - 'CourseLocation' : courseLocation # could increase functionality by separating into building and room number, to allow searching by building + 'CourseLocation' : courseLocation, # could increase functionality by separating into building and room number, to allow searching by building + 'ListEnrolledStudents' : enrolledStudentList } ) @@ -38,18 +40,21 @@ def courseRemove(courseId, courseSection): print(f"Removal response: {courseRemoval}") -def enrollStudent(courseId, courseSection): +def enrollStudent(courseId, courseSection, studentID): studentEnroller = course_table.get_item( Key={ 'CourseId' : courseId, - 'Section' : courseSection + 'Section' : courseSection, }, - AttributesToGet = ['OpenStatus', 'StudentsEnrolled', 'MaxStudents'] + AttributesToGet = ['OpenStatus', 'StudentsEnrolled', 'MaxStudents', 'ListEnrolledStudents'] ) isOpen = (studentEnroller.get('Item')).get('OpenStatus') numStudents = (studentEnroller.get('Item')).get('StudentsEnrolled') maxStudents = (studentEnroller.get('Item')).get('MaxStudents') + listEnrolledStudents = (studentEnroller.get('Item')).get('ListEnrolledStudents') + + if maxStudents - (numStudents + 1) > 0: newOpenStatus = True else: @@ -57,72 +62,94 @@ def enrollStudent(courseId, courseSection): if isOpen == True: numStudents += 1 + listEnrolledStudents.append(studentID) student_enrolled = course_table.update_item( Key={ 'CourseId' : courseId, 'Section' : courseSection }, - UpdateExpression='SET StudentsEnrolled = :val1, OpenStatus = :val2', + UpdateExpression='SET StudentsEnrolled = :val1, OpenStatus = :val2, ListEnrolledStudents = :val3', ExpressionAttributeValues={ ':val1': numStudents, - ':val2': newOpenStatus + ':val2': newOpenStatus, + ':val3': listEnrolledStudents } ) - print(f"{student_enrolled}") + + studentCourseUpdater = student_table.get_item( + Key={ + 'StudentId' : studentID + }, + AttributesToGet = ['EnrolledCourses'] + ) + + studentEnrolledCourses = (studentCourseUpdater.get('Item')).get('EnrolledCourses') + studentEnrolledCourses.append(courseId) + update_courses = student_table.update_item( + Key = { + 'StudentId' : studentID + }, + UpdateExpression = 'SET EnrolledCourses = :val1', + ExpressionAttributeValues = { + ':val1': studentEnrolledCourses + } + ) if __name__ == '__main__': #NOTE: NONE OF THE INFO COLLECTING WILL LIKELY BE DONE IN PYTHON, THIS IS PROBABLY VUE STUFF. JUST FOR DEMO - for i in range(1000): - courseDepartment = random.choice(['CSE', 'COMM', 'ANTH', 'STAT', 'SPAN', 'GERM', 'ERTH']) - courseNumber = random.randint(1000,5000) - courseSection = 1 - if len(str(courseSection)) == 1: - courseSection = '00' + str(courseSection) - elif len(str(courseSection)) == 2: - courseSection = '0' + str(courseSection) - # courseType = input("\nSelect the type of course if needed: Laboratory, Discussion: ") - # if courseType == "Laboratory": - # courseSection += 'L' - # elif courseType == "Discussion": - # courseSection += 'D' - courseLevel = math.floor(courseNumber / 1000) * 1000 - courseId = courseDepartment + str(courseNumber) - courseProfessor = random.choice(['Ariana Grande', 'Jeff Bezos', 'Samuel Alito', 'Greg Jackson', 'Nicholas Couillard', 'Miley Cirus', 'Joe Biden', 'Barack Obama', 'Gabe Vincent', 'Stephen Curry', 'Kamala Harris', '6ix9ine', 'Ghandi']) - courseMaxStudents = random.randint(0,200) - courseFormat = random.choice(["In-person", "Online asynchronous", "Online synchronous"]) - if courseFormat == "In-person" or courseFormat == "Online synchronous": - numDaysTest = random.randint(0,3) - counter = 0 - courseScheduledDays = [] - weekDays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] - while counter < numDaysTest: - counter += 1 - choice = random.choice(weekDays) - courseScheduledDays.append(choice) - weekDays.remove(choice) - times = [["9:05AM","9:55AM"], ["10:10AM","11:00AM"], ["11:15AM","12:05PM"], ["12:20PM","1:10PM"], ["1:25PM","2:15PM"], ["2:30PM","3:20PM"], ["3:35PM","4:25PM"]] - option = random.randint(0,6) - timeSelected = times[option] - courseStartTime = timeSelected[0] - courseEndTime = timeSelected[1] - - else: - courseScheduledDays = "None" - courseStartTime = "None" - courseEndTime = "None" - courseEnrolledStudents = random.randint(0,courseMaxStudents) - courseIsOpen = bool(courseMaxStudents - courseEnrolledStudents) - if courseFormat == "In-person": - courseLocation = random.choice(["MCHU 101", "E2 306", "ITE 114", "MONT 319", "GANT 109", "ARJ 114", "ROWE 307", "ITE 332"]) - else: - courseLocation = "Online" - - coursePrereqs = "None" - - courseInsert(courseId, courseSection, courseLevel, courseDepartment, courseProfessor, courseEnrolledStudents, courseMaxStudents, courseIsOpen, courseFormat, coursePrereqs, courseStartTime, courseEndTime, courseScheduledDays, courseLocation) - - #courseRemove('CSE2102', '001') - #enrollStudent('CSE1010', '001L') \ No newline at end of file + # for i in range(1000): + # courseDepartment = random.choice(['CSE', 'COMM', 'ANTH', 'STAT', 'SPAN', 'GERM', 'ERTH']) + # courseNumber = random.randint(1000,5000) + # courseSection = 1 + # if len(str(courseSection)) == 1: + # courseSection = '00' + str(courseSection) + # elif len(str(courseSection)) == 2: + # courseSection = '0' + str(courseSection) + # # courseType = input("\nSelect the type of course if needed: Laboratory, Discussion: ") + # # if courseType == "Laboratory": + # # courseSection += 'L' + # # elif courseType == "Discussion": + # # courseSection += 'D' + # courseLevel = math.floor(courseNumber / 1000) * 1000 + # courseId = courseDepartment + str(courseNumber) + # courseProfessor = random.choice(['Ariana Grande', 'Jeff Bezos', 'Samuel Alito', 'Greg Jackson', 'Nicholas Couillard', 'Miley Cirus', 'Joe Biden', 'Barack Obama', 'Gabe Vincent', 'Stephen Curry', 'Kamala Harris', '6ix9ine', 'Ghandi']) + # courseMaxStudents = random.randint(0,200) + # courseFormat = random.choice(["In-person", "Online asynchronous", "Online synchronous"]) + # if courseFormat == "In-person" or courseFormat == "Online synchronous": + # numDaysTest = random.randint(0,3) + # counter = 0 + # courseScheduledDays = [] + # weekDays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] + # while counter < numDaysTest: + # counter += 1 + # choice = random.choice(weekDays) + # courseScheduledDays.append(choice) + # weekDays.remove(choice) + # times = [["9:05AM","9:55AM"], ["10:10AM","11:00AM"], ["11:15AM","12:05PM"], ["12:20PM","1:10PM"], ["1:25PM","2:15PM"], ["2:30PM","3:20PM"], ["3:35PM","4:25PM"]] + # option = random.randint(0,6) + # timeSelected = times[option] + # courseStartTime = timeSelected[0] + # courseEndTime = timeSelected[1] + + # else: + # courseScheduledDays = "None" + # courseStartTime = "None" + # courseEndTime = "None" + # courseEnrolledStudents = random.randint(0,courseMaxStudents) + # courseIsOpen = bool(courseMaxStudents - courseEnrolledStudents) + # if courseFormat == "In-person": + # courseLocation = random.choice(["MCHU 101", "E2 306", "ITE 114", "MONT 319", "GANT 109", "ARJ 114", "ROWE 307", "ITE 332"]) + # else: + # courseLocation = "Online" + + # coursePrereqs = "None" + + # enrolledStudentList = [] + + # courseInsert(courseId, courseSection, courseLevel, courseDepartment, courseProfessor, courseEnrolledStudents, courseMaxStudents, courseIsOpen, courseFormat, coursePrereqs, courseStartTime, courseEndTime, courseScheduledDays, courseLocation, enrolledStudentList) + + + enrollStudent('SPAN4173', '001', 'nic21003') \ No newline at end of file diff --git a/StudentFunctions.py b/StudentFunctions.py new file mode 100644 index 0000000..e11d490 --- /dev/null +++ b/StudentFunctions.py @@ -0,0 +1,22 @@ +from boto3 import resource +from boto3.dynamodb.conditions import Attr, Key +import math +import random + +student_table = resource('dynamodb').Table('Students') + +def createStudent(studentID, studentPWD, enrolledCourses = []): + studentCreation = student_table.put_item( + Item = { + 'StudentId' : studentID, + 'StudentPWD' : studentPWD, + 'EnrolledCourses' : enrolledCourses + } + ) + + print(f"Student creation output: {studentCreation}") + + + +if __name__ == "__main__": + createStudent("nic21003", "coolTest420") \ No newline at end of file