author | Alexandre Fayolle <alexandre.fayolle@logilab.fr> |
Wed, 07 Apr 2010 11:53:11 +0200 | |
branch | stable |
changeset 5170 | 427374fe573f |
parent 4467 | 0e73d299730a |
child 5233 | 673b63953e7a |
child 5421 | 8167de96c523 |
permissions | -rw-r--r-- |
0 | 1 |
"""an helper class to display CubicWeb schema using ureports |
2 |
||
3 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3768
diff
changeset
|
4 |
:copyright: 2001-2010 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:
1942
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
2126
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1998
diff
changeset
|
9 |
_ = unicode |
0 | 10 |
|
11 |
from logilab.common.ureports import Section, Title, Table, Link, Span, Text |
|
4087 | 12 |
|
1731
13f948678a6a
delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents:
1522
diff
changeset
|
13 |
from yams.schema2dot import CARD_MAP |
4087 | 14 |
from yams.schema import RelationDefinitionSchema |
0 | 15 |
|
16 |
I18NSTRINGS = [_('read'), _('add'), _('delete'), _('update'), _('order')] |
|
17 |
||
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3768
diff
changeset
|
18 |
|
0 | 19 |
class SchemaViewer(object): |
20 |
"""return an ureport layout for some part of a schema""" |
|
21 |
def __init__(self, req=None, encoding=None): |
|
22 |
self.req = req |
|
23 |
if req is not None: |
|
24 |
self.req.add_css('cubicweb.schema.css') |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2126
diff
changeset
|
25 |
self._possible_views = req.vreg['views'].possible_views |
0 | 26 |
if not encoding: |
27 |
encoding = req.encoding |
|
28 |
else: |
|
29 |
self._possible_views = lambda x: () |
|
30 |
self.encoding = encoding |
|
1731
13f948678a6a
delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents:
1522
diff
changeset
|
31 |
|
0 | 32 |
def format_acls(self, schema, access_types): |
33 |
"""return a layout displaying access control lists""" |
|
34 |
data = [self.req._('access type'), self.req._('groups')] |
|
35 |
for access_type in access_types: |
|
36 |
data.append(self.req._(access_type)) |
|
3768
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
37 |
acls = [Link(self.req.build_url('cwgroup/%s' % group), self.req._(group)) |
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
38 |
for group in schema.get_groups(access_type)] |
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
39 |
acls += (Text(rqlexp.expression) for rqlexp in schema.get_rqlexprs(access_type)) |
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
40 |
acls = [n for _n in acls for n in (_n, Text(', '))][:-1] |
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
41 |
data.append(Span(children=acls)) |
0 | 42 |
return Section(children=(Table(cols=2, cheaders=1, rheaders=1, children=data),), |
43 |
klass='acl') |
|
44 |
||
1731
13f948678a6a
delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents:
1522
diff
changeset
|
45 |
|
2126
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1998
diff
changeset
|
46 |
def visit_schema(self, schema, display_relations=0, skiptypes=()): |
0 | 47 |
"""get a layout for a whole schema""" |
48 |
title = Title(self.req._('Schema %s') % schema.name, |
|
49 |
klass='titleUnderline') |
|
50 |
layout = Section(children=(title,)) |
|
51 |
esection = Section(children=(Title(self.req._('Entities'), |
|
52 |
klass='titleUnderline'),)) |
|
53 |
layout.append(esection) |
|
1132 | 54 |
eschemas = [eschema for eschema in schema.entities() |
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
55 |
if not (eschema.final or eschema in skiptypes)] |
1731
13f948678a6a
delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents:
1522
diff
changeset
|
56 |
for eschema in sorted(eschemas): |
2126
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1998
diff
changeset
|
57 |
esection.append(self.visit_entityschema(eschema, skiptypes)) |
0 | 58 |
if display_relations: |
59 |
title = Title(self.req._('Relations'), klass='titleUnderline') |
|
1731
13f948678a6a
delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents:
1522
diff
changeset
|
60 |
rsection = Section(children=(title,)) |
0 | 61 |
layout.append(rsection) |
62 |
relations = [rschema for rschema in schema.relations() |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
63 |
if not (rschema.final or rschema.type in skiptypes)] |
0 | 64 |
keys = [(rschema.type, rschema) for rschema in relations] |
65 |
for key, rschema in sorted(keys): |
|
66 |
relstr = self.visit_relationschema(rschema) |
|
67 |
rsection.append(relstr) |
|
68 |
return layout |
|
69 |
||
70 |
def _entity_attributes_data(self, eschema): |
|
71 |
_ = self.req._ |
|
72 |
data = [_('attribute'), _('type'), _('default'), _('constraints')] |
|
73 |
for rschema, aschema in eschema.attribute_definitions(): |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3768
diff
changeset
|
74 |
rdef = eschema.rdef(rschema) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3768
diff
changeset
|
75 |
if not rdef.may_have_permission('read', self.req): |
0 | 76 |
continue |
77 |
aname = rschema.type |
|
78 |
if aname == 'eid': |
|
1731
13f948678a6a
delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents:
1522
diff
changeset
|
79 |
continue |
0 | 80 |
data.append('%s (%s)' % (aname, _(aname))) |
81 |
data.append(_(aschema.type)) |
|
82 |
defaultval = eschema.default(aname) |
|
83 |
if defaultval is not None: |
|
84 |
default = self.to_string(defaultval) |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3768
diff
changeset
|
85 |
elif rdef.cardinality[0] == '1': |
0 | 86 |
default = _('required field') |
87 |
else: |
|
88 |
default = '' |
|
89 |
data.append(default) |
|
90 |
constraints = rschema.rproperty(eschema.type, aschema.type, |
|
91 |
'constraints') |
|
92 |
data.append(', '.join(str(constr) for constr in constraints)) |
|
93 |
return data |
|
94 |
||
95 |
def eschema_link_url(self, eschema): |
|
1998
12040c090aa4
[views] change schema views: remove view eschema and use tabs for cwetype
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1977
diff
changeset
|
96 |
return self.req.build_url('cwetype/%s' % eschema) |
1731
13f948678a6a
delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents:
1522
diff
changeset
|
97 |
|
0 | 98 |
def rschema_link_url(self, rschema): |
1998
12040c090aa4
[views] change schema views: remove view eschema and use tabs for cwetype
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1977
diff
changeset
|
99 |
return self.req.build_url('cwrtype/%s' % rschema) |
0 | 100 |
|
101 |
def possible_views(self, etype): |
|
102 |
rset = self.req.etype_rset(etype) |
|
103 |
return [v for v in self._possible_views(self.req, rset) |
|
104 |
if v.category != 'startupview'] |
|
105 |
||
106 |
def stereotype(self, name): |
|
107 |
return Span((' <<%s>>' % name,), klass='stereotype') |
|
1731
13f948678a6a
delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents:
1522
diff
changeset
|
108 |
|
2126
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1998
diff
changeset
|
109 |
def visit_entityschema(self, eschema, skiptypes=()): |
0 | 110 |
"""get a layout for an entity schema""" |
111 |
etype = eschema.type |
|
112 |
layout = Section(children=' ', klass='clear') |
|
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2650
diff
changeset
|
113 |
layout.append(Link(etype,' ' , id=etype)) # anchor |
0 | 114 |
title = Link(self.eschema_link_url(eschema), etype) |
2126
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1998
diff
changeset
|
115 |
boxchild = [Section(children=(title, ' (%s)'% eschema.display_name(self.req)), klass='title')] |
3768
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
116 |
table = Table(cols=4, rheaders=1, klass='listing', |
0 | 117 |
children=self._entity_attributes_data(eschema)) |
118 |
boxchild.append(Section(children=(table,), klass='body')) |
|
119 |
data = [] |
|
120 |
data.append(Section(children=boxchild, klass='box')) |
|
121 |
data.append(Section(children='', klass='vl')) |
|
122 |
data.append(Section(children='', klass='hl')) |
|
123 |
t_vars = [] |
|
124 |
rels = [] |
|
125 |
first = True |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3768
diff
changeset
|
126 |
for rschema, targetschemas, role in eschema.relation_definitions(): |
2126
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1998
diff
changeset
|
127 |
if rschema.type in skiptypes: |
0 | 128 |
continue |
129 |
rschemaurl = self.rschema_link_url(rschema) |
|
130 |
for oeschema in targetschemas: |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3768
diff
changeset
|
131 |
rdef = rschema.role_rdef(eschema, oeschema, role) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3768
diff
changeset
|
132 |
if not rdef.may_have_permission('read', self.req): |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3768
diff
changeset
|
133 |
continue |
0 | 134 |
label = rschema.type |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3768
diff
changeset
|
135 |
if role == 'subject': |
0 | 136 |
cards = rschema.rproperty(eschema, oeschema, 'cardinality') |
137 |
else: |
|
138 |
cards = rschema.rproperty(oeschema, eschema, 'cardinality') |
|
139 |
cards = cards[::-1] |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3768
diff
changeset
|
140 |
label = '%s %s (%s) %s' % (CARD_MAP[cards[1]], label, |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3768
diff
changeset
|
141 |
display_name(self.req, label, role), |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3768
diff
changeset
|
142 |
CARD_MAP[cards[0]]) |
0 | 143 |
rlink = Link(rschemaurl, label) |
144 |
elink = Link(self.eschema_link_url(oeschema), oeschema.type) |
|
145 |
if first: |
|
146 |
t_vars.append(Section(children=(elink,), klass='firstvar')) |
|
147 |
rels.append(Section(children=(rlink,), klass='firstrel')) |
|
148 |
first = False |
|
149 |
else: |
|
150 |
t_vars.append(Section(children=(elink,), klass='var')) |
|
151 |
rels.append(Section(children=(rlink,), klass='rel')) |
|
152 |
data.append(Section(children=rels, klass='rels')) |
|
153 |
data.append(Section(children=t_vars, klass='vars')) |
|
154 |
layout.append(Section(children=data, klass='entityAttributes')) |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
155 |
if eschema.final: # stop here for final entities |
0 | 156 |
return layout |
157 |
_ = self.req._ |
|
158 |
if self.req.user.matching_groups('managers'): |
|
1494
d68aac1cda0d
#342695: add new security section to the schema view
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
0
diff
changeset
|
159 |
# layout.append(self.format_acls(eschema, ('read', 'add', 'delete', 'update'))) |
0 | 160 |
# possible views for this entity type |
161 |
views = [_(view.title) for view in self.possible_views(etype)] |
|
162 |
layout.append(Section(children=(Table(cols=1, rheaders=1, |
|
163 |
children=[_('views')]+views),), |
|
164 |
klass='views')) |
|
165 |
return layout |
|
1731
13f948678a6a
delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents:
1522
diff
changeset
|
166 |
|
0 | 167 |
def visit_relationschema(self, rschema, title=True): |
168 |
"""get a layout for a relation schema""" |
|
169 |
_ = self.req._ |
|
3768
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
170 |
if title: |
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
171 |
title = Link(self.rschema_link_url(rschema), rschema.type) |
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
172 |
stereotypes = [] |
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
173 |
if rschema.meta: |
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
174 |
stereotypes.append('meta') |
4467
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
175 |
if rschema.symmetric: |
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
176 |
stereotypes.append('symmetric') |
3768
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
177 |
if rschema.inlined: |
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
178 |
stereotypes.append('inlined') |
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
179 |
title = Section(children=(title, ' (%s)'%rschema.display_name(self.req)), klass='title') |
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
180 |
if stereotypes: |
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
181 |
title.append(self.stereotype(','.join(stereotypes))) |
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
182 |
layout = Section(children=(title,), klass='schema') |
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
183 |
else: |
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
184 |
layout = Section(klass='schema') |
0 | 185 |
data = [_('from'), _('to')] |
186 |
schema = rschema.schema |
|
187 |
rschema_objects = rschema.objects() |
|
188 |
if rschema_objects: |
|
189 |
# might be empty |
|
4087 | 190 |
properties = [p for p in RelationDefinitionSchema.rproperty_defs(rschema_objects[0]) |
0 | 191 |
if not p in ('cardinality', 'composite', 'eid')] |
192 |
else: |
|
193 |
properties = [] |
|
194 |
data += [_(prop) for prop in properties] |
|
195 |
cols = len(data) |
|
196 |
done = set() |
|
197 |
for subjtype, objtypes in rschema.associations(): |
|
198 |
for objtype in objtypes: |
|
199 |
if (subjtype, objtype) in done: |
|
200 |
continue |
|
201 |
done.add((subjtype, objtype)) |
|
4467
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
202 |
if rschema.symmetric: |
0 | 203 |
done.add((objtype, subjtype)) |
204 |
data.append(Link(self.eschema_link_url(schema[subjtype]), subjtype)) |
|
205 |
data.append(Link(self.eschema_link_url(schema[objtype]), objtype)) |
|
4089
ff92c7d692bf
typos, api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4087
diff
changeset
|
206 |
rdef = rschema.rdef(subjtype, objtype) |
0 | 207 |
for prop in properties: |
4087 | 208 |
val = getattr(rdef, prop) |
0 | 209 |
if val is None: |
210 |
val = '' |
|
211 |
elif isinstance(val, (list, tuple)): |
|
212 |
val = ', '.join(str(v) for v in val) |
|
213 |
elif val and isinstance(val, basestring): |
|
214 |
val = _(val) |
|
215 |
else: |
|
216 |
val = str(val) |
|
217 |
data.append(Text(val)) |
|
3768
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
218 |
table = Table(cols=cols, rheaders=1, children=data, klass='listing') |
0 | 219 |
layout.append(Section(children=(table,), klass='relationDefinition')) |
4092
df554ae4203c
avoid failure by simply not displaying this section for now"
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4089
diff
changeset
|
220 |
#if self.req.user.matching_groups('managers'): |
df554ae4203c
avoid failure by simply not displaying this section for now"
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4089
diff
changeset
|
221 |
# layout.append(self.format_acls(rschema, ('read', 'add', 'delete'))) |
0 | 222 |
layout.append(Section(children='', klass='clear')) |
223 |
return layout |
|
224 |
||
225 |
def to_string(self, value): |
|
226 |
"""used to converte arbitrary values to encoded string""" |
|
227 |
if isinstance(value, unicode): |
|
228 |
return value.encode(self.encoding, 'replace') |
|
229 |
return str(value) |