Skip to content

Commit

Permalink
Use a custom dictionnary instead of making unaccent immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
tonial committed Jan 10, 2025
1 parent 75462ed commit 1bd5d3b
Show file tree
Hide file tree
Showing 32 changed files with 302 additions and 29 deletions.
22 changes: 22 additions & 0 deletions itou/users/migrations/0020_add_simple_unaccent_search_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 5.1.4 on 2025-01-10 09:31

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("users", "0019_drop_jobseekerprofile_ata_allocation_since"),
]

operations = [
migrations.RunSQL(
"CREATE TEXT SEARCH CONFIGURATION simple_unaccent (COPY=simple);",
reverse_sql="DROP TEXT SEARCH CONFIGURATION simple_unaccent;",
),
migrations.RunSQL(
"ALTER TEXT SEARCH CONFIGURATION simple_unaccent "
"ALTER MAPPING FOR hword, hword_part, word "
"WITH unaccent,simple;",
reverse_sql=migrations.RunSQL.noop,
),
]
20 changes: 0 additions & 20 deletions itou/users/migrations/0020_make_unnaccent_immutable.py

This file was deleted.

6 changes: 2 additions & 4 deletions itou/users/migrations/0021_user_full_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class Migration(migrations.Migration):
dependencies = [
("users", "0020_make_unnaccent_immutable"),
("users", "0020_add_simple_unaccent_search_config"),
]

operations = [
Expand All @@ -17,9 +17,7 @@ class Migration(migrations.Migration):
field=models.GeneratedField(
db_persist=True,
expression=django.contrib.postgres.search.SearchVector(
django.contrib.postgres.lookups.Unaccent("first_name"),
django.contrib.postgres.lookups.Unaccent("last_name"),
config="simple",
"first_name", "last_name", config="simple_unaccent"
),
output_field=django.contrib.postgres.search.SearchVectorField(),
verbose_name="nom complet utilisé pour rechercher un utilisateur",
Expand Down
3 changes: 1 addition & 2 deletions itou/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from django.conf import settings
from django.contrib.auth.models import AbstractUser, UserManager
from django.contrib.postgres.indexes import GinIndex, OpClass
from django.contrib.postgres.lookups import Unaccent
from django.contrib.postgres.search import SearchVector, SearchVectorField
from django.core.exceptions import ValidationError
from django.core.serializers.json import DjangoJSONEncoder
Expand Down Expand Up @@ -173,7 +172,7 @@ class User(AbstractUser, AddressMixin):
choices=Title.choices,
)
full_name = models.GeneratedField(
expression=SearchVector(Unaccent("first_name"), Unaccent("last_name"), config="simple"),
expression=SearchVector("first_name", "last_name", config="simple_unaccent"),
output_field=SearchVectorField(),
verbose_name="nom complet utilisé pour rechercher un utilisateur",
db_persist=True,
Expand Down
2 changes: 1 addition & 1 deletion itou/www/autocomplete/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def gps_users_autocomplete(request):
)
)

search_query = SearchQuery(unidecode(term), config="simple")
search_query = SearchQuery(term, config="simple_unaccent")
users_qs = users_qs.filter(full_name=search_query)
users_qs = users_qs.annotate(rank=SearchRank("full_name", search_query)).order_by("-rank")

Expand Down
2 changes: 2 additions & 0 deletions tests/api/__snapshots__/test_geiq.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
"users_user"."ban_api_resolved_address",
"users_user"."insee_city_id",
"users_user"."title",
"users_user"."full_name",
"users_user"."email",
"users_user"."phone",
"users_user"."kind",
Expand Down Expand Up @@ -510,6 +511,7 @@
"users_user"."ban_api_resolved_address",
"users_user"."insee_city_id",
"users_user"."title",
"users_user"."full_name",
"users_user"."email",
"users_user"."phone",
"users_user"."kind",
Expand Down
2 changes: 2 additions & 0 deletions tests/api/employee_record_api/__snapshots__/tests.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"users_user"."ban_api_resolved_address",
"users_user"."insee_city_id",
"users_user"."title",
"users_user"."full_name",
"users_user"."email",
"users_user"."phone",
"users_user"."kind",
Expand Down Expand Up @@ -344,6 +345,7 @@
"users_user"."ban_api_resolved_address",
"users_user"."insee_city_id",
"users_user"."title",
"users_user"."full_name",
"users_user"."email",
"users_user"."phone",
"users_user"."kind",
Expand Down
1 change: 1 addition & 0 deletions tests/companies/__snapshots__/test_admin.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"users_user"."ban_api_resolved_address",
"users_user"."insee_city_id",
"users_user"."title",
"users_user"."full_name",
"users_user"."email",
"users_user"."phone",
"users_user"."kind",
Expand Down
4 changes: 2 additions & 2 deletions tests/gps/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ def get_autocomplete_results(user, term="gps"):
results = get_autocomplete_results(prescriber)
assert set(results) == {first_beneficiary.pk, second_beneficiary.pk}

# with "martin gps" Martin is the first result
# with "martin gps" Martin is the only match
results = get_autocomplete_results(prescriber, term="martin gps")
assert results == [second_beneficiary.pk, first_beneficiary.pk]
assert results == [second_beneficiary.pk]


@pytest.mark.parametrize(
Expand Down
2 changes: 2 additions & 0 deletions tests/job_applications/__snapshots__/test_transfer.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
"users_user"."ban_api_resolved_address",
"users_user"."insee_city_id",
"users_user"."title",
"users_user"."full_name",
"users_user"."email",
"users_user"."phone",
"users_user"."kind",
Expand Down Expand Up @@ -184,6 +185,7 @@
"users_user"."ban_api_resolved_address",
"users_user"."insee_city_id",
"users_user"."title",
"users_user"."full_name",
"users_user"."email",
"users_user"."phone",
"users_user"."kind",
Expand Down
5 changes: 5 additions & 0 deletions tests/users/__snapshots__/test_admin_views.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"users_user"."ban_api_resolved_address",
"users_user"."insee_city_id",
"users_user"."title",
"users_user"."full_name",
"users_user"."email",
"users_user"."phone",
"users_user"."kind",
Expand Down Expand Up @@ -97,6 +98,7 @@
"users_user"."ban_api_resolved_address",
"users_user"."insee_city_id",
"users_user"."title",
"users_user"."full_name",
"users_user"."email",
"users_user"."phone",
"users_user"."kind",
Expand Down Expand Up @@ -287,6 +289,7 @@
"users_user"."ban_api_resolved_address",
"users_user"."insee_city_id",
"users_user"."title",
"users_user"."full_name",
"users_user"."email",
"users_user"."phone",
"users_user"."kind",
Expand Down Expand Up @@ -377,6 +380,7 @@
T3."ban_api_resolved_address",
T3."insee_city_id",
T3."title",
T3."full_name",
T3."email",
T3."phone",
T3."kind",
Expand Down Expand Up @@ -467,6 +471,7 @@
T3."ban_api_resolved_address",
T3."insee_city_id",
T3."title",
T3."full_name",
T3."email",
T3."phone",
T3."kind",
Expand Down
5 changes: 5 additions & 0 deletions tests/www/apply/__snapshots__/test_list_for_siae.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"users_user"."ban_api_resolved_address",
"users_user"."insee_city_id",
"users_user"."title",
"users_user"."full_name",
"users_user"."email",
"users_user"."phone",
"users_user"."kind",
Expand Down Expand Up @@ -257,6 +258,7 @@
"users_user"."ban_api_resolved_address",
"users_user"."insee_city_id",
"users_user"."title",
"users_user"."full_name",
"users_user"."email",
"users_user"."phone",
"users_user"."kind",
Expand Down Expand Up @@ -349,6 +351,7 @@
"users_user"."ban_api_resolved_address",
"users_user"."insee_city_id",
"users_user"."title",
"users_user"."full_name",
"users_user"."email",
"users_user"."phone",
"users_user"."kind",
Expand Down Expand Up @@ -866,6 +869,7 @@
"users_user"."ban_api_resolved_address",
"users_user"."insee_city_id",
"users_user"."title",
"users_user"."full_name",
"users_user"."email",
"users_user"."phone",
"users_user"."kind",
Expand Down Expand Up @@ -934,6 +938,7 @@
T10."ban_api_resolved_address",
T10."insee_city_id",
T10."title",
T10."full_name",
T10."email",
T10."phone",
T10."kind",
Expand Down
10 changes: 10 additions & 0 deletions tests/www/apply/__snapshots__/test_list_prescriptions.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"users_user"."ban_api_resolved_address",
"users_user"."insee_city_id",
"users_user"."title",
"users_user"."full_name",
"users_user"."email",
"users_user"."phone",
"users_user"."kind",
Expand Down Expand Up @@ -193,6 +194,7 @@
"users_user"."ban_api_resolved_address",
"users_user"."insee_city_id",
"users_user"."title",
"users_user"."full_name",
"users_user"."email",
"users_user"."phone",
"users_user"."kind",
Expand Down Expand Up @@ -286,6 +288,7 @@
T3."ban_api_resolved_address",
T3."insee_city_id",
T3."title",
T3."full_name",
T3."email",
T3."phone",
T3."kind",
Expand Down Expand Up @@ -681,6 +684,7 @@
T5."ban_api_resolved_address",
T5."insee_city_id",
T5."title",
T5."full_name",
T5."email",
T5."phone",
T5."kind",
Expand Down Expand Up @@ -749,6 +753,7 @@
"users_user"."ban_api_resolved_address",
"users_user"."insee_city_id",
"users_user"."title",
"users_user"."full_name",
"users_user"."email",
"users_user"."phone",
"users_user"."kind",
Expand Down Expand Up @@ -1223,6 +1228,7 @@
"users_user"."ban_api_resolved_address",
"users_user"."insee_city_id",
"users_user"."title",
"users_user"."full_name",
"users_user"."email",
"users_user"."phone",
"users_user"."kind",
Expand Down Expand Up @@ -1394,6 +1400,7 @@
T5."ban_api_resolved_address",
T5."insee_city_id",
T5."title",
T5."full_name",
T5."email",
T5."phone",
T5."kind",
Expand Down Expand Up @@ -1462,6 +1469,7 @@
"users_user"."ban_api_resolved_address",
"users_user"."insee_city_id",
"users_user"."title",
"users_user"."full_name",
"users_user"."email",
"users_user"."phone",
"users_user"."kind",
Expand Down Expand Up @@ -1805,6 +1813,7 @@
T3."ban_api_resolved_address",
T3."insee_city_id",
T3."title",
T3."full_name",
T3."email",
T3."phone",
T3."kind",
Expand Down Expand Up @@ -1982,6 +1991,7 @@
T3."ban_api_resolved_address",
T3."insee_city_id",
T3."title",
T3."full_name",
T3."email",
T3."phone",
T3."kind",
Expand Down
Loading

0 comments on commit 1bd5d3b

Please sign in to comment.