Пример #1
0
def dashboard(env, headers):
    cookies = _get_cookies(env)
    user_id = auth.is_logined(cookies)
    if user_id is not None:
        headers.append(('Location', '/dashboard'))
        post_data = _get_post_data(env)
        search_list = None
        if 'search' in post_data:
            request = post_data['search'][0]
            search_list = ui.search_page({'results': common.search(request)})
        cond_f = 'user_id={}'
        cond = cond_f.format(user_id)
        users_cmps = db.select('cmps', cond)
        user_answers = db.select('user_answers', cond)
        cond_f = 'id={}'
        solved_cmps = []
        used_tasks = {None}
        for user_answer in user_answers:
            cond = cond_f.format(user_answer[2])
            task_id = db.select('questions', cond)[0][2]
            if not (task_id in used_tasks):
                used_tasks.add(task_id)
                cond = cond_f.format(db.select('questions', cond)[0][2])
                solved_cmps += db.select('cmps', cond)
        return ui.dashboard_page({
            'user_id': user_id,
            'search_page': search_list,
            'users_cmps': users_cmps,
            'solved_cmps': solved_cmps,
            'is_op': auth.is_op(user_id)
        })
    else:
        headers.append(('Location', '/'))
    return ''
Пример #2
0
def new_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):
            return ui.create_cmp_page({'user_id': user_id})
        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 ''
Пример #3
0
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 ''
Пример #4
0
def new_questions(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)
            title = post_data['title'][0]
            description = post_data['description'][0]
            qnumber = int(post_data['qnumber'][0])
            return ui.create_questions_page({
                'user_id': user_id,
                'qnumber': qnumber,
                'title': title,
                'description': description})
        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 ''