diff --git a/app/services/user_service.py b/app/services/user_service.py index 9b5a7f7..251a40b 100644 --- a/app/services/user_service.py +++ b/app/services/user_service.py @@ -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 )) + + \ No newline at end of file diff --git a/app/templates/users/index.html b/app/templates/users/index.html index d50c7c1..63523fd 100644 --- a/app/templates/users/index.html +++ b/app/templates/users/index.html @@ -1,8 +1,177 @@ -{% extends "layout/layout.html" %} -{% block title %} Users {% endblock %} -{% block head %} - {{ super() }} -{% endblock %} -{% block content %} -

User's page!

+{% extends "layout/layout.html" %} {% from "forms/submit-btn.html" import +form_submit_button %} {% from "forms/validation-block.html" import +form_field_validation %} {% from "layout/inner_header.html" import +inner_header%} {% block title %} Users {% endblock %} {% block head %} {{ +super() }} + +{% endblock %} {% block content %} +
+
+
+ {{ inner_header("Welcome, " ~ user.name ~ "!" ) }} +
+
+
+
+ {% with messages = get_flashed_messages() %} + {%if messages %} + {%if messages[0].category == FlashMessageCategory.PERSONAL_INFO %} +
{% include 'message.html' %}
+ {% endif %} + {% endif %} + {% endwith %} +
Personal information
+
+
+ + +
+
+ + +
+
+ + +
+
+ + + {{ form_field_validation(FORM_ERRORS['REQUIRED'] + " " + + FORM_ERRORS['PHONE_NUMBER_FORMAT']) }} +
+
+ + + {{ form_field_validation(FORM_ERRORS['REQUIRED'] + " " + + FORM_ERRORS['VALID_EMAIL']) }} +
+
+ + + {{ form_field_validation(FORM_ERRORS['REQUIRED']) }} +
+ +
+ {{ form_submit_button("Submit") }} +
+
+
+
+
+
+ {% with messages = get_flashed_messages() %} + {%if messages %} + {%if messages[0].category == FlashMessageCategory.PASSWORD %} +
{% include 'message.html' %}
+ {% endif %} + {% endif %} + {% endwith %} +
Update password
+
+
+ + + {{ form_field_validation(FORM_ERRORS['REQUIRED'] + " " + + FORM_ERRORS['PASSWORD_LENGTH']) }} +
+
+ + + {{ form_field_validation(FORM_ERRORS['REQUIRED'] + " " + + FORM_ERRORS['PASSWORD_LENGTH']) }} +
+
+ + + {{ form_field_validation(FORM_ERRORS['REQUIRED'] + " " + + FORM_ERRORS['PASSWORD_LENGTH'] + " " + + FORM_ERRORS['MUST_MATCH_PASSWORD']) }} +
+ +
+ {{ form_submit_button("Submit") }} +
+
+
+
+
+
+
+ + {% endblock %} diff --git a/app/users/routes.py b/app/users/routes.py index 66054c3..6634c7a 100644 --- a/app/users/routes.py +++ b/app/users/routes.py @@ -4,10 +4,19 @@ from app.users import bp from app.utils.form_errors_dict import FORM_ERRORS from app.utils.helpers import login_required -@bp.route('/') +@bp.route('/', methods=["GET", "POST"]) @login_required def index(): - return render_template("users/index.html") + user = UserService.get_user() + if user is None: + return redirect("login") + user = user[0] + if request.method == 'POST': + if request.form.get("password_update"): + UserService.update_password(request.form, user) + elif request.form.get("personal_information"): + UserService.update_personal_info(request.form, user) + return render_template("users/index.html",user=user,FORM_ERRORS=FORM_ERRORS) @bp.route('/login', methods=["GET", "POST"]) def login():