def cmd_tags(self, *args): parser = argparse.ArgumentParser( prog='tags', description="Modify tags of the opened file") parser.add_argument( '-a', '--add', metavar='TAG', help="Add tags to the opened file (comma separated)") parser.add_argument('-d', '--delete', metavar='TAG', help="Delete a tag from the opened file") try: args = parser.parse_args(args) except: return # This command requires a session to be opened. if not __sessions__.is_set(): self.log('error', "No open session") parser.print_usage() return # If no arguments are specified, there's not much to do. # However, it could make sense to also retrieve a list of existing # tags from this command, and not just from the "find" command alone. if args.add is None and args.delete is None: parser.print_usage() return # TODO: handle situation where addition or deletion of a tag fail. db = Database() if not db.find(key='sha256', value=__sessions__.current.file.sha256): self.log( 'error', "The opened file is not stored in the database. " "If you want to add it use the `store` command.") return if args.add: # Add specified tags to the database's entry belonging to # the opened file. db.add_tags(__sessions__.current.file.sha256, args.add) self.log('info', "Tags added to the currently opened file") # We refresh the opened session to update the attributes. # Namely, the list of tags returned by the 'info' command # needs to be re-generated, or it wouldn't show the new tags # until the existing session is closed a new one is opened. self.log('info', "Refreshing session to update attributes...") __sessions__.new(__sessions__.current.file.path) if args.delete: # Delete the tag from the database. db.delete_tag(args.delete, __sessions__.current.file.sha256) # Refresh the session so that the attributes of the file are # updated. self.log('info', "Refreshing session to update attributes...") __sessions__.new(__sessions__.current.file.path)
def cmd_tags(self, *args): parser = argparse.ArgumentParser(prog="tags", description="Modify tags of the opened file") parser.add_argument("-a", "--add", metavar="TAG", help="Add tags to the opened file (comma separated)") parser.add_argument("-d", "--delete", metavar="TAG", help="Delete a tag from the opened file") try: args = parser.parse_args(args) except: return # This command requires a session to be opened. if not __sessions__.is_set(): self.log("error", "No session opened") parser.print_usage() return # If no arguments are specified, there's not much to do. # However, it could make sense to also retrieve a list of existing # tags from this command, and not just from the "find" command alone. if args.add is None and args.delete is None: parser.print_usage() return # TODO: handle situation where addition or deletion of a tag fail. db = Database() if not db.find(key="sha256", value=__sessions__.current.file.sha256): self.log( "error", "The opened file is not stored in the database. " "If you want to add it use the `store` command.", ) return if args.add: # Add specified tags to the database's entry belonging to # the opened file. db.add_tags(__sessions__.current.file.sha256, args.add) self.log("info", "Tags added to the currently opened file") # We refresh the opened session to update the attributes. # Namely, the list of tags returned by the 'info' command # needs to be re-generated, or it wouldn't show the new tags # until the existing session is closed a new one is opened. self.log("info", "Refreshing session to update attributes...") __sessions__.new(__sessions__.current.file.path) if args.delete: # Delete the tag from the database. db.delete_tag(args.delete, __sessions__.current.file.sha256) # Refresh the session so that the attributes of the file are # updated. self.log("info", "Refreshing session to update attributes...") __sessions__.new(__sessions__.current.file.path)
def tags(tag_action=False): # Set DB db = Database() # Search or Delete if request.method == 'GET': action = request.query.action value = request.query.value.strip() if value: if action == 'search': # This will search all projects # Get project list projects = project_list() # Add Main db to list. projects.append('../') # Search All projects p_list = [] results = {} for project in projects: __project__.open(project) # Init DB db = Database() #get results proj_results = [] rows = db.find(key='tag', value=value) for row in rows: if project == '../': project = 'Main' proj_results.append([row.name, row.sha256]) results[project] = proj_results p_list.append(project) # Return the search template return template('search.tpl', projects=p_list, results=results) else: return template( 'error.tpl', error="'{0}' Is not a valid tag action".format(action)) # Add / Delete if request.method == 'POST': file_hash = request.forms.get('sha256') project = request.forms.get('project') tag_name = request.forms.get('tag') if tag_action == 'add': if file_hash and project: tags = request.forms.get('tags') db.add_tags(file_hash, tags) if tag_action == 'del': if file_hash and tag_name: db.delete_tag(tag_name, file_hash) redirect('/file/{0}/{1}'.format(project, file_hash))
def tags(tag_action=False): # Set DB db = Database() # Search or Delete if request.method == 'GET': action = request.query.action value = request.query.value.strip() if value: if action == 'search': # This will search all projects # Get project list projects = project_list() # Add Main db to list. projects.append('../') # Search All projects p_list = [] results = {} for project in projects: __project__.open(project) # Init DB db = Database() #get results proj_results = [] rows = db.find(key='tag', value=value) for row in rows: if project == '../': project = 'Main' proj_results.append([row.name, row.sha256]) results[project] = proj_results p_list.append(project) # Return the search template return template('search.tpl', projects=p_list, results=results) else: return template('error.tpl', error="'{0}' Is not a valid tag action".format(action)) # Add / Delete if request.method == 'POST': file_hash = request.forms.get('sha256') project = request.forms.get('project') tag_name = request.forms.get('tag') if tag_action == 'add': if file_hash and project: tags = request.forms.get('tags') db.add_tags(file_hash, tags) if tag_action == 'del': if file_hash and tag_name: db.delete_tag(tag_name, file_hash) redirect('/file/{0}/{1}'.format(project, file_hash))
def tags(tag_action=False): # Set DB db = Database() # Search or Delete if request.method == "GET": action = request.query.action value = request.query.value.strip() if value: if action == "search": # This will search all projects # Get project list projects = project_list() # Add Main db to list. projects.append("../") # Search All projects p_list = [] results = {} for project in projects: __project__.open(project) # Init DB db = Database() # get results proj_results = [] rows = db.find(key="tag", value=value) for row in rows: if project == "../": project = "Main" proj_results.append([row.name, row.sha256]) results[project] = proj_results p_list.append(project) # Return the search template return template("search.tpl", projects=p_list, results=results) else: return template("error.tpl", error="'{0}' Is not a valid tag action".format(action)) # Add / Delete if request.method == "POST": file_hash = request.forms.get("sha256") project = request.forms.get("project") tag_name = request.forms.get("tag") if tag_action == "add": if file_hash and project: tags = request.forms.get("tags") db.add_tags(file_hash, tags) if tag_action == "del": if file_hash and tag_name: db.delete_tag(tag_name, file_hash) redirect("/file/{0}/{1}".format(project, file_hash))