def course_verify_cancel(request):
    try:
        check_capability_course(request, 'admin')
        course = course_get_by_id(request.matchdict['course_id'])
        if course.verified:
            raise Exception("Source mustn't be canceled")
        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.º 2
0
def course_verify_cancel(request):
    try:
        check_capability_course(request, 'admin')
        course = course_get_by_id(request.matchdict['course_id'])
        if course.verified:
            raise Exception("Source mustn't be canceled")
        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.º 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__()}
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.º 5
0
def course_verify(request):
    try:
        check_capability_course(request, 'admin')
        full_access = int(request.params.get('full_access', 0))
        course = course_get_by_id(request.matchdict['course_id'])
        course.verified = True
        if full_access:
            db_session.add(CourseTreeCap(
                node_id=course.id,
                user_id=course.author,
            ))
        db_session.commit()
        course_make_dump(request)
        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__()}
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.º 9
0
def course_all(request):
    try:
        check_capability_course(request, 'admin')
        courses_res = []
        courses = db_session.query(CourseRaw).\
            filter(CourseRaw.category.in_([24, 34])).all()
        for course in courses:
            nodes = db_session.query(Course).filter(Course.course_id == course.id).all()
            paths = [node.parent.full_name() + ("" if node.verified else " (не разобрано)") \
                for node in nodes]
            courses_res.append({
                'course': course,
                'paths': paths,
                'authors': course_get_users(course.id),
            })  
        return {
            'courses': courses_res,
        }
    except Exception as e:
        return Response("Error: " + e.__str__())
Exemplo n.º 10
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.º 11
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__()}