author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Wed, 23 Sep 2009 12:30:56 +0200 | |
changeset 3421 | 7fec79c1c11f |
parent 2996 | 866a2c135c33 |
child 3689 | deb13e88e037 |
permissions | -rw-r--r-- |
0 | 1 |
"""an helper class to display CubicWeb schema using ureports |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1942
diff
changeset
|
4 |
:copyright: 2001-2009 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)) |
|
34 |
acls = [self.req._(group) for group in schema.get_groups(access_type)] |
|
35 |
acls += (rqlexp.expression for rqlexp in schema.get_rqlexprs(access_type)) |
|
36 |
data.append(', '.join(acls)) |
|
37 |
return Section(children=(Table(cols=2, cheaders=1, rheaders=1, children=data),), |
|
38 |
klass='acl') |
|
39 |
||
1731
13f948678a6a
delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents:
1522
diff
changeset
|
40 |
|
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
|
41 |
def visit_schema(self, schema, display_relations=0, skiptypes=()): |
0 | 42 |
"""get a layout for a whole schema""" |
43 |
title = Title(self.req._('Schema %s') % schema.name, |
|
44 |
klass='titleUnderline') |
|
45 |
layout = Section(children=(title,)) |
|
46 |
esection = Section(children=(Title(self.req._('Entities'), |
|
47 |
klass='titleUnderline'),)) |
|
48 |
layout.append(esection) |
|
1132 | 49 |
eschemas = [eschema for eschema in schema.entities() |
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
|
50 |
if not (eschema.is_final() or eschema in skiptypes)] |
1731
13f948678a6a
delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents:
1522
diff
changeset
|
51 |
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
|
52 |
esection.append(self.visit_entityschema(eschema, skiptypes)) |
0 | 53 |
if display_relations: |
54 |
title = Title(self.req._('Relations'), klass='titleUnderline') |
|
1731
13f948678a6a
delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents:
1522
diff
changeset
|
55 |
rsection = Section(children=(title,)) |
0 | 56 |
layout.append(rsection) |
57 |
relations = [rschema for rschema in schema.relations() |
|
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
|
58 |
if not (rschema.is_final() or rschema.type in skiptypes)] |
0 | 59 |
keys = [(rschema.type, rschema) for rschema in relations] |
60 |
for key, rschema in sorted(keys): |
|
61 |
relstr = self.visit_relationschema(rschema) |
|
62 |
rsection.append(relstr) |
|
63 |
return layout |
|
64 |
||
65 |
def _entity_attributes_data(self, eschema): |
|
66 |
_ = self.req._ |
|
67 |
data = [_('attribute'), _('type'), _('default'), _('constraints')] |
|
68 |
for rschema, aschema in eschema.attribute_definitions(): |
|
69 |
if not (rschema.has_local_role('read') or rschema.has_perm(self.req, 'read')): |
|
70 |
continue |
|
71 |
aname = rschema.type |
|
72 |
if aname == 'eid': |
|
1731
13f948678a6a
delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents:
1522
diff
changeset
|
73 |
continue |
0 | 74 |
data.append('%s (%s)' % (aname, _(aname))) |
75 |
data.append(_(aschema.type)) |
|
76 |
defaultval = eschema.default(aname) |
|
77 |
if defaultval is not None: |
|
78 |
default = self.to_string(defaultval) |
|
79 |
elif eschema.rproperty(rschema, 'cardinality')[0] == '1': |
|
80 |
default = _('required field') |
|
81 |
else: |
|
82 |
default = '' |
|
83 |
data.append(default) |
|
84 |
constraints = rschema.rproperty(eschema.type, aschema.type, |
|
85 |
'constraints') |
|
86 |
data.append(', '.join(str(constr) for constr in constraints)) |
|
87 |
return data |
|
88 |
||
89 |
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
|
90 |
return self.req.build_url('cwetype/%s' % eschema) |
1731
13f948678a6a
delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents:
1522
diff
changeset
|
91 |
|
0 | 92 |
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
|
93 |
return self.req.build_url('cwrtype/%s' % rschema) |
0 | 94 |
|
95 |
def possible_views(self, etype): |
|
96 |
rset = self.req.etype_rset(etype) |
|
97 |
return [v for v in self._possible_views(self.req, rset) |
|
98 |
if v.category != 'startupview'] |
|
99 |
||
100 |
def stereotype(self, name): |
|
101 |
return Span((' <<%s>>' % name,), klass='stereotype') |
|
1731
13f948678a6a
delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents:
1522
diff
changeset
|
102 |
|
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
|
103 |
def visit_entityschema(self, eschema, skiptypes=()): |
0 | 104 |
"""get a layout for an entity schema""" |
105 |
etype = eschema.type |
|
106 |
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
|
107 |
layout.append(Link(etype,' ' , id=etype)) # anchor |
0 | 108 |
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
|
109 |
boxchild = [Section(children=(title, ' (%s)'% eschema.display_name(self.req)), klass='title')] |
1731
13f948678a6a
delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents:
1522
diff
changeset
|
110 |
table = Table(cols=4, rheaders=1, |
0 | 111 |
children=self._entity_attributes_data(eschema)) |
112 |
boxchild.append(Section(children=(table,), klass='body')) |
|
113 |
data = [] |
|
114 |
data.append(Section(children=boxchild, klass='box')) |
|
115 |
data.append(Section(children='', klass='vl')) |
|
116 |
data.append(Section(children='', klass='hl')) |
|
117 |
t_vars = [] |
|
118 |
rels = [] |
|
119 |
first = True |
|
120 |
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
|
121 |
if rschema.type in skiptypes: |
0 | 122 |
continue |
123 |
if not (rschema.has_local_role('read') or rschema.has_perm(self.req, 'read')): |
|
124 |
continue |
|
125 |
rschemaurl = self.rschema_link_url(rschema) |
|
126 |
for oeschema in targetschemas: |
|
127 |
label = rschema.type |
|
128 |
if x == 'subject': |
|
129 |
cards = rschema.rproperty(eschema, oeschema, 'cardinality') |
|
130 |
else: |
|
131 |
cards = rschema.rproperty(oeschema, eschema, 'cardinality') |
|
132 |
cards = cards[::-1] |
|
133 |
label = '%s %s (%s) %s' % (CARD_MAP[cards[1]], label, display_name(self.req, label, x), CARD_MAP[cards[0]]) |
|
134 |
rlink = Link(rschemaurl, label) |
|
135 |
elink = Link(self.eschema_link_url(oeschema), oeschema.type) |
|
136 |
if first: |
|
137 |
t_vars.append(Section(children=(elink,), klass='firstvar')) |
|
138 |
rels.append(Section(children=(rlink,), klass='firstrel')) |
|
139 |
first = False |
|
140 |
else: |
|
141 |
t_vars.append(Section(children=(elink,), klass='var')) |
|
142 |
rels.append(Section(children=(rlink,), klass='rel')) |
|
143 |
data.append(Section(children=rels, klass='rels')) |
|
144 |
data.append(Section(children=t_vars, klass='vars')) |
|
145 |
layout.append(Section(children=data, klass='entityAttributes')) |
|
146 |
if eschema.is_final(): # stop here for final entities |
|
147 |
return layout |
|
148 |
_ = self.req._ |
|
149 |
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
|
150 |
# layout.append(self.format_acls(eschema, ('read', 'add', 'delete', 'update'))) |
0 | 151 |
# possible views for this entity type |
152 |
views = [_(view.title) for view in self.possible_views(etype)] |
|
153 |
layout.append(Section(children=(Table(cols=1, rheaders=1, |
|
154 |
children=[_('views')]+views),), |
|
155 |
klass='views')) |
|
156 |
return layout |
|
1731
13f948678a6a
delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents:
1522
diff
changeset
|
157 |
|
0 | 158 |
def visit_relationschema(self, rschema, title=True): |
159 |
"""get a layout for a relation schema""" |
|
160 |
_ = self.req._ |
|
161 |
title = Link(self.rschema_link_url(rschema), rschema.type) |
|
162 |
stereotypes = [] |
|
163 |
if rschema.meta: |
|
164 |
stereotypes.append('meta') |
|
165 |
if rschema.symetric: |
|
166 |
stereotypes.append('symetric') |
|
167 |
if rschema.inlined: |
|
168 |
stereotypes.append('inlined') |
|
169 |
title = Section(children=(title, ' (%s)'%rschema.display_name(self.req)), klass='title') |
|
170 |
if stereotypes: |
|
171 |
title.append(self.stereotype(','.join(stereotypes))) |
|
172 |
layout = Section(children=(title,), klass='schema') |
|
173 |
data = [_('from'), _('to')] |
|
174 |
schema = rschema.schema |
|
175 |
rschema_objects = rschema.objects() |
|
176 |
if rschema_objects: |
|
177 |
# might be empty |
|
178 |
properties = [p for p in rschema.rproperty_defs(rschema_objects[0]) |
|
179 |
if not p in ('cardinality', 'composite', 'eid')] |
|
180 |
else: |
|
181 |
properties = [] |
|
182 |
data += [_(prop) for prop in properties] |
|
183 |
cols = len(data) |
|
184 |
done = set() |
|
185 |
for subjtype, objtypes in rschema.associations(): |
|
186 |
for objtype in objtypes: |
|
187 |
if (subjtype, objtype) in done: |
|
188 |
continue |
|
189 |
done.add((subjtype, objtype)) |
|
190 |
if rschema.symetric: |
|
191 |
done.add((objtype, subjtype)) |
|
192 |
data.append(Link(self.eschema_link_url(schema[subjtype]), subjtype)) |
|
193 |
data.append(Link(self.eschema_link_url(schema[objtype]), objtype)) |
|
194 |
for prop in properties: |
|
195 |
val = rschema.rproperty(subjtype, objtype, prop) |
|
196 |
if val is None: |
|
197 |
val = '' |
|
198 |
elif isinstance(val, (list, tuple)): |
|
199 |
val = ', '.join(str(v) for v in val) |
|
200 |
elif val and isinstance(val, basestring): |
|
201 |
val = _(val) |
|
202 |
else: |
|
203 |
val = str(val) |
|
204 |
data.append(Text(val)) |
|
205 |
table = Table(cols=cols, rheaders=1, children=data) |
|
206 |
layout.append(Section(children=(table,), klass='relationDefinition')) |
|
207 |
if not self.req.cnx.anonymous_connection: |
|
208 |
layout.append(self.format_acls(rschema, ('read', 'add', 'delete'))) |
|
209 |
layout.append(Section(children='', klass='clear')) |
|
210 |
return layout |
|
211 |
||
212 |
def to_string(self, value): |
|
213 |
"""used to converte arbitrary values to encoded string""" |
|
214 |
if isinstance(value, unicode): |
|
215 |
return value.encode(self.encoding, 'replace') |
|
216 |
return str(value) |