send_locally_feed.py: first version - no ftp
This commit is contained in:
126
procat2/management/commands/send_locally_feed.py
Normal file
126
procat2/management/commands/send_locally_feed.py
Normal file
@ -0,0 +1,126 @@
|
||||
import os
|
||||
import csv
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
from django.conf import settings
|
||||
|
||||
from products.models import Product, SeasonRegionMaterial, UPC, ProductImage, ProductImageFormat
|
||||
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Send a product feed to Locally'
|
||||
|
||||
# def add_arguments(self, parser):
|
||||
# parser.add_argument('cat_ids', nargs='+', type=int)
|
||||
|
||||
|
||||
def handle(self, *args, **options):
|
||||
# NOTE: choose the most current season in the db?
|
||||
season = 'FW20'
|
||||
for region in ['US', 'CANADA', 'UTILITY US', 'UTILITY CANADA']:
|
||||
upcs = self.get_upcs(season, region)
|
||||
self.write_csv(self.csv_path(season, region), upcs, season)
|
||||
|
||||
|
||||
def get_upcs(self, season, region):
|
||||
srms = SeasonRegionMaterial.objects.filter(season=season, region__iexact=region).distinct('material')
|
||||
materials = [srm.material for srm in srms]
|
||||
return UPC.objects.filter(material__in=materials).distinct('upc')
|
||||
|
||||
|
||||
def csv_dir(self):
|
||||
return os.path.join(settings.ASSET_DIR, 'locally')
|
||||
|
||||
|
||||
def csv_path(self, season, region):
|
||||
filename = f'Keen_{season}_{region}.csv'.replace(' ', '_')
|
||||
#mkstemp(suffix=None, prefix=None, dir=None, text=False)
|
||||
return os.path.join(self.csv_dir(), filename)
|
||||
|
||||
|
||||
def write_csv(self, path, upcs, season):
|
||||
Path(self.csv_dir()).mkdir(parents=True, exist_ok=True)
|
||||
with open(path, 'w', newline='') as csvfile:
|
||||
writer = csv.writer(csvfile) #, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
|
||||
writer.writerow(self.upc_headers())
|
||||
for upc in upcs:
|
||||
prod = self.get_product(upc)
|
||||
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')
|
||||
|
||||
if len(prods) < 1:
|
||||
return None
|
||||
|
||||
return prods[0]
|
||||
|
||||
|
||||
def upc_headers(self):
|
||||
return [
|
||||
"Style Number",
|
||||
"Name",
|
||||
"Color",
|
||||
"Division",
|
||||
"Gender",
|
||||
"Category",
|
||||
"Description",
|
||||
"Size Range",
|
||||
#"Wholesale CAD",
|
||||
#"Retail CAD",
|
||||
"Wholesale USD",
|
||||
"Retail USD",
|
||||
"Size 1",
|
||||
"UPC 1",
|
||||
"Image URL",
|
||||
]
|
||||
|
||||
|
||||
def upc_data(self, season, upc, prod):
|
||||
return [
|
||||
upc.material,
|
||||
prod.name.lower(),
|
||||
prod.color.lower(),
|
||||
"footwear",
|
||||
prod.gender.lower(),
|
||||
prod.category.lower(),
|
||||
self.description(prod),
|
||||
prod.size_range,
|
||||
#"Wholesale CAD",
|
||||
#"Retail CAD",
|
||||
prod.wholesale,
|
||||
prod.retail,
|
||||
upc.size_grid,
|
||||
upc.upc,
|
||||
self.image_url(upc.material),
|
||||
]
|
||||
|
||||
|
||||
def description(self, prod):
|
||||
descr = None
|
||||
callouts = [f for f in Product._meta.fields if f.name.startswith('tech_call_out')]
|
||||
for c in callouts:
|
||||
v = c.value_from_object(prod)
|
||||
if v:
|
||||
if descr:
|
||||
descr += f'<br>* {v}'
|
||||
else:
|
||||
descr = f'* {v}'
|
||||
return descr
|
||||
|
||||
|
||||
def image_url(self, material):
|
||||
paths = ProductImage.get_all_image_urls(material, ProductImageFormat.MED_JPG, True)
|
||||
if len(paths) > 0:
|
||||
return paths[0]
|
||||
else:
|
||||
return ''
|
||||
Reference in New Issue
Block a user