entities/__init__.py
changeset 10847 ce5403611cbe
parent 10846 d186820c5f7a
equal deleted inserted replaced
10846:d186820c5f7a 10847:ce5403611cbe
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    18 """base application's entities class implementation: `AnyEntity`"""
    18 """base application's entities class implementation: `AnyEntity`"""
    19 
    19 
    20 __docformat__ = "restructuredtext en"
    20 __docformat__ = "restructuredtext en"
    21 
    21 
       
    22 from warnings import warn
       
    23 
    22 from six import text_type, string_types
    24 from six import text_type, string_types
    23 
    25 
    24 from logilab.common.decorators import classproperty
    26 from logilab.common.decorators import classproperty
       
    27 from logilab.common.deprecation import deprecated
    25 
    28 
    26 from cubicweb import Unauthorized
    29 from cubicweb import Unauthorized
    27 from cubicweb.entity import Entity
    30 from cubicweb.entity import Entity
    28 
    31 
    29 
    32 
    42     def cw_create_url(cls, req, **kwargs):
    45     def cw_create_url(cls, req, **kwargs):
    43         """ return the url of the entity creation form for this entity type"""
    46         """ return the url of the entity creation form for this entity type"""
    44         return req.build_url('add/%s' % cls.__regid__, **kwargs)
    47         return req.build_url('add/%s' % cls.__regid__, **kwargs)
    45 
    48 
    46     @classmethod
    49     @classmethod
       
    50     @deprecated('[3.22] use cw_fti_index_rql_limit instead')
    47     def cw_fti_index_rql_queries(cls, req):
    51     def cw_fti_index_rql_queries(cls, req):
    48         """return the list of rql queries to fetch entities to FT-index
    52         """return the list of rql queries to fetch entities to FT-index
    49 
    53 
    50         The default is to fetch all entities at once and to prefetch
    54         The default is to fetch all entities at once and to prefetch
    51         indexable attributes but one could imagine iterating over
    55         indexable attributes but one could imagine iterating over
    58             varname = attrschema.type.upper()
    62             varname = attrschema.type.upper()
    59             restrictions.append('X %s %s' % (attrschema, varname))
    63             restrictions.append('X %s %s' % (attrschema, varname))
    60             selected.append(varname)
    64             selected.append(varname)
    61         return ['Any %s WHERE %s' % (', '.join(selected),
    65         return ['Any %s WHERE %s' % (', '.join(selected),
    62                                      ', '.join(restrictions))]
    66                                      ', '.join(restrictions))]
       
    67 
       
    68     @classmethod
       
    69     def cw_fti_index_rql_limit(cls, req, limit=1000):
       
    70         """generate rsets of entities to FT-index
       
    71 
       
    72         By default, each successive result set is limited to 1000 entities
       
    73         """
       
    74         if cls.cw_fti_index_rql_queries.__func__ != AnyEntity.cw_fti_index_rql_queries.__func__:
       
    75             warn("[3.22] cw_fti_index_rql_queries is replaced by cw_fti_index_rql_limit",
       
    76                  DeprecationWarning)
       
    77             for rql in cls.cw_fti_index_rql_queries(req):
       
    78                 yield req.execute(rql)
       
    79             return
       
    80         restrictions = ['X is %s' % cls.__regid__]
       
    81         selected = ['X']
       
    82         start = 0
       
    83         for attrschema in sorted(cls.e_schema.indexable_attributes()):
       
    84             varname = attrschema.type.upper()
       
    85             restrictions.append('X %s %s' % (attrschema, varname))
       
    86             selected.append(varname)
       
    87         while True:
       
    88             q_restrictions = restrictions + ['X eid > %s' % start]
       
    89             rset = req.execute('Any %s ORDERBY X LIMIT %s WHERE %s' %
       
    90                                (', '.join(selected),
       
    91                                 limit,
       
    92                                 ', '.join(q_restrictions)))
       
    93             if rset:
       
    94                 start = rset[-1][0]
       
    95                 yield rset
       
    96             else:
       
    97                 break
    63 
    98 
    64     # meta data api ###########################################################
    99     # meta data api ###########################################################
    65 
   100 
    66     def dc_title(self):
   101     def dc_title(self):
    67         """return a suitable *unicode* title for this entity"""
   102         """return a suitable *unicode* title for this entity"""