def productlist(request, keywords=None, page=1, template_name="dhead/itemlist.html", extra_context=None, storefront=None, *args, **kwargs): extra_context = extra_context or {} keywords = deslugify(keywords) page = int(page) # Build search term. if keywords is None: # Use primary keywords as default search. keywords = storefront.primary_keywords else: # Use search terms in URL if specified. keywords = keywords.replace('-', ' ') asin_list = cached.amazon_search(storefront, keywords, page) item_list = [cached.amazon_lookup(storefront, asin) for asin in asin_list] total_pages = cached.amazon_search_pages(storefront, keywords) if total_pages is None: pages = [] else: pages = range(1, total_pages + 1) template_context = dict( extra_context, keywords=keywords, item_list=item_list, pages=pages, storefront=storefront, ) return render_to_response(template_name, template_context, RequestContext(request))
def amazon_search(storefront, keywords, page): """Return a list of ASINs from a search against the Amazon API.""" key = '{0}-{1}-azsearch-{2}'.format(storefront.site.domain, slugify(keywords), page) value = cache.get(key) if value is not None: return value # Cache miss. keywords = deslugify(keywords) az = _amazon(storefront) results = az.ItemSearch( Keywords=keywords, SearchIndex=storefront.amazon_category, ItemPage=page, MinimumPrice=1, ResponseGroup='Medium,OfferSummary,Offers,VariationSummary', ) results = fromstring(results) Items = results.find(ECS_TAG('Items')) item_list = Items.findall(ECS_TAG('Item')) asin_list = [] for item in item_list: asin = azasin(item) asin_list.append(asin) itemkey = '{0}-azlookup-{1}'.format(storefront.site.domain, asin) cache.set(itemkey, item, AMAZON_CACHE_TIMEOUT) pageskey = '{0}-{1}-azsearch-pages'.format(storefront.site.domain, slugify(keywords)) try: total_pages = int(Items.find(ECS_TAG('TotalPages')).text) except AttributeError: pass else: cache.set(pageskey, total_pages, AMAZON_CACHE_TIMEOUT) return asin_list
def productdetail(request, keywords=None, title_slug=None, asin=None, template_name="dhead/itemdetail.html", extra_context=None, storefront=None, *args, **kwargs): extra_context = extra_context or {} keywords = deslugify(keywords) item = cached.amazon_lookup(storefront, asin) template_context = dict( extra_context, asin=asin, item=item, keywords=keywords, title_slug=title_slug, storefront=storefront, ) return render_to_response(template_name, template_context, RequestContext(request))