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