author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 03 Jul 2009 18:00:07 +0200 | |
branch | stable |
changeset 2258 | 79bc598c6411 |
parent 1998 | 12040c090aa4 |
child 2126 | a25859917ccc |
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.meta: |
|
29 |
stereotypes.append(_('meta')) |
|
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): |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
42 |
id = '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): |
1998
12040c090aa4
[views] change schema views: remove view eschema and use tabs for cwetype
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1977
diff
changeset
|
46 |
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
|
47 |
|
0 | 48 |
def dc_long_title(self): |
49 |
stereotypes = [] |
|
50 |
_ = self.req._ |
|
51 |
if self.meta: |
|
52 |
stereotypes.append(_('meta')) |
|
53 |
if self.symetric: |
|
54 |
stereotypes.append(_('symetric')) |
|
55 |
if self.inlined: |
|
56 |
stereotypes.append(_('inlined')) |
|
57 |
if self.final: |
|
58 |
stereotypes.append(_('final')) |
|
59 |
if stereotypes: |
|
60 |
return u'%s <<%s>>' % (self.dc_title(), ', '.join(stereotypes)) |
|
61 |
return self.dc_title() |
|
62 |
||
63 |
def inlined_changed(self, inlined): |
|
64 |
"""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
|
65 |
|
0 | 66 |
* return False if nothing has changed |
67 |
* raise ValidationError if inlining is'nt possible |
|
68 |
* eventually return True |
|
69 |
""" |
|
70 |
rtype = self.name |
|
71 |
rschema = self.schema.rschema(rtype) |
|
72 |
if inlined == rschema.inlined: |
|
73 |
return False |
|
74 |
if inlined: |
|
75 |
for (stype, otype) in rschema.iter_rdefs(): |
|
76 |
card = rschema.rproperty(stype, otype, 'cardinality')[0] |
|
77 |
if not card in '?1': |
|
78 |
msg = self.req._("can't set inlined=%(inlined)s, " |
|
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): |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
90 |
id = '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 |
|
119 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
120 |
class CWAttribute(CWRelation): |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
121 |
id = 'CWAttribute' |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
122 |
|
0 | 123 |
def dc_long_title(self): |
124 |
card = self.cardinality |
|
125 |
scard = u'' |
|
126 |
if card[0] == '1': |
|
127 |
scard = '+' |
|
128 |
return u'%s %s%s %s' % ( |
|
129 |
self.from_entity[0].name, |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
130 |
scard, self.relation_type[0].name, |
0 | 131 |
self.to_entity[0].name) |
132 |
||
133 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
134 |
class CWConstraint(AnyEntity): |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
135 |
id = 'CWConstraint' |
0 | 136 |
fetch_attrs, fetch_order = fetch_config(['value']) |
137 |
||
138 |
def dc_title(self): |
|
139 |
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
|
140 |
|
0 | 141 |
def after_deletion_path(self): |
142 |
"""return (path, parameters) which should be used as redirect |
|
143 |
information when this entity is being deleted |
|
144 |
""" |
|
145 |
if self.reverse_constrained_by: |
|
146 |
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
|
147 |
return super(CWConstraint, self).after_deletion_path() |
0 | 148 |
|
149 |
@property |
|
150 |
def type(self): |
|
151 |
return self.cstrtype[0].name |
|
152 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
153 |
|
0 | 154 |
class RQLExpression(AnyEntity): |
155 |
id = 'RQLExpression' |
|
156 |
fetch_attrs, fetch_order = fetch_config(['exprtype', 'mainvars', 'expression']) |
|
157 |
||
158 |
def dc_title(self): |
|
159 |
return '%s(%s)' % (self.exprtype, self.expression or u'') |
|
160 |
||
161 |
@property |
|
162 |
def expression_of(self): |
|
163 |
for rel in ('read_permission', 'add_permission', 'delete_permission', |
|
164 |
'update_permission', 'condition'): |
|
165 |
values = getattr(self, 'reverse_%s' % rel) |
|
166 |
if values: |
|
167 |
return values[0] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
168 |
|
0 | 169 |
@cached |
170 |
def _rqlexpr(self): |
|
171 |
if self.exprtype == 'ERQLExpression': |
|
172 |
return ERQLExpression(self.expression, self.mainvars, self.eid) |
|
173 |
#if self.exprtype == 'RRQLExpression': |
|
174 |
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
|
175 |
|
0 | 176 |
def check_expression(self, *args, **kwargs): |
177 |
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
|
178 |
|
0 | 179 |
def after_deletion_path(self): |
180 |
"""return (path, parameters) which should be used as redirect |
|
181 |
information when this entity is being deleted |
|
182 |
""" |
|
183 |
if self.expression_of: |
|
184 |
return self.expression_of.rest_path(), {} |
|
185 |
return super(RQLExpression, self).after_deletion_path() |
|
186 |
||
187 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
188 |
class CWPermission(AnyEntity): |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
189 |
id = 'CWPermission' |
0 | 190 |
fetch_attrs, fetch_order = fetch_config(['name', 'label']) |
191 |
||
192 |
def dc_title(self): |
|
193 |
if self.label: |
|
194 |
return '%s (%s)' % (self.req._(self.name), self.label) |
|
195 |
return self.req._(self.name) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
196 |
|
0 | 197 |
def after_deletion_path(self): |
198 |
"""return (path, parameters) which should be used as redirect |
|
199 |
information when this entity is being deleted |
|
200 |
""" |
|
201 |
permissionof = getattr(self, 'reverse_require_permission', ()) |
|
202 |
if len(permissionof) == 1: |
|
203 |
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
|
204 |
return super(CWPermission, self).after_deletion_path() |