Skip to content

Commit

Permalink
[#1456] Added 'infix' (tussenvoegsel) to User model
Browse files Browse the repository at this point in the history
  • Loading branch information
Bart van der Schoor committed May 25, 2023
1 parent d1e4c71 commit 025c12c
Show file tree
Hide file tree
Showing 14 changed files with 148 additions and 73 deletions.
4 changes: 3 additions & 1 deletion src/open_inwoner/accounts/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class _UserAdmin(ImageCroppingMixin, UserAdmin):
{
"fields": (
"first_name",
"infix",
"last_name",
"contact_type",
"bsn",
Expand Down Expand Up @@ -130,13 +131,14 @@ class _UserAdmin(ImageCroppingMixin, UserAdmin):
list_display = (
"email",
"first_name",
"infix",
"last_name",
"login_type",
"is_staff",
"is_active",
"contact_type",
)
search_fields = ("first_name", "last_name", "email")
search_fields = ("first_name", "infix", "last_name", "email")
ordering = ("email",)
filter_horizontal = (
"user_contacts",
Expand Down
8 changes: 8 additions & 0 deletions src/open_inwoner/accounts/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

class CustomRegistrationForm(RegistrationForm):
first_name = forms.CharField(label=_("First name"), max_length=255, required=True)
infix = forms.CharField(label=_("Infix"), max_length=64, required=False)
last_name = forms.CharField(label=_("Last name"), max_length=255, required=True)
invite = forms.ModelChoiceField(
queryset=Invite.objects.all(),
Expand All @@ -43,6 +44,7 @@ class Meta:
fields = (
"email",
"first_name",
"infix",
"last_name",
"phonenumber",
"password1",
Expand Down Expand Up @@ -97,6 +99,7 @@ class Meta:
model = User
fields = (
"first_name",
"infix",
"last_name",
"display_name",
"email",
Expand Down Expand Up @@ -127,6 +130,7 @@ class Meta:
model = User
fields = (
"first_name",
"infix",
"last_name",
"email",
"invite",
Expand All @@ -136,17 +140,21 @@ def __init__(self, user, *args, **kwargs):
super().__init__(*args, **kwargs)

self.fields["first_name"].required = True
self.fields["infix"].required = False
self.fields["last_name"].required = True

if user.is_digid_and_brp():
self.fields["first_name"].disabled = True
self.fields["infix"].disabled = True
self.fields["last_name"].disabled = True

# this is for the rare case of retrieving partial data from haalcentraal
if not user.first_name:
del self.fields["first_name"]
if not user.last_name:
del self.fields["last_name"]
if not user.infix:
del self.fields["infix"]

def clean_email(self):
email = self.cleaned_data["email"]
Expand Down
26 changes: 26 additions & 0 deletions src/open_inwoner/accounts/migrations/0060_user_infix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 3.2.15 on 2023-05-23 12:26

from django.db import migrations, models

import open_inwoner.utils.validators


class Migration(migrations.Migration):

dependencies = [
("accounts", "0059_auto_20230412_1637"),
]

operations = [
migrations.AddField(
model_name="user",
name="infix",
field=models.CharField(
blank=True,
default="",
max_length=64,
validators=[open_inwoner.utils.validators.validate_charfield_entry],
verbose_name="Infix",
),
),
]
14 changes: 9 additions & 5 deletions src/open_inwoner/accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ class User(AbstractBaseUser, PermissionsMixin):
default="",
validators=[validate_charfield_entry],
)
infix = models.CharField(
verbose_name=_("Infix"),
max_length=64,
blank=True,
default="",
validators=[validate_charfield_entry],
)
last_name = models.CharField(
verbose_name=_("Last name"),
max_length=255,
Expand Down Expand Up @@ -204,11 +211,8 @@ def __init__(self, *args, **kwargs):
self._old_bsn = self.bsn

def get_full_name(self):
"""
Returns the first_name plus the last_name, with a space in between.
"""
full_name = "%s %s" % (self.first_name, self.last_name)
return full_name.strip()
parts = (self.first_name, self.infix, self.last_name)
return " ".join(p for p in parts if p)

def get_short_name(self):
"Returns the short name for the user."
Expand Down
7 changes: 7 additions & 0 deletions src/open_inwoner/accounts/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def get_conversations_for_user(self, user: "User") -> "MessageQuerySet":
- other_user_id
- other_user_email
- other_user_first_name
- other_user_infix
- other_user_last_name
other_user_id matches the value of either sender or receiver field,
Expand Down Expand Up @@ -57,6 +58,12 @@ def get_conversations_for_user(self, user: "User") -> "MessageQuerySet":
default=F("receiver__first_name"),
)
)
.annotate(
other_user_infix=Case(
When(receiver=user, then=F("sender__infix")),
default=F("receiver__infix"),
)
)
.annotate(
other_user_last_name=Case(
When(receiver=user, then=F("sender__last_name")),
Expand Down
1 change: 1 addition & 0 deletions src/open_inwoner/accounts/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Meta:
model = "accounts.User"

first_name = factory.Faker("first_name")
infix = factory.fuzzy.FuzzyChoice(["de", "van", "van de"])
last_name = factory.Faker("last_name")
# Note that 'example.org' addresses are always redirected to the registration_necessary view
email = factory.LazyAttribute(
Expand Down
1 change: 1 addition & 0 deletions src/open_inwoner/accounts/tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def test_registration_succeeds_with_right_user_input(self):
form = register_page.forms["registration-form"]
form["email"] = self.user.email
form["first_name"] = self.user.first_name
form["infix"] = ""
form["last_name"] = self.user.last_name
form["password1"] = self.user.password
form["password2"] = self.user.password
Expand Down
2 changes: 1 addition & 1 deletion src/open_inwoner/accounts/tests/test_profile_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ class MyDataTests(HaalCentraalMixin, WebTest):
"first_name": "Merel",
"initials": "M.",
"last_name": "Kooyman",
"prefix": None,
"prefix": "de",
"birthday": "10-04-1982",
"birthday_place": "Leerdam",
"gender": "vrouw",
Expand Down
13 changes: 13 additions & 0 deletions src/open_inwoner/accounts/tests/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@


class UserTests(TestCase):
def test_get_full_name(self):
user = User(first_name="Foo", infix="de", last_name="Bar")
self.assertEqual(user.get_full_name(), "Foo de Bar")

user = User(first_name="Foo", infix="", last_name="Bar")
self.assertEqual(user.get_full_name(), "Foo Bar")

user = User(first_name="", infix="de", last_name="Bar")
self.assertEqual(user.get_full_name(), "de Bar")

user = User(first_name="", infix="", last_name="Bar")
self.assertEqual(user.get_full_name(), "Bar")

@freeze_time("2021-07-07 12:00:00")
def test_get_age_same_day(self):
with freeze_time("1990-07-07"):
Expand Down
7 changes: 5 additions & 2 deletions src/open_inwoner/accounts/views/inbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,12 @@ def annotate_conversations(self, conversations):
"inbox:index", kwargs={self.slug_field: c.other_user_uuid}
)
# note these are annotations (not models)
c.other_user_full_name = " ".join(
(c.other_user_first_name, c.other_user_last_name)
parts = (
c.other_user_first_name,
c.other_user_infix,
c.other_user_last_name,
)
c.other_user_full_name = " ".join(p for p in parts if p)
return conversations

def get_other_user(self, conversations: dict) -> Optional[User]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
"geslachtsaanduiding": "vrouw",
"naam": {
"aanhef": "Geachte mevrouw Kooyman",
"aanschrijfwijze": "M. Kooyman",
"gebruikInLopendeTekst": "mevrouw Kooyman",
"aanschrijfwijze": "M. de Kooyman",
"gebruikInLopendeTekst": "mevrouw de Kooyman",
"aanduidingNaamgebruik": "eigen",
"voornamen": "Merel",
"voorletters": "M.",
"voorvoegsel": "de",
"geslachtsnaam": "Kooyman"
},
"verblijfplaats": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"voornamen": "Merel",
"geslachtsnaam": "Kooyman",
"volledigeNaam": "Merel Kooyman",
"voorvoegsel": "de",
"voorletters": "M."
},
"geboorte": {
Expand Down
Loading

0 comments on commit 025c12c

Please sign in to comment.