def set_displayname_by_username(username, displayname): if username is None or displayname is None: return build_error_msg("Missing Display Name or not authenticated."), 404 if not is_valid_displayname_format(displayname): return build_error_msg("Invalid Display Name format."), 404 dao = UserDAO() # Get the authenticated users data auth_user = dao.get_by_username(username) if not is_valid_displayname_content(auth_user.email, displayname): return ( build_error_msg( "Failed to verify your Display Name. This is known to fail for unusual names, please contact the site administrator verify it manually :/" ), 404, ) # Look for a temporary user who already has this displayname set but never logged on before (no auth_token) # These are most likely temporary users created by the db update script tmp_user = dao.get_temporary_user_by_displayname(displayname) if tmp_user: # Replace tmp user with auth user in the problem reports (if there is any) count = ProblemReportFullDAO().replace_user_id(tmp_user.id, auth_user.id) count = dao.delete_by_id(tmp_user.id) if count < 1: # todo log this, but its not a big issue if tmp users stay in the DB pass update_count = dao.set_displayname_by_id(auth_user.id, displayname) if 1 == update_count: auth_user.displayname = displayname return {"user": auth_user.as_dict()}, 201 # created, maybe just send 200 OK?! return build_error_msg("Failed to update your Display Name"), 404