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