Example #1
0
    def case_owners(self):

        # Get user ids for each user that match the demo_user, admin, Unknown Users, or All Mobile Workers filters
        user_types = EMWF.selected_user_types(self.request)
        user_type_filters = []
        if HQUserType.ADMIN in user_types:
            user_type_filters.append(user_es.admin_users())
        if HQUserType.UNKNOWN in user_types:
            user_type_filters.append(user_es.unknown_users())
            user_type_filters.append(user_es.web_users())
        if HQUserType.DEMO_USER in user_types:
            user_type_filters.append(user_es.demo_users())
        if HQUserType.REGISTERED in user_types:
            user_type_filters.append(user_es.mobile_users())

        if len(user_type_filters) > 0:
            special_q = user_es.UserES().domain(self.domain).OR(*user_type_filters).show_inactive()
            special_user_ids = special_q.run().doc_ids
        else:
            special_user_ids = []

        # Get user ids for each user that was specifically selected
        selected_user_ids = EMWF.selected_user_ids(self.request)

        # Get group ids for each group that was specified
        selected_reporting_group_ids = EMWF.selected_reporting_group_ids(self.request)
        selected_sharing_group_ids = EMWF.selected_sharing_group_ids(self.request)

        # Get user ids for each user in specified reporting groups
        report_group_q = HQESQuery(index="groups").domain(self.domain)\
                                           .doc_type("Group")\
                                           .filter(filters.term("_id", selected_reporting_group_ids))\
                                           .fields(["users"])
        user_lists = [group["users"] for group in report_group_q.run().hits]
        selected_reporting_group_users = list(set().union(*user_lists))

        # Get ids for each sharing group that contains a user from selected_reporting_group_users OR a user that was specifically selected
        share_group_q = HQESQuery(index="groups").domain(self.domain)\
                                                .doc_type("Group")\
                                                .filter(filters.term("case_sharing", True))\
                                                .filter(filters.term("users", selected_reporting_group_users+selected_user_ids+special_user_ids))\
                                                .fields([])
        sharing_group_ids = share_group_q.run().doc_ids

        owner_ids = list(set().union(
            special_user_ids,
            selected_user_ids,
            selected_sharing_group_ids,
            selected_reporting_group_users,
            sharing_group_ids
        ))
        if HQUserType.COMMTRACK in user_types:
            owner_ids.append("commtrack-system")
        if HQUserType.DEMO_USER in user_types:
            owner_ids.append("demo_user_group_id")

        owner_ids += self.location_sharing_owner_ids()
        owner_ids += self.location_reporting_owner_ids()
        return owner_ids
Example #2
0
    def case_owners(self):

        # Get user ids for each user that match the demo_user, admin, Unknown Users, or All Mobile Workers filters
        user_types = EMWF.selected_user_types(self.request)
        user_type_filters = []
        if HQUserType.ADMIN in user_types:
            user_type_filters.append(user_es.admin_users())
        if HQUserType.UNKNOWN in user_types:
            user_type_filters.append(user_es.unknown_users())
            user_type_filters.append(user_es.web_users())
        if HQUserType.DEMO_USER in user_types:
            user_type_filters.append(user_es.demo_users())
        if HQUserType.REGISTERED in user_types:
            user_type_filters.append(user_es.mobile_users())

        if len(user_type_filters) > 0:
            special_q = user_es.UserES().domain(
                self.domain).OR(*user_type_filters)
            special_user_ids = special_q.run().doc_ids
        else:
            special_user_ids = []

        # Get user ids for each user that was specifically selected
        selected_user_ids = EMWF.selected_user_ids(self.request)

        # Get group ids for each group that was specified
        selected_reporting_group_ids = EMWF.selected_reporting_group_ids(
            self.request)
        selected_sharing_group_ids = EMWF.selected_sharing_group_ids(
            self.request)

        # Get user ids for each user in specified reporting groups
        report_group_q = HQESQuery(index="groups").domain(self.domain)\
                                           .doc_type("Group")\
                                           .filter(filters.term("_id", selected_reporting_group_ids))\
                                           .fields(["users"])
        user_lists = [group["users"] for group in report_group_q.run().hits]
        selected_reporting_group_users = list(set().union(*user_lists))

        # Get ids for each sharing group that contains a user from selected_reporting_group_users OR a user that was specifically selected
        share_group_q = HQESQuery(index="groups").domain(self.domain)\
                                                .doc_type("Group")\
                                                .filter(filters.term("case_sharing", True))\
                                                .filter(filters.term("users", selected_reporting_group_users+selected_user_ids+special_user_ids))\
                                                .fields([])
        sharing_group_ids = share_group_q.run().doc_ids

        owner_ids = list(set().union(special_user_ids, selected_user_ids,
                                     selected_sharing_group_ids,
                                     selected_reporting_group_users,
                                     sharing_group_ids))
        if HQUserType.COMMTRACK in EMWF.selected_user_types(self.request):
            owner_ids.append("commtrack-system")
        return owner_ids
Example #3
0
    def _get_aggregated_query(self, start, limit):
        max_size = (start or 0) + (limit or 0)
        query = HQESQuery(self.table_name).size(0)
        for filter in self.filters:
            query = query.filter(filter)

        innermost_agg_col = self.aggregation_columns[-1]
        innermost_agg = TermsAggregation(innermost_agg_col, innermost_agg_col, size=max_size)

        aggregations = []
        for col in self.top_level_columns:
            for agg in col.aggregations(self.config, self.lang):
                innermost_agg.aggregation(agg)

        top_agg = innermost_agg
        # go through aggregations in reverse order so that they are nested properly
        for agg_column in self.aggregation_columns[:-1][::-1]:
            top_agg = TermsAggregation(agg_column, agg_column, size=max_size).aggregation(top_agg)

        if self.order_by:
            col, desc = self.order_by[0]
            valid_columns = (
                self.aggregation_columns[0],
                self.top_level_columns[0].field,
                self.top_level_columns[0].column_id
            )
            if col in valid_columns:
                top_agg = top_agg.order('_term', desc)

        query = query.aggregation(top_agg)

        return query.run()
Example #4
0
    def _get_aggregated_query(self, start, limit):
        max_size = (start or 0) + (limit or 0)
        query = HQESQuery(self.table_name).size(0)
        for filter in self.filters:
            query = query.filter(filter)

        innermost_agg_col = self.aggregation_columns[-1]
        innermost_agg = TermsAggregation(innermost_agg_col,
                                         innermost_agg_col,
                                         size=max_size)

        aggregations = []
        for col in self.top_level_columns:
            for agg in col.aggregations(self.config, self.lang):
                innermost_agg.aggregation(agg)

        top_agg = innermost_agg
        # go through aggregations in reverse order so that they are nested properly
        # todo: Refactor NestedTermAggregationsHelper to support this use case
        for agg_column in self.aggregation_columns[:-1][::-1]:
            top_agg = TermsAggregation(agg_column, agg_column,
                                       size=max_size).aggregation(top_agg)

        if self.order_by:
            # todo sort by more than one column
            # todo sort by by something other than the first aggregate column
            col, desc = self.order_by[0]
            if col == self.aggregation_columns[
                    0] or col == self.top_level_columns[0].field:
                top_agg = top_agg.order('_term', desc)

        query = query.aggregation(top_agg)

        return query.run()
Example #5
0
    def _get_aggregated_query(self, start, limit):
        max_size = (start or 0) + (limit or 0)
        query = HQESQuery(self.table_name).size(0)
        for filter in self.filters:
            query = query.filter(filter)

        innermost_agg_col = self.aggregation_columns[-1]
        innermost_agg = TermsAggregation(innermost_agg_col, innermost_agg_col)

        aggregations = []
        for col in self.top_level_columns:
            for agg in col.aggregations(self.config, self.lang):
                innermost_agg.aggregation(agg)

        top_agg = innermost_agg
        # go through aggregations in reverse order so that they are nested properly
        # todo: Refactor NestedTermAggregationsHelper to support this use case
        for agg_column in self.aggregation_columns[:-1][::-1]:
            top_agg = TermsAggregation(agg_column, agg_column).aggregation(top_agg)

        top_agg.size(max_size)

        if self.order_by:
            # todo sort by more than one column
            # todo sort by by something other than the first aggregate column
            col, desc = self.order_by[0]
            if col == self.aggregation_columns[0] or col == self.top_level_columns[0].field:
                top_agg = top_agg.order('_term', desc)

        query = query.aggregation(top_agg)

        return query.run()
Example #6
0
    def case_owners(self):
        # Get user ids for each user that match the demo_user, admin,
        # Unknown Users, or All Mobile Workers filters
        mobile_user_and_group_slugs = self.request.GET.getlist(EMWF.slug)
        user_types = EMWF.selected_user_types(mobile_user_and_group_slugs)
        special_owner_ids = self.get_special_owner_ids(
            admin=HQUserType.ADMIN in user_types,
            unknown=HQUserType.UNKNOWN in user_types,
            demo=HQUserType.DEMO_USER in user_types,
            commtrack=HQUserType.COMMTRACK in user_types,
        )

        # Get user ids for each user that was specifically selected
        selected_user_ids = EMWF.selected_user_ids(mobile_user_and_group_slugs)

        # Get group ids for each group that was specified
        selected_reporting_group_ids = EMWF.selected_reporting_group_ids(
            mobile_user_and_group_slugs)
        selected_sharing_group_ids = EMWF.selected_sharing_group_ids(
            mobile_user_and_group_slugs)

        # Show cases owned by any selected locations, user locations, or their children
        loc_ids = set(
            EMWF.selected_location_ids(mobile_user_and_group_slugs) +
            get_users_location_ids(self.domain, selected_user_ids))
        location_owner_ids = get_locations_and_children(loc_ids).location_ids()

        # Get user ids for each user in specified reporting groups
        report_group_q = HQESQuery(index="groups").domain(self.domain)\
                                           .doc_type("Group")\
                                           .filter(filters.term("_id", selected_reporting_group_ids))\
                                           .fields(["users"])
        user_lists = [group["users"] for group in report_group_q.run().hits]
        selected_reporting_group_users = list(set().union(*user_lists))

        # Get ids for each sharing group that contains a user from selected_reporting_group_users OR a user that was specifically selected
        share_group_q = (HQESQuery(index="groups").domain(
            self.domain).doc_type("Group").term("case_sharing", True).term(
                "users",
                (selected_reporting_group_users + selected_user_ids)).fields(
                    []))
        sharing_group_ids = share_group_q.run().doc_ids

        owner_ids = list(set().union(
            special_owner_ids,
            selected_user_ids,
            selected_sharing_group_ids,
            selected_reporting_group_users,
            sharing_group_ids,
            location_owner_ids,
        ))
        return owner_ids
Example #7
0
    def case_owners(self):
        # Get user ids for each user that match the demo_user, admin,
        # Unknown Users, or All Mobile Workers filters
        mobile_user_and_group_slugs = self.request.GET.getlist(EMWF.slug)
        user_types = EMWF.selected_user_types(mobile_user_and_group_slugs)
        special_owner_ids = self.get_special_owner_ids(
            admin=HQUserType.ADMIN in user_types,
            unknown=HQUserType.UNKNOWN in user_types,
            demo=HQUserType.DEMO_USER in user_types,
            commtrack=HQUserType.COMMTRACK in user_types,
        )

        # Get user ids for each user that was specifically selected
        selected_user_ids = EMWF.selected_user_ids(mobile_user_and_group_slugs)

        # Get group ids for each group that was specified
        selected_reporting_group_ids = EMWF.selected_reporting_group_ids(mobile_user_and_group_slugs)
        selected_sharing_group_ids = EMWF.selected_sharing_group_ids(mobile_user_and_group_slugs)

        # Show cases owned by any selected locations, user locations, or their children
        loc_ids = set(EMWF.selected_location_ids(mobile_user_and_group_slugs) +
                      get_users_location_ids(self.domain, selected_user_ids))
        location_owner_ids = get_locations_and_children(loc_ids).location_ids()

        # Get user ids for each user in specified reporting groups
        report_group_q = HQESQuery(index="groups").domain(self.domain)\
                                           .doc_type("Group")\
                                           .filter(filters.term("_id", selected_reporting_group_ids))\
                                           .fields(["users"])
        user_lists = [group["users"] for group in report_group_q.run().hits]
        selected_reporting_group_users = list(set().union(*user_lists))

        # Get ids for each sharing group that contains a user from selected_reporting_group_users OR a user that was specifically selected
        share_group_q = (HQESQuery(index="groups")
                         .domain(self.domain)
                         .doc_type("Group")
                         .term("case_sharing", True)
                         .term("users", (selected_reporting_group_users +
                                         selected_user_ids))
                         .fields([]))
        sharing_group_ids = share_group_q.run().doc_ids

        owner_ids = list(set().union(
            special_owner_ids,
            selected_user_ids,
            selected_sharing_group_ids,
            selected_reporting_group_users,
            sharing_group_ids,
            location_owner_ids,
        ))
        return owner_ids
Example #8
0
    def _get_query(self, start=None, limit=None):
        query = HQESQuery(self.table_name).source(self.required_fields)
        for column, order in self.order_by:
            query = query.sort(column, desc=(order == DESCENDING), reset_sort=False)

        if start:
            query = query.start(start)
        if limit:
            query = query.size(limit)

        for filter in self.filters:
            query = query.filter(filter)

        return query.run()
Example #9
0
    def _get_query(self, start=None, limit=None):
        query = HQESQuery(self.table_name).source(self.required_fields)
        for column, order in self.order_by:
            query = query.sort(column, desc=(order == DESCENDING), reset_sort=False)

        if start:
            query = query.start(start)
        if limit:
            query = query.size(limit)

        for filter in self.filters:
            query = query.filter(filter)

        return query.run()
Example #10
0
    def _get_total_aggregated_results(self):
        query = HQESQuery(self.table_name).size(0)
        for filter in self.filters:
            query = query.filter(filter)

        columns = [col for col in self.top_level_columns if col.calculate_total]
        totals_aggregations = []

        for col in columns:
            for agg in col.aggregations(self.config, self.lang):
                totals_aggregations.append(agg)

        query = query.aggregations(totals_aggregations)

        return query.run().aggregations
Example #11
0
    def _get_aggregated_query(self, start, limit):
        max_size = (start or 0) + (limit or 0)
        query = HQESQuery(self.table_name).size(0)
        for filter in self.filters:
            query = query.filter(filter)

        top_agg = TermsAggregation(self.aggregation_columns[0],
                                   self.aggregation_columns[0],
                                   size=max_size)
        for agg_column in self.aggregation_columns[1:]:
            # todo support multiple aggregations
            pass

        aggregations = []
        for col in self.top_level_columns:
            if col.type == 'expanded':
                for sub_col in get_expanded_column_config(
                        self.config, col, 'en').columns:
                    aggregations.append(sub_col.aggregation)
            elif col.type == 'field':
                if col.aggregation == 'sum':
                    # todo push this to the column
                    aggregations.append(SumAggregation(col.field, col.field))

        for agg in aggregations:
            top_agg = top_agg.aggregation(agg)

        if self.order_by:
            # todo sort by more than one column
            # todo sort by by something other than the first aggregate column
            col, desc = self.order_by[0]
            if col == self.aggregation_columns[
                    0] or col == self.top_level_columns[0].field:
                top_agg = top_agg.order('_count', desc)

        query = query.aggregation(top_agg)

        return query.run()