def manage_user(user_id): while True: add_delete_exit = False while not add_delete_exit: from sleipnir import dbi i = 0 all_corpora = {c.get("id"): c for c in dbi.list_corpora()} if not all_corpora: print("No current corpora to add.", end="\n\n") add_delete_exit = True return else: user_corpora = get_user_corpora(user_id) print("\nCurrent user corpora:") deldict = {} for corp_id in user_corpora: if all_corpora.get(corp_id): print("\t({}) {}".format(i, all_corpora.get(corp_id).get("name"))) deldict[str(i)] = corp_id i += 1 corpdict = enum_corpora(all_corpora, user_corpora, i=i) resp = input("Add/delete corpus for user?\nOptions: {{#}} or {{q}}: ") if resp.strip() in corpdict: add_user_corpora(user_id, corpdict[resp.strip()]) print( 'Corpus "{}" added to user "{}"'.format( all_corpora[corpdict[resp.strip()]].get("name"), user_id ), end="\n\n", ) add_delete_exit = True break elif resp.strip() in deldict: corp_name = all_corpora[deldict[resp.strip()]].get("name") while True: yn = input( '\nThis will remove corpus "{}" from user "{}". Continue?\nOptions {{y}} {{n}}'.format( corp_name, user_id ) ) if yn.strip() == "y": del_user_corpora(user_id, deldict[resp.strip()]) print('Corpus "{}" was removed from user "{}".'.format(corp_name, user_id), end="\n\n") add_delete_exit = True break elif yn.strip() == "n": print("Aborted.\n") add_delete_exit = True break elif resp.strip() == "q": return
def prompt_corpora(): # Main loop main_loop = True while main_loop: corpora = dbi.list_corpora() print("Current corpora:") corpus_map = {} for i, corpus in enumerate(sorted(corpora, key=lambda x: x['name'])): corpus_map[i] = corpus print('{:>6} {} [{}]'.format('({})'.format(i), corpus['name'], corpus['id'])) resp = input("Enter a corpus number to delete, or q to quit: ") if resp == 'q': main_loop = False elif resp.strip() and int(resp) in corpus_map: r = int(resp) print('This will delete the corpus "#{}" with name "{}" and id "{}."'.format(r, corpus_map[r]['name'], corpus_map[r]['id'])) del_conf = input("Are you sure? [y/n]") if del_conf.lower() == 'y': dbi.del_corpus(corpus_map[r]['id'])