author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Thu, 24 Sep 2009 20:25:49 +0200 | |
changeset 3462 | 3a79fecdd2b4 |
parent 3432 | 12fe502e5044 |
child 4252 | 6c4f109c2b03 |
permissions | -rw-r--r-- |
0 | 1 |
"""schema definition related entities |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
4 |
:copyright: 2001-2009 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 |
||
12 |
from cubicweb import ValidationError |
|
13 |
from cubicweb.schema import ERQLExpression, RRQLExpression |
|
14 |
||
15 |
from cubicweb.entities import AnyEntity, fetch_config |
|
16 |
||
17 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
18 |
class CWEType(AnyEntity): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3353
diff
changeset
|
19 |
__regid__ = 'CWEType' |
0 | 20 |
fetch_attrs, fetch_order = fetch_config(['name']) |
1154 | 21 |
|
0 | 22 |
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
|
23 |
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
|
24 |
|
0 | 25 |
def dc_long_title(self): |
26 |
stereotypes = [] |
|
3432
12fe502e5044
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3377
diff
changeset
|
27 |
_ = self._cw._ |
0 | 28 |
if self.final: |
29 |
stereotypes.append(_('final')) |
|
30 |
if stereotypes: |
|
31 |
return u'%s <<%s>>' % (self.dc_title(), ', '.join(stereotypes)) |
|
32 |
return self.dc_title() |
|
33 |
||
34 |
def db_key_name(self): |
|
35 |
"""XXX goa specific""" |
|
36 |
return self.get('name') |
|
37 |
||
38 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
39 |
class CWRType(AnyEntity): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3353
diff
changeset
|
40 |
__regid__ = 'CWRType' |
0 | 41 |
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
|
42 |
|
0 | 43 |
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
|
44 |
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
|
45 |
|
0 | 46 |
def dc_long_title(self): |
47 |
stereotypes = [] |
|
3432
12fe502e5044
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3377
diff
changeset
|
48 |
_ = self._cw._ |
0 | 49 |
if self.symetric: |
50 |
stereotypes.append(_('symetric')) |
|
51 |
if self.inlined: |
|
52 |
stereotypes.append(_('inlined')) |
|
53 |
if self.final: |
|
54 |
stereotypes.append(_('final')) |
|
55 |
if stereotypes: |
|
56 |
return u'%s <<%s>>' % (self.dc_title(), ', '.join(stereotypes)) |
|
57 |
return self.dc_title() |
|
58 |
||
59 |
def inlined_changed(self, inlined): |
|
60 |
"""check inlining is necessary and possible: |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
61 |
|
0 | 62 |
* return False if nothing has changed |
63 |
* raise ValidationError if inlining is'nt possible |
|
64 |
* eventually return True |
|
65 |
""" |
|
3432
12fe502e5044
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3377
diff
changeset
|
66 |
rschema = self._cw.vreg.schema.rschema(self.name) |
0 | 67 |
if inlined == rschema.inlined: |
68 |
return False |
|
69 |
if inlined: |
|
3353
1003a6f3d298
[schema sync] fix so that new cardinality are considered when both inlined and cardinality change in the same transaction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2594
diff
changeset
|
70 |
# don't use the persistent schema, we may miss cardinality changes |
1003a6f3d298
[schema sync] fix so that new cardinality are considered when both inlined and cardinality change in the same transaction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2594
diff
changeset
|
71 |
# in the same transaction |
1003a6f3d298
[schema sync] fix so that new cardinality are considered when both inlined and cardinality change in the same transaction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2594
diff
changeset
|
72 |
for rdef in self.reverse_relation_type: |
1003a6f3d298
[schema sync] fix so that new cardinality are considered when both inlined and cardinality change in the same transaction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2594
diff
changeset
|
73 |
card = rdef.cardinality[0] |
0 | 74 |
if not card in '?1': |
3353
1003a6f3d298
[schema sync] fix so that new cardinality are considered when both inlined and cardinality change in the same transaction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2594
diff
changeset
|
75 |
rtype = self.name |
1003a6f3d298
[schema sync] fix so that new cardinality are considered when both inlined and cardinality change in the same transaction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2594
diff
changeset
|
76 |
stype = rdef.stype |
1003a6f3d298
[schema sync] fix so that new cardinality are considered when both inlined and cardinality change in the same transaction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2594
diff
changeset
|
77 |
otype = rdef.otype |
3432
12fe502e5044
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3377
diff
changeset
|
78 |
msg = self._cw._("can't set inlined=%(inlined)s, " |
0 | 79 |
"%(stype)s %(rtype)s %(otype)s " |
80 |
"has cardinality=%(card)s") |
|
81 |
raise ValidationError(self.eid, {'inlined': msg % locals()}) |
|
82 |
return True |
|
83 |
||
84 |
def db_key_name(self): |
|
85 |
"""XXX goa specific""" |
|
86 |
return self.get('name') |
|
87 |
||
88 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
89 |
class CWRelation(AnyEntity): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3353
diff
changeset
|
90 |
__regid__ = 'CWRelation' |
0 | 91 |
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
|
92 |
|
0 | 93 |
def dc_title(self): |
94 |
return u'%s %s %s' % ( |
|
95 |
self.from_entity[0].name, |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
96 |
self.relation_type[0].name, |
0 | 97 |
self.to_entity[0].name) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
98 |
|
0 | 99 |
def dc_long_title(self): |
100 |
card = self.cardinality |
|
101 |
scard, ocard = u'', u'' |
|
102 |
if card[0] != '1': |
|
103 |
scard = '[%s]' % card[0] |
|
104 |
if card[1] != '1': |
|
105 |
ocard = '[%s]' % card[1] |
|
106 |
return u'%s %s%s%s %s' % ( |
|
107 |
self.from_entity[0].name, |
|
108 |
scard, self.relation_type[0].name, ocard, |
|
109 |
self.to_entity[0].name) |
|
110 |
||
111 |
def after_deletion_path(self): |
|
112 |
"""return (path, parameters) which should be used as redirect |
|
113 |
information when this entity is being deleted |
|
114 |
""" |
|
115 |
if self.relation_type: |
|
116 |
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
|
117 |
return super(CWRelation, self).after_deletion_path() |
0 | 118 |
|
2594
92aad5e18405
[R schemaobjs] property accessors for CWRelation/CWAttribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2299
diff
changeset
|
119 |
@property |
92aad5e18405
[R schemaobjs] property accessors for CWRelation/CWAttribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2299
diff
changeset
|
120 |
def rtype(self): |
92aad5e18405
[R schemaobjs] property accessors for CWRelation/CWAttribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2299
diff
changeset
|
121 |
return self.relation_type[0] |
92aad5e18405
[R schemaobjs] property accessors for CWRelation/CWAttribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2299
diff
changeset
|
122 |
|
92aad5e18405
[R schemaobjs] property accessors for CWRelation/CWAttribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2299
diff
changeset
|
123 |
@property |
92aad5e18405
[R schemaobjs] property accessors for CWRelation/CWAttribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2299
diff
changeset
|
124 |
def stype(self): |
92aad5e18405
[R schemaobjs] property accessors for CWRelation/CWAttribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2299
diff
changeset
|
125 |
return self.from_entity[0] |
92aad5e18405
[R schemaobjs] property accessors for CWRelation/CWAttribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2299
diff
changeset
|
126 |
|
92aad5e18405
[R schemaobjs] property accessors for CWRelation/CWAttribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2299
diff
changeset
|
127 |
@property |
92aad5e18405
[R schemaobjs] property accessors for CWRelation/CWAttribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2299
diff
changeset
|
128 |
def otype(self): |
92aad5e18405
[R schemaobjs] property accessors for CWRelation/CWAttribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2299
diff
changeset
|
129 |
return self.to_entity[0] |
92aad5e18405
[R schemaobjs] property accessors for CWRelation/CWAttribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2299
diff
changeset
|
130 |
|
0 | 131 |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
132 |
class CWAttribute(CWRelation): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3353
diff
changeset
|
133 |
__regid__ = 'CWAttribute' |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
134 |
|
0 | 135 |
def dc_long_title(self): |
136 |
card = self.cardinality |
|
137 |
scard = u'' |
|
138 |
if card[0] == '1': |
|
139 |
scard = '+' |
|
140 |
return u'%s %s%s %s' % ( |
|
141 |
self.from_entity[0].name, |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
142 |
scard, self.relation_type[0].name, |
0 | 143 |
self.to_entity[0].name) |
144 |
||
145 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
146 |
class CWConstraint(AnyEntity): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3353
diff
changeset
|
147 |
__regid__ = 'CWConstraint' |
0 | 148 |
fetch_attrs, fetch_order = fetch_config(['value']) |
149 |
||
150 |
def dc_title(self): |
|
151 |
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
|
152 |
|
0 | 153 |
def after_deletion_path(self): |
154 |
"""return (path, parameters) which should be used as redirect |
|
155 |
information when this entity is being deleted |
|
156 |
""" |
|
157 |
if self.reverse_constrained_by: |
|
158 |
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
|
159 |
return super(CWConstraint, self).after_deletion_path() |
0 | 160 |
|
161 |
@property |
|
162 |
def type(self): |
|
163 |
return self.cstrtype[0].name |
|
164 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
165 |
|
0 | 166 |
class RQLExpression(AnyEntity): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3353
diff
changeset
|
167 |
__regid__ = 'RQLExpression' |
0 | 168 |
fetch_attrs, fetch_order = fetch_config(['exprtype', 'mainvars', 'expression']) |
169 |
||
170 |
def dc_title(self): |
|
171 |
return '%s(%s)' % (self.exprtype, self.expression or u'') |
|
172 |
||
173 |
@property |
|
174 |
def expression_of(self): |
|
175 |
for rel in ('read_permission', 'add_permission', 'delete_permission', |
|
176 |
'update_permission', 'condition'): |
|
177 |
values = getattr(self, 'reverse_%s' % rel) |
|
178 |
if values: |
|
179 |
return values[0] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
180 |
|
0 | 181 |
@cached |
182 |
def _rqlexpr(self): |
|
183 |
if self.exprtype == 'ERQLExpression': |
|
184 |
return ERQLExpression(self.expression, self.mainvars, self.eid) |
|
185 |
#if self.exprtype == 'RRQLExpression': |
|
186 |
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
|
187 |
|
0 | 188 |
def check_expression(self, *args, **kwargs): |
189 |
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
|
190 |
|
0 | 191 |
def after_deletion_path(self): |
192 |
"""return (path, parameters) which should be used as redirect |
|
193 |
information when this entity is being deleted |
|
194 |
""" |
|
195 |
if self.expression_of: |
|
196 |
return self.expression_of.rest_path(), {} |
|
197 |
return super(RQLExpression, self).after_deletion_path() |
|
198 |
||
199 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
200 |
class CWPermission(AnyEntity): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3353
diff
changeset
|
201 |
__regid__ = 'CWPermission' |
0 | 202 |
fetch_attrs, fetch_order = fetch_config(['name', 'label']) |
203 |
||
204 |
def dc_title(self): |
|
205 |
if self.label: |
|
3432
12fe502e5044
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3377
diff
changeset
|
206 |
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
|
207 |
return self._cw._(self.name) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
208 |
|
0 | 209 |
def after_deletion_path(self): |
210 |
"""return (path, parameters) which should be used as redirect |
|
211 |
information when this entity is being deleted |
|
212 |
""" |
|
213 |
permissionof = getattr(self, 'reverse_require_permission', ()) |
|
214 |
if len(permissionof) == 1: |
|
215 |
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
|
216 |
return super(CWPermission, self).after_deletion_path() |