-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathuser_man.py
64 lines (51 loc) · 2.08 KB
/
user_man.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# PLP 17.03.2023
import getpass
from crewverve.models import User
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine, select
# Set up the database connection (NO ES IGUAL A FLASK. NO ESTAMOS EN UN PROCESO DE FLASK AHORA!!!!)
engine = create_engine('sqlite:///./instance/crewverve.db')
# Create a new session factory
Session = sessionmaker(bind=engine)
def create_user(username, password):
login = username.lower()
# Create user with username (Lower case)
# email_user es un dato obligatorio, si no lo pasamos el commit lo hace mal y genera un except
user = User(name_user=login, email_user = '[email protected]')
# Hash the password (Lower) using the User model's set_password method
user.set_password(password)
# Add the user to the database
session = Session()
session.add(user)
try:
session.commit()
print(f'User "{login}" created successfully')
except IntegrityError:
session.rollback()
print(f'User "{login}" already exists')
def update_password(username, password):
# Retrieve the user from the database
login = username.lower()
session = Session()
user = session.execute(select(User).filter_by(name_user=login)).scalar_one_or_none()
if user:
# Hash the new password using the User model's set_password method
user.set_password(password)
# Update the user's password in the database
session.commit()
print(f'Password updated successfully for user "{login}"')
else:
print(f'User "{login}" not found')
if __name__ == '__main__':
action = input('What would you like to do? (create/update) ')
if action in 'create':
username = input('Enter username: ')
password = getpass.getpass('Enter password: ')
create_user(username, password)
elif action in 'update':
username = input('Enter username: ')
password = getpass.getpass('Enter new password: ')
update_password(username, password)
else:
print('Invalid action')