def get(self): """ :return a list of logged user's projects """ user = get_current_user() projects = ProjectService.retrieve_all_user_projects(user_id=user.id) return [ProjectDto(project) for project in projects]
def get(self): """ :return: a list of TVB users """ page_number = self.extract_page_number() user_dto_list, pages_no = UserFacade.get_users(get_current_user().username, page_number, USERS_PAGE_SIZE) return {"users": user_dto_list, "pages_no": pages_no}
def get(self): """ :return: a list of TVB users """ user_list, pages_no = UserService.retrieve_all_users( get_current_user().username) return { "users": [UserDto(user) for user in user_list], "pages_no": pages_no }
def post(self, project_gid, algorithm_module, algorithm_classname): """ :generic method of launching Analyzers """ model_file = self.extract_file_from_request( request_file_key=RequestFileKey.LAUNCH_ANALYZERS_MODEL_FILE.value) current_user = get_current_user() operation_gid = self.operation_facade.launch_operation( current_user.id, model_file, project_gid, algorithm_module, algorithm_classname, self.extract_file_from_request) return operation_gid, HTTP_STATUS_CREATED
def post(self, project_gid, algorithm_module, algorithm_classname): """ :generic method of launching Analyzers """ model_file = self.extract_file_from_request(request_file_key=RequestFileKey.LAUNCH_ANALYZERS_MODEL_FILE.value) destination_folder = RestResource.get_destination_folder() h5_path = RestResource.save_temporary_file(model_file, destination_folder) try: project = self.project_service.find_project_lazy_by_gid(project_gid) except ProjectServiceException: raise InvalidIdentifierException(INVALID_PROJECT_GID_MESSAGE % project_gid) algorithm = FlowService.get_algorithm_by_module_and_class(algorithm_module, algorithm_classname) if algorithm is None: raise InvalidIdentifierException('No algorithm found for: %s.%s' % (algorithm_module, algorithm_classname)) try: adapter_instance = ABCAdapter.build_adapter(algorithm) view_model = adapter_instance.get_view_model_class()() view_model_h5 = ViewModelH5(h5_path, view_model) view_model_gid = view_model_h5.gid.load() current_user = get_current_user() operation = self.operation_service.prepare_operation(current_user.id, project.id, algorithm.id, algorithm.algorithm_category, view_model_gid.hex, None, {}) storage_path = self.files_helper.get_project_folder(project, str(operation.id)) if isinstance(adapter_instance, ABCUploader): for key, value in adapter_instance.get_form_class().get_upload_information().items(): data_file = self.extract_file_from_request(request_file_key=key, file_extension=value) data_file_path = RestResource.save_temporary_file(data_file, destination_folder) file_name = os.path.basename(data_file_path) upload_field = getattr(view_model_h5, key) upload_field.store(os.path.join(storage_path, file_name)) shutil.move(data_file_path, storage_path) shutil.move(h5_path, storage_path) os.rmdir(destination_folder) view_model_h5.close() OperationService().launch_operation(operation.id, True) except Exception as excep: self.logger.error(excep, exc_info=True) raise ServiceException(str(excep)) return operation.gid, HTTP_STATUS_CREATED
def put(self, project_gid): """ Add members to the given project :param project_gid: project gid :param """ input_data = flask.request.json new_members_gid = input_data[ FormKeyInput.NEW_MEMBERS_GID. value] if FormKeyInput.NEW_MEMBERS_GID.value in input_data else [] if len(new_members_gid) == 0: raise InvalidInputException("Empty users list.") self.project_facade.add_members_to_project(get_current_user().id, project_gid, new_members_gid)
def post(self, project_gid): """ :start a simulation using a project id and a zip archive with the simulator data serialized """ file = self.extract_file_from_request(request_file_key=RequestFileKey.SIMULATION_FILE_KEY.value, file_extension=FilesHelper.TVB_ZIP_FILE_EXTENSION) zip_path = save_temporary_file(file) result = FilesHelper().unpack_zip(zip_path, os.path.dirname(zip_path)) if len(result) == 0: self.logger.error("Empty zip archive. {}".format(zip_path)) raise InvalidInputException("Empty zip archive") zip_directory = os.path.dirname(result[0]) simulation_gid = self.simulation_facade.launch_simulation(get_current_user().id, zip_directory, project_gid) return simulation_gid, HTTP_STATUS_CREATED
def post(self): """ Create a new project linked to the current user """ input_data = flask.request.json try: project_name = input_data[FormKeyInput.CREATE_PROJECT_NAME.value] project_description = input_data[FormKeyInput.CREATE_PROJECT_DESCRIPTION.value] \ if FormKeyInput.CREATE_PROJECT_DESCRIPTION.value in input_data else "" try: project_gid = ProjectFacade().create_project(get_current_user(), project_name, project_description) return project_gid, HTTP_STATUS_CREATED except formencode.Invalid as exception: raise InvalidInputException(exception.msg) except KeyError: raise InvalidInputException("Invalid create project input.")
def post(self, project_gid): """ :start a simulation using a project id and a zip archive with the simulator data serialized """ file = self.extract_file_from_request( request_file_key=RequestFileKey.SIMULATION_FILE_KEY.value, file_extension=FilesHelper.TVB_ZIP_FILE_EXTENSION) destination_folder = RestResource.get_destination_folder() zip_path = RestResource.save_temporary_file(file, destination_folder) try: project = self.project_service.find_project_lazy_by_gid( project_gid) except ProjectServiceException: raise InvalidIdentifierException(INVALID_PROJECT_GID_MESSAGE % project_gid) result = FilesHelper().unpack_zip(zip_path, os.path.dirname(zip_path)) if len(result) == 0: raise InvalidInputException("Empty zip archive") folder_path = os.path.dirname(result[0]) simulator_algorithm = FlowService().get_algorithm_by_module_and_class( SimulatorAdapter.__module__, SimulatorAdapter.__name__) try: simulator_h5_name = DirLoader( folder_path, None).find_file_for_has_traits_type(Simulator) simulator_file = os.path.join(folder_path, simulator_h5_name) except IOError: raise InvalidInputException( 'No Simulator h5 file found in the archive') try: current_user = get_current_user() operation = self.simulator_service.prepare_simulation_on_server( user_id=current_user.id, project=project, algorithm=simulator_algorithm, zip_folder_path=folder_path, simulator_file=simulator_file) except Exception as excep: self.logger.error(excep, exc_info=True) raise ServiceException(str(excep)) return operation.gid, HTTP_STATUS_CREATED
def post(self): """ Create a new project linked to the current user """ input_data = flask.request.json try: project_name = input_data[FormKeyInput.CREATE_PROJECT_NAME.value] project_description = input_data[FormKeyInput.CREATE_PROJECT_DESCRIPTION.value] \ if FormKeyInput.CREATE_PROJECT_DESCRIPTION.value in input_data else "" try: db_project = ProjectService().store_project( get_current_user(), True, None, name=project_name, description=project_description, users=[]) return db_project.gid, HTTP_STATUS_CREATED except formencode.Invalid as excep: raise InvalidInputException(excep.msg) except KeyError: raise InvalidInputException("Invalid create project input.")
def put(self, project_gid): """ Add members to the given project :param project_gid: project gid :param """ try: project = self.project_service.find_project_lazy_by_gid(project_gid) except Exception: raise InvalidIdentifierException("Invalid project identifier.") if get_current_user().id != project.fk_admin: raise AuthorizationRequestException("Your are not allowed to edit given project") input_data = flask.request.json new_members_gid = input_data[ FormKeyInput.NEW_MEMBERS_GID.value] if FormKeyInput.NEW_MEMBERS_GID.value in input_data else [] new_members_id = [] for gid in new_members_gid: user = self.user_service.get_user_by_gid(gid) if user is None: raise InvalidInputException("Invalid user gid {}".format(gid)) new_members_id.append(user.id) self.project_dao.add_members_to_project(project.id, new_members_id)
def has_access(self): current_user = get_current_user() if self.required_role is not None and current_user.role != self.required_role: return False return self._check_permission(current_user.id)
def get(self): """ :return a list of logged user's projects """ return ProjectFacade.retrieve_logged_user_projects( get_current_user().id)