def make_random_pin(): db = QuizDB() pins = db.retrievePins() new_pin = randint(1000, 9999) for pin in pins: if pin == new_pin: new_pin = randint(9000, 9999) return new_pin
def handleGetQuiz(self): # parts = self.path.split("/") # quiz_id = parts[2] self.send_response(200) self.send_header("Content-Type", "application/json") self.send_header("Access-Control-Allow-Origin", "*") self.end_headers() db = QuizDB() quiz = db.retrieveAllQuiz() self.wfile.write(bytes(json.dumps(quiz), "utf-8"))
def handleRetrieveQuestions(self): parts = self.path.split("/") quiz_id = parts[2] db = QuizDB() questions = db.retrieveQuestions(quiz_id) print(questions) if questions != None: self.send_response(200) self.send_header("Content-Type", "application/json") self.send_header("Access-Control-Allow-Origin", "*") self.end_headers() self.wfile.write(bytes(json.dumps(questions), "utf-8")) else: self.handleNotFound()
def handleQuizCreate(self): self.send_response(201) self.send_header("Content-Type", "application/json") self.send_header("Access-Control-Allow-Origin", "*") self.end_headers() length = self.headers["Content-Length"] body = self.rfile.read(int(length)).decode("utf-8") print("BODY (string):", body) parsed_body = parse_qs(body) print("BODY (parsed):", parsed_body) title = parsed_body["title"][0] pin = parsed_body["pin"][0] db = QuizDB() db.createQuiz(pin, title)
def handleCreateQuestion(self): self.send_response(201) self.send_header("Content-Type", "application/json") self.send_header("Access-Control-Allow-Origin", "*") self.end_headers() length = self.headers["Content-Length"] body = self.rfile.read(int(length)).decode("utf-8") print("BODY (string):", body) parsed_body = parse_qs(body) print("BODY (parsed):", parsed_body) question = parsed_body["question"][0] answer1 = parsed_body["answer1"][0] answer2 = parsed_body["answer2"][0] answer3 = parsed_body["answer3"][0] answer4 = parsed_body["answer4"][0] correct_answer = parsed_body["correct_answer"][0] quiz_id = parsed_body["quiz_id"][0] db = QuizDB() db.insertQuestion(question, answer1, answer2, answer3, answer4, correct_answer, quiz_id)
def __init__(self): self.database = QuizDB('db')
class Quiz: def __init__(self): self.database = QuizDB('db') # This file deals with the actual quiz-taking logic # Request should be pre-validated # Returns a dict def get_quiz(self, quiz_id, start_at, prepare=True): name = os.path.join(config.basedir, '../quiz', quiz_id + '.json') try: with open(name) as f: s = f.read() d = json.loads(s) if prepare: return self.prepare_quiz(d, start_at) else: return d except IOError: print('Error opening ' + name + ': file does not exist') return {} # Strips answers from quiz to send to client # Accepts and returns a dict def prepare_quiz(self, quiz_dict, cur): for question in quiz_dict["questions"]: question.pop("answer", None) for i in range(cur): quiz_dict["questions"][i]["answered"] = 1 return quiz_dict # Adds the user's answer for one question to self.database def handle_submission(self, request): if not self.database.exists(request["username"], request["quiz_id"]): self.database.add(request["username"], request["quiz_id"]) if int(request["question_number"]) == self.questions_answered( request["username"], request["quiz_id"]): submission_so_far = self.database.get_submission( request["username"], request["quiz_id"]) # answer_to_add = {"submission": request["submission"], # "metadata": request["metadata"]} answer_to_add = request submission_so_far["submissions"].append(answer_to_add) self.database.write_submission(request["username"], request["quiz_id"], submission_so_far) return True else: print("Invalid submission -- out of order answers") return False # Returns the number of questions a user has answered so far def questions_answered(self, username, quiz_id): n = 0 try: n = len( TRACE( self.database.get_submission(username, quiz_id)["submissions"])) except KeyError: pass return n