return catalog materials when cat data is fetched

This commit is contained in:
Seth Ladygo
2019-05-14 16:05:55 -07:00
parent 3990774de3
commit 9d18b47aee
3 changed files with 55 additions and 71 deletions

View File

@ -8,6 +8,8 @@ from django.conf import settings
from django.contrib.postgres.fields import JSONField
from django.db import models
from products.models import Product
log = logging.getLogger(__name__)
@ -76,21 +78,25 @@ class Catalog(models.Model):
pdf += '.pdf'
return pdf
def pdf_url(self):
return "http://{}/{}/{}/{}".format(settings.PUBLIC_WEB_HOST,
self.PDF_URL,
self.season.id,
self.pdf_name())
def pdf_file(self):
return os.path.join(settings.ASSET_DIR,
self.PDF_DIR,
self.season.id,
self.pdf_name())
def pdf_exists(self):
return os.path.isfile(self.pdf_file())
def summary(self):
return {
'id': self.id,
@ -112,6 +118,7 @@ class Catalog(models.Model):
'pdf': self.pdf_url(),
}
def update_metadata(self):
"""Update meta properties from what's in 'data'"""
if not self.data or len(self.data) < 1:
@ -149,3 +156,25 @@ class Catalog(models.Model):
self.sections = sections
self.pages = pages
self.materials = materials
def products(self):
"""Return an unordered list of all products in the catalog."""
ids = self.product_ids()
log.info("ids: %s", ids)
# TODO filter on catalog region and season when we
# have that data
return Product.objects.filter(sap__in=ids).distinct('sap')
def product_ids(self):
"""Return an unordered list of all product ids in the catalog."""
materials = set()
if self.data and len(self.data) > 0:
for section in self.data.get('sections', []):
for page in section.get('pages', []):
for block in page:
materials = materials.union(set(block.get('ids', [])))
return materials