author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 22 Jul 2009 18:35:21 +0200 | |
changeset 2423 | f6757021018f |
parent 2299 | 85fb4b808f5b |
child 2594 | 92aad5e18405 |
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 |
|
115 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
116 |
class CWAttribute(CWRelation): |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
117 |
id = 'CWAttribute' |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
118 |
|
0 | 119 |
def dc_long_title(self): |
120 |
card = self.cardinality |
|
121 |
scard = u'' |
|
122 |
if card[0] == '1': |
|
123 |
scard = '+' |
|
124 |
return u'%s %s%s %s' % ( |
|
125 |
self.from_entity[0].name, |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
126 |
scard, self.relation_type[0].name, |
0 | 127 |
self.to_entity[0].name) |
128 |
||
129 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
130 |
class CWConstraint(AnyEntity): |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
131 |
id = 'CWConstraint' |
0 | 132 |
fetch_attrs, fetch_order = fetch_config(['value']) |
133 |
||
134 |
def dc_title(self): |
|
135 |
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
|
136 |
|
0 | 137 |
def after_deletion_path(self): |
138 |
"""return (path, parameters) which should be used as redirect |
|
139 |
information when this entity is being deleted |
|
140 |
""" |
|
141 |
if self.reverse_constrained_by: |
|
142 |
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
|
143 |
return super(CWConstraint, self).after_deletion_path() |
0 | 144 |
|
145 |
@property |
|
146 |
def type(self): |
|
147 |
return self.cstrtype[0].name |
|
148 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
149 |
|
0 | 150 |
class RQLExpression(AnyEntity): |
151 |
id = 'RQLExpression' |
|
152 |
fetch_attrs, fetch_order = fetch_config(['exprtype', 'mainvars', 'expression']) |
|
153 |
||
154 |
def dc_title(self): |
|
155 |
return '%s(%s)' % (self.exprtype, self.expression or u'') |
|
156 |
||
157 |
@property |
|
158 |
def expression_of(self): |
|
159 |
for rel in ('read_permission', 'add_permission', 'delete_permission', |
|
160 |
'update_permission', 'condition'): |
|
161 |
values = getattr(self, 'reverse_%s' % rel) |
|
162 |
if values: |
|
163 |
return values[0] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
164 |
|
0 | 165 |
@cached |
166 |
def _rqlexpr(self): |
|
167 |
if self.exprtype == 'ERQLExpression': |
|
168 |
return ERQLExpression(self.expression, self.mainvars, self.eid) |
|
169 |
#if self.exprtype == 'RRQLExpression': |
|
170 |
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
|
171 |
|
0 | 172 |
def check_expression(self, *args, **kwargs): |
173 |
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
|
174 |
|
0 | 175 |
def after_deletion_path(self): |
176 |
"""return (path, parameters) which should be used as redirect |
|
177 |
information when this entity is being deleted |
|
178 |
""" |
|
179 |
if self.expression_of: |
|
180 |
return self.expression_of.rest_path(), {} |
|
181 |
return super(RQLExpression, self).after_deletion_path() |
|
182 |
||
183 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
184 |
class CWPermission(AnyEntity): |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
185 |
id = 'CWPermission' |
0 | 186 |
fetch_attrs, fetch_order = fetch_config(['name', 'label']) |
187 |
||
188 |
def dc_title(self): |
|
189 |
if self.label: |
|
190 |
return '%s (%s)' % (self.req._(self.name), self.label) |
|
191 |
return self.req._(self.name) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
192 |
|
0 | 193 |
def after_deletion_path(self): |
194 |
"""return (path, parameters) which should be used as redirect |
|
195 |
information when this entity is being deleted |
|
196 |
""" |
|
197 |
permissionof = getattr(self, 'reverse_require_permission', ()) |
|
198 |
if len(permissionof) == 1: |
|
199 |
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
|
200 |
return super(CWPermission, self).after_deletion_path() |