Beispiel #1
0
 def __getitem__(self, item):
     if isinstance(item, slice):
         limit = item.stop - item.start
         result = GroupES().domain(self.domain).sort('name').size(limit).start(item.start).run()
         return map(Group.wrap, result.hits)
     raise ValueError(
         'Invalid type of argument. Item should be an instance of slice class.')
Beispiel #2
0
 def query(self, query_context):
     group_es = (GroupES().domain(
         self.domain).is_case_sharing().search_string_query(
             query_context.query,
             default_fields=['name']).size(query_context.limit).start(
                 query_context.offset).sort('name'))
     return self.get_choices_from_es_query(group_es)
Beispiel #3
0
def get_form_ids_having_multimedia(domain,
                                   app_id,
                                   xmlns,
                                   startdate,
                                   enddate,
                                   user_types=None,
                                   group=None):
    query = (FormES().domain(domain).app(app_id).xmlns(xmlns).submitted(
        gte=startdate, lte=enddate).remove_default_filter("has_user").source(
            ['_id', 'external_blobs']))

    if user_types:
        query = query.user_type(user_types)

    if group:
        results = (GroupES().domain(domain).group_ids([group]).source(
            ['users'])).run().hits
        assert len(results) <= 1
        user_ids = results[0]['users']
        query = query.user_id(user_ids)

    form_ids = set()
    for form in query.scroll():
        try:
            for attachment in _get_attachment_dicts_from_form(form):
                if attachment['content_type'] != "text/xml":
                    form_ids.add(form['_id'])
                    continue
        except AttributeError:
            pass
    return form_ids
Beispiel #4
0
 def _assert_group_in_es(self, group):
     results = GroupES().run()
     self.assertEqual(1, results.total)
     es_group = results.hits[0]
     self.assertEqual(group._id, es_group['_id'])
     self.assertEqual(group.name, es_group['name'])
     self.assertEqual(group.users, es_group['users'])
     self.assertEqual('Group', es_group['doc_type'])
Beispiel #5
0
def get_groups_by_querystring(domain, query, case_sharing_only):
    group_result = (GroupES().domain(domain).not_deleted().search_string_query(
        query, default_fields=['name']).size(10).sort('name.exact').source(
            ('_id', 'name')))
    if case_sharing_only:
        group_result = group_result.is_case_sharing()
    return [{
        'id': group['_id'],
        'text': group['name']
    } for group in group_result.run().hits]
Beispiel #6
0
def get_groups_user_ids(group_ids):
    q = (GroupES().doc_id(group_ids))

    results = []
    for user_list in q.values_list("users", flat=True):
        if isinstance(user_list, six.string_types):
            results.append(user_list)
        else:
            results.extend(user_list)

    return results
Beispiel #7
0
    def group_es_query(self, query, group_type="reporting"):
        if group_type == "reporting":
            type_filter = groups.is_reporting()
        elif group_type == "case_sharing":
            type_filter = groups.is_case_sharing()
        else:
            raise TypeError(
                "group_type '{}' not recognized".format(group_type))

        return (GroupES().domain(
            self.domain).filter(type_filter).not_deleted().search_string_query(
                query, default_fields=["name"]))
Beispiel #8
0
 def _get_user_group_response(self, case_sharing_only=False):
     domain = self.request.domain
     query = self.data.get('searchString')
     group_result = (
         GroupES().domain(domain).not_deleted().search_string_query(
             query, default_fields=['name']).size(10).sort('name').source(
                 ('_id', 'name')))
     if case_sharing_only:
         group_result = group_result.is_case_sharing()
     return [{
         'id': group['_id'],
         'text': group['name']
     } for group in group_result.run().hits]
def check_elasticsearch():
    doc = {
        '_id': 'elasticsearch-service-check',
        'date': datetime.datetime.now().isoformat()
    }
    send_to_elasticsearch('groups', doc)
    time.sleep(1)
    hits = GroupES().remove_default_filters().doc_id(doc['_id']).run().hits
    send_to_elasticsearch('groups', doc, delete=True)  # clean up
    if doc in hits:
        return ServiceStatus(True,
                             "Successfully sent a doc to ES and read it back")
    return ServiceStatus(False, "Something went wrong sending a doc to ES")
def check_elasticsearch():
    cluster_health = check_es_cluster_health()
    if cluster_health != 'green':
        return ServiceStatus(False, "Cluster health at %s" % cluster_health)

    doc = {'_id': 'elasticsearch-service-check',
           'date': datetime.datetime.now().isoformat()}
    send_to_elasticsearch('groups', doc)
    refresh_elasticsearch_index('groups')
    hits = GroupES().remove_default_filters().doc_id(doc['_id']).run().hits
    send_to_elasticsearch('groups', doc, delete=True)  # clean up
    if doc in hits:
        return ServiceStatus(True, "Successfully sent a doc to ES and read it back")
    return ServiceStatus(False, "Something went wrong sending a doc to ES")
Beispiel #11
0
 def schedule_user_group_recipients_response(self):
     domain = self.request.domain
     query = self.data.get('searchString')
     group_result = (
         GroupES()
         .domain(domain)
         .not_deleted()
         .search_string_query(query, default_fields=['name'])
         .size(10)
         .sort('name')
         .source(('_id', 'name'))
     )
     return [
         {'id': group['_id'], 'text': group['name']}
         for group in group_result.run().hits
     ]
    def __getitem__(self, item):
        if isinstance(item, slice):
            limit = item.stop - item.start
            result = GroupES().domain(
                self.domain).sort('name.exact').size(limit).start(
                    item.start).run()
            groups = [WrappedGroup.wrap(group) for group in result.hits]

            active_user_ids = set(self._iter_active_user_ids(groups))
            for group in groups:
                group._precomputed_active_users = [
                    user_id for user_id in group.users
                    if user_id in active_user_ids
                ]
            return groups
        raise ValueError(
            'Invalid type of argument. Item should be an instance of slice class.'
        )
Beispiel #13
0
 def get_choices_for_known_values(self, values, user):
     group_es = GroupES().domain(
         self.domain).is_case_sharing().doc_id(values)
     return self.get_choices_from_es_query(group_es)
Beispiel #14
0
 def query_count(self, query, user=None):
     group_es = (GroupES().domain(
         self.domain).is_case_sharing().search_string_query(
             query, default_fields=['name']))
     return group_es.size(0).run().total
Beispiel #15
0
def get_group_user_ids(group_id):
    q = (GroupES()
            .doc_id(group_id)
            .fields("users"))
    return q.run().hits[0]['users']
 def count(self):
     return GroupES().domain(self.domain).count()
Beispiel #17
0
def get_group_stubs(group_ids):
    return (GroupES().group_ids(group_ids).values('_id', 'name',
                                                  'case_sharing', 'reporting'))
Beispiel #18
0
def get_groups_user_ids(group_ids):
    q = (GroupES().doc_id(group_ids))
    return [
        user for user_list in q.values_list("users", flat=True)
        for user in user_list
    ]