author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 30 Mar 2010 11:18:31 +0200 | |
branch | stable |
changeset 5076 | b0e6134b4324 |
parent 5030 | 5238d9a8dfee |
child 5224 | 34e669b6fd95 |
child 5421 | 8167de96c523 |
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): |
|
165 |
return '%s(%s)' % (self.exprtype, self.expression or u'') |
|
166 |
||
167 |
@property |
|
168 |
def expression_of(self): |
|
169 |
for rel in ('read_permission', 'add_permission', 'delete_permission', |
|
170 |
'update_permission', 'condition'): |
|
171 |
values = getattr(self, 'reverse_%s' % rel) |
|
172 |
if values: |
|
173 |
return values[0] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
174 |
|
0 | 175 |
@cached |
176 |
def _rqlexpr(self): |
|
177 |
if self.exprtype == 'ERQLExpression': |
|
178 |
return ERQLExpression(self.expression, self.mainvars, self.eid) |
|
179 |
#if self.exprtype == 'RRQLExpression': |
|
180 |
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
|
181 |
|
0 | 182 |
def check_expression(self, *args, **kwargs): |
183 |
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
|
184 |
|
0 | 185 |
def after_deletion_path(self): |
186 |
"""return (path, parameters) which should be used as redirect |
|
187 |
information when this entity is being deleted |
|
188 |
""" |
|
189 |
if self.expression_of: |
|
190 |
return self.expression_of.rest_path(), {} |
|
191 |
return super(RQLExpression, self).after_deletion_path() |
|
192 |
||
193 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
194 |
class CWPermission(AnyEntity): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3353
diff
changeset
|
195 |
__regid__ = 'CWPermission' |
0 | 196 |
fetch_attrs, fetch_order = fetch_config(['name', 'label']) |
197 |
||
198 |
def dc_title(self): |
|
199 |
if self.label: |
|
3432
12fe502e5044
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3377
diff
changeset
|
200 |
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
|
201 |
return self._cw._(self.name) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
202 |
|
0 | 203 |
def after_deletion_path(self): |
204 |
"""return (path, parameters) which should be used as redirect |
|
205 |
information when this entity is being deleted |
|
206 |
""" |
|
207 |
permissionof = getattr(self, 'reverse_require_permission', ()) |
|
208 |
if len(permissionof) == 1: |
|
209 |
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
|
210 |
return super(CWPermission, self).after_deletion_path() |