product search respects season & region
This commit is contained in:
@ -1,14 +1,14 @@
|
|||||||
from products.models import Product
|
from products.models import Product, SeasonRegionMaterial
|
||||||
|
|
||||||
|
|
||||||
class ProductDBRouter(object):
|
class ProductDBRouter(object):
|
||||||
|
|
||||||
def db_for_read(self, model, **hints):
|
def db_for_read(self, model, **hints):
|
||||||
if model == Product:
|
if model == Product or model == SeasonRegionMaterial:
|
||||||
return 'products'
|
return 'products'
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def db_for_write(self, model, **hints):
|
def db_for_write(self, model, **hints):
|
||||||
if model == Product:
|
if model == Product or model == SeasonRegionMaterial:
|
||||||
return 'products'
|
return 'products'
|
||||||
return None
|
return None
|
||||||
|
|||||||
@ -71,3 +71,17 @@ class ProductImage:
|
|||||||
path = ProductImage.get_image_path(sap, image_format)
|
path = ProductImage.get_image_path(sap, image_format)
|
||||||
if path:
|
if path:
|
||||||
return path.replace(ProductImage.THUMB_DIR, ProductImage.THUMB_URL)
|
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"
|
||||||
|
|||||||
@ -9,7 +9,7 @@ import re
|
|||||||
|
|
||||||
from account.decorators import login_required
|
from account.decorators import login_required
|
||||||
|
|
||||||
from .models import Product
|
from .models import Product, SeasonRegionMaterial
|
||||||
from procat2.models import Season, Region
|
from procat2.models import Season, Region
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
@ -23,22 +23,25 @@ def search_products(request):
|
|||||||
if not body or len(body) < 1:
|
if not body or len(body) < 1:
|
||||||
return HttpResponse('Bad request: no data', status=400)
|
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
|
text = data.get('text')
|
||||||
# season_id = data.get('season')
|
if not text or len(text) < 1:
|
||||||
# if not season_id or len(season_id) < 1:
|
return HttpResponse('Bad request: no search text', status=400)
|
||||||
# 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')
|
season_id = data.get('season')
|
||||||
# if not region_id or len(region_id) < 1:
|
if not season_id or len(season_id) < 1:
|
||||||
# return HttpResponse('Bad request: no region id', status=400)
|
return HttpResponse('Bad request: no season id', status=400)
|
||||||
# region = Region.objects.get(id=region_id)
|
season = Season.objects.get(id=season_id)
|
||||||
# if not region:
|
if not season:
|
||||||
# return HttpResponse('Bad request: no region found', status=400)
|
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)
|
ids = Product.find_sap_ids(text)
|
||||||
log.info('found ids %s in %s', ids, text)
|
log.info('found ids %s in %s', ids, text)
|
||||||
@ -47,9 +50,10 @@ def search_products(request):
|
|||||||
missing = []
|
missing = []
|
||||||
|
|
||||||
if ids:
|
if ids:
|
||||||
search_prods = Product.objects.filter(sap__in=ids).distinct('sap')
|
srm = SeasonRegionMaterial.objects.filter(material__in=ids, season=season.id, region=region.id).distinct('material')
|
||||||
# TODO: maybe someday
|
srm_ids = [x.material for x in srm]
|
||||||
#.filter(sap__in=ids, season=season, region=region)
|
|
||||||
|
search_prods = Product.objects.filter(sap__in=srm_ids).distinct('sap')
|
||||||
|
|
||||||
# fix product order to match input ids and find missing ids
|
# fix product order to match input ids and find missing ids
|
||||||
prod_dict = dict([(p.sap, p) for p in search_prods])
|
prod_dict = dict([(p.sap, p) for p in search_prods])
|
||||||
|
|||||||
Reference in New Issue
Block a user