author | Nicolas Chauvat <nicolas.chauvat@logilab.fr> |
Sun, 24 May 2009 02:04:07 +0200 | |
branch | stable |
changeset 1917 | eaf6e0edc509 |
parent 1802 | d628defebc17 |
child 1977 | 606923dff11b |
permissions | -rw-r--r-- |
0 | 1 |
"""schema definition related entities |
2 |
||
3 |
:organization: Logilab |
|
1154 | 4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
6 |
""" |
|
7 |
__docformat__ = "restructuredtext en" |
|
8 |
||
9 |
from logilab.common.decorators import cached |
|
10 |
||
11 |
from cubicweb import ValidationError |
|
12 |
from cubicweb.schema import ERQLExpression, RRQLExpression |
|
13 |
||
14 |
from cubicweb.entities import AnyEntity, fetch_config |
|
15 |
||
16 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
17 |
class CWEType(AnyEntity): |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
18 |
id = 'CWEType' |
0 | 19 |
fetch_attrs, fetch_order = fetch_config(['name']) |
1154 | 20 |
|
0 | 21 |
def dc_title(self): |
22 |
return self.req._(self.name) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
23 |
|
0 | 24 |
def dc_long_title(self): |
25 |
stereotypes = [] |
|
26 |
_ = self.req._ |
|
27 |
if self.meta: |
|
28 |
stereotypes.append(_('meta')) |
|
29 |
if self.final: |
|
30 |
stereotypes.append(_('final')) |
|
31 |
if stereotypes: |
|
32 |
return u'%s <<%s>>' % (self.dc_title(), ', '.join(stereotypes)) |
|
33 |
return self.dc_title() |
|
34 |
||
35 |
def db_key_name(self): |
|
36 |
"""XXX goa specific""" |
|
37 |
return self.get('name') |
|
38 |
||
39 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
40 |
class CWRType(AnyEntity): |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
41 |
id = 'CWRType' |
0 | 42 |
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
|
43 |
|
0 | 44 |
def dc_title(self): |
45 |
return self.req._(self.name) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
46 |
|
0 | 47 |
def dc_long_title(self): |
48 |
stereotypes = [] |
|
49 |
_ = self.req._ |
|
50 |
if self.meta: |
|
51 |
stereotypes.append(_('meta')) |
|
52 |
if self.symetric: |
|
53 |
stereotypes.append(_('symetric')) |
|
54 |
if self.inlined: |
|
55 |
stereotypes.append(_('inlined')) |
|
56 |
if self.final: |
|
57 |
stereotypes.append(_('final')) |
|
58 |
if stereotypes: |
|
59 |
return u'%s <<%s>>' % (self.dc_title(), ', '.join(stereotypes)) |
|
60 |
return self.dc_title() |
|
61 |
||
62 |
def inlined_changed(self, inlined): |
|
63 |
"""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
|
64 |
|
0 | 65 |
* return False if nothing has changed |
66 |
* raise ValidationError if inlining is'nt possible |
|
67 |
* eventually return True |
|
68 |
""" |
|
69 |
rtype = self.name |
|
70 |
rschema = self.schema.rschema(rtype) |
|
71 |
if inlined == rschema.inlined: |
|
72 |
return False |
|
73 |
if inlined: |
|
74 |
for (stype, otype) in rschema.iter_rdefs(): |
|
75 |
card = rschema.rproperty(stype, otype, 'cardinality')[0] |
|
76 |
if not card in '?1': |
|
77 |
msg = self.req._("can't set inlined=%(inlined)s, " |
|
78 |
"%(stype)s %(rtype)s %(otype)s " |
|
79 |
"has cardinality=%(card)s") |
|
80 |
raise ValidationError(self.eid, {'inlined': msg % locals()}) |
|
81 |
return True |
|
82 |
||
83 |
def db_key_name(self): |
|
84 |
"""XXX goa specific""" |
|
85 |
return self.get('name') |
|
86 |
||
87 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
88 |
class CWRelation(AnyEntity): |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
89 |
id = 'CWRelation' |
0 | 90 |
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
|
91 |
|
0 | 92 |
def dc_title(self): |
93 |
return u'%s %s %s' % ( |
|
94 |
self.from_entity[0].name, |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
95 |
self.relation_type[0].name, |
0 | 96 |
self.to_entity[0].name) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
97 |
|
0 | 98 |
def dc_long_title(self): |
99 |
card = self.cardinality |
|
100 |
scard, ocard = u'', u'' |
|
101 |
if card[0] != '1': |
|
102 |
scard = '[%s]' % card[0] |
|
103 |
if card[1] != '1': |
|
104 |
ocard = '[%s]' % card[1] |
|
105 |
return u'%s %s%s%s %s' % ( |
|
106 |
self.from_entity[0].name, |
|
107 |
scard, self.relation_type[0].name, ocard, |
|
108 |
self.to_entity[0].name) |
|
109 |
||
110 |
def after_deletion_path(self): |
|
111 |
"""return (path, parameters) which should be used as redirect |
|
112 |
information when this entity is being deleted |
|
113 |
""" |
|
114 |
if self.relation_type: |
|
115 |
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
|
116 |
return super(CWRelation, self).after_deletion_path() |
0 | 117 |
|
118 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
119 |
class CWAttribute(CWRelation): |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
120 |
id = 'CWAttribute' |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
121 |
|
0 | 122 |
def dc_long_title(self): |
123 |
card = self.cardinality |
|
124 |
scard = u'' |
|
125 |
if card[0] == '1': |
|
126 |
scard = '+' |
|
127 |
return u'%s %s%s %s' % ( |
|
128 |
self.from_entity[0].name, |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
129 |
scard, self.relation_type[0].name, |
0 | 130 |
self.to_entity[0].name) |
131 |
||
132 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
133 |
class CWConstraint(AnyEntity): |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
134 |
id = 'CWConstraint' |
0 | 135 |
fetch_attrs, fetch_order = fetch_config(['value']) |
136 |
||
137 |
def dc_title(self): |
|
138 |
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
|
139 |
|
0 | 140 |
def after_deletion_path(self): |
141 |
"""return (path, parameters) which should be used as redirect |
|
142 |
information when this entity is being deleted |
|
143 |
""" |
|
144 |
if self.reverse_constrained_by: |
|
145 |
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
|
146 |
return super(CWConstraint, self).after_deletion_path() |
0 | 147 |
|
148 |
@property |
|
149 |
def type(self): |
|
150 |
return self.cstrtype[0].name |
|
151 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
152 |
|
0 | 153 |
class RQLExpression(AnyEntity): |
154 |
id = 'RQLExpression' |
|
155 |
fetch_attrs, fetch_order = fetch_config(['exprtype', 'mainvars', 'expression']) |
|
156 |
||
157 |
def dc_title(self): |
|
158 |
return '%s(%s)' % (self.exprtype, self.expression or u'') |
|
159 |
||
160 |
@property |
|
161 |
def expression_of(self): |
|
162 |
for rel in ('read_permission', 'add_permission', 'delete_permission', |
|
163 |
'update_permission', 'condition'): |
|
164 |
values = getattr(self, 'reverse_%s' % rel) |
|
165 |
if values: |
|
166 |
return values[0] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
167 |
|
0 | 168 |
@cached |
169 |
def _rqlexpr(self): |
|
170 |
if self.exprtype == 'ERQLExpression': |
|
171 |
return ERQLExpression(self.expression, self.mainvars, self.eid) |
|
172 |
#if self.exprtype == 'RRQLExpression': |
|
173 |
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
|
174 |
|
0 | 175 |
def check_expression(self, *args, **kwargs): |
176 |
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
|
177 |
|
0 | 178 |
def after_deletion_path(self): |
179 |
"""return (path, parameters) which should be used as redirect |
|
180 |
information when this entity is being deleted |
|
181 |
""" |
|
182 |
if self.expression_of: |
|
183 |
return self.expression_of.rest_path(), {} |
|
184 |
return super(RQLExpression, self).after_deletion_path() |
|
185 |
||
186 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
187 |
class CWPermission(AnyEntity): |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1154
diff
changeset
|
188 |
id = 'CWPermission' |
0 | 189 |
fetch_attrs, fetch_order = fetch_config(['name', 'label']) |
190 |
||
191 |
def dc_title(self): |
|
192 |
if self.label: |
|
193 |
return '%s (%s)' % (self.req._(self.name), self.label) |
|
194 |
return self.req._(self.name) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
195 |
|
0 | 196 |
def after_deletion_path(self): |
197 |
"""return (path, parameters) which should be used as redirect |
|
198 |
information when this entity is being deleted |
|
199 |
""" |
|
200 |
permissionof = getattr(self, 'reverse_require_permission', ()) |
|
201 |
if len(permissionof) == 1: |
|
202 |
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
|
203 |
return super(CWPermission, self).after_deletion_path() |