Ejemplo n.º 1
0
def api_list_modules():
	'''List all modules in database.
	Arguments:

	Optional arguments:
		username - String - Returns only modules for given owner.
	'''

	args     = util.parse_request_to_json(request)
	# username = util.html_escape_or_none(args['username'])
	username = util.html_escape_or_none(request.args.get('username'))

	if username:
		try:
			user = User.get_by_name(username)
		except UserNotFoundError:
			return util.make_json_error(data='No user with that name.')

		modules = Module.get_by_userid(user.id)
	else:
		modules = Module.query

	data = [ module.get_public_long_info() for module in modules ]

	return util.make_json_success(data=data)
Ejemplo n.º 2
0
def api_login():
    # username = werkzeug.utils.escape(args['username'])
    # password = werkzeug.utils.escape(args['password'])

    # if not User.authenticate(username, password):
    # 	return util.make_json_error(msg='Authentication failed.')

    args = util.parse_request_to_json(request)
    form = LoginForm.from_json(args)

    if not form.validate():
        return util.make_json_error(msg=form.getErrors())

    user = User.get_by_name(form.username.data)
    login_user(user)
    return util.make_json_success(msg='Welcome.')
Ejemplo n.º 3
0
def api_signup():
    args = util.parse_request_to_json(request)
    form = SignupForm.from_json(args)
    # username = werkzeug.utils.escape(args['username'])
    # password = werkzeug.utils.escape(args['password'])
    # email    = werkzeug.utils.escape(args['email'])

    if not form.validate():
        return util.make_json_error(msg=form.getErrors())

    user = User(username=form.username.data,
                password=form.password.data,
                email=form.email.data)
    db.session.add(user)
    db.session.commit()

    login_user(user)
    return util.make_json_success(msg='Thanks for signing up.')
Ejemplo n.º 4
0
def api_create_module():
	'''Create a module.
	Arguments:
		module_name - String - Name of module. If module does not 
		                       exist it will be created.
	'''
	if not g.user.is_authenticated:
		return util.make_json_error(msg='Not authenticated.')

	print(request)
	args = util.parse_request_to_json(request)

	module_name    = util.html_escape_or_none(args['name'])
	latest_version = None

	if module_name is None or module_name == '':
		return util.make_json_error(msg='Module must have a name.')

	try:
		module = Module.get_by_name(module_name)
		return util.make_json_error(msg='Module ' + module_name + ' already exists.')
	except ModuleNotFound:
		module = Module(
			owner = g.user.id,
			name  = module_name,
			latest_version = latest_version,
			short_desc = 'No description provided yet.',
			long_desc  = 'No description provided for this module yet.'
				' Upload a file README.md containing Github Flavoured Markdown'
				' to provide one.'
		)

	db.session.add(module)

	try:
		db.session.commit()
	except sqlalchemy.exc.IntegrityError as e:
		print(e)
		return util.make_json_error(msg='Invalid arguments.')

	return util.make_json_success(msg='Module created.')
Ejemplo n.º 5
0
def api_delete_module():
	'''Delete a module. Requires user to be authenticated and the owner to 
	complete successfully.
	
	Arguments
		name - String - Required. Deletes the module with the given name.

	Returns
		{status: ok} if operation successful.
		{status: error, msg: '...'} otherwise.
	'''
	if not g.user.is_authenticated:
		return util.make_json_error(msg='Not authenticated.', error_code=403)
	
	args        = util.parse_request_to_json(request)
	module_name = util.html_escape_or_none(args['name'])

	try:
		module = Module.get_by_name(module_name)
	except ModuleNotFound:
		return util.make_json_error(msg='Module {} not found.'.format(module_name))

	if g.user.id != module.owner:
		return util.make_json_error(msg='You do not own module {}.'.format(module_name))

	try:
		for version in Module.get_versions(module_name):
			db.session.delete(version)
		db.session.delete(module)

		db.session.commit()
		manager.delete_module(module)

		return util.make_json_success(msg='Module ' + module_name + ' deleted.')
	except Exception as e:
		db.session.rollback()
		print(e)
		return util.make_json_error(msg='Error deleting module.')
Ejemplo n.º 6
0
def api_connect(plane):
	'''Create a session between user and plane.
	Arguments
		password - String - Password to plane if such exist.
	'''

	if g.user is None or not g.user.is_authenticated:
		return util.make_json_error(msg='Not authenticated.')

	args = util.parse_request_to_json(request)

	password = Plane.get_plane(util.html_escape_or_none(args['password']))
	planar = Plane.get_plane(plane)

	if planar.has_password():
		if planar.password_matching(password):
			connect(g.user, planar)
			return util.make_json_success(msg='Connected.')
		else:
			return util.make_json_error(msg='Incorrect planar password.')
	else:
		connect(g.user, planar)
		return util.make_json_success(msg='Connected.')
Ejemplo n.º 7
0
def api_store_data(plane_id):
	'''Store data on plane.
	Arguments
		data   - String - 
	'''

	if g.user is None or not g.user.is_authenticated:
		return util.make_json_error(msg='Not authenticated.')

	args  = util.parse_request_to_json(request)
	data  = args['data']
	plane = Plane.get_plane(plane_id)

	if plane.is_user_connected(g.user):
		try:
			plane.set_data(data)
		except r.errors.ReqlDriverCompileError:
			return util.make_json_error(msg='Invalid request.')
		except:
			return util.make_json_error(msg='Error setting data.')

		return util.make_json_success(msg='Data stored successfully.')
	else:	
		return util.make_json_error(msg='Not connected to plane.')
Ejemplo n.º 8
0
def api_create_plane():
	'''Create a plane.
	Arguments
		password - String - Password to join plane.
		module - String - Required. Name of module to load.
		name - String - Required. Name of plane to be created.
		public - Boolean - True if plane should be public, False if not.
	'''

	if g.user is None or not g.user.is_authenticated:
		return util.make_json_error(msg='Not authenticated.')

	args = util.parse_request_to_json(request)

	password     = util.html_escape_or_none(args['password'])
	module_name  = util.html_escape_or_none(args['module'])
	plane_name   = util.html_escape_or_none(args['name'])
	hidden       = util.html_escape_or_none(args['hidden'])

	module = Module.query.filter_by(name=module_name).first()

	if module is None:
		return util.make_json_error(msg='Module does not exist.')
	if plane_name is None or plane_name == '':
		return util.make_json_error(msg='No plane name submitted.')
	if Plane.query.filter_by(name=plane_name).scalar() is not None:
		return util.make_json_error(msg='A plane with that name already exists.')
	
	try:
		version = module.get_latest_version()
	except ModuleVersionNotFound:
		return util.make_json_error(msg='Module has no version attached.')

	hidden = (hidden == 'True')

	plane = Plane(
		owner    = g.user.id, 
		password = password, 
		module   = module.id,
		version  = version.id,
		data     = None, 
		name     = plane_name, 
		public   = not bool(hidden),
	)

	db.session.add(plane)

	try:
		db.session.commit()

		import rethinkdb as r
		rethink_connection = r.connect( "localhost", RETHINK_DB_PORT)
		r.db('planeshift')                                \
			.table('planes')                              \
			.insert({'id': plane.get_id(), 'data': None}) \
			.run(rethink_connection)

	except sqlalchemy.exc.IntegrityError as e:
		db.session.rollback()
		return util.make_json_error(msg='Invalid arguments.')
	except Exception as s:
		db.session.rollback()
		return util.make_json_error(msg='Internal error.')

	connect(g.user, plane)
	return util.make_json_success(msg='Plane created.')