author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Sun, 02 Aug 2009 11:54:48 +0200 | |
changeset 2643 | 9976f511003b |
parent 2594 | 92aad5e18405 |
child 3353 | 1003a6f3d298 |
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): |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
19 |
id = 'CWEType' |
0 | 20 |
fetch_attrs, fetch_order = fetch_config(['name']) |
1154 | 21 |
|
0 | 22 |
def dc_title(self): |
1998
12040c090aa4
[views] change schema views: remove view eschema and use tabs for cwetype
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1977
diff
changeset
|
23 |
return u'%s (%s)' % (self.name, self.req._(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 = [] |
|
27 |
_ = self.req._ |
|
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): |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
40 |
id = '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): |
1998
12040c090aa4
[views] change schema views: remove view eschema and use tabs for cwetype
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1977
diff
changeset
|
44 |
return u'%s (%s)' % (self.name, self.req._(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 = [] |
|
48 |
_ = self.req._ |
|
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 |
""" |
|
66 |
rtype = self.name |
|
67 |
rschema = self.schema.rschema(rtype) |
|
68 |
if inlined == rschema.inlined: |
|
69 |
return False |
|
70 |
if inlined: |
|
71 |
for (stype, otype) in rschema.iter_rdefs(): |
|
72 |
card = rschema.rproperty(stype, otype, 'cardinality')[0] |
|
73 |
if not card in '?1': |
|
74 |
msg = self.req._("can't set inlined=%(inlined)s, " |
|
75 |
"%(stype)s %(rtype)s %(otype)s " |
|
76 |
"has cardinality=%(card)s") |
|
77 |
raise ValidationError(self.eid, {'inlined': msg % locals()}) |
|
78 |
return True |
|
79 |
||
80 |
def db_key_name(self): |
|
81 |
"""XXX goa specific""" |
|
82 |
return self.get('name') |
|
83 |
||
84 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
85 |
class CWRelation(AnyEntity): |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
86 |
id = 'CWRelation' |
0 | 87 |
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
|
88 |
|
0 | 89 |
def dc_title(self): |
90 |
return u'%s %s %s' % ( |
|
91 |
self.from_entity[0].name, |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
92 |
self.relation_type[0].name, |
0 | 93 |
self.to_entity[0].name) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
94 |
|
0 | 95 |
def dc_long_title(self): |
96 |
card = self.cardinality |
|
97 |
scard, ocard = u'', u'' |
|
98 |
if card[0] != '1': |
|
99 |
scard = '[%s]' % card[0] |
|
100 |
if card[1] != '1': |
|
101 |
ocard = '[%s]' % card[1] |
|
102 |
return u'%s %s%s%s %s' % ( |
|
103 |
self.from_entity[0].name, |
|
104 |
scard, self.relation_type[0].name, ocard, |
|
105 |
self.to_entity[0].name) |
|
106 |
||
107 |
def after_deletion_path(self): |
|
108 |
"""return (path, parameters) which should be used as redirect |
|
109 |
information when this entity is being deleted |
|
110 |
""" |
|
111 |
if self.relation_type: |
|
112 |
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
|
113 |
return super(CWRelation, self).after_deletion_path() |
0 | 114 |
|
2594
92aad5e18405
[R schemaobjs] property accessors for CWRelation/CWAttribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2299
diff
changeset
|
115 |
@property |
92aad5e18405
[R schemaobjs] property accessors for CWRelation/CWAttribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2299
diff
changeset
|
116 |
def rtype(self): |
92aad5e18405
[R schemaobjs] property accessors for CWRelation/CWAttribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2299
diff
changeset
|
117 |
return self.relation_type[0] |
92aad5e18405
[R schemaobjs] property accessors for CWRelation/CWAttribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2299
diff
changeset
|
118 |
|
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 stype(self): |
92aad5e18405
[R schemaobjs] property accessors for CWRelation/CWAttribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2299
diff
changeset
|
121 |
return self.from_entity[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 otype(self): |
92aad5e18405
[R schemaobjs] property accessors for CWRelation/CWAttribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2299
diff
changeset
|
125 |
return self.to_entity[0] |
92aad5e18405
[R schemaobjs] property accessors for CWRelation/CWAttribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2299
diff
changeset
|
126 |
|
0 | 127 |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
128 |
class CWAttribute(CWRelation): |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
129 |
id = 'CWAttribute' |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
130 |
|
0 | 131 |
def dc_long_title(self): |
132 |
card = self.cardinality |
|
133 |
scard = u'' |
|
134 |
if card[0] == '1': |
|
135 |
scard = '+' |
|
136 |
return u'%s %s%s %s' % ( |
|
137 |
self.from_entity[0].name, |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
138 |
scard, self.relation_type[0].name, |
0 | 139 |
self.to_entity[0].name) |
140 |
||
141 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
142 |
class CWConstraint(AnyEntity): |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
143 |
id = 'CWConstraint' |
0 | 144 |
fetch_attrs, fetch_order = fetch_config(['value']) |
145 |
||
146 |
def dc_title(self): |
|
147 |
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
|
148 |
|
0 | 149 |
def after_deletion_path(self): |
150 |
"""return (path, parameters) which should be used as redirect |
|
151 |
information when this entity is being deleted |
|
152 |
""" |
|
153 |
if self.reverse_constrained_by: |
|
154 |
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
|
155 |
return super(CWConstraint, self).after_deletion_path() |
0 | 156 |
|
157 |
@property |
|
158 |
def type(self): |
|
159 |
return self.cstrtype[0].name |
|
160 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
161 |
|
0 | 162 |
class RQLExpression(AnyEntity): |
163 |
id = 'RQLExpression' |
|
164 |
fetch_attrs, fetch_order = fetch_config(['exprtype', 'mainvars', 'expression']) |
|
165 |
||
166 |
def dc_title(self): |
|
167 |
return '%s(%s)' % (self.exprtype, self.expression or u'') |
|
168 |
||
169 |
@property |
|
170 |
def expression_of(self): |
|
171 |
for rel in ('read_permission', 'add_permission', 'delete_permission', |
|
172 |
'update_permission', 'condition'): |
|
173 |
values = getattr(self, 'reverse_%s' % rel) |
|
174 |
if values: |
|
175 |
return values[0] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
176 |
|
0 | 177 |
@cached |
178 |
def _rqlexpr(self): |
|
179 |
if self.exprtype == 'ERQLExpression': |
|
180 |
return ERQLExpression(self.expression, self.mainvars, self.eid) |
|
181 |
#if self.exprtype == 'RRQLExpression': |
|
182 |
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
|
183 |
|
0 | 184 |
def check_expression(self, *args, **kwargs): |
185 |
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
|
186 |
|
0 | 187 |
def after_deletion_path(self): |
188 |
"""return (path, parameters) which should be used as redirect |
|
189 |
information when this entity is being deleted |
|
190 |
""" |
|
191 |
if self.expression_of: |
|
192 |
return self.expression_of.rest_path(), {} |
|
193 |
return super(RQLExpression, self).after_deletion_path() |
|
194 |
||
195 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
196 |
class CWPermission(AnyEntity): |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
197 |
id = 'CWPermission' |
0 | 198 |
fetch_attrs, fetch_order = fetch_config(['name', 'label']) |
199 |
||
200 |
def dc_title(self): |
|
201 |
if self.label: |
|
202 |
return '%s (%s)' % (self.req._(self.name), self.label) |
|
203 |
return self.req._(self.name) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
204 |
|
0 | 205 |
def after_deletion_path(self): |
206 |
"""return (path, parameters) which should be used as redirect |
|
207 |
information when this entity is being deleted |
|
208 |
""" |
|
209 |
permissionof = getattr(self, 'reverse_require_permission', ()) |
|
210 |
if len(permissionof) == 1: |
|
211 |
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
|
212 |
return super(CWPermission, self).after_deletion_path() |