Skip to content

Commit

Permalink
delete search index for inactive products
Browse files Browse the repository at this point in the history
  • Loading branch information
jrief committed May 4, 2020
1 parent d5609a3 commit 3cff29d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
9 changes: 9 additions & 0 deletions shop/admin/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand All @@ -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):
Expand Down
22 changes: 16 additions & 6 deletions shop/search/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 3cff29d

Please sign in to comment.