def get(self, request, mode_id=None): mode_id = int(mode_id) if not (mode_id in Mode.stat_v1_ids): return HttpResponse(status=404) last_updated = to_unix( cache_value("ranking_stats_last_modified", 600, ranking_stats_last_modified)) now = to_unix(utcnow()) try: if_modified_since = parse_http_date( request.META['HTTP_IF_MODIFIED_SINCE']) except (ValueError, KeyError): if_modified_since = 0 if if_modified_since >= last_updated: response = HttpResponse("", content_type="application/json", status=304) else: response = HttpResponse(cache_value("ranking_stats_%d" % mode_id, 600, rankings_view_client, 'ranking_stats', mode_id), content_type="application/json") response['Cache-Control'] = "max-age=86400" response['Date'] = http_date(now) response['Expires'] = http_date(now + 86400) response['Last-Modified'] = http_date(last_updated) return response
def get(self, request, *args, **kwargs): context = self.get_context_data() current_season = Season.get_current_season() clan = request.GET.get('clan', None) if clan is None: context['clans'] = cache_value("top_clans", 1200, get_top_clans) else: clan = clan.strip() context['search'] = True clans = list(Player.objects .filter(season=current_season, mode=Mode.TEAM_1V1, tag__gt='') .filter(Q(clan__istartswith=clan) | Q(tag__istartswith=clan)) .values('clan', 'tag').annotate(count=Count('tag')).order_by('-count')[:33]) if len(clans) == 33: clans = clans[:32] context['left_out'] = True clans.sort(key=lambda c: c['clan']) if len(clans) == 1: return redirect('clan', tag=clans[0]['tag'], reverse='', sort_key=DEFAULT_SORT_KEY) context['clans'] = clans return self.render_to_response(context)
def get(self, request, version=None, mode=None, reverse=None, sort_key=None): if sort_key == 'ladder-rank': return redirect( self.redirect_mmr_url(request, 'ladder', version=version, mode=mode, reverse=reverse)) context = self.get_context_data() # # Parse parameters. # region_id, race_id, league_id = self.extract_filters(request) is_reverse, sort_key_id = self.extract_common(reverse, sort_key) version_id, mode_id, team_id, request_offset = self.extract_rest( request, version, mode) # # Fetch data. # team_size = Mode.team_size(mode_id) filter_race = None if mode_id != Mode.ARCHON and team_size > 1 else race_id try: if (request_offset is None or request_offset == 0) and not team_id: key = "%s-%s-%s-%s-%s-%s-%s" % (sort_key_id, version_id, mode_id, is_reverse, league_id, region_id, filter_race) data = cache_value(key, 40, self.fetch_data, sort_key_id, version_id, mode_id, is_reverse=is_reverse, league_id=league_id, region_id=region_id, race_id=filter_race, limit=PAGE_SIZE) else: data = self.fetch_data(sort_key_id, version_id, mode_id, is_reverse=is_reverse, league_id=league_id, region_id=region_id, race_id=filter_race, offset=request_offset, team_id=team_id, limit=PAGE_SIZE) except ClientError as e: logger.exception("Fetch from client error: %s" % str(e)) context[ 'error'] = 'The server is not feeling well, try again later.' return self.render_to_response(context) LadderCommon.update_age(data) context['ladder'] = data context['highlight_team_id'] = team_id context['team_size'] = team_size context['mode_id'] = mode_id # # Build navigation based on current parameters. # paths = { 'version': version, 'mode': mode, 'reverse': reverse, 'sort_key': sort_key, } args = { 'region': Region.key_by_ids.get(region_id, Region.ALL_KEY), 'league': League.key_by_ids.get(league_id), 'race': Race.key_by_ids.get(race_id), 'team_id': team_id, } values = [(Mode.name_by_ids[i], Mode.key_by_ids[i]) for i in sorted(Mode.ids) if i != Mode.UNKNOWN] LadderCommon.set_nav(context, request, ladder_url, paths, args, name='mode', values=values) values = [(Version.name_by_ids[i], Version.key_by_ids[i]) for i in reversed(Version.ids) if i != Version.UNKNOWN] LadderCommon.set_nav(context, request, ladder_url, paths, args, name='version', values=values) context['reverse_visiting'] = 'visiting' if reverse else '' context['reverse_href'] = ladder_url(request, paths, args, 'reverse', '' if reverse else '-') values = [('MMR', 'mmr'), ('League points', 'league-points'), ('Games played', 'played'), ('Wins', 'wins'), ('Losses', 'losses'), ('Win rate', 'win-rate')] LadderCommon.set_nav(context, request, ladder_url, paths, args, name='sort_key', values=values) values = [('All', None)] + [(Race.name_by_ids[i], Race.key_by_ids[i]) for i in Race.ids if i != Race.UNKNOWN] LadderCommon.set_nav(context, request, ladder_url, paths, args, name='race', values=values, highlight=Race.key_by_ids.get(filter_race)) values = [('All', None)] + [ (League.name_by_ids[i], League.key_by_ids[i]) for i in League.ids if i not in (League.UNKNOWN, League.ALL) ] LadderCommon.set_nav(context, request, ladder_url, paths, args, name='league', values=values) values = [(Region.name_by_ids[i], Region.key_by_ids[i]) for i in Region.ids if i != Region.UNKNOWN] LadderCommon.set_nav(context, request, ladder_url, paths, args, name='region', values=values) # # Buld pagination, page is 0 indexed (+1 when displayed). # pages = [] count = data['count'] data_offset = data['offset'] # page size: 4 # ladder: 0 1 2 3| 4 5 6 7| 8 9 10 11|12 13 # offset: ^ => current_page = 3, page_offset = 3 # pages: 0| 1 2 3 4| 5 6 7 8| 9 10 11 12|13 if data_offset >= count: current_page = -1 else: current_page = math.ceil(data_offset / PAGE_SIZE) page_offset = -data_offset % PAGE_SIZE page_count = math.ceil((count + page_offset) / PAGE_SIZE) BEG_SIZE = 4 MID_SIZE = 7 # Should be uneven. END_SIZE = 4 start = 0 end = min(BEG_SIZE, page_count) i = 0 for i in range(start, end): page_index = PAGE_SIZE * i - page_offset pages.append({ 'index': i, 'href': ladder_url(request, paths, args, name='offset', key=page_index) }) start = min(max(i + 1, current_page - MID_SIZE // 2), page_count) end = min(current_page + 1 + MID_SIZE // 2, page_count) if start > i + 1: pages.append({'gap': True}) for i in range(start, end): page_index = PAGE_SIZE * i - page_offset pages.append({ 'index': i, 'href': ladder_url(request, paths, args, name='offset', key=page_index) }) start = max(i + 1, page_count - END_SIZE) end = page_count if start > i + 1: pages.append({'gap': True}) for i in range(start, end): page_index = PAGE_SIZE * i - page_offset pages.append({ 'index': i, 'href': ladder_url(request, paths, args, name='offset', key=page_index) }) context['pages'] = pages if len(pages) > 1 else None context['current_page'] = current_page context['page_offset'] = page_offset context['page_count'] = page_count return self.render_to_response(context)
def last_updated_info(): """ Return the date (data, season_id) was last updated. """ return cache_value("last_updated_info", 600, _last_updated_info)
def get_season_list(): """ Return season list as json. """ return cache_value("season_list", 600, _get_season_list)