Skip to content

Commit

Permalink
feat: shorten profile name fields to 150 characters
Browse files Browse the repository at this point in the history
First and last name need to be limited to 150 characters on the Profile model.
Updates to name information are also sent to keycloak, which will put the name
information into the API tokens that it produces.

User model has 150 character name fields. Upon receiving an API token
this backend will try to update the user object with the information in
the token. If the name is over 150 characters an error will be produced
and the user will be unable to use the API. Users with this error will
also be unable to resolve the issue by themselves.

Refs: HP-2344
  • Loading branch information
charn committed Apr 18, 2024
1 parent f974072 commit 0ee92ae
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Generated by Django 4.2.9 on 2024-04-17 10:00

from django.db import migrations
from django.db.models.functions import Substr

import utils.fields


def truncate_name_fields(apps, schema_editor):
Profile = apps.get_model("profiles", "Profile")
Profile.objects.update(
first_name=Substr("first_name", 1, 150), last_name=Substr("last_name", 1, 150)
)


class Migration(migrations.Migration):

dependencies = [
("profiles", "0057_remove_profile_id_default_value__noop"),
]

operations = [
migrations.RunPython(truncate_name_fields, migrations.RunPython.noop),
migrations.AlterField(
model_name="profile",
name="first_name",
field=utils.fields.NullToEmptyCharField(
blank=True, db_index=True, max_length=150
),
),
migrations.AlterField(
model_name="profile",
name="last_name",
field=utils.fields.NullToEmptyCharField(
blank=True, db_index=True, max_length=150
),
),
]
4 changes: 2 additions & 2 deletions profiles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@

class Profile(UUIDModel, SerializableMixin):
user = models.OneToOneField(User, on_delete=models.PROTECT, null=True, blank=True)
first_name = NullToEmptyCharField(max_length=255, blank=True, db_index=True)
last_name = NullToEmptyCharField(max_length=255, blank=True, db_index=True)
first_name = NullToEmptyCharField(max_length=150, blank=True, db_index=True)
last_name = NullToEmptyCharField(max_length=150, blank=True, db_index=True)
nickname = NullToEmptyCharField(max_length=32, blank=True, db_index=True)
language = models.CharField(
max_length=2,
Expand Down
2 changes: 1 addition & 1 deletion profiles/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ def execute_string_field_max_length_validation_test(
class TestProfileValidation(ValidationTestBase):
@pytest.mark.parametrize(
"field_name,max_length",
[("first_name", 255), ("last_name", 255), ("nickname", 32)],
[("first_name", 150), ("last_name", 150), ("nickname", 32)],
)
def test_string_field_max_length(self, field_name, max_length, profile):
self.execute_string_field_max_length_validation_test(
Expand Down

0 comments on commit 0ee92ae

Please sign in to comment.