From c964cc3abdb4f70e0edeb238029ce567d2dec4d5 Mon Sep 17 00:00:00 2001 From: Seth Ladygo Date: Thu, 19 Sep 2019 12:39:15 -0700 Subject: [PATCH] product search respects season & region --- products/dbrouters.py | 6 +++--- products/models.py | 14 ++++++++++++++ products/views.py | 40 ++++++++++++++++++++++------------------ 3 files changed, 39 insertions(+), 21 deletions(-) diff --git a/products/dbrouters.py b/products/dbrouters.py index 08039c1..eafcfa3 100644 --- a/products/dbrouters.py +++ b/products/dbrouters.py @@ -1,14 +1,14 @@ -from products.models import Product +from products.models import Product, SeasonRegionMaterial class ProductDBRouter(object): def db_for_read(self, model, **hints): - if model == Product: + if model == Product or model == SeasonRegionMaterial: return 'products' return None def db_for_write(self, model, **hints): - if model == Product: + if model == Product or model == SeasonRegionMaterial: return 'products' return None diff --git a/products/models.py b/products/models.py index 3095981..fd03211 100644 --- a/products/models.py +++ b/products/models.py @@ -71,3 +71,17 @@ class ProductImage: path = ProductImage.get_image_path(sap, image_format) if path: return path.replace(ProductImage.THUMB_DIR, ProductImage.THUMB_URL) + + + +class SeasonRegionMaterial(models.Model): + """Lists where and when a material is valid.""" + id = models.IntegerField(primary_key=True) + material = models.CharField(max_length=10) + season = models.CharField(max_length=4) + region = models.CharField(max_length=20) + ranking = models.IntegerField() + + class Meta: + managed = False + db_table = "keen_season_region_material" diff --git a/products/views.py b/products/views.py index 3feef33..ef39ffc 100644 --- a/products/views.py +++ b/products/views.py @@ -9,7 +9,7 @@ import re from account.decorators import login_required -from .models import Product +from .models import Product, SeasonRegionMaterial from procat2.models import Season, Region log = logging.getLogger(__name__) @@ -23,22 +23,25 @@ def search_products(request): if not body or len(body) < 1: return HttpResponse('Bad request: no data', status=400) - text = body.decode('utf-8') + data = json.loads(body.decode('utf-8')) - # TODO enable someday, when product data includes them - # season_id = data.get('season') - # if not season_id or len(season_id) < 1: - # return HttpResponse('Bad request: no season id', status=400) - # season = Season.objects.get(id=season_id) - # if not season: - # return HttpResponse('Bad request: no season found', status=400) + text = data.get('text') + if not text or len(text) < 1: + return HttpResponse('Bad request: no search text', status=400) - # region_id = data.get('region') - # if not region_id or len(region_id) < 1: - # return HttpResponse('Bad request: no region id', status=400) - # region = Region.objects.get(id=region_id) - # if not region: - # return HttpResponse('Bad request: no region found', status=400) + season_id = data.get('season') + if not season_id or len(season_id) < 1: + return HttpResponse('Bad request: no season id', status=400) + season = Season.objects.get(id=season_id) + if not season: + return HttpResponse('Bad request: no season found', status=400) + + region_id = data.get('region') + if not region_id or len(region_id) < 1: + return HttpResponse('Bad request: no region id', status=400) + region = Region.objects.get(id=region_id) + if not region: + return HttpResponse('Bad request: no region found', status=400) ids = Product.find_sap_ids(text) log.info('found ids %s in %s', ids, text) @@ -47,9 +50,10 @@ def search_products(request): missing = [] if ids: - search_prods = Product.objects.filter(sap__in=ids).distinct('sap') - # TODO: maybe someday - #.filter(sap__in=ids, season=season, region=region) + srm = SeasonRegionMaterial.objects.filter(material__in=ids, season=season.id, region=region.id).distinct('material') + srm_ids = [x.material for x in srm] + + search_prods = Product.objects.filter(sap__in=srm_ids).distinct('sap') # fix product order to match input ids and find missing ids prod_dict = dict([(p.sap, p) for p in search_prods])