def importScope(scopetype=''): if scopetype == 'blacklist': importBlacklist = True importForm = forms.ImportBlacklistForm() elif scopetype == 'scope': importBlacklist = False importForm = forms.ImportScopeForm() else: abort(404) if importForm.validate_on_submit(): successImports = [] alreadyExists = [] failedImports = [] newScopeItems = importForm.scope.data.split('\n') for item in newScopeItems: item = item.strip() fail, exist, success = ScopeItem.importScope(item, importBlacklist) failedImports = failedImports + fail alreadyExists = alreadyExists + exist successImports = successImports + success db.session.commit() current_app.ScopeManager.update() if len(successImports) > 0: flash('%s targets added to %s!' % (len(successImports), scopetype), 'success') if len(alreadyExists) > 0: flash('%s targets already existed!' % len(alreadyExists), 'info') if len(failedImports) > 0: flash('%s targets failed to import!' % len(failedImports), 'danger') for item in failedImports: flash('%s' % item, 'danger') else: for field, errors in importForm.errors.items(): for error in errors: flash(error, 'danger') return redirect(url_for('admin.%s' % scopetype))
def importScope(file, blacklist, verbose): failedImports = [] alreadyExists = [] successImports = [] with open(file, 'r') as scope: for line in scope.readlines(): line = line.strip() fail, exist, success = ScopeItem.importScope(line, blacklist) failedImports = failedImports + fail alreadyExists = alreadyExists + exist successImports = successImports + success db.session.commit() summaryMessages = [ f"{len(successImports)} successfully imported.", f"{len(alreadyExists)} already existed.", f"{len(failedImports)} failed to import." ] print('\n'.join(summaryMessages)) if verbose: print_imports("\nSuccessful Imports:", successImports) print_imports("\nAlready Existed:", alreadyExists) print_imports("\nFailed Imports:", failedImports)