Skip to content

Commit

Permalink
splitted update_personal_data() into 2 APIs: update_user_name() and u…
Browse files Browse the repository at this point in the history
…pdate_user_language()
  • Loading branch information
alessandrodi committed Sep 26, 2024
1 parent 2bea72f commit 8da5c26
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/eduid/webapp/personal_data/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,25 @@ class PersonalDataSchema(EduidSchema):
legal_name = fields.String(required=False)
language = fields.String(required=True, attribute="preferredLanguage")

class UserNameRequestSchema(EduidSchema, CSRFRequestMixin):
given_name = fields.String(required=True, validate=[validate_nonempty])
chosen_given_name = fields.String(required=False)
surname = fields.String(required=True, validate=[validate_nonempty])
legal_name = fields.String(required=False)

class UserNameSchema(EduidSchema):
given_name = fields.String(required=True, attribute="givenName")
chosen_given_name = fields.String(required=False)
surname = fields.String(required=True)
legal_name = fields.String(required=False)
language = fields.String(required=True, attribute="preferredLanguage")


class UserLanguageRequestSchema(EduidSchema, CSRFRequestMixin):
language = fields.String(required=True, default="en", validate=validate_language)

class UserLanguageSchema(EduidSchema):
language = fields.String(required=True, attribute="preferredLanguage")

class UserPreferencesSchema(EduidSchema):
always_use_security_key = fields.Boolean(required=True, default=True)
Expand All @@ -49,6 +68,19 @@ class PersonalDataResponsePayload(PersonalDataSchema, CSRFResponseMixin):

payload = fields.Nested(PersonalDataResponsePayload)

class UserNameResponseSchema(FluxStandardAction):
class UserNameResponsePayload(UserNameSchema, CSRFResponseMixin):
pass

payload = fields.Nested(UserNameResponsePayload)


class UserLanguageResponseSchema(FluxStandardAction):
class UserLanguageResponsePayload(UserLanguageSchema, CSRFResponseMixin):
pass

payload = fields.Nested(UserLanguageResponsePayload)


class IdentitiesResponseSchema(FluxStandardAction):
class IdentitiesResponsePayload(EmailSchema, CSRFResponseMixin):
Expand Down
63 changes: 63 additions & 0 deletions src/eduid/webapp/personal_data/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
IdentitiesResponseSchema,
PersonalDataRequestSchema,
PersonalDataResponseSchema,
UserLanguageRequestSchema,
UserLanguageResponseSchema,
UserNameRequestSchema,
UserNameResponseSchema,
UserPreferencesRequestSchema,
UserPreferencesResponseSchema,
)
Expand Down Expand Up @@ -82,6 +86,65 @@ def update_personal_data(
personal_data = personal_data_user.to_dict()
return success_response(payload=personal_data, message=PDataMsg.save_success)

@pd_views.route("/user/name", methods=["POST"])
@UnmarshalWith(UserNameRequestSchema)
@MarshalWith(UserNameResponseSchema)
@require_user
def update_user_name(
user: User, given_name: str, surname: str, chosen_given_name: str | None = None
) -> FluxData:
personal_data_user = PersonalDataUser.from_user(user, current_app.private_userdb)
current_app.logger.debug(f"Trying to save user {user}")

# disallow change of first name, surname if the user is verified
if not user.identities.is_verified:
personal_data_user.given_name = given_name
personal_data_user.surname = surname

# set chosen given name to either given name or a subset of given name if supplied
# also allow to set chosen given name to None
if (
chosen_given_name is not None
and is_valid_chosen_given_name(personal_data_user.given_name, chosen_given_name) is False
):
return error_response(message=PDataMsg.chosen_given_name_invalid)

# mypy borked?
# error: Incompatible types in assignment (expression has type "str | None", variable has type "str")
personal_data_user.chosen_given_name = chosen_given_name

try:
save_and_sync_user(personal_data_user)
except UserOutOfSync:
return error_response(message=CommonMsg.out_of_sync)
current_app.stats.count(name="personal_data_saved", value=1)
current_app.logger.info(f"Saved personal data for user {personal_data_user}")

personal_data = personal_data_user.to_dict()
return success_response(payload=personal_data, message=PDataMsg.save_success)

@pd_views.route("/use/language", methods=["POST"])
@UnmarshalWith(UserLanguageRequestSchema)
@MarshalWith(UserLanguageResponseSchema)
@require_user
def update_user_language(
user: User, language: str
) -> FluxData:
personal_data_user = PersonalDataUser.from_user(user, current_app.private_userdb)
current_app.logger.debug(f"Trying to save user {user}")

personal_data_user.language = language

try:
save_and_sync_user(personal_data_user)
except UserOutOfSync:
return error_response(message=CommonMsg.out_of_sync)
current_app.stats.count(name="personal_data_saved", value=1)
current_app.logger.info(f"Saved personal data for user {personal_data_user}")

personal_data = personal_data_user.to_dict()
return success_response(payload=personal_data, message=PDataMsg.save_success)


@pd_views.route("/preferences", methods=["GET"])
@MarshalWith(UserPreferencesResponseSchema)
Expand Down

0 comments on commit 8da5c26

Please sign in to comment.