Example #1
0
 def cache_users_at_location(self, selected_users):
     user_cache_list = []
     for doc in iter_docs(CommCareUser.get_db(), selected_users):
         display_username = user_display_string(
             doc['username'], doc.get('first_name', ''), doc.get('last_name', ''))
         user_cache_list.append({'text': display_username, 'id': doc['_id']})
     self.get_users_at_location.set_cached_value(self).to(user_cache_list)
Example #2
0
 def _ids_to_users(self, user_ids):
     users = (UserES()
              .domain(self.domain)
              .user_ids(user_ids)
              .values('_id', 'username', 'first_name', 'last_name'))
     return {
         u['_id']: user_display_string(u['username'], u['first_name'], u['last_name'])
         for u in users
     }
Example #3
0
 def get_users_at_location(self):
     user_query = UserES().domain(
         self.domain_object.name
     ).mobile_users().location(
         self.location.location_id
     ).fields(['_id', 'username', 'first_name', 'last_name'])
     return [
         dict(id=u['_id'], text=user_display_string(
             u['username'], u.get('first_name', ''), u.get('last_name', '')
         )) for u in user_query.run().hits]
Example #4
0
 def get_all_users(self):
     user_query = (UserES()
                   .domain(self.domain_object.name)
                   .mobile_users()
                   .fields(['_id', 'username', 'first_name', 'last_name']))
     return [
         (u['_id'], user_display_string(u['username'],
                                        u.get('first_name', ''),
                                        u.get('last_name', '')))
         for u in user_query.run().hits
     ]
Example #5
0
 def process_users(self, users, fmt_for_export=False):
     rows = []
     first = self.pagination.start
     last = first + self.pagination.count
     for user in users[first:last]:
         rows.append([
             user_display_string(user.user_dim.username,
                                 user.user_dim.first_name,
                                 user.user_dim.last_name),
             _fmt_date(user.last_form_submission_date, fmt_for_export),
             _fmt_date(user.last_sync_log_date, fmt_for_export),
             getattr(user.app_dim, 'name', '---'),
             user.last_form_app_build_version,
             user.last_form_app_commcare_version
         ])
     return rows
Example #6
0
 def process_rows(self, users, fmt_for_export=False):
     rows = []
     for user in users:
         last_build = last_seen = last_sub = last_sync = last_sync_date = app_name = commcare_version = None
         build_version = _("Unknown")
         reporting_metadata = user.get('reporting_metadata', {})
         if self.selected_app_id:
             last_submissions = reporting_metadata.get('last_submissions')
             if last_submissions:
                 last_sub = self.get_data_for_app(last_submissions, self.selected_app_id)
             last_syncs = reporting_metadata.get('last_syncs')
             if last_syncs:
                 last_sync = self.get_data_for_app(last_syncs, self.selected_app_id)
                 if last_sync is None:
                     last_sync = self.get_data_for_app(last_syncs, None)
             last_builds = reporting_metadata.get('last_builds')
             if last_builds:
                 last_build = self.get_data_for_app(last_builds, self.selected_app_id)
         else:
             last_sub = reporting_metadata.get('last_submission_for_user', {})
             last_sync = reporting_metadata.get('last_sync_for_user', {})
             last_build = reporting_metadata.get('last_build_for_user', {})
         if last_sub and last_sub.get('commcare_version'):
             commcare_version = _get_commcare_version(last_sub.get('commcare_version'))
         else:
             devices = user.get('devices', None)
             if devices:
                 device = max(devices, key=lambda dev: dev['last_used'])
                 if device.get('commcare_version', None):
                     commcare_version = _get_commcare_version(device['commcare_version'])
         if last_sub and last_sub.get('submission_date'):
             last_seen = string_to_utc_datetime(last_sub['submission_date'])
         if last_sync and last_sync.get('sync_date'):
             last_sync_date = string_to_utc_datetime(last_sync['sync_date'])
         if last_build:
             build_version = last_build.get('build_version') or build_version
             if last_build.get('app_id'):
                 app_name = self.get_app_name(last_build['app_id'])
         rows.append([
             user_display_string(user.get('username', ''),
                                 user.get('first_name', ''),
                                 user.get('last_name', '')),
             _fmt_date(last_seen, fmt_for_export), _fmt_date(last_sync_date, fmt_for_export),
             app_name or "---", build_version, commcare_version or '---'
         ])
     return rows
Example #7
0
 def test_is_escaped(self):
     result = user_display_string('test@d<i>magi.com', 'T<e>st', 'U<s>er')
     self.assertEqual(result, 'test@d&lt;i&gt;magi.com "T&lt;e&gt;st U&lt;s&gt;er"')
Example #8
0
 def test_only_username(self):
     result = user_display_string('*****@*****.**', '', '')
     self.assertEqual(result, '*****@*****.**')
Example #9
0
    def process_rows(self, users, fmt_for_export=False):
        rows = []
        users = list(users)

        if self.include_location_data():
            location_ids = {user['location_id'] for user in users if user['location_id']}
            grouped_ancestor_locs = self.get_bulk_ancestors(location_ids)
            self.required_loc_columns = self.get_location_columns(grouped_ancestor_locs)

        for user in users:
            last_build = last_seen = last_sub = last_sync = last_sync_date = app_name = commcare_version = None
            last_build_profile_name = None
            build_version = _("Unknown")
            reporting_metadata = user.get('reporting_metadata', {})
            if self.selected_app_id:
                last_submissions = reporting_metadata.get('last_submissions')
                if last_submissions:
                    last_sub = self.get_data_for_app(last_submissions, self.selected_app_id)
                last_syncs = reporting_metadata.get('last_syncs')
                if last_syncs:
                    last_sync = self.get_data_for_app(last_syncs, self.selected_app_id)
                    if last_sync is None:
                        last_sync = self.get_data_for_app(last_syncs, None)
                last_builds = reporting_metadata.get('last_builds')
                if last_builds:
                    last_build = self.get_data_for_app(last_builds, self.selected_app_id)
            else:
                last_sub = reporting_metadata.get('last_submission_for_user', {})
                last_sync = reporting_metadata.get('last_sync_for_user', {})
                last_build = reporting_metadata.get('last_build_for_user', {})
            if last_sub and last_sub.get('commcare_version'):
                commcare_version = _get_commcare_version(last_sub.get('commcare_version'))
            else:
                devices = user.get('devices', None)
                if devices:
                    device = max(devices, key=lambda dev: dev['last_used'])
                    if device.get('commcare_version', None):
                        commcare_version = _get_commcare_version(device['commcare_version'])
            if last_sub and last_sub.get('submission_date'):
                last_seen = string_to_utc_datetime(last_sub['submission_date'])
            if last_sync and last_sync.get('sync_date'):
                last_sync_date = string_to_utc_datetime(last_sync['sync_date'])
            if last_build:
                build_version = last_build.get('build_version') or build_version
                if last_build.get('app_id'):
                    app_name = self.get_app_name(last_build['app_id'])
                if self.show_build_profile:
                    last_build_profile_id = last_build.get('build_profile_id')
                    if last_build_profile_id:
                        last_build_profile_name = _("Unknown")
                        build_profiles = self._get_app_details(last_build['app_id']).get('build_profiles', {})
                        if last_build_profile_id in build_profiles:
                            last_build_profile_name = build_profiles[last_build_profile_id]

            row_data = [
                user_display_string(user.get('username', ''),
                                    user.get('first_name', ''),
                                    user.get('last_name', '')),
                _fmt_date(last_seen, fmt_for_export), _fmt_date(last_sync_date, fmt_for_export),
                app_name or "---", build_version, commcare_version or '---'
            ]
            if self.show_build_profile:
                row_data.append(last_build_profile_name)

            if self.include_location_data():
                location_data = self.user_locations(grouped_ancestor_locs.get(user['location_id'], []),
                                                    self.required_loc_columns)
                row_data = location_data + row_data

            rows.append(row_data)
        return rows