From 33e8c10556efe391478da35fac3698b557c2fe77 Mon Sep 17 00:00:00 2001 From: Mark Jacobson <52427991+marksparkza@users.noreply.github.com> Date: Thu, 1 Jul 2021 11:00:10 +0200 Subject: [PATCH] Use a single field for a user's personal name Ref #18 --- ...fba3c5e8ab83_single_field_for_user_name.py | 32 +++++++++++++++++++ odp/api/models/auth.py | 3 +- odp/db/models/user.py | 3 +- odp/identity/forms.py | 3 +- odp/identity/templates/profile.html | 3 +- odp/lib/auth.py | 3 +- odp/lib/users.py | 4 +-- 7 files changed, 39 insertions(+), 12 deletions(-) create mode 100644 migrate/versions/2021_07_01_fba3c5e8ab83_single_field_for_user_name.py diff --git a/migrate/versions/2021_07_01_fba3c5e8ab83_single_field_for_user_name.py b/migrate/versions/2021_07_01_fba3c5e8ab83_single_field_for_user_name.py new file mode 100644 index 00000000..a77fba7e --- /dev/null +++ b/migrate/versions/2021_07_01_fba3c5e8ab83_single_field_for_user_name.py @@ -0,0 +1,32 @@ +"""Single field for user name + +Revision ID: fba3c5e8ab83 +Revises: 249e24966335 +Create Date: 2021-07-01 10:37:02.780672 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'fba3c5e8ab83' +down_revision = '249e24966335' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic ### + op.add_column('user', sa.Column('name', sa.String(), nullable=True)) + op.drop_column('user', 'given_name') + op.drop_column('user', 'family_name') + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic ### + op.add_column('user', sa.Column('family_name', sa.VARCHAR(), autoincrement=False, nullable=True)) + op.add_column('user', sa.Column('given_name', sa.VARCHAR(), autoincrement=False, nullable=True)) + op.drop_column('user', 'name') + # ### end Alembic commands ### diff --git a/odp/api/models/auth.py b/odp/api/models/auth.py index 76d71265..89b6e5d6 100644 --- a/odp/api/models/auth.py +++ b/odp/api/models/auth.py @@ -72,8 +72,7 @@ class IDTokenData(BaseModel): sub: str email: EmailStr email_verified: bool - family_name: Optional[str] - given_name: Optional[str] + name: Optional[str] picture: Optional[str] # The `role` field is used strictly for the special case of indicating a user's diff --git a/odp/db/models/user.py b/odp/db/models/user.py index beaef354..bbf1795d 100644 --- a/odp/db/models/user.py +++ b/odp/db/models/user.py @@ -20,8 +20,7 @@ class User(Base): superuser = Column(Boolean, nullable=False) active = Column(Boolean, nullable=False) verified = Column(Boolean, nullable=False) - family_name = Column(String) - given_name = Column(String) + name = Column(String) picture = Column(String) # many-to-many relationship between institution and user represented by member diff --git a/odp/identity/forms.py b/odp/identity/forms.py index e3a3a2dd..f6b9341b 100644 --- a/odp/identity/forms.py +++ b/odp/identity/forms.py @@ -75,7 +75,6 @@ class AutoLoginForm(FlaskForm): class ProfileForm(FlaskForm): - given_name = StringField(label='First name') - family_name = StringField(label='Last name') + name = StringField(label='Full name') picture = StringField(label='Photo URL') submit = SubmitField(label='Log in') diff --git a/odp/identity/templates/profile.html b/odp/identity/templates/profile.html index 606c2ed5..72fd0a80 100644 --- a/odp/identity/templates/profile.html +++ b/odp/identity/templates/profile.html @@ -8,8 +8,7 @@ {% block content %}
{{ form.csrf_token }} - {{ render_field(form.given_name) }} - {{ render_field(form.family_name) }} + {{ render_field(form.name) }} {{ render_field(form.submit) }}
{% endblock %} diff --git a/odp/lib/auth.py b/odp/lib/auth.py index 94982828..791dc700 100644 --- a/odp/lib/auth.py +++ b/odp/lib/auth.py @@ -29,8 +29,7 @@ def get_token_data(user: User, scopes: List[str]) -> Tuple[AccessTokenData, IDTo sub=user.id, email=user.email, email_verified=user.verified, - family_name=user.family_name, - given_name=user.given_name, + name=user.name, picture=user.picture, role=[], ) diff --git a/odp/lib/users.py b/odp/lib/users.py index 0874a8f0..e9f92426 100644 --- a/odp/lib/users.py +++ b/odp/lib/users.py @@ -304,7 +304,7 @@ def update_user_profile(user_id, **userinfo): """ with transaction(): user = User.query.get(user_id) - for attr in 'family_name', 'given_name', 'picture': + for attr in 'name', 'picture': if attr in userinfo: setattr(user, attr, userinfo[attr]) user.save() @@ -316,6 +316,6 @@ def get_user_profile(user_id): """ user = User.query.get(user_id) info = {} - for attr in 'family_name', 'given_name', 'picture': + for attr in 'name', 'picture': info[attr] = getattr(user, attr) return info