author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 05 Jul 2010 12:04:32 +0200 | |
changeset 5877 | 0c7b7b76a84f |
parent 5460 | 52ae8f68d9e2 |
child 7152 | 39c1ffc7d93f |
permissions | -rw-r--r-- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4467
diff
changeset
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4467
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4467
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4467
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4467
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4467
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4467
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4467
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4467
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4467
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4467
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4467
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4467
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4467
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4467
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4467
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
0 | 18 |
"""an helper class to display CubicWeb schema using ureports |
19 |
||
20 |
""" |
|
21 |
__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
|
22 |
_ = unicode |
0 | 23 |
|
24 |
from logilab.common.ureports import Section, Title, Table, Link, Span, Text |
|
4087 | 25 |
|
1731
13f948678a6a
delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents:
1522
diff
changeset
|
26 |
from yams.schema2dot import CARD_MAP |
4087 | 27 |
from yams.schema import RelationDefinitionSchema |
5372
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
28 |
from operator import attrgetter |
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
29 |
|
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
30 |
TYPE_GETTER = attrgetter('type') |
0 | 31 |
|
32 |
I18NSTRINGS = [_('read'), _('add'), _('delete'), _('update'), _('order')] |
|
33 |
||
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3768
diff
changeset
|
34 |
|
0 | 35 |
class SchemaViewer(object): |
36 |
"""return an ureport layout for some part of a schema""" |
|
37 |
def __init__(self, req=None, encoding=None): |
|
38 |
self.req = req |
|
39 |
if req is not None: |
|
5372
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
40 |
req.add_css('cubicweb.schema.css') |
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
41 |
if encoding is None: |
0 | 42 |
encoding = req.encoding |
5372
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
43 |
self._ = req._ |
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
44 |
else: |
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
45 |
encoding = 'ascii' |
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
46 |
self._ = unicode |
0 | 47 |
self.encoding = encoding |
1731
13f948678a6a
delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents:
1522
diff
changeset
|
48 |
|
5372
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
49 |
# no self.req managements |
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
50 |
|
5460
52ae8f68d9e2
[web/schemaviewer] fix bad call
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5426
diff
changeset
|
51 |
def may_read(self, rdef, action='read'): |
5372
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
52 |
"""Return true if request user may read the given schema. |
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
53 |
Always return True when no request is provided. |
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
54 |
""" |
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
55 |
if self.req is None: |
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
56 |
return True |
5460
52ae8f68d9e2
[web/schemaviewer] fix bad call
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5426
diff
changeset
|
57 |
return rdef.may_have_permission(action, self.req) |
5372
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
58 |
|
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
59 |
def format_eschema(self, eschema): |
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
60 |
text = eschema.type |
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
61 |
if self.req is None: |
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
62 |
return Text(text) |
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
63 |
return Link(self.req.build_url('cwetype/%s' % eschema), text) |
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
64 |
|
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
65 |
def format_rschema(self, rschema, label=None): |
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
66 |
if label is None: |
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
67 |
label = rschema.type |
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
68 |
if self.req is None: |
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
69 |
return Text(label) |
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
70 |
return Link(self.req.build_url('cwrtype/%s' % rschema), label) |
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
71 |
|
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
72 |
# end of no self.req managements |
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
73 |
|
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
|
74 |
def visit_schema(self, schema, display_relations=0, skiptypes=()): |
0 | 75 |
"""get a layout for a whole schema""" |
5372
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
76 |
title = Title(self._('Schema %s') % schema.name, |
0 | 77 |
klass='titleUnderline') |
78 |
layout = Section(children=(title,)) |
|
5372
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
79 |
esection = Section(children=(Title(self._('Entities'), |
0 | 80 |
klass='titleUnderline'),)) |
81 |
layout.append(esection) |
|
1132 | 82 |
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
|
83 |
if not (eschema.final or eschema in skiptypes)] |
5372
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
84 |
for eschema in sorted(eschemas, key=TYPE_GETTER): |
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
|
85 |
esection.append(self.visit_entityschema(eschema, skiptypes)) |
0 | 86 |
if display_relations: |
5372
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
87 |
title = Title(self._('Relations'), klass='titleUnderline') |
1731
13f948678a6a
delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents:
1522
diff
changeset
|
88 |
rsection = Section(children=(title,)) |
0 | 89 |
layout.append(rsection) |
5372
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
90 |
relations = [rschema for rschema in sorted(schema.relations(), key=TYPE_GETTER) |
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
91 |
if not (rschema.final or rschema.type in skiptypes)] |
0 | 92 |
keys = [(rschema.type, rschema) for rschema in relations] |
5372
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
93 |
for key, rschema in sorted(keys, cmp=(lambda x, y: cmp(x[1], y[1]))): |
0 | 94 |
relstr = self.visit_relationschema(rschema) |
95 |
rsection.append(relstr) |
|
96 |
return layout |
|
97 |
||
98 |
def _entity_attributes_data(self, eschema): |
|
5372
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
99 |
_ = self._ |
0 | 100 |
data = [_('attribute'), _('type'), _('default'), _('constraints')] |
5372
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
101 |
attributes = sorted(eschema.attribute_definitions(), cmp=(lambda x, y: cmp(x[0].type, y[0].type))) |
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
102 |
for rschema, aschema in attributes: |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3768
diff
changeset
|
103 |
rdef = eschema.rdef(rschema) |
5372
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
104 |
if not self.may_read(rdef): |
0 | 105 |
continue |
106 |
aname = rschema.type |
|
107 |
if aname == 'eid': |
|
1731
13f948678a6a
delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents:
1522
diff
changeset
|
108 |
continue |
0 | 109 |
data.append('%s (%s)' % (aname, _(aname))) |
110 |
data.append(_(aschema.type)) |
|
111 |
defaultval = eschema.default(aname) |
|
112 |
if defaultval is not None: |
|
113 |
default = self.to_string(defaultval) |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3768
diff
changeset
|
114 |
elif rdef.cardinality[0] == '1': |
0 | 115 |
default = _('required field') |
116 |
else: |
|
117 |
default = '' |
|
118 |
data.append(default) |
|
119 |
constraints = rschema.rproperty(eschema.type, aschema.type, |
|
120 |
'constraints') |
|
121 |
data.append(', '.join(str(constr) for constr in constraints)) |
|
122 |
return data |
|
123 |
||
124 |
||
125 |
def stereotype(self, name): |
|
126 |
return Span((' <<%s>>' % name,), klass='stereotype') |
|
1731
13f948678a6a
delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents:
1522
diff
changeset
|
127 |
|
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
|
128 |
def visit_entityschema(self, eschema, skiptypes=()): |
0 | 129 |
"""get a layout for an entity schema""" |
130 |
etype = eschema.type |
|
131 |
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
|
132 |
layout.append(Link(etype,' ' , id=etype)) # anchor |
5372
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
133 |
title = self.format_eschema(eschema) |
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
|
134 |
boxchild = [Section(children=(title,), klass='title')] |
0 | 135 |
data = [] |
136 |
data.append(Section(children=boxchild, klass='box')) |
|
137 |
data.append(Section(children='', klass='vl')) |
|
138 |
data.append(Section(children='', klass='hl')) |
|
139 |
t_vars = [] |
|
140 |
rels = [] |
|
141 |
first = True |
|
5372
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
142 |
|
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
143 |
rel_defs = sorted(eschema.relation_definitions(), |
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
144 |
cmp=(lambda x, y: cmp((x[0].type, x[0].cardinality), |
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
145 |
(y[0].type, y[0].cardinality)))) |
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
146 |
for rschema, targetschemas, role in rel_defs: |
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
|
147 |
if rschema.type in skiptypes: |
0 | 148 |
continue |
5372
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
149 |
for oeschema in sorted(targetschemas, key=TYPE_GETTER): |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3768
diff
changeset
|
150 |
rdef = rschema.role_rdef(eschema, oeschema, role) |
5372
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
151 |
if not self.may_read(rdef): |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3768
diff
changeset
|
152 |
continue |
0 | 153 |
label = rschema.type |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3768
diff
changeset
|
154 |
if role == 'subject': |
0 | 155 |
cards = rschema.rproperty(eschema, oeschema, 'cardinality') |
156 |
else: |
|
157 |
cards = rschema.rproperty(oeschema, eschema, 'cardinality') |
|
158 |
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
|
159 |
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
|
160 |
CARD_MAP[cards[0]]) |
5372
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
161 |
rlink = self.format_rschema(rschema, label) |
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
162 |
elink = self.format_eschema(oeschema) |
0 | 163 |
if first: |
164 |
t_vars.append(Section(children=(elink,), klass='firstvar')) |
|
165 |
rels.append(Section(children=(rlink,), klass='firstrel')) |
|
166 |
first = False |
|
167 |
else: |
|
168 |
t_vars.append(Section(children=(elink,), klass='var')) |
|
169 |
rels.append(Section(children=(rlink,), klass='rel')) |
|
170 |
data.append(Section(children=rels, klass='rels')) |
|
171 |
data.append(Section(children=t_vars, klass='vars')) |
|
172 |
layout.append(Section(children=data, klass='entityAttributes')) |
|
173 |
return layout |
|
1731
13f948678a6a
delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents:
1522
diff
changeset
|
174 |
|
0 | 175 |
def visit_relationschema(self, rschema, title=True): |
176 |
"""get a layout for a relation schema""" |
|
5372
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
177 |
_ = self._ |
3768
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
178 |
if title: |
5372
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
179 |
title = self.format_rschema(rschema) |
3768
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
180 |
stereotypes = [] |
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
181 |
if rschema.meta: |
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
182 |
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
|
183 |
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
|
184 |
stereotypes.append('symmetric') |
3768
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
185 |
if rschema.inlined: |
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
186 |
stereotypes.append('inlined') |
5372
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
187 |
title = Section(children=(title,), klass='title') |
3768
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
188 |
if stereotypes: |
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
189 |
title.append(self.stereotype(','.join(stereotypes))) |
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
190 |
layout = Section(children=(title,), klass='schema') |
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
191 |
else: |
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
192 |
layout = Section(klass='schema') |
0 | 193 |
data = [_('from'), _('to')] |
194 |
schema = rschema.schema |
|
195 |
rschema_objects = rschema.objects() |
|
196 |
if rschema_objects: |
|
197 |
# might be empty |
|
4087 | 198 |
properties = [p for p in RelationDefinitionSchema.rproperty_defs(rschema_objects[0]) |
0 | 199 |
if not p in ('cardinality', 'composite', 'eid')] |
200 |
else: |
|
201 |
properties = [] |
|
202 |
data += [_(prop) for prop in properties] |
|
203 |
cols = len(data) |
|
204 |
done = set() |
|
5372
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
205 |
for subjtype, objtypes in sorted(rschema.associations()): |
0 | 206 |
for objtype in objtypes: |
207 |
if (subjtype, objtype) in done: |
|
208 |
continue |
|
209 |
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
|
210 |
if rschema.symmetric: |
0 | 211 |
done.add((objtype, subjtype)) |
5372
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
212 |
data.append(self.format_eschema(schema[subjtype])) |
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
213 |
data.append(self.format_eschema(schema[objtype])) |
4089
ff92c7d692bf
typos, api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4087
diff
changeset
|
214 |
rdef = rschema.rdef(subjtype, objtype) |
0 | 215 |
for prop in properties: |
4087 | 216 |
val = getattr(rdef, prop) |
0 | 217 |
if val is None: |
218 |
val = '' |
|
5234
e2476d78b060
ticket #787103, relation schema view enhancements
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
5233
diff
changeset
|
219 |
elif prop == 'constraints': |
e2476d78b060
ticket #787103, relation schema view enhancements
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
5233
diff
changeset
|
220 |
val = ', '.join([c.restriction for c in val]) |
5372
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
221 |
elif isinstance(val, dict): |
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
222 |
for key, value in val.iteritems(): |
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
223 |
if isinstance(value, (list, tuple)): |
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
224 |
val[key] = ', '.join(sorted( str(v) for v in value)) |
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
225 |
val = str(val) |
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
226 |
|
0 | 227 |
elif isinstance(val, (list, tuple)): |
5372
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5239
diff
changeset
|
228 |
val = sorted(val) |
0 | 229 |
val = ', '.join(str(v) for v in val) |
230 |
elif val and isinstance(val, basestring): |
|
231 |
val = _(val) |
|
232 |
else: |
|
233 |
val = str(val) |
|
234 |
data.append(Text(val)) |
|
3768
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
235 |
table = Table(cols=cols, rheaders=1, children=data, klass='listing') |
0 | 236 |
layout.append(Section(children=(table,), klass='relationDefinition')) |
237 |
layout.append(Section(children='', klass='clear')) |
|
238 |
return layout |
|
239 |
||
240 |
def to_string(self, value): |
|
241 |
"""used to converte arbitrary values to encoded string""" |
|
242 |
if isinstance(value, unicode): |
|
243 |
return value.encode(self.encoding, 'replace') |
|
244 |
return str(value) |