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)
Example #2
0
def get_latest_ver_string(module_name):
    try:
        module = Module.get_by_name(module_name)
        version = module.get_latest_version()
        sVersion = version.get_escaped_version()
    except ModuleVersionNotFound:
        raise ModuleHasNoData()
    return sVersion
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)
def api_info(module_name):
	'''Get meta-data for a given module.
	'''
	try:
		module = Module.get_by_name(module_name);
	except ModuleNotFound:
		return util.make_json_error(msg='Module {} not found'.format(module_name));

	data = module.get_public_long_info()
	data['is_owner'] = g.user.is_authenticated and g.user.id == module.owner;
	return util.make_json_success(data=data);
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.')
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.')
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)
def api_content_path_get(module_name, version, file):
	if version == 'latest':
		version = None

	try:
		module         = Module.get_by_name(module_name)
		module_version = module.get_version(version) if version is not None else None
		data           = manager.get_path_for_module_content(file, module, module_version)
	except ModuleNotFound:
		return util.make_json_error(msg='Module {} not found.'.format(module_name))
	except ModuleVersionNotFound:
		return util.make_json_error(msg='Version {} not found for module {}'.format(version, module_name))
	except ModuleHasNoData:
		return util.make_json_error(msg='No version uploaded for module {} yet!'.format(module_name))
	
	return util.make_json_success(data=data)
def api_content_path_post(name, version, file):
	if version == 'latest':
		version = None

	try:
		module         = Module.get_by_name(module_name)
		module_version = module.get_version(version) if version is not None else None

		# TODO: Implement upload here.

	except ModuleNotFound:
		return util.make_json_error(msg='Module {} not found.'.format(module_name))
	except ModuleVersionNotFound:
		return util.make_json_error(msg='Version {} not found for module {}'.format(version, module_name))
	except ModuleHasNoData:
		return util.make_json_error(msg='No version uploaded for module {} yet!'.format(module_name))

	return util.make_json_error(msg='Not implemented yet')	
Example #10
0
def api_version(module_name):
	'''Create a new version. Must be authenticated as owner.
	Arguments
		name - String - Required. Name of the new version.
		
		This function also takes and unspecified number of
		form-data encoded files.

	Returns
		{status: ok} if successful
		{status: error, msg: '...'} if user not authenticated or not owner.
	'''
	if not g.user.is_authenticated:
		return util.make_json_error(msg='Not authenticated.')

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

	if not module.is_owner(g.user.username):
		return util.make_json_error(msg='You do not have the correct permissions.')

	try:		
		escaped_version = werkzeug.utils.escape(request.form['version'])
		files           = sum( request.files.listvalues(), [])

		# TODO: Argument validation should be standardised.
		if escaped_version == "":
			return util.make_json_error(msg='Version name cannot be empty.')
		if module.has_version(escaped_version):
			return util.make_json_error(msg='Version name already exists.')

		manager.upload_version(module=module, sVersion=escaped_version, added_files=files)

	except manager.ModuleDuplicateVersionError as e:
		return util.make_json_error(msg='Version name already exists.')

	return util.make_json_success(msg='Success.')