feat(user); adds update methods and template

This commit is contained in:
2024-11-30 01:02:49 -03:00
parent 59a44f4ee2
commit 5b8bf004fa
3 changed files with 237 additions and 9 deletions

View File

@@ -7,6 +7,7 @@ from app.extensions import db
from app.utils.alert_type import AlertType
from app.utils.errors.users.user_register_errors import UserRegisterErrors
from app.utils.flash_message import FlashMessage
from app.utils.flash_message_category import FlashMessageCategory
from app.utils.validators import UserValidators
class UserService:
@@ -64,4 +65,53 @@ class UserService:
except UserRegisterErrors as e:
flash(FlashMessage(e.message, AlertType.DANGER.value ))
@staticmethod
def get_user():
user_id = session.get("id")
if user_id is None:
session.clear()
return None
user = db.session.execute(db.select(User).filter_by(id=user_id)).one_or_none()
if user is None:
session.clear()
return user
@staticmethod
def update_password(form: ImmutableMultiDict, user: User):
try:
password = UserValidators.is_valid_password(form.get("password"))
new_password = UserValidators.is_valid_password(form.get("new_password"))
new_password_confirmation = UserValidators.is_valid_password(form.get("new_password_confirmation"))
UserValidators.passwords_match(new_password, new_password_confirmation)
if check_password_hash(user.password, password):
user.password = generate_password_hash(new_password)
db.session.commit()
db.session.flush()
flash(FlashMessage("Password updated", AlertType.SUCCESS.value, FlashMessageCategory.PASSWORD))
else:
raise UserRegisterErrors("Invalid password")
except UserRegisterErrors as e:
db.session.rollback()
flash(FlashMessage(e.message, AlertType.DANGER.value, FlashMessageCategory.PASSWORD ))
@staticmethod
def update_personal_info(form: ImmutableMultiDict, user: User):
try:
email: str = UserValidators.is_valid_email(form.get("email"))
phone_number: str = UserValidators.is_valid_phone_number(form.get("phonenumber"))
address: str = UserValidators.is_valid_address(form.get("address"))
user.email = email
user.phone_number = phone_number
user.address = address
db.session.commit()
db.session.flush()
flash(FlashMessage("Personal information updated", AlertType.SUCCESS.value, FlashMessageCategory.PERSONAL_INFO))
except UserRegisterErrors as e:
db.session.rollback()
flash(FlashMessage(e.message, AlertType.DANGER.value, FlashMessageCategory.PERSONAL_INFO ))