product search respects season & region

This commit is contained in:
2019-09-19 12:39:15 -07:00
parent da41a912b0
commit c964cc3abd
3 changed files with 39 additions and 21 deletions

View File

@ -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

View File

@ -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"

View File

@ -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])