def get_section(self, section_identity: SectionIdentity) -> Option[Section]: c = self.connection.cursor() term = ( section_identity.course.course_code, section_identity.year, str(section_identity.semester.value), section_identity.section_code, ) c.execute( "SELECT * FROM sections WHERE course=%s AND year=%s AND semester=%s AND " "section_code=%s", term, ) section = None res = c.fetchone() if res: def get_inst(user_name): c.execute("SELECT * FROM instructors WHERE user_name=%s", (str(user_name), )) res = c.fetchone() if res: return Instructor(res[0], res[1], res[3]) return None section = Section( Course(res[0]), res[1], Semester(int(res[2])), res[3], get_inst(res[4]), res[5], ) return maybe(section)
def query_courses(self, filters=None) -> List[Course]: c = self.connection.cursor() # TODO: filters c.execute("SELECT * FROM courses") courses = c.fetchall() if len(courses) > 0: courses = map(lambda x: Course(x[0]), courses) return list(courses)
def get_course(self, course_code: str) -> Option[Course]: c = self.connection.cursor() term = (course_code,) c.execute("SELECT * FROM courses WHERE course_code=%s", term) course = None res = c.fetchone() if res: course = Course(res[0]) return maybe(course)
def query_courses(self, filters=None) -> List[Course]: c = self.connection.cursor() if filters is None: c.execute("SELECT * FROM courses") elif "course_code" in filters: c.execute("SELECT * FROM courses WHERE course_code=%s", (filters["course_code"])) courses = c.fetchall() if len(courses) > 0: courses = map(lambda x: Course(x[0]), courses) return list(courses)
def create_course(self, course_code: str) -> Result[Course, str]: """ Create a new course in the system. Args: course_code: The course code of the course Returns: The new course created """ course = Course(course_code) return self.course_presistence.create_course(course)
def to_section_identity(id): params = id.split(";delimiter;") return SectionIdentity( Course(params[0]), int(params[1]), Semester(int(params[2])), params[3] )
def query_sections(self, filters=None) -> List[Section]: c = self.connection.cursor() if filters is None: c.execute("SELECT * FROM sections") else: terms = [] where_text = "" if "course_code" in filters: if where_text == "": where_text += " WHERE" else: where_text += " AND" where_text += " course=%s" terms.append(str(filters["course_code"])) if "year" in filters: if where_text == "": where_text += " WHERE" else: where_text += " AND" where_text += " year=%s" terms.append(str(filters["year"])) if "semester" in filters: if where_text == "": where_text += " WHERE" else: where_text += " AND" where_text += " semester=%s" terms.append(str(filters["semester"].value)) if "section_code" in filters: if where_text == "": where_text += " WHERE" else: where_text += " AND" where_text += " section_code=%s" terms.append(filters["section_code"]) if "taught_by" in filters: if where_text == "": where_text += " WHERE" else: where_text += " AND" where_text += " taught_by=%s" terms.append(filters["taught_by"]) c.execute("SELECT * FROM sections" + where_text, tuple(terms)) sections = c.fetchall() if len(sections) > 0: def get_inst(user_name): c.execute(f"SELECT * FROM instructors WHERE user_name='{user_name}'") res = c.fetchone() if res: return Instructor(res[0], res[1], res[3]) return None sections = map( lambda res: Section( Course(res[0]), res[1], Semester(int(res[2])), res[3], get_inst(res[4]), res[5], ), sections, ) return list(sections)
def fake_course() -> Course: course_code = fake.pystr() return Course(course_code)