send_locally_feed.py: get_product() use specific season

This commit is contained in:
2020-02-19 14:41:29 -08:00
parent dc01f894f7
commit 8a1b94b4dc

View File

@ -44,22 +44,30 @@ class Command(BaseCommand):
writer = csv.writer(csvfile) #, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL) writer = csv.writer(csvfile) #, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
writer.writerow(self.upc_headers()) writer.writerow(self.upc_headers())
for upc in upcs: for upc in upcs:
prod = self.get_product(upc) prod = self.get_product(upc, season)
if prod: if prod:
writer.writerow(self.upc_data(season, upc, prod)) writer.writerow(self.upc_data(season, upc, prod))
def get_product(self, upc, season):
prods = Product.objects.filter(sap=upc.material, season=season)
prod = None
if len(prods) == 1:
prod = prods[0]
elif len(prods) > 1:
self.notice(f'{upc.upc}: {len(prods)} prods for {upc.material}/{season} - using first')
prod = prods[0]
else: else:
self.stdout.write(self.style.NOTICE(f'No prods for {upc.material} for {upc.upc}'))
def get_product(self, upc):
# product may not be in current season, so find the most # product may not be in current season, so find the most
# recent one. # recent one. "shouldn't happen"
prods = Product.objects.filter(sap=upc.material).order_by('-season_ranking') prods = Product.objects.filter(sap=upc.material).order_by('-season_ranking')
if len(prods) > 0:
self.notice(f'{upc.upc}: no prods for {upc.material}/{season} - {len(prods)} in all seasons')
prod = prods[0]
else:
self.notice(f'{upc.upc}: no prods for {upc.material} in any season')
if len(prods) < 1: return prod
return None
return prods[0]
def upc_headers(self): def upc_headers(self):
@ -123,3 +131,7 @@ class Command(BaseCommand):
return paths[0] return paths[0]
else: else:
return '' return ''
def notice(self, s):
self.stdout.write(self.style.NOTICE(s))