diff --git a/app.py b/app.py index df779ba..bd71e31 100644 --- a/app.py +++ b/app.py @@ -3,6 +3,7 @@ import streamlit_authenticator as stauth import yaml from yaml.loader import SafeLoader import main +import create_credentials with open('credentials.yaml') as file: config = yaml.load(file, Loader=SafeLoader) @@ -15,47 +16,70 @@ authenticator = stauth.Authenticate( config['preauthorized'] ) -name, authentication_status, username = authenticator.login('Logga in', 'main') - -if authentication_status: - - # Say hello - st.markdown(f':blue[**Hej {name}!**]') - - # Ask for input - help_text = '[Du kan hitta en profils Facebook-ID här](https://lookup-id.com/)' - user_input = str(st.text_input('Skriv in telefonnummer eller Facebook-ID', help=help_text, placeholder='Skriv här...')).strip() - - if user_input != '': - s = [] - for i in user_input: - if i.isnumeric(): - s.append(str(i)) - s = ''.join(s) - - if s[0:2] == '07' and len(s) == 10: - s = '46' + s[1:] +def add_user(): + st.write(':red[Skapa ny användare]') + with st.form("new_credentials"): + new_username = st.text_input('Användarnamn') + new_name = st.text_input('Namn') + new_pwd = st.text_input('Lösenord') + submitted = st.form_submit_button("Lägg till användare", ) + if submitted: + credentials, valid = create_credentials.credentials(new_username, new_name, new_pwd) + if valid: + create_credentials.update(credentials, print_out=False, update=True) + st.write('Användare tillagd.') + else: + st.write('Användare finns redan.') + + +def main(): + name, authentication_status, username = authenticator.login('Logga in', 'main') + + if authentication_status: - # Search for info. - r = main.search(s) + # Say hello + st.markdown(f':blue[**Hej {name}!**]') + + if username == 'admin': + add_user() + + # Ask for input + help_text = '[Du kan hitta en profils Facebook-ID här](https://lookup-id.com/)' + user_input = str(st.text_input('Skriv in telefonnummer eller Facebook-ID', help=help_text, placeholder='Skriv här...')).strip() - # Present result. - if r != None: - if r['column_find'] == 'ID': - id = r['find'] - r['find'] = f'{id}' - st.write(f"{r['name']}, {r['column_find']}: {r['find']}", unsafe_allow_html=True) + if user_input != '': + s = [] + for i in user_input: + if i.isnumeric(): + s.append(str(i)) + s = ''.join(s) + + if s[0:2] == '07' and len(s) == 10: + s = '46' + s[1:] + + # Search for info. + r = main.search(s) + + # Present result. + if r != None: + if r['column_find'] == 'ID': + id = r['find'] + r['find'] = f'{id}' + st.write(f"{r['name']}, {r['column_find']}: {r['find']}", unsafe_allow_html=True) + + with st.expander('Mer information'): + st.dataframe(r['df'], use_container_width=True) + else: + st.text('Not found.') - with st.expander('Mer information'): - st.dataframe(r['df'], use_container_width=True) - else: - st.text('Not found.') + authenticator.logout('Logout', 'main') + elif authentication_status is False: + st.error('Fel användarnamn/lösenord') -elif authentication_status is False: - st.error('Fel användarnamn/lösenord') + elif authentication_status is None: + st.warning('Skriv in användarnamn och lösenord') -elif authentication_status is None: - st.warning('Skriv in användarnamn och lösenord') +main() diff --git a/create_credentials.py b/create_credentials.py index 8dcac11..d55031f 100644 --- a/create_credentials.py +++ b/create_credentials.py @@ -2,62 +2,77 @@ import streamlit_authenticator as stauth from sys import argv import yaml -def credentials(username, email, name, pwd): +def load_credentials(): + with open('credentials.yaml','r') as f: + return yaml.safe_load(f) + +def credentials(username, name, pwd): pwd = stauth.Hasher([pwd]).generate()[0] + if username in list(load_credentials()['credentials']['usernames'].keys()): + print('Användarnamnet finns redan.') + return 'Användarnamnet finns redan.', False credentials = { 'text': f''' {username}: - email: {email} + email: '', name: {name} - password: {pwd} + password: '***' ''', 'data': {username: - {'email': email, + {'email': '', 'name': name, - 'pwd': pwd} + 'password': pwd} } } - return credentials + for k, v in credentials.items(): + print(k, v) + return credentials, True -def update(c): - print(c['text']) - if input('Update credentials file? (y/n)') in ['y', 'yes']: +def update(c, print_out=True, update=False): + if print_out: + print(c['text']) + if not update: + if input('Update credentials file? (y/n)') in ['y', 'yes']: + update = True + if update: try: - with open('credentials.yaml','r') as f: - cur_yaml = yaml.safe_load(f) # Note the safe_load - cur_yaml['credentials']['usernames'].update(c['data']) - + cur_yaml = load_credentials() + cur_yaml['credentials']['usernames'].update(c['data']) + + + if cur_yaml: with open('credentials.yaml','w') as f: - yaml.safe_dump(cur_yaml, f) # Also note the safe_dump + yaml.safe_dump(cur_yaml, f) except FileNotFoundError: print('Found no yaml file') + + return c['text'], False + +# if __name__ == '__main__': +# if len(argv) == 2: +# if argv[1] == 'help': +# print('username, name, pwd') +# exit() +# pwd = argv[1] +# print(stauth.Hasher([pwd]).generate()[0]) + +# elif len(argv) == 4: +# username = argv[1] +# name = argv[2] +# pwd = argv[3] +# c = credentials(username, name, pwd) +# update(c) -if len(argv) == 2: - if argv[1] == 'help': - print('username, email, name, pwd') - exit() - pwd = argv[1] - print(stauth.Hasher([pwd]).generate()[0]) - -elif len(argv) == 5: - username = argv[1] - email = argv[2] - name = argv[3] - pwd = argv[4] - c = credentials(username, email, name, pwd) - update(c) - -else: - pwd = input('Password: ').strip() - username = input('Username: ') - if username != '': - email = input('Email: ') - name = input('Name: ') - c = credentials(username, email, name, pwd) - update(c) - else: - print(stauth.Hasher([pwd]).generate()[0]) +# else: +# pwd = input('Password: ').strip() +# username = input('Username: ') +# if username != '': +# name = input('Name: ') +# c = credentials(username, name, pwd) +# update(c) +# else: +# print(stauth.Hasher([pwd]).generate()[0])