Skip to content

Commit

Permalink
BE: Fix period at end of pref. name being rejected (#509)
Browse files Browse the repository at this point in the history
  • Loading branch information
justuswilhelm authored May 24, 2024
2 parents a082ab5 + 9b83e21 commit 31f1ed0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
#
# Copyright (C) 2024 JWP Consulting GK
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""Allow for user preferred names to end on period or colon."""
# Generated by Django 5.0.3 on 2024-05-24 12:45
from django.db import migrations, models


class Migration(migrations.Migration):
"""Migration."""

dependencies = [
("auth", "0012_alter_user_first_name_max_length"),
("user", "0014_user_preferred_name"),
]

operations = [
migrations.RemoveConstraint(
model_name="user",
name="preferred_name",
),
migrations.AddConstraint(
model_name="user",
constraint=models.CheckConstraint(
check=models.Q(
("preferred_name__regex", "^([.:]\\s|[^.:])+[.:]?$")
),
name="preferred_name",
violation_error_message="Preferred name can only contain '.' or ':' if followed by whitespace or if located at the end.",
),
),
]
10 changes: 7 additions & 3 deletions backend/projectify/user/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,14 @@ class Meta(BaseModel.Meta, AbstractBaseUser.Meta):
constraints = (
models.CheckConstraint(
name="preferred_name",
# Match period followed by space, or not period
check=models.Q(preferred_name__regex=r"^([.:]\s|[^.:])+$"),
# Match period, colon followed by space, or not period
# or period, colon at end of word
check=models.Q(
preferred_name__regex=r"^([.:]\s|[^.:])+[.:]?$"
),
violation_error_message=_(
"Preferred name can only contain '.' or ':' if followed by whitespace."
"Preferred name can only contain '.' or ':' if followed "
"by whitespace or if located at the end."
),
),
)
7 changes: 7 additions & 0 deletions backend/projectify/user/test/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,18 @@ def test_preferred_name_validation(self, user: User) -> None:
with pytest.raises(ValidationError):
user.full_clean()

user.preferred_name = "www.google.com."
with pytest.raises(ValidationError):
user.full_clean()

user.preferred_name = "http://localhost"
with pytest.raises(ValidationError):
user.full_clean()

# Allowed
user.preferred_name = "John McHurDur Jr."
user.full_clean()

user.preferred_name = "http: //localhost"
user.full_clean()

Expand Down

0 comments on commit 31f1ed0

Please sign in to comment.