author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 14 Apr 2010 08:49:23 +0200 | |
changeset 5239 | 471554b842d2 |
parent 5234 | schemaviewer.py@e2476d78b060 |
child 5372 | b74eed7e8b37 |
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') |
|
25 |
if not encoding: |
|
26 |
encoding = req.encoding |
|
27 |
self.encoding = encoding |
|
1731
13f948678a6a
delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents:
1522
diff
changeset
|
28 |
|
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
|
29 |
def visit_schema(self, schema, display_relations=0, skiptypes=()): |
0 | 30 |
"""get a layout for a whole schema""" |
31 |
title = Title(self.req._('Schema %s') % schema.name, |
|
32 |
klass='titleUnderline') |
|
33 |
layout = Section(children=(title,)) |
|
34 |
esection = Section(children=(Title(self.req._('Entities'), |
|
35 |
klass='titleUnderline'),)) |
|
36 |
layout.append(esection) |
|
1132 | 37 |
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
|
38 |
if not (eschema.final or eschema in skiptypes)] |
1731
13f948678a6a
delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents:
1522
diff
changeset
|
39 |
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
|
40 |
esection.append(self.visit_entityschema(eschema, skiptypes)) |
0 | 41 |
if display_relations: |
42 |
title = Title(self.req._('Relations'), klass='titleUnderline') |
|
1731
13f948678a6a
delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents:
1522
diff
changeset
|
43 |
rsection = Section(children=(title,)) |
0 | 44 |
layout.append(rsection) |
45 |
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
|
46 |
if not (rschema.final or rschema.type in skiptypes)] |
0 | 47 |
keys = [(rschema.type, rschema) for rschema in relations] |
48 |
for key, rschema in sorted(keys): |
|
49 |
relstr = self.visit_relationschema(rschema) |
|
50 |
rsection.append(relstr) |
|
51 |
return layout |
|
52 |
||
53 |
def _entity_attributes_data(self, eschema): |
|
54 |
_ = self.req._ |
|
55 |
data = [_('attribute'), _('type'), _('default'), _('constraints')] |
|
56 |
for rschema, aschema in eschema.attribute_definitions(): |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3768
diff
changeset
|
57 |
rdef = eschema.rdef(rschema) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3768
diff
changeset
|
58 |
if not rdef.may_have_permission('read', self.req): |
0 | 59 |
continue |
60 |
aname = rschema.type |
|
61 |
if aname == 'eid': |
|
1731
13f948678a6a
delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents:
1522
diff
changeset
|
62 |
continue |
0 | 63 |
data.append('%s (%s)' % (aname, _(aname))) |
64 |
data.append(_(aschema.type)) |
|
65 |
defaultval = eschema.default(aname) |
|
66 |
if defaultval is not None: |
|
67 |
default = self.to_string(defaultval) |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3768
diff
changeset
|
68 |
elif rdef.cardinality[0] == '1': |
0 | 69 |
default = _('required field') |
70 |
else: |
|
71 |
default = '' |
|
72 |
data.append(default) |
|
73 |
constraints = rschema.rproperty(eschema.type, aschema.type, |
|
74 |
'constraints') |
|
75 |
data.append(', '.join(str(constr) for constr in constraints)) |
|
76 |
return data |
|
77 |
||
78 |
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
|
79 |
return self.req.build_url('cwetype/%s' % eschema) |
1731
13f948678a6a
delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents:
1522
diff
changeset
|
80 |
|
0 | 81 |
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
|
82 |
return self.req.build_url('cwrtype/%s' % rschema) |
0 | 83 |
|
84 |
def stereotype(self, name): |
|
85 |
return Span((' <<%s>>' % name,), klass='stereotype') |
|
1731
13f948678a6a
delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents:
1522
diff
changeset
|
86 |
|
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
|
87 |
def visit_entityschema(self, eschema, skiptypes=()): |
0 | 88 |
"""get a layout for an entity schema""" |
89 |
etype = eschema.type |
|
90 |
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
|
91 |
layout.append(Link(etype,' ' , id=etype)) # anchor |
0 | 92 |
title = Link(self.eschema_link_url(eschema), etype) |
5239
471554b842d2
[schema view] the final touch. Things are getting nicely displayed, and code clean
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5234
diff
changeset
|
93 |
boxchild = [Section(children=(title,), klass='title')] |
0 | 94 |
data = [] |
95 |
data.append(Section(children=boxchild, klass='box')) |
|
96 |
data.append(Section(children='', klass='vl')) |
|
97 |
data.append(Section(children='', klass='hl')) |
|
98 |
t_vars = [] |
|
99 |
rels = [] |
|
100 |
first = True |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3768
diff
changeset
|
101 |
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
|
102 |
if rschema.type in skiptypes: |
0 | 103 |
continue |
104 |
rschemaurl = self.rschema_link_url(rschema) |
|
105 |
for oeschema in targetschemas: |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3768
diff
changeset
|
106 |
rdef = rschema.role_rdef(eschema, oeschema, role) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3768
diff
changeset
|
107 |
if not rdef.may_have_permission('read', self.req): |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3768
diff
changeset
|
108 |
continue |
0 | 109 |
label = rschema.type |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3768
diff
changeset
|
110 |
if role == 'subject': |
0 | 111 |
cards = rschema.rproperty(eschema, oeschema, 'cardinality') |
112 |
else: |
|
113 |
cards = rschema.rproperty(oeschema, eschema, 'cardinality') |
|
114 |
cards = cards[::-1] |
|
5239
471554b842d2
[schema view] the final touch. Things are getting nicely displayed, and code clean
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5234
diff
changeset
|
115 |
label = '%s %s %s' % (CARD_MAP[cards[1]], label, |
471554b842d2
[schema view] the final touch. Things are getting nicely displayed, and code clean
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5234
diff
changeset
|
116 |
CARD_MAP[cards[0]]) |
0 | 117 |
rlink = Link(rschemaurl, label) |
118 |
elink = Link(self.eschema_link_url(oeschema), oeschema.type) |
|
119 |
if first: |
|
120 |
t_vars.append(Section(children=(elink,), klass='firstvar')) |
|
121 |
rels.append(Section(children=(rlink,), klass='firstrel')) |
|
122 |
first = False |
|
123 |
else: |
|
124 |
t_vars.append(Section(children=(elink,), klass='var')) |
|
125 |
rels.append(Section(children=(rlink,), klass='rel')) |
|
126 |
data.append(Section(children=rels, klass='rels')) |
|
127 |
data.append(Section(children=t_vars, klass='vars')) |
|
128 |
layout.append(Section(children=data, klass='entityAttributes')) |
|
129 |
return layout |
|
1731
13f948678a6a
delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents:
1522
diff
changeset
|
130 |
|
0 | 131 |
def visit_relationschema(self, rschema, title=True): |
132 |
"""get a layout for a relation schema""" |
|
133 |
_ = self.req._ |
|
3768
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
134 |
if title: |
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
135 |
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
|
136 |
stereotypes = [] |
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
137 |
if rschema.meta: |
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
138 |
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
|
139 |
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
|
140 |
stereotypes.append('symmetric') |
3768
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
141 |
if rschema.inlined: |
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
142 |
stereotypes.append('inlined') |
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
143 |
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
|
144 |
if stereotypes: |
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
145 |
title.append(self.stereotype(','.join(stereotypes))) |
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
146 |
layout = Section(children=(title,), klass='schema') |
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
147 |
else: |
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
148 |
layout = Section(klass='schema') |
0 | 149 |
data = [_('from'), _('to')] |
150 |
schema = rschema.schema |
|
151 |
rschema_objects = rschema.objects() |
|
152 |
if rschema_objects: |
|
153 |
# might be empty |
|
4087 | 154 |
properties = [p for p in RelationDefinitionSchema.rproperty_defs(rschema_objects[0]) |
0 | 155 |
if not p in ('cardinality', 'composite', 'eid')] |
156 |
else: |
|
157 |
properties = [] |
|
158 |
data += [_(prop) for prop in properties] |
|
159 |
cols = len(data) |
|
160 |
done = set() |
|
161 |
for subjtype, objtypes in rschema.associations(): |
|
162 |
for objtype in objtypes: |
|
163 |
if (subjtype, objtype) in done: |
|
164 |
continue |
|
165 |
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
|
166 |
if rschema.symmetric: |
0 | 167 |
done.add((objtype, subjtype)) |
168 |
data.append(Link(self.eschema_link_url(schema[subjtype]), subjtype)) |
|
169 |
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
|
170 |
rdef = rschema.rdef(subjtype, objtype) |
0 | 171 |
for prop in properties: |
4087 | 172 |
val = getattr(rdef, prop) |
0 | 173 |
if val is None: |
174 |
val = '' |
|
5234
e2476d78b060
ticket #787103, relation schema view enhancements
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
5233
diff
changeset
|
175 |
elif prop == 'constraints': |
e2476d78b060
ticket #787103, relation schema view enhancements
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
5233
diff
changeset
|
176 |
val = ', '.join([c.restriction for c in val]) |
0 | 177 |
elif isinstance(val, (list, tuple)): |
178 |
val = ', '.join(str(v) for v in val) |
|
179 |
elif val and isinstance(val, basestring): |
|
180 |
val = _(val) |
|
181 |
else: |
|
182 |
val = str(val) |
|
183 |
data.append(Text(val)) |
|
3768
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
184 |
table = Table(cols=cols, rheaders=1, children=data, klass='listing') |
0 | 185 |
layout.append(Section(children=(table,), klass='relationDefinition')) |
186 |
layout.append(Section(children='', klass='clear')) |
|
187 |
return layout |
|
188 |
||
189 |
def to_string(self, value): |
|
190 |
"""used to converte arbitrary values to encoded string""" |
|
191 |
if isinstance(value, unicode): |
|
192 |
return value.encode(self.encoding, 'replace') |
|
193 |
return str(value) |