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

@ -33,7 +33,11 @@ def catalogedit(request, id=0):
@login_required @login_required
def get_catalog(request, id): def get_catalog(request, id):
cat = get_object_or_404(Catalog, Q(id=id) & (Q(owner=request.user) | Q(public=True))) cat = get_object_or_404(Catalog, Q(id=id) & (Q(owner=request.user) | Q(public=True)))
return JsonResponse(cat.data, safe=False) prods = cat.products()
return JsonResponse({
'catalog': cat.data,
'products': [p.serialize() for p in prods],
})
@csrf_exempt @csrf_exempt

View File

@ -9,73 +9,7 @@ Vue.use(Vuex)
export const store = new Vuex.Store({ export const store = new Vuex.Store({
state: { state: {
catalog: null, catalog: null,
materials: { materials: {},
'1001001': { id: '1001001', // call them sap maybe?
model: 'AW123',
family: 'MF123',
name: 'Targhee WP',
color: 'Green / Blue' },
'1001002': { id: '1001002',
model: 'AW123',
family: 'MF123',
name: 'Targhee WP',
color: 'Red / Purple' },
'1001011': { id: '1001011',
model: 'WA999',
family: 'MF123',
name: 'Targhee WP FA',
color: 'Green / Blue' },
'1001012': { id: '1001012',
model: 'WA999',
family: 'MF123',
name: 'Targhee WP FA',
color: 'Green / Blue' },
'1001021': { id: '1001021',
model: 'BBB22',
family: 'BBBMF',
name: 'Slither',
color: 'Green / Blue' },
'1001022': { id: '1001022',
model: 'BBB22',
family: 'BBBMF',
name: 'Slither',
color: 'Red / Blue' },
'1001023': { id: '1001023',
model: 'BBB22',
family: 'BBBMF',
name: 'Slither',
color: 'Blue / Green' },
'1001024': { id: '1001024',
model: 'BBB22',
family: 'BBBMF',
name: 'Slither',
color: 'Red / Green' },
'1001025': { id: '1001025',
model: 'BBB22',
family: 'BBBMF',
name: 'Slither',
color: 'Orange / Blue' },
'1001026': { id: '1001026',
model: 'BBB22',
family: 'BBBMF',
name: 'Slither',
color: 'Yellow / Blue' },
'1001027': { id: '1001027',
model: 'BBB22',
family: 'BBBMF',
name: 'Slither',
color: 'Yellow / Blue' },
'1001028': { id: '1001028',
model: 'BBB22',
family: 'BBBMF',
name: 'Slither',
color: 'Yellow / Blue' },
'1001029': { id: '1001029',
model: 'BBB22',
family: 'BBBMF',
name: 'Slither',
color: 'Yellow / Blue' },
},
selectedSectionID: null, selectedSectionID: null,
//selectedFamilyID: null, //selectedFamilyID: null,
selectedModelID: null, selectedModelID: null,
@ -280,6 +214,15 @@ export const store = new Vuex.Store({
// state.selectedMaterial = payload // state.selectedMaterial = payload
// }, // },
addMaterialsToLibrary(state, materials) {
console.log('adding', materials.length, 'materials')
let map = {}
for (let m of materials) {
map[m.id] = m
}
state.materials = { ...state.materials, ...map }
},
setCatalog(state, cat) { setCatalog(state, cat) {
state.catalog = cat state.catalog = cat
}, },
@ -417,9 +360,17 @@ export const store = new Vuex.Store({
try { try {
commit('setLoadingCatalog', true) commit('setLoadingCatalog', true)
const response = await axios.get('/api/v1/catalogs/id/' + id) const response = await axios.get('/api/v1/catalogs/id/' + id)
if (response.data) { // bundled materials
console.log('recieved catalog:', response.data) if (response.data && response.data.products) {
commit('setCatalog', response.data) console.log('recieved materials:', response.data.products)
commit('addMaterialsToLibrary', response.data.products)
} else {
console.log('no materials')
}
// the catalog
if (response.data && response.data.catalog) {
console.log('recieved catalog:', response.data.catalog)
commit('setCatalog', response.data.catalog)
} else { } else {
console.log('no catalog') console.log('no catalog')
commit('setCatalog', null) commit('setCatalog', null)

View File

@ -8,6 +8,8 @@ from django.conf import settings
from django.contrib.postgres.fields import JSONField from django.contrib.postgres.fields import JSONField
from django.db import models from django.db import models
from products.models import Product
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -76,21 +78,25 @@ class Catalog(models.Model):
pdf += '.pdf' pdf += '.pdf'
return pdf return pdf
def pdf_url(self): def pdf_url(self):
return "http://{}/{}/{}/{}".format(settings.PUBLIC_WEB_HOST, return "http://{}/{}/{}/{}".format(settings.PUBLIC_WEB_HOST,
self.PDF_URL, self.PDF_URL,
self.season.id, self.season.id,
self.pdf_name()) self.pdf_name())
def pdf_file(self): def pdf_file(self):
return os.path.join(settings.ASSET_DIR, return os.path.join(settings.ASSET_DIR,
self.PDF_DIR, self.PDF_DIR,
self.season.id, self.season.id,
self.pdf_name()) self.pdf_name())
def pdf_exists(self): def pdf_exists(self):
return os.path.isfile(self.pdf_file()) return os.path.isfile(self.pdf_file())
def summary(self): def summary(self):
return { return {
'id': self.id, 'id': self.id,
@ -112,6 +118,7 @@ class Catalog(models.Model):
'pdf': self.pdf_url(), 'pdf': self.pdf_url(),
} }
def update_metadata(self): def update_metadata(self):
"""Update meta properties from what's in 'data'""" """Update meta properties from what's in 'data'"""
if not self.data or len(self.data) < 1: if not self.data or len(self.data) < 1:
@ -149,3 +156,25 @@ class Catalog(models.Model):
self.sections = sections self.sections = sections
self.pages = pages self.pages = pages
self.materials = materials 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