author | Rémi Cardona <remi.cardona@logilab.fr> |
Tue, 08 Dec 2015 16:28:20 +0100 | |
changeset 10969 | b4de8b1cc135 |
parent 10847 | ce5403611cbe |
permissions | -rw-r--r-- |
9350
480600497e2d
cwetype is a *class* property, rename its argument accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8930
diff
changeset
|
1 |
# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
5556
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
18 |
"""base application's entities class implementation: `AnyEntity`""" |
0 | 19 |
|
20 |
__docformat__ = "restructuredtext en" |
|
21 |
||
10847
ce5403611cbe
[fti] add cw_fti_index_rql_limit method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10846
diff
changeset
|
22 |
from warnings import warn |
ce5403611cbe
[fti] add cw_fti_index_rql_limit method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10846
diff
changeset
|
23 |
|
10675
e0db937f5add
[py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10612
diff
changeset
|
24 |
from six import text_type, string_types |
0 | 25 |
|
8930
6a02be304486
remove unused import
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8899
diff
changeset
|
26 |
from logilab.common.decorators import classproperty |
10847
ce5403611cbe
[fti] add cw_fti_index_rql_limit method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10846
diff
changeset
|
27 |
from logilab.common.deprecation import deprecated |
0 | 28 |
|
8748
f5027f8d2478
drop typed_eid() in favour of int() (closes #2742462)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8450
diff
changeset
|
29 |
from cubicweb import Unauthorized |
713
5adb6d8e5fa7
update imports of "cubicweb.common.entity" and use the new module path "cubicweb.entity"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
30 |
from cubicweb.entity import Entity |
0 | 31 |
|
32 |
||
33 |
class AnyEntity(Entity): |
|
34 |
"""an entity instance has e_schema automagically set on the class and |
|
35 |
instances have access to their issuing cursor |
|
36 |
""" |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
37 |
__regid__ = 'Any' |
0 | 38 |
|
9350
480600497e2d
cwetype is a *class* property, rename its argument accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8930
diff
changeset
|
39 |
@classproperty |
480600497e2d
cwetype is a *class* property, rename its argument accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8930
diff
changeset
|
40 |
def cw_etype(cls): |
10095
200bd6a601dc
[warnings] put an end to warnings in the sqlite driver over `str` being sent instead of unicode strings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10019
diff
changeset
|
41 |
"""entity type as a unicode string""" |
10675
e0db937f5add
[py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10612
diff
changeset
|
42 |
return text_type(cls.__regid__) |
9350
480600497e2d
cwetype is a *class* property, rename its argument accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8930
diff
changeset
|
43 |
|
6039
6e84db1b3e44
[view] deprecate create_url in favor of a cw_create_url class method on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5557
diff
changeset
|
44 |
@classmethod |
6e84db1b3e44
[view] deprecate create_url in favor of a cw_create_url class method on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5557
diff
changeset
|
45 |
def cw_create_url(cls, req, **kwargs): |
6e84db1b3e44
[view] deprecate create_url in favor of a cw_create_url class method on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5557
diff
changeset
|
46 |
""" return the url of the entity creation form for this entity type""" |
6e84db1b3e44
[view] deprecate create_url in favor of a cw_create_url class method on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5557
diff
changeset
|
47 |
return req.build_url('add/%s' % cls.__regid__, **kwargs) |
6e84db1b3e44
[view] deprecate create_url in favor of a cw_create_url class method on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5557
diff
changeset
|
48 |
|
8450
11063635c4e4
[fti] allow usage of custom RQL to fetch entities to index (closes #2410509)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7875
diff
changeset
|
49 |
@classmethod |
10847
ce5403611cbe
[fti] add cw_fti_index_rql_limit method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10846
diff
changeset
|
50 |
@deprecated('[3.22] use cw_fti_index_rql_limit instead') |
8450
11063635c4e4
[fti] allow usage of custom RQL to fetch entities to index (closes #2410509)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7875
diff
changeset
|
51 |
def cw_fti_index_rql_queries(cls, req): |
10846
d186820c5f7a
[fti] backout 166c6f7b1be4
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10841
diff
changeset
|
52 |
"""return the list of rql queries to fetch entities to FT-index |
8450
11063635c4e4
[fti] allow usage of custom RQL to fetch entities to index (closes #2410509)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7875
diff
changeset
|
53 |
|
10846
d186820c5f7a
[fti] backout 166c6f7b1be4
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10841
diff
changeset
|
54 |
The default is to fetch all entities at once and to prefetch |
d186820c5f7a
[fti] backout 166c6f7b1be4
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10841
diff
changeset
|
55 |
indexable attributes but one could imagine iterating over |
8450
11063635c4e4
[fti] allow usage of custom RQL to fetch entities to index (closes #2410509)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7875
diff
changeset
|
56 |
"smaller" resultsets if the table is very big or returning |
11063635c4e4
[fti] allow usage of custom RQL to fetch entities to index (closes #2410509)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7875
diff
changeset
|
57 |
a subset of entities that match some business-logic condition. |
11063635c4e4
[fti] allow usage of custom RQL to fetch entities to index (closes #2410509)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7875
diff
changeset
|
58 |
""" |
10846
d186820c5f7a
[fti] backout 166c6f7b1be4
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10841
diff
changeset
|
59 |
restrictions = ['X is %s' % cls.__regid__] |
8450
11063635c4e4
[fti] allow usage of custom RQL to fetch entities to index (closes #2410509)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7875
diff
changeset
|
60 |
selected = ['X'] |
10251
2daa5c6dea4d
[entities] make cw_fti_index_rql_queries predictable
Julien Cristau <julien.cristau@logilab.fr>
parents:
10095
diff
changeset
|
61 |
for attrschema in sorted(cls.e_schema.indexable_attributes()): |
8450
11063635c4e4
[fti] allow usage of custom RQL to fetch entities to index (closes #2410509)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7875
diff
changeset
|
62 |
varname = attrschema.type.upper() |
11063635c4e4
[fti] allow usage of custom RQL to fetch entities to index (closes #2410509)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7875
diff
changeset
|
63 |
restrictions.append('X %s %s' % (attrschema, varname)) |
11063635c4e4
[fti] allow usage of custom RQL to fetch entities to index (closes #2410509)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7875
diff
changeset
|
64 |
selected.append(varname) |
10846
d186820c5f7a
[fti] backout 166c6f7b1be4
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10841
diff
changeset
|
65 |
return ['Any %s WHERE %s' % (', '.join(selected), |
d186820c5f7a
[fti] backout 166c6f7b1be4
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10841
diff
changeset
|
66 |
', '.join(restrictions))] |
8450
11063635c4e4
[fti] allow usage of custom RQL to fetch entities to index (closes #2410509)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7875
diff
changeset
|
67 |
|
10847
ce5403611cbe
[fti] add cw_fti_index_rql_limit method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10846
diff
changeset
|
68 |
@classmethod |
ce5403611cbe
[fti] add cw_fti_index_rql_limit method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10846
diff
changeset
|
69 |
def cw_fti_index_rql_limit(cls, req, limit=1000): |
ce5403611cbe
[fti] add cw_fti_index_rql_limit method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10846
diff
changeset
|
70 |
"""generate rsets of entities to FT-index |
ce5403611cbe
[fti] add cw_fti_index_rql_limit method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10846
diff
changeset
|
71 |
|
ce5403611cbe
[fti] add cw_fti_index_rql_limit method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10846
diff
changeset
|
72 |
By default, each successive result set is limited to 1000 entities |
ce5403611cbe
[fti] add cw_fti_index_rql_limit method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10846
diff
changeset
|
73 |
""" |
ce5403611cbe
[fti] add cw_fti_index_rql_limit method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10846
diff
changeset
|
74 |
if cls.cw_fti_index_rql_queries.__func__ != AnyEntity.cw_fti_index_rql_queries.__func__: |
ce5403611cbe
[fti] add cw_fti_index_rql_limit method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10846
diff
changeset
|
75 |
warn("[3.22] cw_fti_index_rql_queries is replaced by cw_fti_index_rql_limit", |
ce5403611cbe
[fti] add cw_fti_index_rql_limit method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10846
diff
changeset
|
76 |
DeprecationWarning) |
ce5403611cbe
[fti] add cw_fti_index_rql_limit method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10846
diff
changeset
|
77 |
for rql in cls.cw_fti_index_rql_queries(req): |
ce5403611cbe
[fti] add cw_fti_index_rql_limit method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10846
diff
changeset
|
78 |
yield req.execute(rql) |
ce5403611cbe
[fti] add cw_fti_index_rql_limit method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10846
diff
changeset
|
79 |
return |
ce5403611cbe
[fti] add cw_fti_index_rql_limit method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10846
diff
changeset
|
80 |
restrictions = ['X is %s' % cls.__regid__] |
ce5403611cbe
[fti] add cw_fti_index_rql_limit method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10846
diff
changeset
|
81 |
selected = ['X'] |
ce5403611cbe
[fti] add cw_fti_index_rql_limit method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10846
diff
changeset
|
82 |
start = 0 |
ce5403611cbe
[fti] add cw_fti_index_rql_limit method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10846
diff
changeset
|
83 |
for attrschema in sorted(cls.e_schema.indexable_attributes()): |
ce5403611cbe
[fti] add cw_fti_index_rql_limit method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10846
diff
changeset
|
84 |
varname = attrschema.type.upper() |
ce5403611cbe
[fti] add cw_fti_index_rql_limit method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10846
diff
changeset
|
85 |
restrictions.append('X %s %s' % (attrschema, varname)) |
ce5403611cbe
[fti] add cw_fti_index_rql_limit method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10846
diff
changeset
|
86 |
selected.append(varname) |
ce5403611cbe
[fti] add cw_fti_index_rql_limit method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10846
diff
changeset
|
87 |
while True: |
ce5403611cbe
[fti] add cw_fti_index_rql_limit method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10846
diff
changeset
|
88 |
q_restrictions = restrictions + ['X eid > %s' % start] |
ce5403611cbe
[fti] add cw_fti_index_rql_limit method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10846
diff
changeset
|
89 |
rset = req.execute('Any %s ORDERBY X LIMIT %s WHERE %s' % |
ce5403611cbe
[fti] add cw_fti_index_rql_limit method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10846
diff
changeset
|
90 |
(', '.join(selected), |
ce5403611cbe
[fti] add cw_fti_index_rql_limit method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10846
diff
changeset
|
91 |
limit, |
ce5403611cbe
[fti] add cw_fti_index_rql_limit method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10846
diff
changeset
|
92 |
', '.join(q_restrictions))) |
ce5403611cbe
[fti] add cw_fti_index_rql_limit method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10846
diff
changeset
|
93 |
if rset: |
ce5403611cbe
[fti] add cw_fti_index_rql_limit method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10846
diff
changeset
|
94 |
start = rset[-1][0] |
ce5403611cbe
[fti] add cw_fti_index_rql_limit method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10846
diff
changeset
|
95 |
yield rset |
ce5403611cbe
[fti] add cw_fti_index_rql_limit method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10846
diff
changeset
|
96 |
else: |
ce5403611cbe
[fti] add cw_fti_index_rql_limit method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10846
diff
changeset
|
97 |
break |
ce5403611cbe
[fti] add cw_fti_index_rql_limit method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10846
diff
changeset
|
98 |
|
0 | 99 |
# meta data api ########################################################### |
100 |
||
101 |
def dc_title(self): |
|
102 |
"""return a suitable *unicode* title for this entity""" |
|
103 |
for rschema, attrschema in self.e_schema.attribute_definitions(): |
|
104 |
if rschema.meta: |
|
105 |
continue |
|
5557
1a534c596bff
[entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5556
diff
changeset
|
106 |
value = self.cw_attr_value(rschema.type) |
10396
33e44293b0eb
[entities] fix dc_title output for bool(value) == False
Aurelien Campeas <aurelien.campeas@pythonian.fr>
parents:
10095
diff
changeset
|
107 |
if value is not None: |
0 | 108 |
# make the value printable (dates, floats, bytes, etc.) |
109 |
return self.printable_value(rschema.type, value, attrschema.type, |
|
110 |
format='text/plain') |
|
111 |
return u'%s #%s' % (self.dc_type(), self.eid) |
|
112 |
||
113 |
def dc_long_title(self): |
|
114 |
"""return a more detailled title for this entity""" |
|
115 |
return self.dc_title() |
|
1493 | 116 |
|
0 | 117 |
def dc_description(self, format='text/plain'): |
118 |
"""return a suitable description for this entity""" |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3578
diff
changeset
|
119 |
if 'description' in self.e_schema.subjrels: |
0 | 120 |
return self.printable_value('description', format=format) |
121 |
return u'' |
|
122 |
||
123 |
def dc_authors(self): |
|
124 |
"""return a suitable description for the author(s) of the entity""" |
|
125 |
try: |
|
126 |
return ', '.join(u.name() for u in self.owned_by) |
|
127 |
except Unauthorized: |
|
128 |
return u'' |
|
129 |
||
130 |
def dc_creator(self): |
|
131 |
"""return a suitable description for the creator of the entity""" |
|
132 |
if self.creator: |
|
133 |
return self.creator.name() |
|
134 |
return u'' |
|
135 |
||
136 |
def dc_date(self, date_format=None):# XXX default to ISO 8601 ? |
|
137 |
"""return latest modification date of this entity""" |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3418
diff
changeset
|
138 |
return self._cw.format_date(self.modification_date, date_format=date_format) |
0 | 139 |
|
140 |
def dc_type(self, form=''): |
|
141 |
"""return the display name for the type of this entity (translated)""" |
|
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
142 |
return self.e_schema.display_name(self._cw, form) |
0 | 143 |
|
144 |
def dc_language(self): |
|
145 |
"""return language used by this entity (translated)""" |
|
146 |
# check if entities has internationalizable attributes |
|
147 |
# XXX one is enough or check if all String attributes are internationalizable? |
|
148 |
for rschema, attrschema in self.e_schema.attribute_definitions(): |
|
4147
52fe79a98b21
3.6 remove deprecation warning
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
3720
diff
changeset
|
149 |
if rschema.rdef(self.e_schema, attrschema).internationalizable: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3418
diff
changeset
|
150 |
return self._cw._(self._cw.user.property_value('ui.language')) |
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
151 |
return self._cw._(self._cw.vreg.property_value('ui.language')) |
1493 | 152 |
|
0 | 153 |
@property |
154 |
def creator(self): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
155 |
"""return the CWUser entity which has created this entity, or None if |
0 | 156 |
unknown or if the curent user doesn't has access to this euser |
157 |
""" |
|
158 |
try: |
|
159 |
return self.created_by[0] |
|
160 |
except (Unauthorized, IndexError): |
|
161 |
return None |
|
162 |
||
163 |
# abstractions making the whole things (well, some at least) working ###### |
|
1493 | 164 |
|
0 | 165 |
def sortvalue(self, rtype=None): |
166 |
"""return a value which can be used to sort this entity or given |
|
167 |
entity's attribute |
|
168 |
""" |
|
169 |
if rtype is None: |
|
170 |
return self.dc_title().lower() |
|
5557
1a534c596bff
[entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5556
diff
changeset
|
171 |
value = self.cw_attr_value(rtype) |
0 | 172 |
# do not restrict to `unicode` because Bytes will return a `str` value |
10612
84468b90e9c1
[py3k] basestring → six.string_types
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10411
diff
changeset
|
173 |
if isinstance(value, string_types): |
0 | 174 |
return self.printable_value(rtype, format='text/plain').lower() |
175 |
return value |
|
176 |
||
1493 | 177 |
|
7827
9bbf83f68bcc
[entity] upgrade fetch_[unrelated_]order to benefit from changes introduced in 3.14 (closes #1942758)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7143
diff
changeset
|
178 |
def fetch_config(fetchattrs, mainattr=None, pclass=AnyEntity, order='ASC'): |
9bbf83f68bcc
[entity] upgrade fetch_[unrelated_]order to benefit from changes introduced in 3.14 (closes #1942758)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7143
diff
changeset
|
179 |
"""function to ease basic configuration of an entity class ORM. Basic usage |
9bbf83f68bcc
[entity] upgrade fetch_[unrelated_]order to benefit from changes introduced in 3.14 (closes #1942758)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7143
diff
changeset
|
180 |
is: |
9bbf83f68bcc
[entity] upgrade fetch_[unrelated_]order to benefit from changes introduced in 3.14 (closes #1942758)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7143
diff
changeset
|
181 |
|
9bbf83f68bcc
[entity] upgrade fetch_[unrelated_]order to benefit from changes introduced in 3.14 (closes #1942758)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7143
diff
changeset
|
182 |
.. sourcecode:: python |
9bbf83f68bcc
[entity] upgrade fetch_[unrelated_]order to benefit from changes introduced in 3.14 (closes #1942758)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7143
diff
changeset
|
183 |
|
9bbf83f68bcc
[entity] upgrade fetch_[unrelated_]order to benefit from changes introduced in 3.14 (closes #1942758)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7143
diff
changeset
|
184 |
class MyEntity(AnyEntity): |
0 | 185 |
|
7827
9bbf83f68bcc
[entity] upgrade fetch_[unrelated_]order to benefit from changes introduced in 3.14 (closes #1942758)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7143
diff
changeset
|
186 |
fetch_attrs, cw_fetch_order = fetch_config(['attr1', 'attr2']) |
9bbf83f68bcc
[entity] upgrade fetch_[unrelated_]order to benefit from changes introduced in 3.14 (closes #1942758)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7143
diff
changeset
|
187 |
# uncomment line below if you want the same sorting for 'unrelated' entities |
9bbf83f68bcc
[entity] upgrade fetch_[unrelated_]order to benefit from changes introduced in 3.14 (closes #1942758)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7143
diff
changeset
|
188 |
# cw_fetch_unrelated_order = cw_fetch_order |
9bbf83f68bcc
[entity] upgrade fetch_[unrelated_]order to benefit from changes introduced in 3.14 (closes #1942758)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7143
diff
changeset
|
189 |
|
9bbf83f68bcc
[entity] upgrade fetch_[unrelated_]order to benefit from changes introduced in 3.14 (closes #1942758)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7143
diff
changeset
|
190 |
Using this, when using ORM methods retrieving this type of entity, 'attr1' |
9bbf83f68bcc
[entity] upgrade fetch_[unrelated_]order to benefit from changes introduced in 3.14 (closes #1942758)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7143
diff
changeset
|
191 |
and 'attr2' will be automatically prefetched and results will be sorted on |
9bbf83f68bcc
[entity] upgrade fetch_[unrelated_]order to benefit from changes introduced in 3.14 (closes #1942758)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7143
diff
changeset
|
192 |
'attr1' ascending (ie the first attribute in the list). |
9bbf83f68bcc
[entity] upgrade fetch_[unrelated_]order to benefit from changes introduced in 3.14 (closes #1942758)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7143
diff
changeset
|
193 |
|
9bbf83f68bcc
[entity] upgrade fetch_[unrelated_]order to benefit from changes introduced in 3.14 (closes #1942758)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7143
diff
changeset
|
194 |
This function will automatically add to fetched attributes those defined in |
9bbf83f68bcc
[entity] upgrade fetch_[unrelated_]order to benefit from changes introduced in 3.14 (closes #1942758)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7143
diff
changeset
|
195 |
parent class given using the `pclass` argument. |
9bbf83f68bcc
[entity] upgrade fetch_[unrelated_]order to benefit from changes introduced in 3.14 (closes #1942758)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7143
diff
changeset
|
196 |
|
9bbf83f68bcc
[entity] upgrade fetch_[unrelated_]order to benefit from changes introduced in 3.14 (closes #1942758)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7143
diff
changeset
|
197 |
Also, You can use `mainattr` and `order` argument to have a different |
9bbf83f68bcc
[entity] upgrade fetch_[unrelated_]order to benefit from changes introduced in 3.14 (closes #1942758)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7143
diff
changeset
|
198 |
sorting. |
9bbf83f68bcc
[entity] upgrade fetch_[unrelated_]order to benefit from changes introduced in 3.14 (closes #1942758)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7143
diff
changeset
|
199 |
""" |
0 | 200 |
if pclass is not None: |
201 |
fetchattrs += pclass.fetch_attrs |
|
202 |
if mainattr is None: |
|
203 |
mainattr = fetchattrs[0] |
|
204 |
@classmethod |
|
7827
9bbf83f68bcc
[entity] upgrade fetch_[unrelated_]order to benefit from changes introduced in 3.14 (closes #1942758)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7143
diff
changeset
|
205 |
def fetch_order(cls, select, attr, var): |
0 | 206 |
if attr == mainattr: |
7827
9bbf83f68bcc
[entity] upgrade fetch_[unrelated_]order to benefit from changes introduced in 3.14 (closes #1942758)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7143
diff
changeset
|
207 |
select.add_sort_var(var, order=='ASC') |
0 | 208 |
return fetchattrs, fetch_order |