def create_reset_token(self, login): """Insert a token into the db Parameters ---------- login : str username or email Returns ------- str The reset token """ token = "{}:{}".format(int(time.time()), Utils.get_random_string(20)) database_field = 'username' if validate_email(login): database_field = 'email' database = Database(self.app, self.session) query = """ UPDATE users SET reset_token=? WHERE {}=? """.format(database_field) database.execute_sql_query(query, (token, login)) return token
def update_apikey(self, user): """Create a new api key and store in the database Parameters ---------- user : dict The current user Returns ------- dict error, error message and updated user """ error = False error_message = '' database = Database(self.app, self.session) # get a new api key new_apikey = Utils.get_random_string(20) query = ''' UPDATE users SET apikey=? WHERE username=? ''' database.execute_sql_query(query, (new_apikey, user['username'])) user['apikey'] = new_apikey return {'error': error, 'error_message': error_message, 'user': user}
def get_file_name(self): """Get a random file name Returns ------- string file name """ name = Utils.get_random_string(10) file_path = "{}/{}".format(self.upload_path, name) # Make sure it is not in use already while os.path.isfile(file_path): name = Utils.get_random_string(10) file_path = "{}/{}".format(self.upload_path, name) return name
def get_file_name(self): """Get a random file name Returns ------- string file name """ return Utils.get_random_string(10)
def persist_user_admin(self, inputs): """Persist a new user (admin action) Parameters ---------- inputs : User input The new user info Returns ------- dict The new user """ inputs["password"] = Utils.get_random_string(8) return self.persist_user(inputs, return_password=True)
def create_reset_token(self, username, old_token=False): """Create a reset token""" if old_token: old_timestamp = int(time.time()) - 14400 token = "{}:{}".format(old_timestamp, Utils.get_random_string(20)) database = Database(self.app, self.session) query = """ UPDATE users SET reset_token=? WHERE username=? """ database.execute_sql_query(query, (token, username)) return token else: auth = LocalAuth(self.app, self.session) return auth.create_reset_token(username)
def integrate(self, public=False): """Integrate the file into the triplestore Parameters ---------- public : bool, optional Integrate in private or public graph """ sparql = SparqlQueryLauncher(self.app, self.session) tse = TriplestoreExplorer(self.app, self.session) self.public = public method = self.settings.get('triplestore', 'upload_method') # Load file into a RDF graph self.graph_chunk.parse(self.path, format=self.type_dict[self.type]) # get metadata self.set_metadata() # Remove metadata from data self.delete_metadata_location() # insert metadata sparql.insert_data(self.graph_metadata, self.file_graph, metadata=True) if method == "load": # write rdf into a tmpfile and load it temp_file_name = 'tmp_{}_{}.{}'.format(Utils.get_random_string(5), self.name, self.rdf_extention) # Try to load data. if failure, wait 5 sec and retry 5 time Utils.redo_if_failure(self.log, 5, 5, self.load_graph, self.graph_chunk, temp_file_name) else: # Insert # Try to insert data. if failure, wait 5 sec and retry 5 time Utils.redo_if_failure(self.log, 5, 5, sparql.insert_data, self.graph_chunk, self.file_graph) # Remove chached abstraction tse.uncache_abstraction(public=self.public) self.set_triples_number()
def __init__(self, app, session, result_info, force_no_db=False): """init object Parameters ---------- app : Flask flask app session : AskOmics session, contain the user result_info : dict Result file info """ Params.__init__(self, app, session) if "user" in self.session: self.result_path = "{}/{}_{}/results".format( self.settings.get("askomics", "data_directory"), self.session['user']['id'], self.session['user']['username']) if "id" in result_info and not force_no_db: self.id = result_info["id"] if not self.set_info_from_db_with_id(): return None else: self.id = result_info["id"] if "id" in result_info else None self.graph_state = result_info[ "graph_state"] if "graph_state" in result_info else None self.graphs = result_info[ "graphs"] if "graphs" in result_info else [] self.endpoints = result_info[ "endpoints"] if "endpoints" in result_info else [] self.sparql_query = result_info[ "sparql_query"] if "sparql_query" in result_info else None self.celery_id = result_info[ "celery_id"] if "celery_id" in result_info else None self.file_name = result_info[ "file_name"] if "file_name" in result_info else Utils.get_random_string( 10) self.file_path = "{}/{}".format(self.result_path, self.file_name) self.start = None self.end = None self.nrows = 0 self.has_form_attr = False self.template = False self.form = False
def download_datasets(self, datasets_id): """Download galaxy datasets into AskOmics Parameters ---------- datasets_id : list List of Galaxy datasets id """ galaxy_instance = galaxy.GalaxyInstance(self.url, self.apikey) files_handler = FilesHandler(self.app, self.session) for dataset_id in datasets_id: dataset = galaxy_instance.datasets.show_dataset(dataset_id) file_name = Utils.get_random_string(10) path = "{}/{}".format(self.upload_path, file_name) filetype = dataset["file_ext"] size = dataset["file_size"] galaxy_instance.datasets.download_dataset( dataset_id, file_path=path, use_default_filename=False) files_handler.store_file_info_in_db(dataset["name"], filetype, file_name, size)
def integrate(self, public=False): """Integrate the file into the triplestore Parameters ---------- public : bool, optional Integrate in private or public graph """ sparql = SparqlQueryLauncher(self.app, self.session) self.public = public method = self.settings.get('triplestore', 'upload_method') # insert metadata sparql.insert_data(self.get_metadata(), self.file_graph, metadata=True) if method == "load": # cp file into ttl dir tmp_file_name = 'tmp_{}_{}.ttl'.format( Utils.get_random_string(5), self.name, ) temp_file_path = '{}/{}'.format(self.ttl_dir, tmp_file_name) copyfile(self.path, temp_file_path) # Load the chunk sparql.load_data(tmp_file_name, self.file_graph, self.host_url) # Remove tmp file if not self.settings.getboolean('askomics', 'debug_ttl'): os.remove(temp_file_path) else: with open(self.path) as ttl_file: ttl_content = ttl_file.read() sparql.insert_ttl_string(ttl_content, self.user_graph) self.set_triples_number()
def update_pw_db(self, username, password): """Update a password in database Parameters ---------- username : str User username password : str New password """ database = Database(self.app, self.session) salt = Utils.get_random_string(20) salted_pw = self.settings.get('askomics', 'password_salt') + password + salt sha512_pw = hashlib.sha512(salted_pw.encode('utf-8')).hexdigest() query = ''' UPDATE users SET password=?, salt=? WHERE username=? ''' database.execute_sql_query(query, (sha512_pw, salt, username))
def integrate(self, dataset_id=None): """Integrate the file into the triplestore""" sparql = SparqlQueryLauncher(self.app, self.session) # insert metadata sparql.insert_data(self.get_metadata(), self.file_graph, metadata=True) content_generator = self.generate_rdf_content() # Insert content chunk_number = 0 for _ in content_generator: if self.graph_chunk.ntriple >= self.max_chunk_size: if self.graph_chunk.percent and dataset_id: self.update_percent_in_db(self.graph_chunk.percent, dataset_id) if self.method == 'load': # write rdf into a tmpfile and load it temp_file_name = 'tmp_{}_{}_chunk_{}.{}'.format( Utils.get_random_string(5), self.name, chunk_number, self.rdf_extention) # Try to load data. if failure, wait 5 sec and retry 5 time Utils.redo_if_failure(self.log, 5, 5, self.load_graph, self.graph_chunk, temp_file_name) else: # Insert # Try to insert data. if failure, wait 5 sec and retry 5 time Utils.redo_if_failure(self.log, 5, 5, sparql.insert_data, self.graph_chunk, self.file_graph) chunk_number += 1 self.graph_chunk = RdfGraph(self.app, self.session) # Load the last chunk if self.graph_chunk.percent and dataset_id: self.update_percent_in_db(100, dataset_id) if self.method == 'load': temp_file_name = 'tmp_{}_{}_chunk_{}.{}'.format( Utils.get_random_string(5), self.name, chunk_number, self.rdf_extention) # Try to load data. if failure, wait 5 sec and retry 5 time Utils.redo_if_failure(self.log, 5, 5, self.load_graph, self.graph_chunk, temp_file_name) else: # Insert # Try to insert data. if failure, wait 5 sec and retry 5 time Utils.redo_if_failure(self.log, 5, 5, sparql.insert_data, self.graph_chunk, self.file_graph) # Content is inserted, now insert abstraction and domain_knowledge self.set_rdf_abstraction_domain_knowledge() if self.method == 'load': temp_file_name = 'tmp_{}_{}_abstraction_domain_knowledge.{}'.format( Utils.get_random_string(5), self.name, self.rdf_extention) self.load_graph(self.graph_abstraction_dk, temp_file_name) else: # Insert sparql.insert_data(self.graph_abstraction_dk, self.file_graph) self.set_triples_number()
def persist_user(self, inputs, ldap_login=False, return_password=False): """ Persist user in the TS Parameters ---------- inputs : dict User infos ldap_login : bool, optional If True, user is ldap Returns ------- dict The user """ database = Database(self.app, self.session) # Check if user is the first. if yes, set him admin if self.get_number_of_users() == 0: admin = True blocked = False else: admin = False blocked = self.settings.getboolean('askomics', 'default_locked_account') api_key = Utils.get_random_string(20) if "apikey" not in inputs else inputs["apikey"] query = ''' INSERT INTO users VALUES( NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NULL ) ''' salt = None sha512_pw = None email = None fname = None lname = None if not ldap_login: # Create a salt salt = Utils.get_random_string(20) if "salt" not in inputs else inputs["salt"] # Concat askomics_salt + user_password + salt salted_pw = self.settings.get('askomics', 'password_salt') + inputs['password'] + salt # hash sha512_pw = hashlib.sha512(salted_pw.encode('utf8')).hexdigest() email = inputs["email"] fname = inputs["fname"] lname = inputs["lname"] # Store user in db user_id = database.execute_sql_query( query, (ldap_login, fname, lname, inputs['username'], email, sha512_pw, salt, api_key, admin, blocked, Utils.humansize_to_bytes(self.settings.get("askomics", "quota")), int(time.time())), True) user = { 'id': user_id, 'ldap': ldap_login, 'fname': fname, 'lname': lname, 'username': inputs['username'], 'email': email, 'admin': admin, 'blocked': blocked, 'quota': Utils.humansize_to_bytes(self.settings.get("askomics", "quota")), 'apikey': api_key, 'galaxy': None } if return_password and not ldap_login: user["password"] = inputs["password"] # Return user infos return user