add basic product search
This commit is contained in:
@ -48,6 +48,12 @@ LOGGING = {
|
||||
#'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO'),
|
||||
'propagate': True,
|
||||
},
|
||||
'products': {
|
||||
'handlers': ['console', 'file'],
|
||||
'level': 'DEBUG',
|
||||
#'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO'),
|
||||
'propagate': True,
|
||||
},
|
||||
'catalogedit': {
|
||||
'handlers': ['console', 'file'],
|
||||
'level': 'DEBUG',
|
||||
@ -84,6 +90,7 @@ INSTALLED_APPS = [
|
||||
'webpack_loader',
|
||||
'procat2',
|
||||
'dashboard',
|
||||
'products',
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
@ -146,6 +153,8 @@ AUTH_PASSWORD_VALIDATORS = [
|
||||
},
|
||||
]
|
||||
|
||||
DATABASE_ROUTERS = ('products.dbrouters.ProductDBRouter',)
|
||||
|
||||
# Internationalization
|
||||
# https://docs.djangoproject.com/en/2.1/topics/i18n/
|
||||
LANGUAGE_CODE = 'en-us'
|
||||
|
||||
@ -22,6 +22,7 @@ from lazysignup.views import convert
|
||||
from dashboard.views import dashboard
|
||||
from cataloglist.views import cataloglist, my_catalogs, public_catalogs
|
||||
from catalogedit.views import catalogedit, get_catalog, save_catalog
|
||||
from products.views import search_products
|
||||
|
||||
from .forms import UserCreationForm
|
||||
from .views import login_guest, lazy_convert_done
|
||||
@ -41,6 +42,8 @@ urlpatterns = [
|
||||
path('api/v1/catalogs/id/<int:id>', get_catalog, name='get_catalog'),
|
||||
path('api/v1/catalogs/save', save_catalog, name='save_catalog'),
|
||||
|
||||
path('api/v1/products/search', search_products, name='search_products'),
|
||||
|
||||
path('admin/', admin.site.urls),
|
||||
path("account/", include("account.urls")),
|
||||
path('convert/', convert, { 'form_class': UserCreationForm }, name='lazysignup_convert'),
|
||||
|
||||
0
products/__init__.py
Normal file
0
products/__init__.py
Normal file
6
products/apps.py
Normal file
6
products/apps.py
Normal file
@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class ProductsConfig(AppConfig):
|
||||
name = 'products'
|
||||
verbose_name = "Products"
|
||||
14
products/dbrouters.py
Normal file
14
products/dbrouters.py
Normal file
@ -0,0 +1,14 @@
|
||||
from products.models import Product
|
||||
|
||||
|
||||
class ProductDBRouter(object):
|
||||
|
||||
def db_for_read(self, model, **hints):
|
||||
if model == Product:
|
||||
return 'products'
|
||||
return None
|
||||
|
||||
def db_for_write(self, model, **hints):
|
||||
if model == Product:
|
||||
return 'products'
|
||||
return None
|
||||
0
products/migrations/__init__.py
Normal file
0
products/migrations/__init__.py
Normal file
84
products/models.py
Normal file
84
products/models.py
Normal file
@ -0,0 +1,84 @@
|
||||
import logging
|
||||
import re
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import models
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Product(models.Model):
|
||||
sap_id_regex = r'\b(\d{7})\b'
|
||||
sap = models.CharField(max_length=10, db_column='sap_article_number')
|
||||
name = models.CharField(max_length=100, db_column='short_name')
|
||||
model = models.CharField(max_length=100, db_column='model')
|
||||
family = models.CharField(max_length=100, db_column='product_family')
|
||||
color = models.CharField(max_length=100, db_column='color')
|
||||
|
||||
class Meta:
|
||||
managed = False
|
||||
db_table = "adilog_product"
|
||||
|
||||
@staticmethod
|
||||
def find_sap_ids(text):
|
||||
matches = re.finditer(Product.sap_id_regex, text)
|
||||
return [match.group(1) for match in matches]
|
||||
|
||||
def serialize(self):
|
||||
return {
|
||||
'id': self.sap,
|
||||
'name': self.name,
|
||||
'model': self.model,
|
||||
'family': self.family,
|
||||
'color': self.color,
|
||||
}
|
||||
|
||||
|
||||
"""
|
||||
create view adilog_product as
|
||||
SELECT DISTINCT ks.style AS id, ks.sap_article_number,
|
||||
(((kr.ranksection || '-'::text) || kr.rankcategory) || '-'::text) || kr.modelrank
|
||||
AS grouping_number,
|
||||
km.name AS short_name, kr.model, kr.color,
|
||||
kr.category AS product_type, km.category AS model_product_type,
|
||||
kr.men_women AS gender, kr.segmentation AS segment, ks.color_name AS colorway,
|
||||
km.summary AS blurb, km.description,
|
||||
km.tech_icon_1, km.tech_icon_2, km.tech_icon_3, km.tech_icon_4,
|
||||
km.tech_icon_5, km.tech_icon_6, km.tech_icon_7, km.tech_icon_8,
|
||||
km.tech_icon_9, km.tech_icon_10, km.tech_icon_11, km.tech_icon_12,
|
||||
km.tech_icon_13, km.tech_icon_14, km.tech_icon_15,
|
||||
km.sizing AS size,
|
||||
km.tech_call_out_1 AS point0, km.tech_call_out_2 AS point1,
|
||||
km.tech_call_out_3 AS point2, km.tech_call_out_4 AS point3,
|
||||
km.tech_call_out_5 AS point4, km.tech_call_out_6 AS point5,
|
||||
km.tech_call_out_7 AS point6, km.tech_call_out_8 AS point7,
|
||||
km.tech_call_out_9 AS point8, km.tech_call_out_10 AS point9,
|
||||
km.tech_call_out_11 AS point10, km.tech_call_out_12 AS point11,
|
||||
km.tech_call_out_13 AS point12, km.tech_call_out_14 AS point13,
|
||||
km.tech_call_out_15 AS point14, km.tech_call_out_16 AS point15,
|
||||
km.tech_call_out_17 AS point16, km.tech_call_out_18 AS point17,
|
||||
km.tech_call_out_19 AS point18, km.tech_call_out_20 AS point19,
|
||||
kr.modelstatus, kr.stylestatus, kr.hero, kr.alt_hero, ks.hero AS stylehero,
|
||||
kr.stylerank, km.relatedkidsmodel1, km.relatedkidsmodel2,
|
||||
km.dimensions, km.height, km.weight, km.capacity, km.origin, km.fiber,
|
||||
km.upper, km.lining, km.tooling, kr.in_current_season, km.imageistall,
|
||||
km.retail, km.wholesale, kr.country, kr.pagination, km.legal, -- km.legal2,
|
||||
km.duty_type,
|
||||
km.heavy_metal_fabrication,
|
||||
km.masonry,
|
||||
km.energy,
|
||||
km.construction,
|
||||
km.utilities,
|
||||
km.landscaping,
|
||||
km.transportation,
|
||||
km.maintenance,
|
||||
km.manufacturing,
|
||||
km.warehouse_distribution,
|
||||
km.service,
|
||||
-- km.public_safety_duty,
|
||||
km.extra_1, km.extra_2, km.extra_3, km.extra_4, km.extra_5, km.extra_6,
|
||||
km.product_family
|
||||
FROM keen_ranking kr
|
||||
JOIN keen_style ks ON kr.style = ks.sap_article_number
|
||||
JOIN keen_model km ON kr.model = km.model;
|
||||
"""
|
||||
69
products/views.py
Normal file
69
products/views.py
Normal file
@ -0,0 +1,69 @@
|
||||
from django.http import HttpResponseRedirect, HttpResponse, JsonResponse
|
||||
from django.shortcuts import render, get_object_or_404
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
from django.views.decorators.http import require_http_methods
|
||||
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
|
||||
from account.decorators import login_required
|
||||
|
||||
from .models import Product
|
||||
from procat2.models import Season, Region
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@csrf_exempt
|
||||
@login_required
|
||||
@require_http_methods(["POST"])
|
||||
def search_products(request):
|
||||
body = request.body
|
||||
if not body or len(body) < 1:
|
||||
return HttpResponse('Bad request: no data', status=400)
|
||||
|
||||
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)
|
||||
|
||||
# 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)
|
||||
|
||||
text = data.get('text')
|
||||
ids = Product.find_sap_ids(text)
|
||||
log.info('found ids %s in %s', ids, text)
|
||||
|
||||
prods = []
|
||||
missing = []
|
||||
|
||||
if ids:
|
||||
search_prods = Product.objects.filter(sap__in=ids).distinct('sap')
|
||||
# TODO: maybe someday
|
||||
#.filter(sap__in=ids, season=season, region=region)
|
||||
|
||||
# fix product order to match input ids and find missing ids
|
||||
prod_dict = dict([(p.sap, p) for p in search_prods])
|
||||
|
||||
for i in ids:
|
||||
if prod_dict.get(i):
|
||||
prods.append(prod_dict[i])
|
||||
else:
|
||||
missing.append(i)
|
||||
|
||||
out = {
|
||||
'found': [p.serialize() for p in prods],
|
||||
'missing': missing,
|
||||
}
|
||||
|
||||
return JsonResponse(out)
|
||||
Reference in New Issue
Block a user