From 8a1b94b4dc4180c0aad090d38ee0000640c29cfc Mon Sep 17 00:00:00 2001 From: Seth Ladygo Date: Wed, 19 Feb 2020 14:41:29 -0800 Subject: [PATCH] send_locally_feed.py: get_product() use specific season --- .../management/commands/send_locally_feed.py | 34 +++++++++++++------ 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/procat2/management/commands/send_locally_feed.py b/procat2/management/commands/send_locally_feed.py index 2f34925..4d28744 100644 --- a/procat2/management/commands/send_locally_feed.py +++ b/procat2/management/commands/send_locally_feed.py @@ -44,22 +44,30 @@ class Command(BaseCommand): writer = csv.writer(csvfile) #, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL) writer.writerow(self.upc_headers()) for upc in upcs: - prod = self.get_product(upc) + prod = self.get_product(upc, season) if prod: writer.writerow(self.upc_data(season, upc, prod)) - 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 - # recent one. - prods = Product.objects.filter(sap=upc.material).order_by('-season_ranking') + 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: + # product may not be in current season, so find the most + # recent one. "shouldn't happen" + 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 None - - return prods[0] + return prod def upc_headers(self): @@ -123,3 +131,7 @@ class Command(BaseCommand): return paths[0] else: return '' + + + def notice(self, s): + self.stdout.write(self.style.NOTICE(s))