25 |
25 |
26 from cubicweb import Unauthorized |
26 from cubicweb import Unauthorized |
27 from cubicweb.entity import Entity |
27 from cubicweb.entity import Entity |
28 |
28 |
29 |
29 |
|
30 def chunks(seq, step): |
|
31 """See http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python)""" |
|
32 return (seq[i:i+step] |
|
33 for i in xrange(0, len(seq), step)) |
|
34 |
|
35 |
30 class AnyEntity(Entity): |
36 class AnyEntity(Entity): |
31 """an entity instance has e_schema automagically set on the class and |
37 """an entity instance has e_schema automagically set on the class and |
32 instances have access to their issuing cursor |
38 instances have access to their issuing cursor |
33 """ |
39 """ |
34 __regid__ = 'Any' |
40 __regid__ = 'Any' |
43 """ return the url of the entity creation form for this entity type""" |
49 """ return the url of the entity creation form for this entity type""" |
44 return req.build_url('add/%s' % cls.__regid__, **kwargs) |
50 return req.build_url('add/%s' % cls.__regid__, **kwargs) |
45 |
51 |
46 @classmethod |
52 @classmethod |
47 def cw_fti_index_rql_queries(cls, req): |
53 def cw_fti_index_rql_queries(cls, req): |
48 """return the list of rql queries to fetch entities to FT-index |
54 """return an iterator on rql queries to fetch entities to FT-index |
49 |
55 |
50 The default is to fetch all entities at once and to prefetch |
56 The default is to fetch entities 1000 per 1000 and to prefetch |
51 indexable attributes but one could imagine iterating over |
57 indexable attributes, but one could imagine iterating over |
52 "smaller" resultsets if the table is very big or returning |
58 "smaller" resultsets if the table is very big or returning |
53 a subset of entities that match some business-logic condition. |
59 a subset of entities that match some business-logic condition. |
54 """ |
60 """ |
55 restrictions = ['X is %s' % cls.__regid__] |
61 restrictions = [] |
56 selected = ['X'] |
62 selected = ['X'] |
57 for attrschema in sorted(cls.e_schema.indexable_attributes()): |
63 for attrschema in sorted(cls.e_schema.indexable_attributes()): |
58 varname = attrschema.type.upper() |
64 varname = attrschema.type.upper() |
59 restrictions.append('X %s %s' % (attrschema, varname)) |
65 restrictions.append('X %s %s' % (attrschema, varname)) |
60 selected.append(varname) |
66 selected.append(varname) |
61 return ['Any %s WHERE %s' % (', '.join(selected), |
67 rset = req.execute('Any EID WHERE X eid EID, X is %s' % cls.__regid__) |
62 ', '.join(restrictions))] |
68 for rows in chunks(rset.rows, 1000): |
|
69 q_restrictions = restrictions + [ |
|
70 'X eid IN (%s)' % ', '.join(str(r[0]) for r in rows)] |
|
71 yield 'Any %s WHERE %s' % (', '.join(selected), |
|
72 ', '.join(q_restrictions)) |
63 |
73 |
64 # meta data api ########################################################### |
74 # meta data api ########################################################### |
65 |
75 |
66 def dc_title(self): |
76 def dc_title(self): |
67 """return a suitable *unicode* title for this entity""" |
77 """return a suitable *unicode* title for this entity""" |