Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add tags and last 4 chars to gift card search vector (#18738)
Iga Karbowiak332e00b48d20
diff --git a/saleor/celeryconf.py b/saleor/celeryconf.pyindex 854b16f8b2..d1d32d731a 100644--- a/saleor/celeryconf.py+++ b/saleor/celeryconf.py@@ -45,6 +45,7 @@ app.autodiscover_tasks("saleor.account.migrations.tasks","saleor.attribute.migrations.tasks","saleor.channel.migrations.tasks",+ "saleor.giftcard.migrations.tasks",],related_name="saleor3_22",)diff --git a/saleor/giftcard/migrations/0023_mark_gift_cards_search_vector_as_dirty.py b/saleor/giftcard/migrations/0023_mark_gift_cards_search_vector_as_dirty.pynew file mode 100644index 0000000000..a28d8a16da--- /dev/null+++ b/saleor/giftcard/migrations/0023_mark_gift_cards_search_vector_as_dirty.py@@ -0,0 +1,27 @@+# Generated by Django 5.2.8 on 2026-01-30 12:58++from django.apps import apps as registry+from django.db import migrations+from django.db.models.signals import post_migrate++from .tasks.saleor3_22 import mark_gift_cards_search_index_as_dirty_task+++def mark_gift_cards_search_index_as_dirty(apps, _schema_editor):+ def on_migrations_complete(sender=None, **kwargs):+ mark_gift_cards_search_index_as_dirty_task.delay()++ sender = registry.get_app_config("attribute")+ post_migrate.connect(on_migrations_complete, weak=False, sender=sender)+++class Migration(migrations.Migration):+ dependencies = [+ ("giftcard", "0022_merge_20250527_1210"),+ ]++ operations = [+ migrations.RunPython(+ mark_gift_cards_search_index_as_dirty, migrations.RunPython.noop+ ),+ ]diff --git a/saleor/giftcard/migrations/tasks/__init__.py b/saleor/giftcard/migrations/tasks/__init__.pynew file mode 100644index 0000000000..e69de29bb2diff --git a/saleor/giftcard/migrations/tasks/saleor3_22.py b/saleor/giftcard/migrations/tasks/saleor3_22.pynew file mode 100644index 0000000000..9523376a0e--- /dev/null+++ b/saleor/giftcard/migrations/tasks/saleor3_22.py@@ -0,0 +1,11 @@+from django.conf import settings++from ....celeryconf import app+from ....core.db.connection import allow_writer+from ...models import GiftCard++[email protected](queue=settings.DATA_MIGRATIONS_TASKS_QUEUE_NAME)+@allow_writer()+def mark_gift_cards_search_index_as_dirty_task():+ GiftCard.objects.filter(search_index_dirty=False).update(search_index_dirty=True)diff --git a/saleor/giftcard/search.py b/saleor/giftcard/search.pyindex 63ef510c23..ce6b4b5ff2 100644--- a/saleor/giftcard/search.py+++ b/saleor/giftcard/search.py@@ -1,3 +1,4 @@+from django.conf import settingsfrom django.contrib.postgres.search import SearchQueryfrom django.db.models import Q, QuerySet, Value, prefetch_related_objects@@ -9,26 +10,34 @@ from .models import GiftCardGIFTCARD_FIELDS_TO_PREFETCH = ["used_by", "created_by"]-def _add_vector(vectors: list[NoValidationSearchVector], field):+def _add_vector(vectors: list[NoValidationSearchVector], field, weight: str):if field:- vectors += [NoValidationSearchVector(Value(field), config="simple")]+ vectors += [+ NoValidationSearchVector(Value(field), config="simple", weight=weight)+ ]def prepare_gift_card_search_vector_value(gift_card: GiftCard,) -> list[NoValidationSearchVector]:- search_vectors = [NoValidationSearchVector(Value(gift_card.code), config="simple")]- _add_vector(search_vectors, gift_card.used_by_email)- _add_vector(search_vectors, gift_card.created_by_email)+ search_vectors = [+ NoValidationSearchVector(+ Value(gift_card.code[-4:]), config="simple", weight="A"+ ),+ NoValidationSearchVector(Value(gift_card.code), config="simple", weight="B"),+ ]+ for tag in gift_card.tags.all()[: settings.GIFT_CARD_MAX_INDEXED_TAGS]:+ _add_vector(search_vectors, tag.name, weight="B")+ _add_vector(search_vectors, gift_card.used_by_email, weight="C")+ _add_vector(search_vectors, gift_card.created_by_email, weight="C")if gift_card.used_by:- _add_vector(search_vectors, gift_card.used_by.email)- _add_vector(search_vectors, gift_card.used_by.first_name)- _add_vector(search_vectors, gift_card.used_by.last_name)+ _add_vector(search_vectors, gift_card.used_by.email, weight="C")+ _add_vector(search_vectors, gift_card.used_by.first_name, weight="C")+ _add_vector(search_vectors, gift_card.used_by.last_name, weight="C")if gift_card.created_by:- _add_vector(search_vectors, gift_card.created_by.email)- _add_vector(search_vectors, gift_card.created_by.first_name)- _add_vector(search_vectors, gift_card.created_by.last_name)-+ _add_vector(search_vectors, gift_card.created_by.email, weight="C")+ _add_vector(search_vectors, gift_card.created_by.first_name, weight="C")+ _add_vector(search_vectors, gift_card.created_by.last_name, weight="C")return search_vectorsdiff --git a/saleor/graphql/giftcard/mutations/gift_card_update.py b/saleor/graphql/giftcard/mutations/gift_card_update.pyindex 9d3885a917..cb25f5782b 100644--- a/saleor/graphql/giftcard/mutations/gift_card_update.py+++ b/saleor/graphql/giftcard/mutations/gift_card_update.py@@ -117,6 +117,8 @@ class GiftCardUpdate(GiftCardCreate):)instance = cls.construct_instance(instance, cleaned_input)+ if tags_updated:+ instance.search_index_dirty = Truecls.validate_and_update_metadata(instance, metadata_collection, private_metadata_collectiondiff --git a/saleor/graphql/giftcard/tests/mutations/test_gift_card_update.py b/saleor/graphql/giftcard/tests/mutations/test_gift_card_update.pyindex 09bd728608..d373f0c88b 100644--- a/saleor/graphql/giftcard/tests/mutations/test_gift_card_update.py+++ b/saleor/graphql/giftcard/tests/mutations/test_gift_card_update.py@@ -224,6 +224,9 @@ def test_update_gift_card_by_app(gift_card_tag_list,):# given+ gift_card.search_index_dirty = False+ gift_card.save(update_fields=["search_index_dirty"])+old_initial_balance = float(gift_card.initial_balance.amount)old_current_balance = float(gift_card.current_balance.amount)old_tag = gift_card.tags.first()@@ -329,6 +332,9 @@ def test_update_gift_card_by_app(for event in data["events"]:assert event in events+ gift_card.refresh_from_db()+ assert gift_card.search_index_dirty is True+with pytest.raises(old_tag._meta.model.DoesNotExist):old_tag.refresh_from_db()@@ -367,6 +373,9 @@ def test_update_gift_card_balance(permission_manage_apps,):# given+ gift_card.search_index_dirty = False+ gift_card.save(update_fields=["search_index_dirty"])+old_initial_balance = float(gift_card.initial_balance.amount)old_current_balance = float(gift_card.current_balance.amount)@@ -433,6 +442,11 @@ def test_update_gift_card_balance(}assert expected_event == data["events"][0]+ gift_card.refresh_from_db()+ assert gift_card.search_index_dirty is False, (+ "Search index dirty should not be updated when only balance changed"+ )+def test_update_gift_card_change_to_never_expire(staff_api_client,@@ -442,6 +456,9 @@ def test_update_gift_card_change_to_never_expire(permission_manage_apps,):# given+ gift_card_expiry_date.search_index_dirty = False+ gift_card_expiry_date.save(update_fields=["search_index_dirty"])+gift_card = gift_card_expiry_dateold_expiry_date = gift_card.expiry_date@@ -490,6 +507,11 @@ def test_update_gift_card_change_to_never_expire(}assert expected_event == data["events"][0]+ gift_card.refresh_from_db()+ assert gift_card.search_index_dirty is False, (+ "Search index dirty should not be updated when only expiryDate changed"+ )+def test_update_used_gift_card_to_expiry_date(staff_api_client,@@ -842,6 +864,9 @@ def test_update_gift_card_metadata_empty(permission_manage_gift_card,):# given+ gift_card.search_index_dirty = False+ gift_card.save(update_fields=["search_index_dirty"])+metadata_key = "metadata_key"metadata_value = "metadata_value"@@ -879,6 +904,11 @@ def test_update_gift_card_metadata_empty(assert data["privateMetadata"][0]["key"] == metadata_keyassert data["privateMetadata"][0]["value"] == metadata_value+ gift_card.refresh_from_db()+ assert gift_card.search_index_dirty is False, (+ "Search index dirty should not be updated when only metadata changed"+ )+def test_update_gift_card_metadata_existed(staff_api_client,diff --git a/saleor/graphql/giftcard/tests/queries/test_gift_card_search.py b/saleor/graphql/giftcard/tests/queries/test_gift_card_search.pyindex 9032ecc7df..4dee2a68bf 100644--- a/saleor/graphql/giftcard/tests/queries/test_gift_card_search.py+++ b/saleor/graphql/giftcard/tests/queries/test_gift_card_search.py@@ -25,7 +25,25 @@ QUERY_GIFT_CARDS = """@pytest.mark.parametrize(("search", "indexes"),- [("expiry", [0, 1]), ("[email protected]", [2]), ("banana", [])],+ [+ # Search by tag name+ ("test-tag", [0]),+ ("another-tag", [1]),+ ("tag", [0, 1, 2]),+ # Search by created_by email (customer_user creates [0,1], staff_user creates [2])+ ("[email protected]", [2]),+ # Search by used_by email (customer_user is used_by for gift_card_used)+ # and created_by for others - should match all+ ("[email protected]", [0, 1, 2]),+ # Search by code+ ("never_expiry", [0]), # gift_card code+ ("expiry_date", [1]), # gift_card_expiry_date code+ # Search by last 3 characters of the code+ ("used", [2]), # gift_card_used code+ # No match+ ("banana", []),+ ("nonexistent", []),+ ],)def test_query_gift_cards_with_search(search,diff --git a/saleor/settings.py b/saleor/settings.pyindex f508df4ad1..13950a4d42 100644--- a/saleor/settings.py+++ b/saleor/settings.py@@ -991,6 +991,9 @@ PRODUCT_MAX_INDEXED_VARIANTS = 1000PAGE_MAX_INDEXED_ATTRIBUTES = 1000PAGE_MAX_INDEXED_ATTRIBUTE_VALUES = 100+# Maximum related objects that can be indexed in a gift card+GIFT_CARD_MAX_INDEXED_TAGS = 100+# Patch SubscriberExecutionContext class from `graphql-core-legacy` package# to fix bug causing not returning errors for subscription queries.