author | Julien Cristau <julien.cristau@logilab.fr> |
Wed, 09 Apr 2014 10:49:18 +0200 | |
changeset 9670 | 5b661f58978c |
parent 7407 | a78cc15227ef |
child 9704 | 43c9896d0e9f |
permissions | -rw-r--r-- |
7152
39c1ffc7d93f
[schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5460
diff
changeset
|
1 |
# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
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
|
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/>. |
7152
39c1ffc7d93f
[schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5460
diff
changeset
|
18 |
"""an helper class to display CubicWeb schema using ureports""" |
0 | 19 |
|
20 |
__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
|
21 |
_ = unicode |
0 | 22 |
|
23 |
from logilab.common.ureports import Section, Title, Table, Link, Span, Text |
|
4087 | 24 |
|
1731
13f948678a6a
delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents:
1522
diff
changeset
|
25 |
from yams.schema2dot import CARD_MAP |
4087 | 26 |
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
|
27 |
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
|
28 |
|
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 |
TYPE_GETTER = attrgetter('type') |
0 | 30 |
|
31 |
I18NSTRINGS = [_('read'), _('add'), _('delete'), _('update'), _('order')] |
|
32 |
||
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3768
diff
changeset
|
33 |
|
0 | 34 |
class SchemaViewer(object): |
35 |
"""return an ureport layout for some part of a schema""" |
|
36 |
def __init__(self, req=None, encoding=None): |
|
37 |
self.req = req |
|
38 |
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
|
39 |
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
|
40 |
if encoding is None: |
0 | 41 |
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
|
42 |
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
|
43 |
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
|
44 |
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
|
45 |
self._ = unicode |
0 | 46 |
self.encoding = encoding |
1731
13f948678a6a
delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents:
1522
diff
changeset
|
47 |
|
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
|
48 |
# 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
|
49 |
|
5460
52ae8f68d9e2
[web/schemaviewer] fix bad call
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5426
diff
changeset
|
50 |
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
|
51 |
"""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
|
52 |
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
|
53 |
""" |
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 |
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
|
55 |
return True |
5460
52ae8f68d9e2
[web/schemaviewer] fix bad call
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5426
diff
changeset
|
56 |
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
|
57 |
|
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 |
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
|
59 |
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
|
60 |
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
|
61 |
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
|
62 |
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
|
63 |
|
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 |
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
|
65 |
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
|
66 |
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
|
67 |
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
|
68 |
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
|
69 |
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
|
70 |
|
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 |
# 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
|
72 |
|
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
|
73 |
def visit_schema(self, schema, display_relations=0, skiptypes=()): |
0 | 74 |
"""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
|
75 |
title = Title(self._('Schema %s') % schema.name, |
0 | 76 |
klass='titleUnderline') |
77 |
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
|
78 |
esection = Section(children=(Title(self._('Entities'), |
0 | 79 |
klass='titleUnderline'),)) |
80 |
layout.append(esection) |
|
1132 | 81 |
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
|
82 |
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
|
83 |
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
|
84 |
esection.append(self.visit_entityschema(eschema, skiptypes)) |
0 | 85 |
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
|
86 |
title = Title(self._('Relations'), klass='titleUnderline') |
1731
13f948678a6a
delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents:
1522
diff
changeset
|
87 |
rsection = Section(children=(title,)) |
0 | 88 |
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
|
89 |
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
|
90 |
if not (rschema.final or rschema.type in skiptypes)] |
0 | 91 |
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
|
92 |
for key, rschema in sorted(keys, cmp=(lambda x, y: cmp(x[1], y[1]))): |
0 | 93 |
relstr = self.visit_relationschema(rschema) |
94 |
rsection.append(relstr) |
|
95 |
return layout |
|
96 |
||
97 |
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
|
98 |
_ = self._ |
0 | 99 |
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
|
100 |
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
|
101 |
for rschema, aschema in attributes: |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3768
diff
changeset
|
102 |
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
|
103 |
if not self.may_read(rdef): |
0 | 104 |
continue |
105 |
aname = rschema.type |
|
106 |
if aname == 'eid': |
|
1731
13f948678a6a
delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents:
1522
diff
changeset
|
107 |
continue |
0 | 108 |
data.append('%s (%s)' % (aname, _(aname))) |
109 |
data.append(_(aschema.type)) |
|
110 |
defaultval = eschema.default(aname) |
|
111 |
if defaultval is not None: |
|
112 |
default = self.to_string(defaultval) |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3768
diff
changeset
|
113 |
elif rdef.cardinality[0] == '1': |
0 | 114 |
default = _('required field') |
115 |
else: |
|
116 |
default = '' |
|
117 |
data.append(default) |
|
118 |
constraints = rschema.rproperty(eschema.type, aschema.type, |
|
119 |
'constraints') |
|
120 |
data.append(', '.join(str(constr) for constr in constraints)) |
|
121 |
return data |
|
122 |
||
123 |
||
124 |
def stereotype(self, name): |
|
125 |
return Span((' <<%s>>' % name,), klass='stereotype') |
|
1731
13f948678a6a
delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents:
1522
diff
changeset
|
126 |
|
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 |
def visit_entityschema(self, eschema, skiptypes=()): |
0 | 128 |
"""get a layout for an entity schema""" |
129 |
etype = eschema.type |
|
130 |
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
|
131 |
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
|
132 |
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
|
133 |
boxchild = [Section(children=(title,), klass='title')] |
0 | 134 |
data = [] |
135 |
data.append(Section(children=boxchild, klass='box')) |
|
136 |
data.append(Section(children='', klass='vl')) |
|
137 |
data.append(Section(children='', klass='hl')) |
|
138 |
t_vars = [] |
|
139 |
rels = [] |
|
140 |
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
|
141 |
|
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 |
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
|
143 |
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
|
144 |
(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
|
145 |
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
|
146 |
if rschema.type in skiptypes: |
0 | 147 |
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
|
148 |
for oeschema in sorted(targetschemas, key=TYPE_GETTER): |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3768
diff
changeset
|
149 |
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
|
150 |
if not self.may_read(rdef): |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3768
diff
changeset
|
151 |
continue |
0 | 152 |
label = rschema.type |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3768
diff
changeset
|
153 |
if role == 'subject': |
7407
a78cc15227ef
[deprecation] drop remaining warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7152
diff
changeset
|
154 |
cards = rschema.rdef(eschema, oeschema).cardinality |
0 | 155 |
else: |
7407
a78cc15227ef
[deprecation] drop remaining warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7152
diff
changeset
|
156 |
cards = rschema.rdef(oeschema, eschema).cardinality |
0 | 157 |
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
|
158 |
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
|
159 |
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
|
160 |
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
|
161 |
elink = self.format_eschema(oeschema) |
0 | 162 |
if first: |
163 |
t_vars.append(Section(children=(elink,), klass='firstvar')) |
|
164 |
rels.append(Section(children=(rlink,), klass='firstrel')) |
|
165 |
first = False |
|
166 |
else: |
|
167 |
t_vars.append(Section(children=(elink,), klass='var')) |
|
168 |
rels.append(Section(children=(rlink,), klass='rel')) |
|
169 |
data.append(Section(children=rels, klass='rels')) |
|
170 |
data.append(Section(children=t_vars, klass='vars')) |
|
171 |
layout.append(Section(children=data, klass='entityAttributes')) |
|
172 |
return layout |
|
1731
13f948678a6a
delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents:
1522
diff
changeset
|
173 |
|
0 | 174 |
def visit_relationschema(self, rschema, title=True): |
175 |
"""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
|
176 |
_ = self._ |
3768
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
177 |
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
|
178 |
title = self.format_rschema(rschema) |
3768
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
179 |
stereotypes = [] |
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
180 |
if rschema.meta: |
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
181 |
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
|
182 |
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
|
183 |
stereotypes.append('symmetric') |
3768
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
184 |
if rschema.inlined: |
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
185 |
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
|
186 |
title = Section(children=(title,), klass='title') |
3768
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
187 |
if stereotypes: |
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
188 |
title.append(self.stereotype(','.join(stereotypes))) |
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
189 |
layout = Section(children=(title,), klass='schema') |
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
190 |
else: |
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
191 |
layout = Section(klass='schema') |
0 | 192 |
data = [_('from'), _('to')] |
193 |
schema = rschema.schema |
|
194 |
rschema_objects = rschema.objects() |
|
195 |
if rschema_objects: |
|
196 |
# might be empty |
|
4087 | 197 |
properties = [p for p in RelationDefinitionSchema.rproperty_defs(rschema_objects[0]) |
0 | 198 |
if not p in ('cardinality', 'composite', 'eid')] |
199 |
else: |
|
200 |
properties = [] |
|
201 |
data += [_(prop) for prop in properties] |
|
202 |
cols = len(data) |
|
203 |
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
|
204 |
for subjtype, objtypes in sorted(rschema.associations()): |
0 | 205 |
for objtype in objtypes: |
206 |
if (subjtype, objtype) in done: |
|
207 |
continue |
|
208 |
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
|
209 |
if rschema.symmetric: |
0 | 210 |
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
|
211 |
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
|
212 |
data.append(self.format_eschema(schema[objtype])) |
4089
ff92c7d692bf
typos, api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4087
diff
changeset
|
213 |
rdef = rschema.rdef(subjtype, objtype) |
0 | 214 |
for prop in properties: |
4087 | 215 |
val = getattr(rdef, prop) |
0 | 216 |
if val is None: |
217 |
val = '' |
|
5234
e2476d78b060
ticket #787103, relation schema view enhancements
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
5233
diff
changeset
|
218 |
elif prop == 'constraints': |
7152
39c1ffc7d93f
[schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5460
diff
changeset
|
219 |
val = ', '.join([c.expression 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
|
220 |
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
|
221 |
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
|
222 |
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
|
223 |
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
|
224 |
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
|
225 |
|
0 | 226 |
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
|
227 |
val = sorted(val) |
0 | 228 |
val = ', '.join(str(v) for v in val) |
229 |
elif val and isinstance(val, basestring): |
|
230 |
val = _(val) |
|
231 |
else: |
|
232 |
val = str(val) |
|
233 |
data.append(Text(val)) |
|
3768
8c85a2f7f5ad
nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
234 |
table = Table(cols=cols, rheaders=1, children=data, klass='listing') |
0 | 235 |
layout.append(Section(children=(table,), klass='relationDefinition')) |
236 |
layout.append(Section(children='', klass='clear')) |
|
237 |
return layout |
|
238 |
||
239 |
def to_string(self, value): |
|
240 |
"""used to converte arbitrary values to encoded string""" |
|
241 |
if isinstance(value, unicode): |
|
242 |
return value.encode(self.encoding, 'replace') |
|
243 |
return str(value) |