def update_description(self, description: str) -> bool: r"""Updates the description for this course. Method should only be called on the courses that are already initialized and pushed to the DB. Parameters ---------- description : str Returns ------- bool `True` if the update operation was successful, `False` otherwise """ try: self.description = description db.courses.find_one_and_update( {"_id": self.id}, {"$set": { "description": self.description }}) return True except Exception as e: logger.exception( f"Error while updating description {description} in class {self.id}" ) return False
def update_department_description(self, department_description: list) -> bool: r"""Updates the department descriptions. Parameters ---------- department_description : list Returns ------- bool `True` if the update operation was successful, `False` otherwise """ try: self.department_description = department_description db.general_info.find_one_and_update({ "_id": self.id, "$set": { "department_description": self.department_description }, }) return True except Exception as e: logger.exception( f"Error while updating department descriptions {department_description}: {e}" ) return False
def update_schedule_days(self, schedule_days: str) -> bool: r"""Updates the schedule days for this course. Method should only be called on the courses that are already initialized and pushed to the DB. Parameters ---------- schedule_days : str Returns ------- bool `True` if the update operation was successful, `False` otherwise """ try: self.schedule_days = schedule_days db.courses.find_one_and_update( {"_id": self.id}, {"$set": { "schedule_days": self.schedule_days }}) return True except Exception as e: logger.exception( f"Error while updating schedule_days {schedule_days} in class {self.id}" ) return False
def update_school_settings(config: SchoolConfig): try: dictionary = config.to_dict() school_name = dictionary["school_name"] school_address = dictionary["school_address"] phone_number = dictionary["phone_number"] school_email = dictionary["school_email"] principal = dictionary["principal"] principal_email = dictionary["principal_email"] departments = dictionary["departments"] department_description = dictionary["deparment_description"] grade_weights = dictionary["grade_weights"] grading = dictionary["grading"] SchoolConfig.update( school_name, school_address, phone_number, school_email, principal, principal_email, departments, department_description, grade_weights, grading, ) return True except: logger.exception( f"An error occured while updating school settings") return False
def update_phone_number(self, phone_number: str) -> bool: r"""Updates the school's adress. Parameters ---------- phone_number : str Returns ------- bool `True` if the update operation was successful, `False` otherwise """ try: self.phone_number = phone_number db.general_info.find_one_and_update({ "_id": self.id, "$set": { "phone_number": self.phone_number } }) return True except Exception as e: logger.exception( f"Error while updating school phone number {phone_number}: {e}" ) return False
def update_principal_email(self, principal_email: str) -> bool: r"""Updates the principal's email. Parameters ---------- principal_email : str Returns ------- bool `True` if the update operation was successful, `False` otherwise """ try: self.principal_email = principal_email db.general_info.find_one_and_update({ "_id": self.id, "$set": { "principal_email": self.principal_email } }) return True except Exception as e: logger.exception( f"Error while updating principal's email {principal_email}: {e}" ) return False
def update_grading(self, grading: list) -> bool: r"""Updates the grading system. Parameters ---------- grading : list Returns ------- bool `True` if the update operation was successful, `False` otherwise """ try: self.grading = grading db.general_info.find_one_and_update({ "_id": self.id, "$set": { "grading": self.grading } }) return True except Exception as e: logger.exception( f"Error while updating grading system {grading}: {e}") return False
def update_school_address(self, school_address: str) -> bool: r"""Updates the school's adress. Parameters ---------- school_address : str Returns ------- bool `True` if the update operation was successful, `False` otherwise """ try: self.school_address = school_address db.general_info.find_one_and_update({ "_id": self.id, "$set": { "school_address": self.school_address } }) return True except Exception as e: logger.exception( f"Error while updating school adresss {school_address}: {e}") return False
def update_grade_range(self, grade_range: Tuple[int, int]) -> bool: """Update the grade range for this course Parameters ---------- grade_range : Tuple[int, int] The grade range (min, max) Returns ------- bool `True` if operation was a success. `False` otherwise """ try: self.grade_range = grade_range db.courses.find_one_and_update( {"_id": self._id}, {"$set": { "grade_range": self.grade_range }}) return True except: logger.exception( f"Error while updating grade_range {grade_range} in class {self.id}: {e}" ) return False
def update_students(self, student_ids: List) -> bool: r"""Updates the students for this course. Method should only be called on the courses that are already initialized and pushed to the DB. Parameters ---------- student_ids : List Returns ------- bool `True` if the update operation was successful, `False` otherwise """ for _id in student_ids: try: db.students.find_one({"_id": ObjectId(_id)}, {"$push": { "courses": self.id }}) db.courses.find_one({"_id": self.id}, {"$push": { "students": ObjectId(_id) }}) except Exception as e: logger.exception( f"Error while updating student {_id} in class {self.id}: {e}" ) return False return True
def update_syllabus(self, syllabus: Tuple[str, str]) -> bool: r"""Updates the syllabus for this course. Method should only be called on the courses that are already initialized and pushed to the DB. Parameters ---------- syllabus : Tuple[str, str] The syllabus in the format (syllabus_id, syllabus_filename) Returns ------- bool `True` if the update operation was successful, `False` otherwise """ try: self.syllabus = syllabus db.courses.find_one_and_update( {"_id": self._id}, {"$set": { "syllabus": self.syllabus }}) return True except: logger.exception( f"Error while updating syllabus {syllabus} in class {self.id}") return False
def update_grade_weights(self, grade_weights: bool) -> bool: r"""Updates the grade weights. Parameters ---------- grade_weights : bool Returns ------- bool `True` if the update operation was successful, `False` otherwise """ try: self.grade_weights = grade_weights db.general_info.find_one_and_update({ "_id": self.id, "$set": { "grade_weights": self.grade_weights } }) return True except Exception as e: logger.exception( f"Error while updating grade weights {grade_weights}: {e}") return False
def update_teacher(self, teacher_id: str) -> bool: r"""Updates the teacher for this course. Method should only be called on the courses that are already initialized and pushed to the DB. Parameters ---------- teacher_id : str Returns ------- bool `True` if the update operation was successful, `False` otherwise """ try: self.teacher = teacher_id db.courses.find_one_and_update( {"_id": self.id}, {"$set": { "teacher": ObjectId(self.teacher) }}) return True except Exception as e: logger.exception( f"Error while updating teacher {teacher_id} in class {self.id}" ) return False
def teacher(self, teacher_id: Union[str, ObjectId]): from api.classes import Teacher if isinstance(teacher_id, ObjectId): teacher_id = str(teacher_id) if not isinstance(teacher_id, str): raise InvalidTypeException( f"The teacher_id provided is not a str (type provided is {type(teacher_id)})." ) if teacher_id == "": self._teacher = teacher_id return try: ObjectId(teacher_id) except Exception as e: logger.exception(f"Error while validating teacher id {teacher_id}") raise e try: if Teacher.get_by_id(teacher_id) is None: raise Exception( f"The teacher with id {teacher_id} does not exist.") except Exception as e: logger.exception( f"Error while validating the existence of teacher {teacher_id}" ) raise e self._teacher = teacher_id
def remove(self) -> bool: """Remove this course from the database""" try: db.courses.remove({"_id": self.id}) return True except Exception as e: logger.exception(f"Error while deleting course {_id}") return False
def remove(self) -> bool: r"""Removes this admin from the database.""" try: db.admins.delete_one({"_id": ObjectId(self.id)}) except Exception as e: logger.exception(f"Error while removing Admin {self.id}") return False else: return True
def remove(self) -> bool: r"""Removes this teacher from the database.""" try: db.teachers.delete_one({"_id": ObjectId(self.id)}) except: logger.exception(f"Error while removing Teacher {self.id}") return False else: return True
def add(self) -> bool: r"""Adds the student to the DB.""" try: self.id = db.students.insert_one(self.to_dict()).inserted_id except Exception as e: logger.exception(f"Error while adding Student {self.id}") return False else: return True
def add(self) -> bool: """Add this course to the database""" try: self.id = db.courses.insert_one(self.to_dict()).inserted_id return True except pymongo.errors.DuplicateKeyError: logger.exception( f"The Course with the id {self.id} already exists, you should not be calling the add() method." ) return False except Exception as e: logger.exception(f"Error while adding course {self.to_dict()}") return False
def update(self, **kwargs): r"""Updates the school's data. Parameters ---------- school_name: str, optional school_address: str, optional phone_number: str, optional school_email: str, optional principal: str, optional principal_email: str, optional departments: List[str], optional department_description: List[str], optional grade_weights: bool, optional grading: List[str], optional Returns ------- bool `True` if all update operations were successful, `False` otherwise Notes ----- For all the data formats please refer to `SchoolConfig.__init__` docstrings. **Important**: to avoid confusion, we suggest to avoid using positional parameters when calling this method. """ PARAMETER_TO_METHOD = { "school_name": self.update_school_name, "school_adress": self.update_school_address, "phone_number": self.update_phone_number, "school_email": self.update_school_email, "principal": self.update_principal, "principal_email": self.update_principal_email, "departments": self.update_departments, "department_description": self.update_department_description, "grade_weights": self.update_grade_weights, "grading": self.update_grading, } # Go through all the parameters that are None for key, value in kwargs.items(): response = PARAMETER_TO_METHOD[key](value) if not response: logger.exception( f"Error while updating school information attribute:{parameter} value:{value}" ) return False return True
def add(self) -> bool: r"""Adds the teacher to the DB.""" try: self.id = db.teachers.insert_one(self.to_dict()).inserted_id except pymongo.errors.DuplicateKeyError: logger.exception( f"The Teacher with the id {self.id} already exists, you should not be calling the add() method." ) return False except: logger.exception(f"Error while adding Teacher {self.id}") return False else: return True
def remove(self) -> bool: r"""Removes this student from the database.""" try: db.students.delete_one({"_id": ObjectId(self.id)}) except pymongo.errors.DuplicateKeyError: logger.exception( f"The Student with the id {self.id} already exists, you should not be calling the add() method." ) return False except Exception as e: logger.exception(f"Error while removing Student {self.id}") return False else: return True
def get_by_email(email: str) -> Student: r"""Returns Student with a specified email. Parameters --------- email: str Returns ------ Student """ try: return Student.from_dict(db.students.find_one({"email": email})) except BaseException as e: logger.exception(f"Error while getting a student by email {id}") return None
def get_by_email(email: str) -> Parent: r"""Returns Parent with a specified email. Parameters --------- email : str Returns ------ Parent """ try: return Parent.from_dict(db.parents.find_one({"email": email})) except: logger.exception(f"Error when returning Parent by email {email}") return None
def get_by_id(id: str) -> Parent: r"""Returns a Parent object with a specified id. Parameters --------- id : str ID to look up in the database Returns ------- Parent """ try: return Parent.from_dict(db.parents.find_one({"_id": ObjectId(id)})) except: logger.exception(f"Error when returning Parent by id {id}") return None
def from_dict(dictionary: dict) -> Admin: r"""Creates an Admin object from a dictionary. Parameters ---------- dictionary: dict """ if dictionary is None: return None try: return Admin(**dictionary) except Exception as e: logger.exception( f"Error while generating an Admin from dictionary {dictionary}" ) return None
def get_by_id(id: str) -> Student: r"""Returns a Student object with a specified id. Parameters --------- id: str ID to look up in the database Returns ------- Student """ try: return Student.from_dict( db.students.find_one({"_id": ObjectId(id)})) except BaseException as e: logger.exception(f"Error while getting a student by id {id}") return None
def add_assignment(self, assignment: Assignment): """Add an assignment to this course Parameters ---------- assignment : Assignment The assignment to add """ try: dictionary = assignment.to_dict() dictionary["_id"] = ObjectId() db.courses.find_one_and_update( {"_id": self._id}, {"$push": { "assignments": dictionary }}) except: logger.exception( f"Error while adding assignment {assignment._id} to course {self._id}" )
def delete_assignment(self, assignment_id: str): """Delete an assignment from this course Parameters ---------- assignment_id : str The ID of the assignment """ try: db.courses.update( {"_id": self._id}, {"$pull": { "assignments": { "_id": assignment_id } }}) except: logger.exception( f"Error while deleting assignment {assignment_id} from class {self._id}" )
def get_by_keyword(keyword: str) -> Teacher: r"""Returns Teacher with a specified keyword. Parameters --------- first_name: str Returns ------ List[Teacher] """ try: teachers = db.teachers.aggregate([ { "$search": { "autocomplete": { "query": keyword, "path": "first_name" } } }, { "$project": { "_id": 1, "first_name": 1, "last_name": 1 } }, { "$limit": 5 }, ]) possible_teachers = [] for teacher in teachers: possible_teachers.append(Teacher.from_dict(teacher)) return possible_teachers except BaseException as e: logger.exception( f"Error while getting a teacher by name {id}: {e}") return None