def get_context_data(self, **kwargs): context = super(IndexView, self).get_context_data(**kwargs) org = self.request.org context["org"] = org latest_poll = Poll.get_main_poll(org) context["latest_poll"] = latest_poll if context["latest_poll"]: context["trending_words"] = latest_poll.get_trending_words() brick_poll_ids = Poll.get_brick_polls_ids(org) context["recent_polls"] = Poll.objects.filter(id__in=brick_poll_ids).order_by("-created_on") context["stories"] = Story.objects.filter(org=org, is_active=True, featured=True).order_by("-created_on") videos = Video.objects.filter(is_active=True, org=org).order_by("-created_on") context["videos"] = videos news = NewsItem.objects.filter(is_active=True, org=org).order_by("-created_on") context["news"] = news.count() > 0 context["most_active_regions"] = org.get_regions_stats() # global counter if org.get_config("common.is_global"): context["global_counter"] = get_global_count() context["gender_stats"] = org.get_gender_stats() context["age_stats"] = org.get_age_stats() context["reporters"] = org.get_reporters_count() return context
def get_context_data(self, **kwargs): context = super(IndexView, self).get_context_data(**kwargs) org = self.request.org context['org'] = org latest_poll = Poll.get_main_poll(org) context['latest_poll'] = latest_poll if context['latest_poll']: context['trending_words'] = latest_poll.get_trending_words() brick_poll_ids = Poll.get_brick_polls_ids(org) context['recent_polls'] = Poll.objects.filter(id__in=brick_poll_ids) context['stories'] = Story.objects.filter(org=org, is_active=True, featured=True).order_by('-created_on') videos = Video.objects.filter(is_active=True, org=org).order_by('-created_on') context['videos'] = videos news = NewsItem.objects.filter(is_active=True, org=org).order_by('-created_on') context['news'] = news.count() > 0 context['most_active_regions'] = org.get_regions_stats() # global counter if org.get_config('common.is_global'): context['global_counter'] = get_global_count() context['gender_stats'] = org.get_gender_stats() context['age_stats'] = org.get_age_stats() context['reporters'] = org.get_reporters_count() return context
def get_context_data(self, **kwargs): context = super(IndexView, self).get_context_data(**kwargs) org = self.request.org context['org'] = org latest_poll = Poll.get_main_poll(org) context['latest_poll'] = latest_poll if context['latest_poll']: context['trending_words'] = latest_poll.get_trending_words() context['recent_polls'] = Poll.get_brick_polls(org) context['stories'] = Story.objects.filter(org=org, is_active=True, featured=True).order_by('created_on') other_stories = Story.objects.filter(org=org, is_active=True).exclude(pk__in=context['stories']) other_stories = other_stories.order_by('created_on') context['other_stories'] = other_stories videos = Video.objects.filter(is_active=True, org=org).order_by('-created_on') context['videos'] = videos news = NewsItem.objects.filter(is_active=True, org=org).order_by('-created_on') context['news'] = news.count() > 0 # we use gender label to estimate the most active region if org.get_config('gender_label'): context['most_active_regions'] = org.get_most_active_regions() # global counter if org.get_config('is_global'): context['global_counter'] = get_global_count() return context
def get_context_data(self, **kwargs): context = super(UreportersView, self).get_context_data(**kwargs) org = self.request.org context["org"] = org translation.activate(org.language) # remove the first option '' from calender.month_abbr context["months"] = [ six.text_type(_("%s")) % m for m in calendar.month_abbr ][1:] context["states"] = sorted( [ dict(id=k, name=v) for k, v in Boundary.get_org_top_level_boundaries_name(org).items() ], key=lambda c: c["name"], ) context["gender_stats"] = org.get_gender_stats() context["age_stats"] = json.loads(org.get_age_stats()) context["registration_stats"] = org.get_registration_stats() context["occupation_stats"] = org.get_occupation_stats() context["reporters"] = org.get_reporters_count() context["main_stories"] = Story.objects.filter( org=org, featured=True, is_active=True).order_by("-created_on") # global counter context["global_counter"] = get_global_count() context["average_response_rate"] = PollStats.get_average_response_rate( org) context["data_time_filters"] = [ dict(time_filter_number=key, label=str(val)) for key, val in PollStats.DATA_TIME_FILTERS.items() ] backend_options = org.backends.filter(is_active=True).values_list( "slug", flat=True) show_maps = reduce(operator.or_, [ bool(org.get_config("%s.state_label" % option)) for option in backend_options ], False) context["data_segments"] = [ dict(segment_type=key, label=str(val)) for key, val in PollStats.DATA_SEGMENTS.items() if key != "location" or show_maps ] context["data_metrics"] = [ dict(slug=key, title=str(val)) for key, val in PollStats.DATA_METRICS.items() ] return context
def test_get_global_count(self): with self.settings(CACHES = {'default': {'BACKEND': 'redis_cache.cache.RedisCache', 'LOCATION': '127.0.0.1:6379:1', 'OPTIONS': { 'CLIENT_CLASS': 'redis_cache.client.DefaultClient', } }}): self.assertEqual(get_global_count(), 0) ReportersCounter.objects.create(org=self.org, type='total-reporters', count=5) # ignored if not on the global homepage self.assertEqual(get_global_count(), 0) # add the org to the homepage self.org.set_config('is_on_landing_page', True) self.assertEqual(get_global_count(), 5)
def test_get_global_count(self): with self.settings( CACHES={ "default": { "BACKEND": "django_redis.cache.RedisCache", "LOCATION": "redis://127.0.0.1:6379/1", "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient" }, } }): with patch("ureport.utils.fetch_old_sites_count" ) as mock_old_sites_count: mock_old_sites_count.return_value = [] self.assertEqual(get_global_count(), 0) mock_old_sites_count.return_value = [ { "time": 500, "results": dict(size=300) }, { "time": 500, "results": dict(size=50) }, ] from django.core.cache import cache cache.delete(GLOBAL_COUNT_CACHE_KEY) cache.set("org:ignored:reporters:old-site", { "time": 500, "results": dict(size=100) }, None) self.assertEqual(get_global_count(), 350) with patch("django.core.cache.cache.get") as cache_get_mock: cache_get_mock.return_value = 20 self.assertEqual(get_global_count(), 20) cache_get_mock.assert_called_once_with("global_count", None)
def test_get_global_count(self): with self.settings(CACHES={'default': {'BACKEND': 'django_redis.cache.RedisCache', 'LOCATION': '127.0.0.1:6379:1', 'OPTIONS': { 'CLIENT_CLASS': 'django_redis.client.DefaultClient', }}}): with patch('ureport.utils.fetch_old_sites_count') as mock_old_sites_count: mock_old_sites_count.return_value = [] self.assertEqual(get_global_count(), 0) ReportersCounter.objects.create(org=self.org, type='total-reporters', count=5) # ignored if not on the global homepage self.assertEqual(get_global_count(), 0) # add the org to the homepage self.org.set_config('common.is_on_landing_page', True) self.assertEqual(get_global_count(), 5) mock_old_sites_count.return_value = [{'time': 500, 'results': dict(size=300)}, {'time': 500, 'results': dict(size=50)}] from django.core.cache import cache cache.delete(GLOBAL_COUNT_CACHE_KEY) self.assertEqual(get_global_count(), 355) cache.delete(GLOBAL_COUNT_CACHE_KEY) self.org.set_config('common.is_on_landing_page', False) self.assertEqual(get_global_count(), 350) with patch('django.core.cache.cache.get') as cache_get_mock: cache_get_mock.return_value = 20 self.assertEqual(get_global_count(), 20) cache_get_mock.assert_called_once_with('global_count', None)
def get_context_data(self, **kwargs): context = super(IndexView, self).get_context_data(**kwargs) org = self.request.org context["org"] = org latest_poll = Poll.get_main_poll(org) context["latest_poll"] = latest_poll if context["latest_poll"]: context["trending_words"] = latest_poll.get_trending_words() brick_poll_ids = Poll.get_brick_polls_ids(org) context["recent_polls"] = Poll.objects.filter( id__in=brick_poll_ids).order_by("-created_on") context["stories"] = Story.objects.filter( org=org, is_active=True, featured=True).order_by("-created_on") videos = Video.objects.filter(is_active=True, org=org).order_by("-created_on") context["videos"] = videos news = NewsItem.objects.filter(is_active=True, org=org).order_by("-created_on") context["news"] = news.count() > 0 context["most_active_regions"] = org.get_regions_stats() # global counters context["global_contact_count"] = get_global_count() context["global_org_count"] = Org.objects.filter( is_active=True).count() + len(settings.PREVIOUS_ORG_SITES) context["gender_stats"] = org.get_gender_stats() context["age_stats"] = json.loads(org.get_age_stats()) context["reporters"] = org.get_reporters_count() context["feat_images"] = range(10) # fake photos, generated from stories that are featured and have a photo context["photos"] = (Story.objects.filter( org=org, featured=True, is_active=True).exclude(images=None).order_by("-created_on")[4:]) context["main_stories"] = Story.objects.filter( org=org, featured=True, is_active=True).order_by("-created_on") return context
def get(self, request, *args, **kwargs): global_count = get_global_count() linked_sites = list(getattr(settings, "COUNTRY_FLAGS_SITES", [])) for elt in linked_sites: if ( elt.get("show_icon", True) and elt.get("flag", "") and not elt["flag"].startswith("https://ureport.in/sitestatic/img/site_flags/") ): elt["flag"] = f'https://ureport.in/sitestatic/img/site_flags/{elt["flag"]}' unique_countries = set() for elt in linked_sites: unique_countries.update(elt.get("countries_codes", [])) countries_count = len(unique_countries) json_dict = dict(global_count=global_count, linked_sites=linked_sites, countries_count=countries_count) return HttpResponse(json.dumps(json_dict), status=200, content_type="application/json")
def test_get_global_count(self): with self.settings( CACHES={ "default": { "BACKEND": "django_redis.cache.RedisCache", "LOCATION": "127.0.0.1:6379:1", "OPTIONS": {"CLIENT_CLASS": "django_redis.client.DefaultClient"}, } } ): with patch("ureport.utils.fetch_old_sites_count") as mock_old_sites_count: mock_old_sites_count.return_value = [] self.assertEqual(get_global_count(), 0) ReportersCounter.objects.create(org=self.org, type="total-reporters", count=5) # ignored if not on the global homepage self.assertEqual(get_global_count(), 0) # add the org to the homepage self.org.set_config("common.is_on_landing_page", True) self.assertEqual(get_global_count(), 5) mock_old_sites_count.return_value = [ {"time": 500, "results": dict(size=300)}, {"time": 500, "results": dict(size=50)}, ] from django.core.cache import cache cache.delete(GLOBAL_COUNT_CACHE_KEY) self.assertEqual(get_global_count(), 355) cache.delete(GLOBAL_COUNT_CACHE_KEY) self.org.set_config("common.is_on_landing_page", False) self.assertEqual(get_global_count(), 350) with patch("django.core.cache.cache.get") as cache_get_mock: cache_get_mock.return_value = 20 self.assertEqual(get_global_count(), 20) cache_get_mock.assert_called_once_with("global_count", None)