Exemplo n.º 1
0
def course_my_categories(request):
    try:
        frame = int(request.params.get('frame', 1))
        course_root = db_session.query(Course).filter(Course.id == 1).one()
        course_list = []
        make_course_list(course_root, course_list, 0)
        my_nodes = db_session.query(Course).filter(
            Course.id != 1,
            Course.course_id == 0,
            Course.author == RequestGetUserId(request),
        ).order_by(Course.verified).all()
        my_verified_nodes = [node for node in my_nodes if node.verified]
        my_unverified_nodes = [node for node in my_nodes if not node.verified]
        root_nodes = course_tree_get_root_nodes(RequestGetUserId(request))
        '''
        my_nodes = [node for node in my_nodes if node in root_nodes] + \
                    [node for node in my_nodes if node not in root_nodes]
        '''
        return {
            'frame': frame,
            'course_list': course_list,
            'my_nodes': my_nodes,
            'my_unverified_nodes': my_unverified_nodes,
            'root_nodes': root_nodes,
            'default_storage': json.dumps({}),
            'GetNodeUsers': GetNodeUsers,
        }
    except Exception as e:
        return Response("Error: " + e.__str__())
Exemplo n.º 2
0
def course_add(request):
    try:
        add_dict = dict(request.params)
        names = request.params['name'].split('\n')
        order = request.params['order']
        parent_id = int(request.params['parent_id'])
        user_id = int(RequestGetUserId(request))
        if order == 'end':
            parent = db_session.query(Course).filter(
                Course.id == request.params['parent_id']).one()
            max_order = max(course.order for course in parent.children) \
                if list(parent.children) else 0
            order = "{0}:{1}".format(max_order, max_order + 10**7)
        order, order_right = map(int, order.split(':'))
        order_shift = (order_right - order) / (len(names) + 1)
        ids = []
        for name in names:
            vals = name.split("+++")
            title, course_id = name, 0
            if len(vals) == 2:
                title, course_id = vals
            similar = db_session.query(Course).filter(
                Course.parent_id == request.params['parent_id'],
                Course.course_id == course_id,
            ).all()
            verified = check_capability_ex_course(request, 'admin') \
                    or bool(course_tree_check_owner(parent_id, user_id))
            if course_id:
                if similar:
                    continue
                if course_check_owner(int(course_id),
                                      int(RequestGetUserId(request))):
                    check_capability_course(request, 'teacher')
                else:
                    check_capability_course(request, 'admin')
            else:
                check_capability_course(request, 'teacher')

            order += order_shift
            add_dict['name'] = title
            add_dict['order'] = order
            add_dict['verified'] = verified
            add_dict['course_id'] = course_id
            add_dict['author'] = max(0, user_id)
            node_id = course_add_by_dict(add_dict)
            ids.append(node_id)
        db_session.commit()
        course_make_dump(request)
        return {
            "result": "ok",
            "content": {
                "new_id": ids,
            }
        }
    except Exception as e:
        return {"result": "error", "content": e.__str__()}
Exemplo n.º 3
0
def course_erase(request):
    return course_erase_all(request)
    try:
        raise Exception("DEPRECATED")
        course = course_get_by_id(request.matchdict['course_id'])
        if course_check_owner(course.course_id, int(RequestGetUserId(request))) \
            or course_tree_check_owner(course.id, RequestGetUserId(request)):
            check_capability_course(request, 'teacher')
        else:
            check_capability_course(request, 'admin')
        db_session.delete(course)
        db_session.commit()
        course_make_dump(request)
        return {'result': 'ok'}
    except Exception as e:
        return {'result': 'error', 'content': e.__str__()}
Exemplo n.º 4
0
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__()}
Exemplo n.º 5
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__()}
Exemplo n.º 6
0
def course_erase_all(request):
    try:
        course = course_get_by_id(request.matchdict['course_id'])
        teacher = False
        teacher = teacher or (not course.verified \
                              and course.author == int(RequestGetUserId(request)))
        teacher = teacher or course_check_owner(course.course_id,
                                                int(RequestGetUserId(request)))
        teacher = teacher or course_tree_check_owner(course.id,
                                                     RequestGetUserId(request))
        check_capability_course(request, 'teacher' if teacher else 'admin')
        erase_tree(course)
        db_session.commit()
        course_make_dump(request)
        return {'result': 'ok'}
    except Exception as e:
        return {'result': 'error', 'content': e.__str__()}
Exemplo n.º 7
0
def course_update(request):
    try:
        course = course_get_by_id(request.matchdict['course_id'])
        if course_check_owner(course.course_id, int(RequestGetUserId(request))) \
            or course_tree_check_owner(course.id, RequestGetUserId(request)):
            check_capability_course(request, 'teacher')
        else:
            check_capability_course(request, 'admin')
        for field in ['parent_id', 'name', 'order', 'verified', 'course_id', 'displayed']:
            if field in request.params:
                course.__setattr__(field, request.params[field])
        course.author = RequestGetUserId(request)
        db_session.commit()
        course_make_dump(request)
        return {'result': 'ok'}
    except Exception as e:
        return {'result': 'error', 'content': e.__str__()}
Exemplo n.º 8
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__()}
Exemplo n.º 9
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__()}
Exemplo n.º 10
0
        def tmp(request, *args, **kwargs):
            if type(roles) == str:
                roles_list = (roles, )
            else:
                roles_list = roles
            userid = RequestGetUserId(request)

            if userid == -1 and "guest" not in roles_list:
                return {'result': 'autherror', 'message': 'You do not have permissions for this operation'}
            
            req = DBSession.query(RoleAssignment).filter_by(userid=userid)
    
            for role in roles_list:
                roleid = DBSession.query(Role).filter_by(shortname=role).one().id
                if req.filter_by(roleid=roleid).all():
                    result = func(request, *args, **kwargs)
                    return result    

            result = {'result': 'autherror', 'message': 'You do not have permissions for this operation'}
            return result
Exemplo n.º 11
0
def is_admin(request):
    userid = RequestGetUserId(request)
    req = DBSession.query(RoleAssignment).filter_by(userid=userid)
    role_admin_id = DBSession.query(Role).filter_by(shortname='admin').one().id
    return bool(req.filter_by(roleid=role_admin_id).all())