diff --git a/catalogedit/views.py b/catalogedit/views.py index 8acee2f..fccd537 100644 --- a/catalogedit/views.py +++ b/catalogedit/views.py @@ -33,7 +33,11 @@ def catalogedit(request, id=0): @login_required def get_catalog(request, id): 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 diff --git a/cateditor/src/pages/editor/store/index.js b/cateditor/src/pages/editor/store/index.js index 8108667..ec8817f 100644 --- a/cateditor/src/pages/editor/store/index.js +++ b/cateditor/src/pages/editor/store/index.js @@ -9,73 +9,7 @@ Vue.use(Vuex) export const store = new Vuex.Store({ state: { catalog: null, - 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' }, - }, + materials: {}, selectedSectionID: null, //selectedFamilyID: null, selectedModelID: null, @@ -280,6 +214,15 @@ export const store = new Vuex.Store({ // 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) { state.catalog = cat }, @@ -417,9 +360,17 @@ export const store = new Vuex.Store({ try { commit('setLoadingCatalog', true) const response = await axios.get('/api/v1/catalogs/id/' + id) - if (response.data) { - console.log('recieved catalog:', response.data) - commit('setCatalog', response.data) + // bundled materials + if (response.data && response.data.products) { + 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 { console.log('no catalog') commit('setCatalog', null) diff --git a/procat2/models.py b/procat2/models.py index 0a2cf5a..894455b 100644 --- a/procat2/models.py +++ b/procat2/models.py @@ -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