Exemple #1
0
def contest_add_source(request):
    try:
        check_capability(request, 'admin')
        statement_id = request.matchdict['contest_id']
        source_id = request.matchdict['source_id']
        statement_problems = db_session.query(StatementProblem).filter(
            StatementProblem.statement_id == statement_id).order_by(
                StatementProblem.rank).all()
        for i in range(len(statement_problems)):
            letter = ''
            cur = i + 1
            while cur:
                cur -= 1
                letter = chr(ord('A') + cur % 26) + letter
                cur //= 26

            source_add_by_dict({
                'name': 'Задача ' + letter,
                'parent_id': source_id,
                'order': (i + 1) * 10**7,
                'author': RequestGetUserId(request),
                'verified': True,
                'problem_id': statement_problems[i].problem_id
            })
        db_session.commit()
        return {"result": "ok"}
    except Exception as e:
        return {"result": "error", "content": e.__str__()}
def problem_add_source(request):
    try:
        check_capability(request, 'user')
        problem_id = request.matchdict['problem_id']
        subject_id = request.matchdict['subject_id']
        problem = problem_get_by_id(problem_id)
        subject = source_get_by_id(subject_id)
        if subject.get_type() != '_subject':
            check_capability(request, 'admin')
        cur_subject = db_session.query(Source).filter(
            Source.parent_id == subject_id,
            Source.problem_id == problem_id).all()
        if cur_subject:
            raise Exception('Already exists')
        verified = check_capability_ex(request, 'edit')
        add_dict = {
            'name': problem.name,
            'parent_id': subject_id,
            'order': 0,
            'problem_id': problem_id,
            'author': RequestGetUserId(request),
            'verified': verified
        }
        source_add_by_dict(add_dict)
        db_session.commit()
        return {'result': 'ok', 'verified': verified}
    except Exception as e:
        return {'result': 'error', 'content': e.__str__()}
def contest_add_source(request):
    try:
        check_capability(request, 'admin')
        statement_id = request.matchdict['contest_id']
        source_id = request.matchdict['source_id']
        statement_problems = db_session.query(StatementProblem).filter(
                StatementProblem.statement_id == statement_id).order_by(StatementProblem.rank).all()
        for i in range(len(statement_problems)):
            letter = ''
            cur = i + 1
            while cur:
                cur -= 1
                letter = chr(ord('A') + cur % 26) + letter
                cur //= 26

            source_add_by_dict({
                'name': 'Задача ' + letter,
                'parent_id': source_id,
                'order': (i + 1) * 10**7,
                'author': RequestGetUserId(request),
                'verified': True,
                'problem_id': statement_problems[i].problem_id
            })
        db_session.commit()
        return {"result": "ok"}
    except Exception as e:
        return {"result": "error", "content": e.__str__()}
def problem_add_source(request):
    try:
        check_capability(request, 'user')
        problem_id = request.matchdict['problem_id']
        subject_id = request.matchdict['subject_id']
        problem = problem_get_by_id(problem_id)
        subject = source_get_by_id(subject_id)
        if subject.get_type() != '_subject':
            check_capability(request, 'admin')
        cur_subject = db_session.query(Source).filter(Source.parent_id == subject_id, Source.problem_id == problem_id).all()
        if cur_subject:
            raise Exception('Already exists')
        verified = check_capability_ex(request, 'edit')
        add_dict = {
            'name': problem.name,
            'parent_id': subject_id,
            'order': 0,
            'problem_id': problem_id,
            'author': RequestGetUserId(request),
            'verified': verified
        }
        source_add_by_dict(add_dict)
        db_session.commit()
        return {'result': 'ok', 'verified': verified}
    except Exception as e:
        return {'result': 'error', 'content': e.__str__()}
def source_dir_contest_new(request):
    try:
        check_capability(request, 'manage_contest')
        contest_id, course_id = map(int, request.matchdict['contest'].split('-'))
        request.session['basket'] = Basket(contest_id, course_id)
        return exc.HTTPFound(request.route_url('source.dir.home'))
    except Exception as e:
        return exc.HTTPForbidden() if e.__str__() == 'Access denied'else exc.HTTPInternalServerError()
def source_verify(request):
    try:
        check_capability(request, 'edit')
        source = source_get_by_id(request.matchdict['source_id'])
        source.verified = True
        db_session.commit()
        return {'result': 'ok'}
    except Exception as e:
        return {'result': 'error', 'content': e.__str__()}
def source_verify(request):
    try:
        check_capability(request, 'edit')
        source = source_get_by_id(request.matchdict['source_id'])
        source.verified = True
        db_session.commit()
        return {'result': 'ok'}
    except Exception as e:
        return {'result': 'error', 'content': e.__str__()}
def source_dir_contest_new(request):
    try:
        check_capability(request, 'manage_contest')
        contest_id, course_id = map(int,
                                    request.matchdict['contest'].split('-'))
        request.session['basket'] = Basket(contest_id, course_id)
        return exc.HTTPFound(request.route_url('source.dir.home'))
    except Exception as e:
        return exc.HTTPForbidden() if e.__str__(
        ) == 'Access denied' else exc.HTTPInternalServerError()
def source_erase(request):
    try:
        check_capability(request, 'edit')
        source = source_get_by_id(request.matchdict['source_id'])
        if source.get_type() != '_subject' or not source.problem_id:
            check_capability(request, 'admin')
        db_session.delete(source)
        db_session.commit()
        return {"result": "ok"}
    except Exception as e:
        return {"result": "error", "content": e.__str__()}
Exemple #10
0
def source_verify_cancel(request):
    try:
        check_capability(request, 'edit')
        source = source_get_by_id(request.matchdict['source_id'])
        if source.verified:
            raise Exception("Source mustn't be canceled")
        db_session.delete(source)
        db_session.commit()
        return {'result': 'ok'}
    except Exception as e:
        return {'result': 'error', 'content': e.__str__()}
def source_erase(request):
    try:
        check_capability(request, 'edit')
        source = source_get_by_id(request.matchdict['source_id'])
        if source.get_type() != '_subject' or not source.problem_id:
            check_capability(request, 'admin')
        db_session.delete(source)
        db_session.commit()
        return {"result": "ok"}
    except Exception as e:
        return {"result": "error", "content": e.__str__()}
def source_verify_cancel(request):
    try:
        check_capability(request, 'edit')
        source = source_get_by_id(request.matchdict['source_id'])
        if source.verified:
            raise Exception("Source mustn't be canceled")
        db_session.delete(source)
        db_session.commit()
        return {'result': 'ok'}
    except Exception as e:
        return {'result': 'error', 'content': e.__str__()}
def source_update(request):
    try:
        check_capability(request, 'admin')
        source = source_get_by_id(request.matchdict['source_id'])
        for field in request.params:
            if field in ['name', 'parent_id', 'order', 'problem_id', 'verified']:
                source.__setattr__(field, request.params[field])
        source.author = RequestGetUserId(request)
        db_session.commit()
        return {"result": "ok"}
    except Exception as e:
        return {"result": "error", "content": e.__str__()}
Exemple #14
0
def source_update(request):
    try:
        check_capability(request, 'admin')
        source = source_get_by_id(request.matchdict['source_id'])
        for field in request.params:
            if field in [
                    'name', 'parent_id', 'order', 'problem_id', 'verified'
            ]:
                source.__setattr__(field, request.params[field])
        source.author = RequestGetUserId(request)
        db_session.commit()
        return {"result": "ok"}
    except Exception as e:
        return {"result": "error", "content": e.__str__()}
Exemple #15
0
def source_dir_contest_create(request):
    try:
        check_capability(request, 'manage_contest')
        basket = request.session['basket']
        for i in range(len(basket.problems)):
            db_session.add(
                StatementProblem(basket.contest_id, basket.problems[i], i + 1))
        db_session.commit()
        course_id = basket.course_id
        del request.session['basket']
        return exc.HTTPFound(location='/moodle/mod/statements/view.php?id=' +
                             str(course_id))
    except Exception as e:
        return exc.HTTPForbidden() if e.__str__(
        ) == 'Access denied' else exc.HTTPInternalServerError()
def source_erase_all(request):
    def erase_tree(source):
        if not source:
            return
        for child in source.children:
            erase_tree(child)
        db_session.delete(source)
    try:
        check_capability(request, 'admin')
        source = source_get_by_id(request.matchdict['source_id'])
        erase_tree(source)
        db_session.commit()
        return {"result": "ok"}
    except Exception as e:
        return {"result": "error", "content": e.__str__()}
Exemple #17
0
def source_erase_all(request):
    def erase_tree(source):
        if not source:
            return
        for child in source.children:
            erase_tree(child)
        db_session.delete(source)

    try:
        check_capability(request, 'admin')
        source = source_get_by_id(request.matchdict['source_id'])
        erase_tree(source)
        db_session.commit()
        return {"result": "ok"}
    except Exception as e:
        return {"result": "error", "content": e.__str__()}
def source_dir_contest_create(request):
    try:
        check_capability(request, 'manage_contest')
        basket = request.session['basket']
        for i in range(len(basket.problems)):
            db_session.add(StatementProblem(
                basket.contest_id, 
                basket.problems[i], 
                i + 1
        ))
        db_session.commit()
        course_id = basket.course_id
        del request.session['basket']
        return exc.HTTPFound(location='/moodle/mod/statements/view.php?id=' + str(course_id))
    except Exception as e:
        return exc.HTTPForbidden() if e.__str__() == 'Access denied' else exc.HTTPInternalServerError()
def source_add(request):
    try:
        check_capability(request, 'admin')
        add_dict = dict(request.params)
        names = request.params['name'].split('\n')
        order, order_right = map(int, request.params['order'].split(':'))
        order_shift = (order_right - order) / (len(names) + 1)
        for name in names:
            order += order_shift
            add_dict['name'] = name
            add_dict['order'] = order
            add_dict['verified'] = True
            add_dict['author'] = RequestGetUserId(request)
            source_add_by_dict(add_dict)
        db_session.commit()
        return {"result": "ok"}
    except Exception as e:
        return {"result": "error", "content": e.__str__()}
Exemple #20
0
def source_add(request):
    try:
        check_capability(request, 'admin')
        add_dict = dict(request.params)
        names = request.params['name'].split('\n')
        order, order_right = map(int, request.params['order'].split(':'))
        order_shift = (order_right - order) / (len(names) + 1)
        for name in names:
            order += order_shift
            add_dict['name'] = name
            add_dict['order'] = order
            add_dict['verified'] = True
            add_dict['author'] = RequestGetUserId(request)
            source_add_by_dict(add_dict)
        db_session.commit()
        return {"result": "ok"}
    except Exception as e:
        return {"result": "error", "content": e.__str__()}