def cmp_solve(env, headers, cmp_id): cookies = _get_cookies(env) user_id = auth.is_logined(cookies) if user_id is not None: sql_str = 'id={}' sql_str = sql_str.format(cmp_id) rows = db.select('CMPS', sql_str) if rows: post_data = _get_post_data(env) format_a = 'answer-{}' sql_str = 'cmp_id={}' sql_str = sql_str.format(cmp_id) answers = ( (r[0], common.escape(post_data[format_a.format(r[0])][0])) for r in db.select('QUESTIONS', sql_str)) username = db.username_by_id(user_id) solver.save_answers(username, answers, cmp_id) headers.append(('Location', '/quiz/{}/results'.format(cmp_id))) else: err_msg = "User ID {} tried to solve non-existing cmp {}" err_msg = err_msg.format(str(user_id), str(cmp_id)) common.dbg_log(err_msg) return (ui.error_page({'error_msg': '404: Competition not found'}), '404 Not Found') else: headers.append(('Location', '/')) return ''
def cmp_page(env, headers, cmp_id): cookies = _get_cookies(env) user_id = auth.is_logined(cookies) if user_id is not None: sql_str = 'id={}' sql_str = sql_str.format(cmp_id) rows = db.select('CMPS', sql_str) if rows and len(rows): row = rows[0] if common.is_user_solve_cmp(cmp_id, user_id): headers.append(('Location', '/quiz/{}/results'.format(cmp_id))) else: username = db.username_by_id(user_id) sql_str = 'cmp_id={}' sql_str = sql_str.format(cmp_id) questions = [(r[0], r[1]) for r in db.select('QUESTIONS', sql_str)] return ui.solve_page({ 'cmp_id': row[0], 'title': row[1], 'description': row[2], 'user_name': username, 'questions': questions}) else: err_msg = "User ID {} tried to solve non-existing cmp {}" err_msg = err_msg.format(str(user_id), str(cmp_id)) common.dbg_log(err_msg) return (ui.error_page({'error_msg': '404: Competition not found'}), '404 Not Found') else: headers.append(('Location', '/')) return ''
def cmp_participants_page(data): """data user_id - int - Currently logined user id user_name - str - Currently logined user name(only if user_id is not given) cmp_id - int - ID of this competition title - str - Title of created competition description - str - Description of this competition users - list of cortages: [user_id_1, user_id_2, ...] EXAMPLE: [2, 45, 67] """ username = get_username(data) html = get_html(HTML_FOLDER + "participants.html") htpl = get_html(HTML_FOLDER + "participants_tpl.html") html = prepare(html) hparts = "\n".join( htpl.format( uid=j, uname=db.username_by_id(j), cmp_id=data['cmp_id'] ) for i, j in enumerate(data['users']) ) html = html.format( username=username, part_list=hparts, title=data["title"], description=data["description"] ) return html
def get_username(data): username = "" if "user_name" in data: username = data["user_name"] elif "user_id" in data: username = db.username_by_id(data["user_id"]) else: username = "******" return username
def cmp_results(env, headers, cmp_id): cookies = _get_cookies(env) user_id = auth.is_logined(cookies) logineduid = user_id if user_id is not None: cond_f = 'id={}' cond = cond_f.format(cmp_id) rows = db.select('CMPS', cond) if rows: row = rows[0] qs = _get_query_data(env) is_cmp_author = False if 'uid' in qs: uid = int(qs['uid'][0]) if (uid != user_id) and (row[3] == user_id): user_id = uid is_cmp_author = True if common.is_user_solve_cmp(cmp_id, user_id): username = db.username_by_id(logineduid) cond_f = 'cmp_id={}' cond = cond_f.format(cmp_id) tasks = db.select('questions', cond) answers = common.user_answers_for_cmp(cmp_id, user_id) restuple = solver.is_cmp_right(cmp_id, answers) results = [ (task[0], task[1], ans[0], ans[3], res) for task, ans, res in zip(tasks, answers, restuple)] return ui.results_page({ 'cmp_id': row[0], 'title': row[1], 'description': row[2], 'user_name': username, 'results': results, 'is_cmp_author': is_cmp_author}) else: headers.append(('Location', '/quiz/{}'.format(cmp_id))) err_msg = "User ID {} tried to see result for non-solved cmp {}" err_msg = err_msg.format(str(user_id), str(cmp_id)) common.dbg_log(err_msg) else: err_msg = "User ID {} tried to view result for non-existing cmp {}" err_msg = err_msg.format(str(user_id), str(cmp_id)) common.dbg_log(err_msg) return (ui.error_page({'error_msg': '404: Competition not found'}), '404 Not Found') else: headers.append(('Location', '/')) return ''
def create_cmp(env, headers): cookies = _get_cookies(env) user_id = auth.is_logined(cookies) if user_id is not None: if auth.is_op(user_id): post_data = _get_post_data(env) checks = ('title' in post_data) and ('description' in post_data) if checks and ('qnumber' in post_data): title = common.escape(post_data['title'][0]) description = common.escape(post_data['description'][0]) qnumber = int(common.escape(post_data['qnumber'][0])) format_q = 'question-{}' format_a = 'answer-{}' tasks = [] for i in range(qnumber): question_tpl = format_q.format(i) answer_tpl = format_a.format(i) if (answer_tpl in post_data) and (question_tpl in post_data): q = post_data[question_tpl][0] a = post_data[answer_tpl][0] answers = common.escape(a).split('##') answers = [j for j in answers if j] tasks.append( tuple( [common.escape(q)] + answers )) username = db.username_by_id(user_id) creator.create(title, description, username, tuple(tasks)) headers.append(('Location', '/dashboard')) else: err_msg = "User ID {} tried to create cmp without op" err_msg = err_msg.format(str(user_id)) common.dbg_log(err_msg) em = '403: You don\'t have permissions to create competition' return (ui.error_page({'error_msg': em}), '403 Forbidden') else: headers.append(('Location', '/')) return ''