Example #1
0
 def search(self, keyword, index, size):
     """
     Search users with keyword.
     :param keyword: {string} The keyword.
     :param index: {int} The page index.
     :param size: {int} The page size.
     :return: ({list}, {int})
         The UserModel list.
         The number found.
     """
     plus, minus = utils.parse_keyword(keyword)
     query_string = ''
     if len(plus) > 0:
         keyword = ' '.join(plus)
         query_string += '((name:{1}) OR (email:{1}))'.replace('{1}', keyword)
     if len(minus) > 0:
         keyword = ' '.join(minus)
         query_string += 'NOT ((name:{1}) OR (email:{1}))'.replace('{1}', keyword)
     name_asc = search.SortExpression(
         expression='name',
         direction=search.SortExpression.ASCENDING,
         default_value=0,
     )
     options = search.QueryOptions(
         offset=size * index,
         limit=size,
         sort_options=search.SortOptions(expressions=[name_asc], limit=1000),
         returned_fields=['doc_id'],
     )
     query = search.Query(query_string=query_string, options=options)
     search_result = utils.get_index_users().search(query)
     total = search_result.number_found
     users = UserModel.get_by_id([long(x.doc_id) for x in search_result])
     return [x for x in users if not x is None], total
Example #2
0
 def put(self, **kwargs):
     super(UserModel, self).put(**kwargs)
     index = utils.get_index_users()
     document = search.Document(
         doc_id=str(self.key().id()),
         fields=[
             search.TextField(name='name', value=self.name),
             search.TextField(name='email', value=self.email),
         ]
     )
     index.put(document)
Example #3
0
 def test_utils_get_index_users(self):
     utils.search.Index.return_value = 'index'
     index = utils.get_index_users()
     self.assertEqual(index, 'index')
     utils.search.Index.assert_called_with(namespace='UserModel', name='default')
Example #4
0
 def delete(self, **kwargs):
     index = utils.get_index_users()
     index.delete(str(self.key().id()))
     super(UserModel, self).delete(**kwargs)