Skip to content
Permalink
main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
from boto3 import resource
from boto3.dynamodb.conditions import Attr, Key
import math
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', enrolledStudentList = []):
courseInsertion = course_table.put_item(
Item = {
'CourseId' : courseId, # partition key
'Section' : courseSection, # sort key?
'Level' : courseLevel, # useful attribute for searching
'Department' : courseDepartment, # useful attribute for searching
'Professor' : courseProfessor, # TODO: support multiple instructors
'StudentsEnrolled' : courseEnrolledStudents, # useful attribute for professor
'MaxStudents' : courseMaxStudents, # can calculate OpenStatus using some logic
'OpenStatus' : courseIsOpen, # this attribute could be useful for hiding unavailable courses
'CourseFormat' : courseFormat,
'Prerequisites' : prerequisites, # this will be a list of courses
'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
'ListEnrolledStudents' : enrolledStudentList
}
)
print(f"Insert response: {courseInsertion}")
def courseRemove(courseId, courseSection):
courseRemoval = course_table.delete_item(
Key={
'CourseId' : courseId, # partition key
'Section' : courseSection # sort key
}
)
print(f"Removal response: {courseRemoval}")
def enrollStudent(courseId, courseSection, studentID):
studentEnroller = course_table.get_item(
Key={
'CourseId' : courseId,
'Section' : courseSection,
},
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:
newOpenStatus = False
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, ListEnrolledStudents = :val3',
ExpressionAttributeValues={
':val1': numStudents,
':val2': newOpenStatus,
':val3': listEnrolledStudents
}
)
studentCourseUpdater = student_table.get_item(
Key={
'StudentId' : studentID
},
AttributesToGet = ['EnrolledCourses']
)
studentEnrolledCourses = (studentCourseUpdater.get('Item')).get('EnrolledCourses')
studentEnrolledCourses.append([courseId, courseSection])
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"
# enrolledStudentList = []
# courseInsert(courseId, courseSection, courseLevel, courseDepartment, courseProfessor, courseEnrolledStudents, courseMaxStudents, courseIsOpen, courseFormat, coursePrereqs, courseStartTime, courseEndTime, courseScheduledDays, courseLocation, enrolledStudentList)
enrollStudent('SPAN1818', '001L', 'admin')