From d61c402aeafa2a54adde0fe0420e801b550d3674 Mon Sep 17 00:00:00 2001 From: Son NK Date: Mon, 22 Jul 2019 21:28:17 +0200 Subject: [PATCH] user can choose name --- app/models.py | 12 +++++++++++- app/oauth/templates/oauth/authorize.html | 17 ++++++++++++++--- app/oauth/views/authorize.py | 24 ++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/app/models.py b/app/models.py index 42cc0a9d..4acbafe9 100644 --- a/app/models.py +++ b/app/models.py @@ -224,6 +224,13 @@ class User(db.Model, ModelMixin, UserMixin): list(set(all_gen_emails).difference(set([suggested_gen_email]))), ) + def suggested_names(self) -> (str, [str]): + """return suggested name and other name choices """ + + other_name = convert_to_id(self.name) + + return self.name, [other_name, "Anonymous", "whoami"] + class ActivationCode(db.Model, ModelMixin): """For activate user account""" @@ -433,7 +440,10 @@ class ClientUser(db.Model, ModelMixin): for scope in self.client.get_scopes(): if scope == Scope.NAME: - res[Scope.NAME.value] = self.user.name + if self.name: + res[Scope.NAME.value] = self.name + else: + res[Scope.NAME.value] = self.user.name elif scope == Scope.AVATAR_URL: if self.user.profile_picture_id: res[Scope.AVATAR_URL.value] = self.user.profile_picture.get_url() diff --git a/app/oauth/templates/oauth/authorize.html b/app/oauth/templates/oauth/authorize.html index 6ff80efa..9970a61d 100644 --- a/app/oauth/templates/oauth/authorize.html +++ b/app/oauth/templates/oauth/authorize.html @@ -76,15 +76,26 @@ -
+
- +
- {{ current_user.name }} + + +
OR
+
+ +
+ {% if current_user.profile_picture_id %}
diff --git a/app/oauth/views/authorize.py b/app/oauth/views/authorize.py index c20af9c7..38fae020 100644 --- a/app/oauth/views/authorize.py +++ b/app/oauth/views/authorize.py @@ -65,6 +65,7 @@ def authorize(): if request.method == "GET": if current_user.is_authenticated: suggested_email, other_emails, email_suffix = None, [], None + suggested_name, other_names = None, [] # user has already allowed this client client_user: ClientUser = ClientUser.get_by( @@ -76,6 +77,7 @@ def authorize(): user_info = client_user.get_user_info() else: suggested_email, other_emails = current_user.suggested_emails() + suggested_name, other_names = current_user.suggested_names() email_suffix = random_string(6) return render_template( @@ -86,6 +88,8 @@ def authorize(): Scope=Scope, suggested_email=suggested_email, personal_email=current_user.email, + suggested_name=suggested_name, + other_names=other_names, other_emails=other_emails, email_suffix=email_suffix, EMAIL_DOMAIN=EMAIL_DOMAIN, @@ -115,6 +119,9 @@ def authorize(): custom_email_prefix = request.form.get("custom-email-prefix") chosen_email = request.form.get("suggested-email") + suggested_name = request.form.get("suggested-name") + custom_name = request.form.get("custom-name") + gen_email = None if custom_email_prefix: # check if user can generate custom email @@ -143,6 +150,23 @@ def authorize(): if gen_email: client_user.gen_email_id = gen_email.id + if custom_name: + LOG.d( + "use custom name %s for user %s client %s", + custom_name, + current_user, + client, + ) + client_user.name = custom_name + elif suggested_name != current_user.name: + LOG.d( + "use another name %s for user %s client %s", + custom_name, + current_user, + client, + ) + client_user.name = suggested_name + db.session.flush() LOG.d("create client-user for client %s, user %s", client, current_user)