Пример #1
0
def demo_eval():
    try:
        rad = request.form['rad']
    except KeyError as e:
        return error_response("the 'rad' param is required")
    try:
        libs = request.form['libs']
        libs = json.loads(libs)
    except KeyError as e:
        return error_response("the 'libs' param is required")
    except ValueError as e:
        return error_response("'libs' param is not valid JSON: " + unicode(e))

    with patch('radlibs.parser.load_lib', lambda name: libs[name]):
        try:
            radlib = unicode(parse(rad))
        except KeyError as e:
            return error_response("No such Library '{0}'".format(e.message))
        except ParseError as e:
            return error_response(e.message)

    return {
        'status': 'ok',
        'radlib': radlib,
    }
Пример #2
0
def new_rad_by_name():
    if not g.user:
        return error_response('login required')
    try:
        parse(request.form['rad'])
    except ParseError as e:
        return error_response('parse error: {0}'.format(e.message))
    session = Client().session()
    lib_name = request.form['lib']
    association_id = request.form['association_id']
    try:
        find_association(association_id)
    except NoResultFound:
        return error_response('no such association')
    try:
        lib = session.query(Lib).\
            filter(Lib.name == lib_name).\
            filter(Lib.association_id == association_id).\
            one()
    except NoResultFound:
        return error_response("no such lib '{0}'".format(lib_name))
    rad = Rad(created_by=g.user.user_id,
              lib_id=lib.lib_id,
              rad=request.form['rad'])
    session.add(rad)
    radlibs.lib.decache_lib(lib.name, lib.association_id)
    return {
        'status': 'ok',
        'rad_id': rad.rad_id,
    }
Пример #3
0
def edit_rad(rad_id):
    if rad_id == 0:
        abort(404)
    if not g.user:
        return error_response('login required')
    try:
        parse(request.form['rad'])
    except ParseError as e:
        return error_response('parse error: {0}'.format(e.message))
    session = Client().session()
    try:
        (rad, lib) = session.query(Rad, Lib).\
            join(Lib).\
            join(Association).\
            join(UserAssociation).\
            filter(UserAssociation.user_id == g.user.user_id).\
            filter(Rad.rad_id == rad_id).\
            one()
    except NoResultFound:
        return error_response('no such rad')

    rad.rad = request.form['rad']
    session.add(rad)
    radlibs.lib.decache_lib(lib.name, lib.association_id)
    return {'status': 'ok'}
Пример #4
0
def test_radlib(association_id):
    session = Client().session()
    if not g.user:
        return error_response('login required')
    try:
        association = association_for_logged_in_user(association_id)
    except NoResultFound:
        return error_response('no such association')

    g.association_id = association_id
    try:
        radlib = unicode(parse(request.form['rad']))
    except KeyError as e:
        return error_response("no such lib '{0}'".format(e.message))
    except ParseError as e:
        return error_response(e.message)
    return {'status': 'ok', 'radlib': radlib}
Пример #5
0
def create_lib(association_id):
    if not g.user:
        return error_response('login required')
    name = request.form['name']
    if not re.search('^[A-Z][a-z_]*$', name):
        return error_response("'{0}' is not a valid lib name".format(name))
    session = Client().session()
    try:
        find_association(association_id)
    except NoResultFound:
        return error_response('no such association')
    lib = Lib(association_id=association_id,
              name=name)
    session.add(lib)
    try:
        session.flush()
    except IntegrityError:
        return error_response('lib already exists')
    return {'status': 'ok', 'lib_id': lib.lib_id}
Пример #6
0
def new_rad(lib_id):
    if lib_id == 0:
        return error_response('lib_id is required')
    if not g.user:
        return error_response('login required')
    try:
        parse(request.form['rad'])
    except ParseError as e:
        return error_response('parse error: {0}'.format(e.message))
    session = Client().session()
    try:
        lib = find_lib(lib_id)
    except NoResultFound:
        return error_response('no such lib')
    rad = Rad(created_by=g.user.user_id,
              lib_id=lib_id,
              rad=request.form['rad'])
    session.add(rad)
    radlibs.lib.decache_lib(lib.name, lib.association_id)
    return {
        'status': 'ok',
        'rad_id': rad.rad_id
    }
Пример #7
0
def invite_user(association_id):
    if not g.user:
        return error_response('login required')
    session = Client().session()
    email = request.form['email']
    if not re.search(r'@.*\.', email):
        return error_response("invalid email address '{0}'".format(email))
    try:
        association = association_for_logged_in_user(association_id)
    except NoResultFound:
        return error_response('no such association')
    try:
        user = session.query(User).\
            filter(User.email == email).\
            one()
    except NoResultFound:
        invite = AssociationInvite.generate(association_id, email)
        try:
            session.flush()
        except IntegrityError:
            return error_response('already invited')
        send_invitation_mail(
            email,
            g.user.email,
            association.name,
            url_for('accept_invitation', token=invite.token, _external=True))
        return {'status': 'ok', 'action': 'invited'}

    user_association = UserAssociation(
        user_id=user.user_id, association_id=association_id)
    session.add(user_association)
    try:
        session.flush()
    except IntegrityError:
        return error_response('already in association')
    return {'status': 'ok', 'action': 'added'}
Пример #8
0
def test_authorization():
    if g.user:
        return {'status': 'ok'}
    else:
        return error_response('not logged in')