def get_all(): key = 'ratings_all' data = memcache.get(key) #@UndefinedVariable if data is not None: return data all_ratings = [x for x in db.GqlQuery("SELECT * FROM RatingPart")] rating_dict = defaultdict(list) for r in all_ratings: if r.duration != None: rating_dict[r.hotel_name].append(r) hotel_dict = dict() for h in place.get_hotels(): hotel_dict[h.name] = h result = [] for hotel_name, ratings in rating_dict.items(): if len(ratings) >= 8: duration =sum([x.duration for x in ratings]) expenses =sum([x.expenses for x in ratings]) metric =sum([x.metric for x in ratings]) name_rus = hotel_dict[hotel_name].name_rus result.append(Rating(hotel_name, name_rus, duration, expenses, metric)) result = sorted(result, key=lambda rating: rating.duration) memcache.add(key, result, 24*60*60) #@UndefinedVariable return result
def do_hotels(request, response, stars = None): ratings = rater.get_all() if stars is not None: hotels = [x for x in place.get_hotels() if int(x.rating) == stars] ratings = [x for x in ratings if any([y for y in hotels if y.name == x.hotel_name])] data = { 'ratings': ratings, 'title': (_("Hotel rating title N stars") % stars) if stars is not None else _("Hotel rating title") } view.to_html(data, 'hotels', request, response)
def do_calculate_hotel_rating(request, response): current_index_str = param.get('current_hotel_index') if current_index_str == None: current_index = 0 else: current_index = int(current_index_str) + 1 hotels = place.get_hotels() if len(hotels) <= current_index: current_index = 0 hotel = hotels[current_index] rater.get(hotel) param.add('current_hotel_index', str(current_index)) response.out.write('Processed the rating of hotel ' + hotel.name) logging.info('Processed the rating of hotel ' + hotel.name)