def get_task_description(self, task_id: int) -> Task: """ Get task information (title, descriptions, etc...) args: task_id(int) : Task id that you'd like to fetch returns: info(TaskInfo) : Information object """ conn = get_connection() cur = conn.cursor(dictionary=True) try: cur.execute( "SELECT query, title, description, search_url" + " FROM tasks WHERE id = %s" % (task_id,) ) rs = cur.fetchone() return rs except Exception as e: print(f"[ERROR] {e}") return Task() finally: cur.close() conn.close()
def create_answer(self, answer: Answer) -> None: """ Insert user's answer to DB args: answer(Answer) : User's task answer and reason returns: None """ conn = get_connection() cur = conn.cursor() try: cur.execute( "INSERT INTO" + " answers(user_id, task_id, condition_id, answer, reason)" + " VALUES('%s', '%s', '%s', '%s', '%s')" % ( answer.user_id, answer.task_id, answer.condition_id, answer.answer, answer.reason, ) ) except Exception as e: print(f"[ERROR] {e}") else: conn.commit() finally: cur.close() conn.close()
def create_user(self, params: UserParam) -> UserSimple: """ Create user. args: params(UserParam) : User parameter for creation returns: user(UserSimple) : Simple user information """ connection = get_connection() cur = connection.cursor(dictionary=True) try: cur.execute( """ INSERT INTO user VALUES( ? ) """, params.external_id, ) res = cur.fetchall() return res[-1] except Exception: raise finally: cur.close() connection.close()
def get_serp(self, task_id: int, offset: int) -> List[SearchResult]: """ Get search result pages by given task INIT_DB args: task_id(int) : Task ID offset(int) : Offset number of pages returns: serps(List[Serp]) : Listed result page objects """ conn = get_connection() cur = conn.cursor(dictionary=True) try: cur.execute( "SELECT * FROM search_pages WHERE task_id = %s LIMIT %s, 10" % ( task_id, offset * 10, ) ) result = cur.fetchall() return result except Exception as e: print(f"[ERROR] {e}") return [] finally: cur.close() conn.close()
def get_completion_code(self, user_id: int) -> str: """ Get user's completion code from DB. args: user_id(int) : User ID that you'd like to get returns: completion_code(str) : Completion code """ connection = get_connection() cur = connection.cursor(dictionary=True) try: cur.execute( """ SELECT completion_code FROM completion_codes RIGHT JOIN users ON users.id = completion_codes.uid WHERE users.id = ? """, user_id, ) res = cur.fetchall() if len(res) != 1: print("[WARNING] User Id duplication is detected!!") return res[-1]["completion_code"] except Exception: raise finally: cur.close() connection.close()