diff --git a/shop/admin/product.py b/shop/admin/product.py index 47d63a9cc..f2f7292fa 100644 --- a/shop/admin/product.py +++ b/shop/admin/product.py @@ -109,6 +109,11 @@ def save_model(self, request, product, form, change): if change: product.update_search_index() + def delete_model(self, request, product): + product.active = False + product.update_search_index() + super().delete_model(request, product) + class InvalidateProductCacheMixin: """ @@ -120,6 +125,10 @@ def save_model(self, request, product, form, change): product.invalidate_cache() return super().save_model(request, product, form, change) + def delete_model(self, request, product): + product.invalidate_cache() + super().delete_model(request, product) + class UnitPriceMixin: def get_list_display(self, request): diff --git a/shop/search/documents.py b/shop/search/documents.py index 5e89e3930..96cf4a231 100644 --- a/shop/search/documents.py +++ b/shop/search/documents.py @@ -2,7 +2,9 @@ from django.utils import translation from django_elasticsearch_dsl import fields, Document, Index +from elasticsearch.exceptions import NotFoundError +from shop.conf import app_settings from shop.models.product import ProductModel from shop.search.analyzers import body_analyzers @@ -22,7 +24,7 @@ class _ProductDocument(Document): class Django: model = ProductModel fields = ['id'] - ignore_signals = True # performed by ProductModel.save() + ignore_signals = True # performed by ProductModel.update_search_index() def __str__(self): return "{} {}: {}".format(self.product_type, self.id, self.product_name) @@ -57,12 +59,20 @@ def prepare_body(self, instance): body = template.render({'product': instance}) return body - def update(self, thing, refresh=None, action='index', parallel=False, **kwargs): - if self._language: - with translation.override(self._language): - super().update(thing, refresh=None, action='index', parallel=False, **kwargs) + def update(self, product, refresh=None, action='index', parallel=False, **kwargs): + if product.active: + if self._language: + with translation.override(self._language): + super().update(product, refresh=None, action='index', parallel=False, **kwargs) + else: + super().update(product, refresh=None, action='index', parallel=False, **kwargs) else: - super().update(thing, refresh=None, action='index', parallel=False, **kwargs) + try: + doc = self.get(id=product.id) + except NotFoundError: + pass + else: + doc.delete() class ProductDocument: