Esempio n. 1
0
    def _get_users_filter(self, mobile_user_and_group_slugs):
        user_ids = (EMWF.user_es_query(
            self.domain, mobile_user_and_group_slugs,
            self.request.couch_user).values_list('_id', flat=True))

        if HQUserType.UNKNOWN in EMWF.selected_user_types(
                mobile_user_and_group_slugs):
            user_ids.append(SYSTEM_USER_ID)

        return form_es.user_id(user_ids)
Esempio n. 2
0
    def _get_users_filter(self, mobile_user_and_group_slugs):
        user_ids = (EMWF.user_es_query(self.domain,
                                       mobile_user_and_group_slugs,
                                       self.request.couch_user)
                    .values_list('_id', flat=True))

        if HQUserType.UNKNOWN in EMWF.selected_user_types(mobile_user_and_group_slugs):
            user_ids.append(SYSTEM_USER_ID)

        return form_es.user_id(user_ids)
Esempio n. 3
0
    def _es_extra_filters(self):
        if FormsByApplicationFilter.has_selections(self.request):

            def form_filter(form):
                app_id = form.get('app_id', None)
                if app_id and app_id != MISSING_APP_ID:
                    return {
                        'and': [{
                            'term': {
                                'xmlns.exact': form['xmlns']
                            }
                        }, {
                            'term': {
                                'app_id': app_id
                            }
                        }]
                    }
                return {'term': {'xmlns.exact': form['xmlns']}}

            form_values = self.all_relevant_forms.values()
            if form_values:
                yield {'or': [form_filter(f) for f in form_values]}

        truthy_only = functools.partial(filter, None)
        mobile_user_and_group_slugs = self.request.GET.getlist(
            ExpandedMobileWorkerFilter.slug)
        users_data = ExpandedMobileWorkerFilter.pull_users_and_groups(
            self.domain, mobile_user_and_group_slugs, include_inactive=True)
        all_mobile_workers_selected = 't__0' in self.request.GET.getlist('emw')
        if not all_mobile_workers_selected or users_data.admin_and_demo_users:
            yield {
                'terms': {
                    'form.meta.userID':
                    truthy_only(u.user_id for u in users_data.combined_users)
                }
            }
        else:
            negated_ids = util.get_all_users_by_domain(
                self.domain,
                user_filter=HQUserType.all_but_users(),
                simplified=True,
            )
            yield {
                'not': {
                    'terms': {
                        'form.meta.userID':
                        truthy_only(user.user_id for user in negated_ids)
                    }
                }
            }

        if HQUserType.UNKNOWN not in ExpandedMobileWorkerFilter.selected_user_types(
                mobile_user_and_group_slugs):
            yield {'not': {'term': {'xmlns.exact': SYSTEM_FORM_XMLNS}}}
Esempio n. 4
0
    def _es_extra_filters(self):
        if FormsByApplicationFilter.has_selections(self.request):
            def form_filter(form):
                app_id = form.get('app_id', None)
                if app_id and app_id != MISSING_APP_ID:
                    return {'and': [{'term': {'xmlns.exact': form['xmlns']}},
                                    {'term': {'app_id': app_id}}]}
                return {'term': {'xmlns.exact': form['xmlns']}}
            form_values = self.all_relevant_forms.values()
            if form_values:
                yield {'or': [form_filter(f) for f in form_values]}

        truthy_only = functools.partial(filter, None)
        mobile_user_and_group_slugs = self.request.GET.getlist(ExpandedMobileWorkerFilter.slug)
        users_data = ExpandedMobileWorkerFilter.pull_users_and_groups(
            self.domain,
            mobile_user_and_group_slugs,
            include_inactive=True
        )
        all_mobile_workers_selected = 't__0' in self.request.GET.getlist('emw')
        if not all_mobile_workers_selected or users_data.admin_and_demo_users:
            yield {
                'terms': {
                    'form.meta.userID': truthy_only(
                        u.user_id for u in users_data.combined_users
                    )
                }
            }
        else:
            negated_ids = util.get_all_users_by_domain(
                self.domain,
                user_filter=HQUserType.all_but_users(),
                simplified=True,
            )
            yield {
                'not': {
                    'terms': {
                        'form.meta.userID': truthy_only(
                            user.user_id for user in negated_ids
                        )
                    }
                }
            }

        props = truthy_only(self.request.GET.get('form_data', '').split(','))
        for prop in props:
            yield {
                'term': {'__props_for_querying': prop}
            }

        if HQUserType.UNKNOWN not in ExpandedMobileWorkerFilter.selected_user_types(mobile_user_and_group_slugs):
            yield {
                'not': {'term': {'xmlns.exact': SYSTEM_FORM_XMLNS}}
            }
Esempio n. 5
0
    def _to_form_export_instance_filters(self, can_access_all_locations, accessible_location_ids):
        emwf_selections = self.cleaned_data["emwf_form_filter"]

        return FormExportInstanceFilters(
            date_period=DatePeriod(
                period_type=self.cleaned_data['date_range'],
                days=self.cleaned_data['days'],
                begin=self.cleaned_data['start_date'],
                end=self.cleaned_data['end_date'],
            ),
            users=ExpandedMobileWorkerFilter.selected_user_ids(emwf_selections),
            reporting_groups=ExpandedMobileWorkerFilter.selected_reporting_group_ids(emwf_selections),
            locations=ExpandedMobileWorkerFilter.selected_location_ids(emwf_selections),
            user_types=ExpandedMobileWorkerFilter.selected_user_types(emwf_selections),
            can_access_all_locations=can_access_all_locations,
            accessible_location_ids=accessible_location_ids,
        )
Esempio n. 6
0
    def _to_form_export_instance_filters(self, can_access_all_locations, accessible_location_ids):
        emwf_selections = [x['id'] for x in self.cleaned_data["emwf_form_filter"]]

        return FormExportInstanceFilters(
            date_period=DatePeriod(
                period_type=self.cleaned_data['date_range'],
                days=self.cleaned_data['days'],
                begin=self.cleaned_data['start_date'],
                end=self.cleaned_data['end_date'],
            ),
            users=ExpandedMobileWorkerFilter.selected_user_ids(emwf_selections),
            reporting_groups=ExpandedMobileWorkerFilter.selected_reporting_group_ids(emwf_selections),
            locations=ExpandedMobileWorkerFilter.selected_location_ids(emwf_selections),
            user_types=ExpandedMobileWorkerFilter.selected_user_types(emwf_selections),
            can_access_all_locations=can_access_all_locations,
            accessible_location_ids=accessible_location_ids,
        )
Esempio n. 7
0
 def _get_users_filter(self, mobile_user_and_group_slugs):
     truthy_only = functools.partial(filter, None)
     users_data = EMWF.pull_users_and_groups(self.domain,
                                             mobile_user_and_group_slugs,
                                             include_inactive=True)
     selected_user_types = EMWF.selected_user_types(
         mobile_user_and_group_slugs)
     all_mobile_workers_selected = HQUserType.REGISTERED in selected_user_types
     if not all_mobile_workers_selected or users_data.admin_and_demo_users:
         return form_es.user_id(
             truthy_only(u.user_id for u in users_data.combined_users))
     else:
         negated_ids = util.get_all_users_by_domain(
             self.domain,
             user_filter=HQUserType.all_but_users(),
             simplified=True,
         )
         return es_filters.NOT(
             form_es.user_id(
                 truthy_only(user.user_id for user in negated_ids)))
Esempio n. 8
0
    def es_query(self):
        time_filter = form_es.submitted if self.by_submission_time else form_es.completed
        mobile_user_and_group_slugs = self.request.GET.getlist(EMWF.slug)

        query = (form_es.FormES()
                .domain(self.domain)
                .filter(time_filter(gte=self.datespan.startdate,
                                    lt=self.datespan.enddate_adjusted))
                .filter(self._get_users_filter(mobile_user_and_group_slugs)))

        # filter results by app and xmlns if applicable
        if FormsByApplicationFilter.has_selections(self.request):
            form_values = self.all_relevant_forms.values()
            if form_values:
                query = query.OR(*[self._form_filter(f) for f in form_values])

        # Exclude system forms unless they selected "Unknown User"
        if HQUserType.UNKNOWN not in EMWF.selected_user_types(mobile_user_and_group_slugs):
            query = query.NOT(form_es.xmlns(SYSTEM_FORM_XMLNS))

        return query
Esempio n. 9
0
    def es_query(self):
        time_filter = form_es.submitted if self.by_submission_time else form_es.completed
        mobile_user_and_group_slugs = self.request.GET.getlist(EMWF.slug)

        query = (form_es.FormES()
                 .domain(self.domain)
                 .filter(time_filter(gte=self.datespan.startdate,
                                     lt=self.datespan.enddate_adjusted))
                 .filter(self._get_users_filter(mobile_user_and_group_slugs)))

        # filter results by app and xmlns if applicable
        if FormsByApplicationFilter.has_selections(self.request):
            form_values = list(self.all_relevant_forms.values())
            if form_values:
                query = query.OR(*[self._form_filter(f) for f in form_values])

        # Exclude system forms unless they selected "Unknown User"
        if HQUserType.UNKNOWN not in EMWF.selected_user_types(mobile_user_and_group_slugs):
            query = query.NOT(form_es.xmlns(SYSTEM_FORM_XMLNS))

        return query
Esempio n. 10
0
 def _get_users_filter(self, mobile_user_and_group_slugs):
     truthy_only = functools.partial(filter, None)
     users_data = EMWF.pull_users_and_groups(
         self.domain,
         mobile_user_and_group_slugs,
         include_inactive=True
     )
     selected_user_types = EMWF.selected_user_types(mobile_user_and_group_slugs)
     all_mobile_workers_selected = HQUserType.REGISTERED in selected_user_types
     if not all_mobile_workers_selected or users_data.admin_and_demo_users:
         return form_es.user_id(truthy_only(
             u.user_id for u in users_data.combined_users
         ))
     else:
         negated_ids = util.get_all_users_by_domain(
             self.domain,
             user_filter=HQUserType.all_but_users(),
             simplified=True,
         )
         return es_filters.NOT(form_es.user_id(truthy_only(
             user.user_id for user in negated_ids
         )))