Exemplo 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)
Exemplo n.º 2
0
def api_list():
	'''List all planes in database
	'''
	username = util.html_escape_or_none(request.args.get('username'))
	if username:
		if username and (g.user is None or not g.user.is_authenticated):
			return util.make_json_error(msg='Not authenticated.')

		if username != g.user.username:
			return util.make_json_error(msg='No permission for this action.')

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

		planes = list(map(lambda x: x.get_plane(), Session.get_sessions(user)))
	else:
		try:
			planes = Plane.get_public_planes()
		except Exception:
			return util.make_json_error()
	
	data = [p.get_public_info(g.user) for p in planes]
	return util.make_json_success(data=data)
Exemplo n.º 3
0
def info_module(name):
	try:
		name   = util.html_escape_or_none(name)
		module = Module.get_by_name(name)
	except ModuleNotFound:
		return render_template('modules/404.html', name=name)

	is_owner = g.user.is_authenticated and g.user.id == module.owner
	return render_template('modules/module.html', module_name = module.name)
Exemplo n.º 4
0
def upload_module(name):
	if not g.user.is_authenticated:
		return render_template('modules/403.html', name=name)

	try:
		name   = util.html_escape_or_none(name)
		module = Module.get_by_name(name)
	except ModuleNotFound:
		return render_template('modules/404.html', name=name)

	if not g.user.id == module.owner:
		return render_template('modules/403.html', name=name)

	return render_template('modules/upload.html', module_name=name)
Exemplo n.º 5
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.')
Exemplo n.º 6
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.')
Exemplo n.º 7
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.')
Exemplo 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.')