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)
def api_get_data(plane_id): '''Get information for plane. Arguments key - String - JSON key supporting nested selection. ''' if g.user is None or not g.user.is_authenticated: return util.make_json_error(msg='Not authenticated.') args = request.args key = args['key'] try: key = json.loads(key) except: key = True plane = Plane.get_plane(plane_id) if plane.is_user_connected(g.user): try: data = plane.get_data(key) return util.make_json_success(data=data) except: return util.make_json_error(msg='Error retrieving data for key: ' + str(key)) else: return util.make_json_error(msg='No session to plane.')
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_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)
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')
def api_sessions_by_user(): '''Returns a list of planes current user is connected to Returns data - List of plane names for each plane current user is connected to. ''' if not g.user.is_authenticated: return util.make_json_error(msg='Not authenticated.') data = [s.get_plane().get_name() for s in Session.get_sessions(g.user)] return util.make_json_success(data=data)
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_disconnect(plane): '''Remove a session between current user and <plane>. ''' if g.user is None or not g.user.is_authenticated: return util.make_json_error(msg='Not authenticated.') planar = Plane.get_plane(plane) disconnect(g.user, plane) return util.make_json_success(msg='Disconnected.')
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 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.')
def api_get_plane(plane_id): '''Get information for plane. Returns is_owner - Boolean - True if current user is owner of plane, False if not. module_name - String - Name of module related to plane. module_version - String - Latest version of module related to plane. Note: the actual version when the plane is created and possibility to update should be added in the future. plane_data - String - Data stored on plane. name - String - Name of plane. public - Boolean - True if plane is public, False if not. users - List of String - List of names of users that are connected to plane. ''' print(plane_id) if g.user is None or not g.user.is_authenticated: return util.make_json_error(msg='Not authenticated.') plane = Plane.get_plane(plane_id) if plane is None: return util.make_json_error(msg='Plane does not exist.') if plane.is_user_connected(g.user): return util.make_json_success(data=plane.get_public_info(g.user)) else: return util.make_json_error(msg='Unauthorised. Did you join the plane?')
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.')
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.')
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.')
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.')
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.')