src/pyams_content_es/zmi/test.py
changeset 8 dc4ca82098cc
parent 0 5af41c7a366f
child 29 7c67d484b5e8
equal deleted inserted replaced
7:01016144130c 8:dc4ca82098cc
     7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
     7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
    10 # FOR A PARTICULAR PURPOSE.
    10 # FOR A PARTICULAR PURPOSE.
    11 #
    11 #
    12 
    12 from onf_website.shared.news import WfNewsEvent
    13 
    13 
    14 __docformat__ = 'restructuredtext'
    14 __docformat__ = 'restructuredtext'
    15 
    15 
    16 
    16 
    17 # import standard library
    17 # import standard library
    19 # import interfaces
    19 # import interfaces
    20 from pyams_i18n.interfaces import II18n
    20 from pyams_i18n.interfaces import II18n
    21 from zope.intid.interfaces import IIntIds
    21 from zope.intid.interfaces import IIntIds
    22 
    22 
    23 # import packages
    23 # import packages
       
    24 from elasticsearch_dsl import Search, A
    24 from pyams_utils.registry import get_utility
    25 from pyams_utils.registry import get_utility
    25 from pyramid.response import Response
    26 from pyramid.response import Response
    26 from pyramid.view import view_config
    27 from pyramid.view import view_config
    27 from pyramid_es import get_client
    28 from pyramid_es import get_client
    28 
    29 
    36         #               .filter_term('title.fr', 'breve') \
    37         #               .filter_term('title.fr', 'breve') \
    37         #               .add_term_aggregate('status', 'workflow.status') \
    38         #               .add_term_aggregate('status', 'workflow.status') \
    38         #               .size(100)
    39         #               .size(100)
    39         # return query.execute(fields=['title.fr']).raw
    40         # return query.execute(fields=['title.fr']).raw
    40 
    41 
    41         query = client.query() \
    42         # query = client.query() \
    42                       .filter_term('_type', 'WfNewsEvent') \
    43         #               .filter_term('_type', 'WfNewsEvent') \
    43                       .filter_terms('workflow.status', ['published', 'retiring']) \
    44         #               .filter_terms('workflow.status', ['published', 'retiring']) \
    44                       .add_term_aggregate('status', 'workflow.status') \
    45         #               .add_term_aggregate('status', 'workflow.status') \
    45                       .add_date_aggregate('wf_date', 'workflow.date') \
    46         #               .add_date_aggregate('wf_date', 'workflow.date') \
    46                       .size(100)
    47         #               .size(100)
       
    48 
       
    49         query = Search(using=client.es).query('term', **{'_type': WfNewsEvent.__name__}) \
       
    50                                        .query('terms', **{'workflow.status': ['published', 'retiring']}) \
       
    51                                        .source(['internal_id'])
       
    52         agg = A('terms', field='workflow.status')
       
    53         query.aggs.bucket('status', agg)
    47         intids = get_utility(IIntIds)
    54         intids = get_utility(IIntIds)
    48         for result in query.execute(fields=['internal_id']):
    55         for result in query.execute():
    49             if not result.internal_id:
    56             if not result.internal_id:
    50                 continue
    57                 continue
    51             target = intids.queryObject(result.internal_id[0])
    58             target = intids.queryObject(result.internal_id)
    52             yield II18n(target).query_attribute('title').encode() + b'\n'
    59             yield II18n(target).query_attribute('title').encode() + b'\n'
    53         # return pformat(query.execute(fields=['title.fr', 'workflow.status', 'internal_id']).raw)
       
    54 
    60 
    55     return Response(app_iter=get_response())
    61     return Response(app_iter=get_response())