def _photos_from_products(self, results_with_dupes): products_by_photo = {} photo_ids = set() for result in results_with_dupes: if result.doc['photo']['_ref'] not in photo_ids: photo_ids.add(result.doc['photo']['_ref']) products_by_photo[result.doc['photo']['_ref']] = result.doc['code'] with self.couchish.session() as S: results_with_dupes= S.view('photo/all',include_docs=True,keys=list(photo_ids)) photos = [] lastid = None master_photos = set() for result in results_with_dupes: if result.doc['code'] not in master_photos: master_photos.add(result.doc['code']) result.doc['product_code'] = products_by_photo.get(result.doc['_id'],result.doc['code']) photos.append(_dictify_photo(result.doc)) location_categories = {} subject_categories = {} photos = list(photos) for photo in photos: for category in photo['location_category']: location_categories[ category['path'] ] = category for category in photo['subject_category']: subject_categories[ category['path'] ] = category categories = {} categories['location'] = {'flatcats': location_categories, 'tree': mktree(location_categories.values())} categories['subject'] = {'flatcats': subject_categories, 'tree': mktree(subject_categories.values())} return {'photos': photos, 'products_by_photo': products_by_photo, 'categories': categories }
def products_from_keys(self, product_keys): with self.couchish.session() as S: products = S.docs_by_view('product/all',keys=product_keys) products_by_photo = {} products = list(products) for product in products: products_by_photo.setdefault(product['photo']['_ref'],[]).append(product) photo_keys = products_by_photo.keys() with self.couchish.session() as S: photos = S.docs_by_view('photo/all',keys=photo_keys) location_categories = {} subject_categories = {} photos = list(photos) for photo in photos: for category in photo['location_category']: location_categories[ category['path'] ] = category for category in photo['subject_category']: subject_categories[ category['path'] ] = category categories = {} categories['location'] = {'flatcats': location_categories, 'tree': mktree(location_categories.values())} categories['subject'] = {'flatcats': subject_categories, 'tree': mktree(subject_categories.values())} return {'products': products, 'photos': photos, 'products_by_photo': products_by_photo, 'categories': categories }
def categories(self): result = self.cache.get(cache.key('categories',self.type )) if result: return result['data'] if self.type: startkey = [self.type] endkey = [self.type] + [{}] with self.couchish.session() as S: results= list(S.view('product/by_type_with_categories',startkey=startkey,endkey=endkey)) else: with self.couchish.session() as S: results= list(S.view('product/by_type_with_categories')) all_categories = {'location':{}, 'subject': {}} for result in results: facet = result.value['facet'] category = result.value['category'] all_categories[facet][category['path']] = category with self.couchish.session() as S: location_categories= S.doc_by_view('facet_location/all')['category'] with self.couchish.session() as S: subject_categories= S.doc_by_view('facet_subject/all')['category'] all_sorted_categories = {'location':[], 'subject': []} for category in location_categories: if category['path'] in all_categories['location']: all_sorted_categories['location'].append( all_categories['location'][category['path']] ) for category in subject_categories: if category['path'] in all_categories['subject']: all_sorted_categories['subject'].append( all_categories['subject'][category['path']] ) categories = {'_id': cache.key('categories',self.type), 'data': {}} categories['data']['location'] = {'flatcats': all_sorted_categories['location'], 'tree': mktree(all_sorted_categories['location'])} categories['data']['subject'] = {'flatcats': all_sorted_categories['subject'], 'tree': mktree(all_sorted_categories['subject'])} self.cache.update([categories]) return categories['data']