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']
def get_result(self, request): cache_db = request.environ['cache'] cache_id = cache.key('photos',self.type, self.facet, self.category) cache_result = cache_db.get(cache_id) P = photoengine.PhotoEngine(request) if cache_result: print 'CACHE HIT' result = cache_result['data'] else: result = P.search_products(self.facet, self.category) result.update( {'type': self.type, 'facet': self.facet, 'category': self.category} ) photolist = photoordering.PhotoList() photolist.add(result['photos']) ordered_photos = photolist.process() result['opdict'] = [[p.photodict for p in row] for row in ordered_photos] print 'CACHE BUILD' cache_db.update( [ {'_id': cache_id, 'data': result} ] ) p = paged_list(request, result['opdict'], max_pagesize=5) page_of_ordered_photos = p['items'] result.update( {'p':Paging(request,p), 'rows': page_of_ordered_photos} ) allcategories = P.categories() result.update( {'allcategories': allcategories} ) return result