def __add_search_cache(self, product_name, allegro_price, allegro_url, nokaut_price, nokaut_url): cache = SearchCache(product_name=product_name, allegro_price=allegro_price, allegro_url=allegro_url, nokaut_price=nokaut_price, nokaut_url=nokaut_url) cache.put()
def get(self): product_name = self.request.get('product', '') product_name.rstrip().lstrip() (url, url_linktext) = get_login_url_and_text(self.request.uri) user_nick = self.__get_user_nickname() last_searches = self.__get_user_last_searches(5) self.__add_search(product_name) cache_query = SearchCache.query(SearchCache.product_name == product_name) if (cache_query.count() > 0): cache = cache_query.fetch(1).pop() time_limit = datetime.datetime.now()-cache.insert_date if (time_limit.days > settings.DATABASE_EXPIRE_NUMBER_OF_DAYS): # update (allegro_price, allegro_url) = \ self.__get_price_and_url_from_allegro(product_name) (nokaut_price, nokaut_url) = \ self.__get_price_and_url_from_nokaut(product_name) self.__update_search_cache(cache, product_name, allegro_price, allegro_url, nokaut_price, nokaut_url) else: # use previous results allegro_price = cache.allegro_price allegro_url = cache.allegro_url nokaut_price = cache.nokaut_price nokaut_url = cache.nokaut_url else: # add (allegro_price, allegro_url) = \ self.__get_price_and_url_from_allegro(product_name) (nokaut_price, nokaut_url) = \ self.__get_price_and_url_from_nokaut(product_name) self.__add_search_cache(product_name, allegro_price, allegro_url, nokaut_price, nokaut_url) pass last_searches_url = [encode_url('product', _search) for _search in last_searches] template_values = { 'product_name': product_name, 'nokaut_price': nokaut_price, 'nokaut_url': nokaut_url, 'allegro_price': allegro_price, 'allegro_url': allegro_url, 'url': url, 'url_linktext': url_linktext, 'nick': user_nick, 'last_searches': last_searches, 'last_searches_url': last_searches_url } self.response.write(render_template('search.html', **template_values))