def answer_question(question_number: int, answer: str, user: UserTable) -> dict: answer = (answer or "").strip() if len(answer) > 1000: return incorrect_answer if not answer: return incorrect_answer question: Questions = get_ques_by_id(question_number) if not question: return no_question(question_number) correct = replace("", question.answer) current = replace("", answer) is_correct = correct == current data_dict = { "user": user.user, "school": user.school, "attempt": current, "is_correct": is_correct, "timestamp": js_time(), "level": question_number, } js = f"{user.user} ({user.school}) tried to answer {user.current_level} with {current} ({'✅' if is_correct else '❌'})" run_threaded_tasks(js, data_dict, is_correct) if is_correct: # no user.current_level = user.current_level + 1 # +=1 yeah user.last_question_answered_at = js_time() save_to_db() return {"result": True, "next_level": user.current_level} else: return incorrect_answer
def __init__(self, token_type: str, token_for: str): if token_type not in ["email_verify", "forgot_password"]: raise Exception("Token not of expected type") self.token_generated_at = js_time() self.token_user = token_for self.token_type = token_type self.token_string = token_hex(8) # sounds good?
def __init__( self, user: str = None, name: str = None, email: str = None, school: str = None, password_hash: str = None, ig_user_id: str = None, is_admin: bool = False, is_disqualified: bool = False, last_question_answered_at: int = 0, has_verified_email: bool = False, ): if any( self.is_invalid_data(x) for x in (user, name, email, password_hash)): raise Exception("Invalid Data") self.user = user.lower() self.password_hash = password_hash self.name = name self.email = validate_email_address(email) self.school = school self.ig_user_id = ig_user_id self.current_level = 0 self.is_admin = is_admin self.is_disqualified = is_disqualified self.has_verified_email = has_verified_email self.last_question_answered_at = (last_question_answered_at or js_time() ) # javascript times in ms
def __init__( self, action_type: str, action_user: str, ip_addr: str, action_timestamp: int, js_dict: dict, ): if not action_type: raise Exception("Invalid data") self.action_type = sanitize(action_type) self.action_timestamp = action_timestamp or js_time() self.ip_addr = ip_addr self.action_user = action_user self._js_dict = dumps(js_dict)
def handler(data: ParsedRequest): action = data.action ip = data.client_ip current = get_current_user() if action == "1": return {"success": "Ok"} js = data.json type_ = js.pop("type") user = current or None log = Logs(type_, user, ip, js_time(), js) add_to_db(log) return {"success": "Ok"} if action == "get": return {} if current is None: return {"error": "Not authenticated"} user = get_user_by_id(current) if user is None or not user.is_admin: return {"error": "Not authenticated"} ret = query_all(Logs) return map_to_list(lambda x: x.as_json, ret)
def answer_question(question_number: int, answer: str, user: UserTable) -> dict: answer = (answer or "").strip() if not answer: return incorrect_answer question: Questions = get_ques_by_id(question_number) if not question: return no_question(question_number) correct = replace("", question.answer) current = replace("", answer) is_correct = correct == current js = f"{user.user} ({user.school}) tried to answer {user.current_level} with {current} ({'✅' if is_correct else '❌'})" if is_correct: post_level_up_webhook(js) else: post_incorrect_webhook(js) if is_correct: # no user.current_level = user.current_level + 1 # +=1 yeah user.last_question_answered_at = js_time() save_to_db() return {"result": True, "next_level": user.current_level} else: return incorrect_answer
def is_expired(self) -> bool: return (js_time() - self.token_generated_at) > THREE_HOURS_JS_TIME