def assignment_id(self, assignment_id: Union[str, ObjectId]): from . import Assignment try: if isinstance(assignment_id, str): ObjectId(assignment_id) else: assignment_id = str(assignment_id) except Exception as e: logger.exception( f"The assignment_id {id} is not of valid format (has to be either bson.objectid.ObjectId or convertible to bson.objectid.ObjectId)" ) raise InvalidFormatException( f"The assignment_id {id} is not of valid format (has to be either bson.objectid.ObjectId or convertible to bson.objectid.ObjectId)" ) try: if Assignment.get_by_id(assignment_id) is None: raise InvalidFormatException( f"The assignment with provided id {id} does not exist") except InvalidFormatException as e: logger.exception(f"Assignment with id {id} does not exist") raise e from InvalidFormatException except Exception as e: logger.exception( f"Error while retrieving Assignment with id {id}: {e}") self._assignment_id = assignment_id
def schedule_time(self, schedule_time: str): if not isinstance(schedule_time, str): raise InvalidTypeException( f"The schedule_time provided is not a str (type provided is {type(schedule_time)}" ) if schedule_time == "": self._schedule_time = "" return if not re.match( r"([0-1][0-9]|2[0-4]):[0-5][0-9]-([0-1][0-9]|2[0-4]):[0-5][0-9]", schedule_time, ): raise InvalidFormatException( f"The format for schedule_time doesn't match. Expected '([0-1][0-9] | 2[0-4]):[0-5][0-9]-([0-1][0-9] | 2[0-4]):[0-5][0-9]', got {schedule_time}" ) start_time, finish_time = schedule_time.split("-") start_time_h, start_time_m = list(map(int, start_time.split(":"))) finish_time_h, finish_time_m = list(map(int, finish_time.split(":"))) if (start_time_h * 60 + start_time_m >= finish_time_h * 60 + finish_time_m) and not (start_time_h == 23 and finish_time_h == 0): raise InvalidFormatException( f"The start time for schedule_time must be earlier than the finish time (got {schedule_time})" ) self._schedule_time = schedule_time
def student_id(self, student_id: Union[str, ObjectId]): from . import Student try: if isinstance(student_id, str): ObjectId(student_id) else: student_id = str(student_id) except Exception as e: logger.exception( f"The student_id {id} is not of valid format (has to be either bson.objectid.ObjectId or convertible to bson.objectid.ObjectId)" ) raise InvalidFormatException( f"The student_id {id} is not of valid format (has to be either bson.objectid.ObjectId or convertible to bson.objectid.ObjectId)" ) try: if Student.get_by_id(student_id) is None: raise InvalidFormatException( f"The Student with provided id {id} does not exist") except InvalidFormatException as e: logger.exception(f"Student with id {id} does not exist") raise InvalidFormatException from e except Exception as e: logger.exception( f"Error while retrieving Student with id {id}: {e}") self._student_id = student_id
def bio(self, bio: str): if not isinstance(bio, str): raise InvalidTypeException( f"The bio provided is not a str (type provided is {type(bio)})." ) if bio == "": self._bio = "A short bio." return if not 0 < len(bio) <= 100: raise InvalidFormatException( f"The string provided is too long. The bio should not exceed 100 characters. (currently: {len(bio)})" ) if not re.match( r'[\w \.\+\(\)\[\]\{\}\?\*\&\^\%\$\#\/\'"~<>,:;!-_=@]{1,100}', bio, flags=re.UNICODE, ): raise InvalidFormatException( r"The format for bio doesn't match. Expected '[\w \.\+\(\)\[\]\{\}\?\*\&\^\%\$\#\/\'\"~<>,:;!-_=@]{1, 500}', got {bio}" .format(bio=bio)) self._bio = bio
def principal(self, principal: str): if not isinstance(principal, str): raise InvalidTypeException( f"The principal name provided is not a str (type provided is {type(principal)})." ) if not 0 < len(principal) <= 100: raise InvalidFormatException( f"The length of the name should not exceed 100 characters (currently: {len(principal)})" ) if not re.match("[\w \.]{1,50}", name, flags=re.UNICODE): raise InvalidFormatException( f"The format for the name doesn't match. Expected only alpha characters, space, or dot, got {principal}" ) self._principal = principal
def school_name(self, school_name: str): if not isinstance(school_name, str): raise InvalidTypeException( f"The school name provided is not a str (type provided is {type(school_name)})." ) if not 0 < len(school_name) <= 100: raise InvalidFormatException( f"The length of the school name should not exceed 100 characters (currently: {len(school_name)})" ) if not re.match("[\w \.]{1,50}", school_name, flags=re.UNICODE): raise InvalidFormatException( f"The format for the name doesn't match. Expected only alpha characters, space, or dot, got {school_name}" ) self._school_name = school_name
def course_analytics(self, analyticsDict: dict): dictKeys = [ "total_average", "starting_average", "no_students", "assignment_history", ] assignmentHistoryKeys = ["assignment_name", "assignment_scores"] for key in dictKeys: if key not in analyticsDict: raise InvalidFormatException( f"The analyticsDict {analyticsDict} is missing the key: {key}" ) for key in assignmentHistoryKeys: if key not in dictKeys["assignment_history"]: raise InvalidFormatException( f"The assignment_history dictionary in analyticsDict {analyticsDict} is missing the key: {key}" )
def id(self, id: Union[str, ObjectId]): try: if isinstance(id, str): ObjectId(id) else: id = str(id) except Exception as e: raise InvalidFormatException( f"The id should be a valid bson.objectid.ObjectId string or object: {e}" ) self._id = id
def description(self, description: str): if not isinstance(description, str): raise InvalidTypeException( f"The description provided is not a str (type provided is {type(description)})" ) if not 0 < len(description) <= 500: raise InvalidFormatException( f"The string provided is too long. The description should not exceed 500 characters. (currently: {len(description)})" ) if not re.match( r'[\w \.\+\(\)\[\]\{\}\?\*\&\^\%\$\#\/\'"~<>,:;!-_=@]{1,500}', description, flags=re.UNICODE, ): raise InvalidFormatException( r"The format for description doesn't match. Expected '[\w \.\+\(\)\[\]\{\}\?\*\&\^\%\$\#\/\'\"~<>,:;!-_=@]{1, 500}', got {description}" .format(description=description)) self._description = description
def number(self, number: int): if not isinstance(number, int): raise InvalidTypeException( f"The course number provided is not an int (type provided is {type(number)})." ) if not 0 < number < 100000: raise InvalidFormatException( f"The format for course number doesn't match. Expected 0 < number < 100000, got {number}" ) self._number = number
def email(self, email: str): if not isinstance(email, str): raise InvalidTypeException( f"The email provided is not a str (type provided is {type(email)})." ) if not re.match( r"^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$", email): raise InvalidFormatException( f"The email given is not in a valid email format (got {email})" ) self._email = email
def date_time_submitted(self, date_time_submitted: Union[datetime, str]): try: if isinstance(date_time_submitted, str): datetime(date_time_submitted) else: date_time_submitted = str(date_time_submitted) except Exception as e: logger.exception( f"date_time_submitted provided is not of a valid datetime.datetime format (got {date_time_submitted})" ) raise InvalidFormatException( f"date_time_submitted provided is not of a valid datetime.datetime format (got {date_time_submitted})" ) self._date_time_submitted = date_time_submitted
def id(self, id: Union[ObjectId, str]): if not isinstance(id, (ObjectId, str)): raise InvalidTypeException( f"The id provided is not a str or bson.objectid.ObjectId (type provided is {type(id)})." ) try: if isinstance(id, str): ObjectId(id) else: id = str(id) except Exception as e: raise InvalidFormatException( f"Cannot convert provided id to bson.ObjectId: {e}") self._id = id
def syllabus(self, syllabus: Tuple[str, str]): if not isinstance(syllabus, tuple): # TODO: logger raise InvalidTypeException( f"The syllabus provided is not a tuple (type provided is {type(syllabus)})" ) if syllabus == (): self._syllabus = syllabus return if (len(syllabus) != 2 or not isinstance(syllabus[0], str) or not isinstance(syllabus[1], str)): # TODO: logger raise InvalidFormatException( f"The format for syllabus does not match: expected Tuple[str, str], got {syllabus}" ) # TODO: add check for a valid syllabus self._syllabus = syllabus
def date_of_birth(self, date_of_birth: str): date_format = "%d-%m-%Y" if not isinstance(date_of_birth, str): raise InvalidTypeException( f"The date of birth provided is not a str (type provided is {type(name)})." ) if date_of_birth == "": self._date_of_birth = "14-03-1879" # Einstein birthdate return try: date_obj = datetime.datetime.strptime(date_of_birth, date_format) except ValueError: raise InvalidFormatException( f"Incorrect data format, should be DD-MM-YYYY (got {date_of_birth})" ) # TODO: check so the date is not in the future self._date_of_birth = date_of_birth