author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 13 Apr 2010 13:21:10 +0200 | |
changeset 5224 | 34e669b6fd95 |
parent 5030 | 5238d9a8dfee |
child 5239 | 471554b842d2 |
permissions | -rw-r--r-- |
0 | 1 |
"""schema definition related entities |
2 |
||
3 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3353
diff
changeset
|
4 |
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
10 |
from logilab.common.decorators import cached |
|
11 |
||
5030
5238d9a8dfee
[form] put qualified name on validation error, should fix #784299
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4839
diff
changeset
|
12 |
from yams.schema import role_name |
5238d9a8dfee
[form] put qualified name on validation error, should fix #784299
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4839
diff
changeset
|
13 |
|
0 | 14 |
from cubicweb import ValidationError |
15 |
from cubicweb.schema import ERQLExpression, RRQLExpression |
|
16 |
||
17 |
from cubicweb.entities import AnyEntity, fetch_config |
|
18 |
||
19 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
20 |
class CWEType(AnyEntity): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3353
diff
changeset
|
21 |
__regid__ = 'CWEType' |
0 | 22 |
fetch_attrs, fetch_order = fetch_config(['name']) |
1154 | 23 |
|
0 | 24 |
def dc_title(self): |
3432
12fe502e5044
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3377
diff
changeset
|
25 |
return u'%s (%s)' % (self.name, self._cw._(self.name)) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
26 |
|
0 | 27 |
def dc_long_title(self): |
28 |
stereotypes = [] |
|
3432
12fe502e5044
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3377
diff
changeset
|
29 |
_ = self._cw._ |
0 | 30 |
if self.final: |
31 |
stereotypes.append(_('final')) |
|
32 |
if stereotypes: |
|
33 |
return u'%s <<%s>>' % (self.dc_title(), ', '.join(stereotypes)) |
|
34 |
return self.dc_title() |
|
35 |
||
36 |
def db_key_name(self): |
|
37 |
"""XXX goa specific""" |
|
38 |
return self.get('name') |
|
39 |
||
40 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
41 |
class CWRType(AnyEntity): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3353
diff
changeset
|
42 |
__regid__ = 'CWRType' |
0 | 43 |
fetch_attrs, fetch_order = fetch_config(['name']) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
44 |
|
0 | 45 |
def dc_title(self): |
3432
12fe502e5044
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3377
diff
changeset
|
46 |
return u'%s (%s)' % (self.name, self._cw._(self.name)) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
47 |
|
0 | 48 |
def dc_long_title(self): |
49 |
stereotypes = [] |
|
3432
12fe502e5044
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3377
diff
changeset
|
50 |
_ = self._cw._ |
4467
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
51 |
if self.symmetric: |
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
52 |
stereotypes.append(_('symmetric')) |
0 | 53 |
if self.inlined: |
54 |
stereotypes.append(_('inlined')) |
|
55 |
if self.final: |
|
56 |
stereotypes.append(_('final')) |
|
57 |
if stereotypes: |
|
58 |
return u'%s <<%s>>' % (self.dc_title(), ', '.join(stereotypes)) |
|
59 |
return self.dc_title() |
|
60 |
||
4839
f482dbdf2f8c
[schema hooks] properly check for changes of schema properties avoid useless costly operation (such as full-text reindexing all entities of a type). Refactor inlined handling and fix a bug trying to reindex a final entity type on the way.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4467
diff
changeset
|
61 |
def check_inlined_allowed(self): |
f482dbdf2f8c
[schema hooks] properly check for changes of schema properties avoid useless costly operation (such as full-text reindexing all entities of a type). Refactor inlined handling and fix a bug trying to reindex a final entity type on the way.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4467
diff
changeset
|
62 |
"""check inlining is possible, raise ValidationError if not possible |
0 | 63 |
""" |
4839
f482dbdf2f8c
[schema hooks] properly check for changes of schema properties avoid useless costly operation (such as full-text reindexing all entities of a type). Refactor inlined handling and fix a bug trying to reindex a final entity type on the way.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4467
diff
changeset
|
64 |
# don't use the persistent schema, we may miss cardinality changes |
f482dbdf2f8c
[schema hooks] properly check for changes of schema properties avoid useless costly operation (such as full-text reindexing all entities of a type). Refactor inlined handling and fix a bug trying to reindex a final entity type on the way.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4467
diff
changeset
|
65 |
# in the same transaction |
f482dbdf2f8c
[schema hooks] properly check for changes of schema properties avoid useless costly operation (such as full-text reindexing all entities of a type). Refactor inlined handling and fix a bug trying to reindex a final entity type on the way.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4467
diff
changeset
|
66 |
for rdef in self.reverse_relation_type: |
f482dbdf2f8c
[schema hooks] properly check for changes of schema properties avoid useless costly operation (such as full-text reindexing all entities of a type). Refactor inlined handling and fix a bug trying to reindex a final entity type on the way.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4467
diff
changeset
|
67 |
card = rdef.cardinality[0] |
f482dbdf2f8c
[schema hooks] properly check for changes of schema properties avoid useless costly operation (such as full-text reindexing all entities of a type). Refactor inlined handling and fix a bug trying to reindex a final entity type on the way.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4467
diff
changeset
|
68 |
if not card in '?1': |
5030
5238d9a8dfee
[form] put qualified name on validation error, should fix #784299
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4839
diff
changeset
|
69 |
qname = role_name('inlined', 'subject') |
4839
f482dbdf2f8c
[schema hooks] properly check for changes of schema properties avoid useless costly operation (such as full-text reindexing all entities of a type). Refactor inlined handling and fix a bug trying to reindex a final entity type on the way.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4467
diff
changeset
|
70 |
rtype = self.name |
f482dbdf2f8c
[schema hooks] properly check for changes of schema properties avoid useless costly operation (such as full-text reindexing all entities of a type). Refactor inlined handling and fix a bug trying to reindex a final entity type on the way.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4467
diff
changeset
|
71 |
stype = rdef.stype |
f482dbdf2f8c
[schema hooks] properly check for changes of schema properties avoid useless costly operation (such as full-text reindexing all entities of a type). Refactor inlined handling and fix a bug trying to reindex a final entity type on the way.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4467
diff
changeset
|
72 |
otype = rdef.otype |
f482dbdf2f8c
[schema hooks] properly check for changes of schema properties avoid useless costly operation (such as full-text reindexing all entities of a type). Refactor inlined handling and fix a bug trying to reindex a final entity type on the way.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4467
diff
changeset
|
73 |
msg = self._cw._("can't set inlined=%(inlined)s, " |
f482dbdf2f8c
[schema hooks] properly check for changes of schema properties avoid useless costly operation (such as full-text reindexing all entities of a type). Refactor inlined handling and fix a bug trying to reindex a final entity type on the way.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4467
diff
changeset
|
74 |
"%(stype)s %(rtype)s %(otype)s " |
f482dbdf2f8c
[schema hooks] properly check for changes of schema properties avoid useless costly operation (such as full-text reindexing all entities of a type). Refactor inlined handling and fix a bug trying to reindex a final entity type on the way.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4467
diff
changeset
|
75 |
"has cardinality=%(card)s") |
5030
5238d9a8dfee
[form] put qualified name on validation error, should fix #784299
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4839
diff
changeset
|
76 |
raise ValidationError(self.eid, {qname: msg % locals()}) |
0 | 77 |
|
78 |
def db_key_name(self): |
|
79 |
"""XXX goa specific""" |
|
80 |
return self.get('name') |
|
81 |
||
82 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
83 |
class CWRelation(AnyEntity): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3353
diff
changeset
|
84 |
__regid__ = 'CWRelation' |
0 | 85 |
fetch_attrs = fetch_config(['cardinality'])[0] |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
86 |
|
0 | 87 |
def dc_title(self): |
88 |
return u'%s %s %s' % ( |
|
89 |
self.from_entity[0].name, |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
90 |
self.relation_type[0].name, |
0 | 91 |
self.to_entity[0].name) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
92 |
|
0 | 93 |
def dc_long_title(self): |
94 |
card = self.cardinality |
|
95 |
scard, ocard = u'', u'' |
|
96 |
if card[0] != '1': |
|
97 |
scard = '[%s]' % card[0] |
|
98 |
if card[1] != '1': |
|
99 |
ocard = '[%s]' % card[1] |
|
100 |
return u'%s %s%s%s %s' % ( |
|
101 |
self.from_entity[0].name, |
|
102 |
scard, self.relation_type[0].name, ocard, |
|
103 |
self.to_entity[0].name) |
|
104 |
||
105 |
def after_deletion_path(self): |
|
106 |
"""return (path, parameters) which should be used as redirect |
|
107 |
information when this entity is being deleted |
|
108 |
""" |
|
109 |
if self.relation_type: |
|
110 |
return self.relation_type[0].rest_path(), {} |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
111 |
return super(CWRelation, self).after_deletion_path() |
0 | 112 |
|
2594
92aad5e18405
[R schemaobjs] property accessors for CWRelation/CWAttribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2299
diff
changeset
|
113 |
@property |
92aad5e18405
[R schemaobjs] property accessors for CWRelation/CWAttribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2299
diff
changeset
|
114 |
def rtype(self): |
92aad5e18405
[R schemaobjs] property accessors for CWRelation/CWAttribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2299
diff
changeset
|
115 |
return self.relation_type[0] |
92aad5e18405
[R schemaobjs] property accessors for CWRelation/CWAttribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2299
diff
changeset
|
116 |
|
92aad5e18405
[R schemaobjs] property accessors for CWRelation/CWAttribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2299
diff
changeset
|
117 |
@property |
92aad5e18405
[R schemaobjs] property accessors for CWRelation/CWAttribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2299
diff
changeset
|
118 |
def stype(self): |
92aad5e18405
[R schemaobjs] property accessors for CWRelation/CWAttribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2299
diff
changeset
|
119 |
return self.from_entity[0] |
92aad5e18405
[R schemaobjs] property accessors for CWRelation/CWAttribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2299
diff
changeset
|
120 |
|
92aad5e18405
[R schemaobjs] property accessors for CWRelation/CWAttribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2299
diff
changeset
|
121 |
@property |
92aad5e18405
[R schemaobjs] property accessors for CWRelation/CWAttribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2299
diff
changeset
|
122 |
def otype(self): |
92aad5e18405
[R schemaobjs] property accessors for CWRelation/CWAttribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2299
diff
changeset
|
123 |
return self.to_entity[0] |
92aad5e18405
[R schemaobjs] property accessors for CWRelation/CWAttribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2299
diff
changeset
|
124 |
|
0 | 125 |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
126 |
class CWAttribute(CWRelation): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3353
diff
changeset
|
127 |
__regid__ = 'CWAttribute' |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
128 |
|
0 | 129 |
def dc_long_title(self): |
130 |
card = self.cardinality |
|
131 |
scard = u'' |
|
132 |
if card[0] == '1': |
|
133 |
scard = '+' |
|
134 |
return u'%s %s%s %s' % ( |
|
135 |
self.from_entity[0].name, |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
136 |
scard, self.relation_type[0].name, |
0 | 137 |
self.to_entity[0].name) |
138 |
||
139 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
140 |
class CWConstraint(AnyEntity): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3353
diff
changeset
|
141 |
__regid__ = 'CWConstraint' |
0 | 142 |
fetch_attrs, fetch_order = fetch_config(['value']) |
143 |
||
144 |
def dc_title(self): |
|
145 |
return '%s(%s)' % (self.cstrtype[0].name, self.value or u'') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
146 |
|
0 | 147 |
def after_deletion_path(self): |
148 |
"""return (path, parameters) which should be used as redirect |
|
149 |
information when this entity is being deleted |
|
150 |
""" |
|
151 |
if self.reverse_constrained_by: |
|
152 |
return self.reverse_constrained_by[0].rest_path(), {} |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
153 |
return super(CWConstraint, self).after_deletion_path() |
0 | 154 |
|
155 |
@property |
|
156 |
def type(self): |
|
157 |
return self.cstrtype[0].name |
|
158 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
159 |
|
0 | 160 |
class RQLExpression(AnyEntity): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3353
diff
changeset
|
161 |
__regid__ = 'RQLExpression' |
0 | 162 |
fetch_attrs, fetch_order = fetch_config(['exprtype', 'mainvars', 'expression']) |
163 |
||
164 |
def dc_title(self): |
|
5224
34e669b6fd95
[mq]: worklfow view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5030
diff
changeset
|
165 |
return self.expression or u'' |
34e669b6fd95
[mq]: worklfow view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5030
diff
changeset
|
166 |
|
34e669b6fd95
[mq]: worklfow view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5030
diff
changeset
|
167 |
def dc_long_title(self): |
0 | 168 |
return '%s(%s)' % (self.exprtype, self.expression or u'') |
169 |
||
170 |
@property |
|
171 |
def expression_of(self): |
|
172 |
for rel in ('read_permission', 'add_permission', 'delete_permission', |
|
173 |
'update_permission', 'condition'): |
|
174 |
values = getattr(self, 'reverse_%s' % rel) |
|
175 |
if values: |
|
176 |
return values[0] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
177 |
|
0 | 178 |
@cached |
179 |
def _rqlexpr(self): |
|
180 |
if self.exprtype == 'ERQLExpression': |
|
181 |
return ERQLExpression(self.expression, self.mainvars, self.eid) |
|
182 |
#if self.exprtype == 'RRQLExpression': |
|
183 |
return RRQLExpression(self.expression, self.mainvars, self.eid) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
184 |
|
0 | 185 |
def check_expression(self, *args, **kwargs): |
186 |
return self._rqlexpr().check(*args, **kwargs) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
187 |
|
0 | 188 |
def after_deletion_path(self): |
189 |
"""return (path, parameters) which should be used as redirect |
|
190 |
information when this entity is being deleted |
|
191 |
""" |
|
192 |
if self.expression_of: |
|
193 |
return self.expression_of.rest_path(), {} |
|
194 |
return super(RQLExpression, self).after_deletion_path() |
|
195 |
||
196 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
197 |
class CWPermission(AnyEntity): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3353
diff
changeset
|
198 |
__regid__ = 'CWPermission' |
0 | 199 |
fetch_attrs, fetch_order = fetch_config(['name', 'label']) |
200 |
||
201 |
def dc_title(self): |
|
202 |
if self.label: |
|
3432
12fe502e5044
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3377
diff
changeset
|
203 |
return '%s (%s)' % (self._cw._(self.name), self.label) |
12fe502e5044
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3377
diff
changeset
|
204 |
return self._cw._(self.name) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
205 |
|
0 | 206 |
def after_deletion_path(self): |
207 |
"""return (path, parameters) which should be used as redirect |
|
208 |
information when this entity is being deleted |
|
209 |
""" |
|
210 |
permissionof = getattr(self, 'reverse_require_permission', ()) |
|
211 |
if len(permissionof) == 1: |
|
212 |
return permissionof[0].rest_path(), {} |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
213 |
return super(CWPermission, self).after_deletion_path() |