Exemplo n.º 1
0
    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
Exemplo n.º 2
0
    def render_to_response(self, context, **kwargs):
        output_data = []

        results_params = self.request.GET.get("results_params", None)
        if results_params:
            results_params = json.loads(results_params)
            metric = results_params.get("metric")
            segment_slug = results_params.get("segment")
            time_filter = int(results_params.get("filter", "12"))

            output_data = PollStats.get_engagement_data(self.get_object(), metric, segment_slug, time_filter)

        return HttpResponse(json.dumps(output_data))
Exemplo n.º 3
0
def polls_stats_squash():
    from ureport.stats.models import PollStats

    PollStats.squash()