author | sylvain.thenault@logilab.fr |
Tue, 28 Apr 2009 11:22:31 +0200 | |
branch | tls-sprint |
changeset 1498 | 2c6eec0b46b9 |
parent 1398 | 5fe84a5f7035 |
child 1522 | 47b2ffbee760 |
permissions | -rw-r--r-- |
0 | 1 |
"""Set of HTML startup views. A startup view is global, e.g. doesn't |
2 |
apply to a result set. |
|
3 |
||
4 |
:organization: Logilab |
|
513
907c18c01c60
while EntityView has been used here?? (changeset 501 by laure)
sylvain.thenault@logilab.fr
parents:
505
diff
changeset
|
5 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0 | 6 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
7 |
""" |
|
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
1132 | 10 |
from logilab.common.textutils import unormalize |
0 | 11 |
from logilab.mtconverter import html_escape |
12 |
||
872 | 13 |
from cubicweb.view import StartupView |
1132 | 14 |
from cubicweb.common.uilib import ureport_as_html, ajax_replace_url |
0 | 15 |
from cubicweb.web.httpcache import EtagHTTPCacheManager |
16 |
||
17 |
_ = unicode |
|
18 |
||
19 |
||
20 |
class ManageView(StartupView): |
|
21 |
id = 'manage' |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
22 |
title = _('manage') |
0 | 23 |
http_cache_manager = EtagHTTPCacheManager |
24 |
||
25 |
def display_folders(self): |
|
26 |
return False |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
27 |
|
0 | 28 |
def call(self, **kwargs): |
29 |
"""The default view representing the application's management""" |
|
853
4f6cd02bd71f
Use specific css files directly in views code
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
513
diff
changeset
|
30 |
self.req.add_css('cubicweb.manageview.css') |
0 | 31 |
self.w(u'<div>\n') |
32 |
if not self.display_folders(): |
|
33 |
self._main_index() |
|
34 |
else: |
|
35 |
self.w(u'<table><tr>\n') |
|
36 |
self.w(u'<td style="width:40%">') |
|
37 |
self._main_index() |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
38 |
self.w(u'</td><td style="width:60%">') |
0 | 39 |
self.folders() |
40 |
self.w(u'</td>') |
|
41 |
self.w(u'</tr></table>\n') |
|
42 |
self.w(u'</div>\n') |
|
43 |
||
44 |
def _main_index(self): |
|
45 |
req = self.req |
|
46 |
manager = req.user.matching_groups('managers') |
|
47 |
if not manager and 'Card' in self.schema: |
|
48 |
rset = self.req.execute('Card X WHERE X wikiid "index"') |
|
49 |
else: |
|
50 |
rset = None |
|
51 |
if rset: |
|
52 |
self.wview('inlined', rset, row=0) |
|
53 |
else: |
|
54 |
self.entities() |
|
55 |
self.w(u'<div class="hr"> </div>') |
|
56 |
self.startup_views() |
|
57 |
if manager and 'Card' in self.schema: |
|
58 |
self.w(u'<div class="hr"> </div>') |
|
59 |
if rset: |
|
60 |
href = rset.get_entity(0, 0).absolute_url(vid='edition') |
|
61 |
label = self.req._('edit the index page') |
|
62 |
else: |
|
63 |
href = req.build_url('view', vid='creation', etype='Card', wikiid='index') |
|
64 |
label = self.req._('create an index page') |
|
65 |
self.w(u'<br/><a href="%s">%s</a>\n' % (html_escape(href), label)) |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
66 |
|
0 | 67 |
def folders(self): |
68 |
self.w(u'<h4>%s</h4>\n' % self.req._('Browse by category')) |
|
69 |
self.vreg.select_view('tree', self.req, None).dispatch(w=self.w) |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
70 |
|
0 | 71 |
def startup_views(self): |
72 |
self.w(u'<h4>%s</h4>\n' % self.req._('Startup views')) |
|
73 |
self.startupviews_table() |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
74 |
|
0 | 75 |
def startupviews_table(self): |
76 |
for v in self.vreg.possible_views(self.req, None): |
|
77 |
if v.category != 'startupview' or v.id in ('index', 'tree', 'manage'): |
|
78 |
continue |
|
79 |
self.w('<p><a href="%s">%s</a></p>' % ( |
|
80 |
html_escape(v.url()), html_escape(self.req._(v.title).capitalize()))) |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
81 |
|
0 | 82 |
def entities(self): |
83 |
schema = self.schema |
|
84 |
self.w(u'<h4>%s</h4>\n' % self.req._('The repository holds the following entities')) |
|
85 |
manager = self.req.user.matching_groups('managers') |
|
86 |
self.w(u'<table class="startup">') |
|
87 |
if manager: |
|
88 |
self.w(u'<tr><th colspan="4">%s</th></tr>\n' % self.req._('application entities')) |
|
89 |
self.entity_types_table(eschema for eschema in schema.entities() |
|
178
b5478a96da3d
only hide strict suboject in default view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
90 |
if not eschema.meta and not eschema.is_subobject(strict=True)) |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
91 |
if manager: |
0 | 92 |
self.w(u'<tr><th colspan="4">%s</th></tr>\n' % self.req._('system entities')) |
93 |
self.entity_types_table(eschema for eschema in schema.entities() |
|
94 |
if eschema.meta and not eschema.schema_entity()) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
95 |
if 'CWAttribute' in schema: # check schema support |
0 | 96 |
self.w(u'<tr><th colspan="4">%s</th></tr>\n' % self.req._('schema entities')) |
97 |
self.entity_types_table(schema.eschema(etype) |
|
98 |
for etype in schema.schema_entity_types()) |
|
99 |
self.w(u'</table>') |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
100 |
|
0 | 101 |
def entity_types_table(self, eschemas): |
102 |
newline = 0 |
|
103 |
infos = sorted(self.entity_types(eschemas), |
|
104 |
key=lambda (l,a,e):unormalize(l)) |
|
105 |
q, r = divmod(len(infos), 2) |
|
106 |
if r: |
|
107 |
infos.append( (None, ' ', ' ') ) |
|
108 |
infos = zip(infos[:q+r], infos[q+r:]) |
|
109 |
for (_, etypelink, addlink), (_, etypelink2, addlink2) in infos: |
|
110 |
self.w(u'<tr>\n') |
|
111 |
self.w(u'<td class="addcol">%s</td><td>%s</td>\n' % (addlink, etypelink)) |
|
112 |
self.w(u'<td class="addcol">%s</td><td>%s</td>\n' % (addlink2, etypelink2)) |
|
113 |
self.w(u'</tr>\n') |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
114 |
|
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
115 |
|
0 | 116 |
def entity_types(self, eschemas): |
117 |
"""return a list of formatted links to get a list of entities of |
|
118 |
a each entity's types |
|
119 |
""" |
|
120 |
req = self.req |
|
121 |
for eschema in eschemas: |
|
122 |
if eschema.is_final() or (not eschema.has_perm(req, 'read') and |
|
123 |
not eschema.has_local_role('read')): |
|
124 |
continue |
|
125 |
etype = eschema.type |
|
126 |
label = display_name(req, etype, 'plural') |
|
127 |
nb = req.execute('Any COUNT(X) WHERE X is %s' % etype)[0][0] |
|
128 |
if nb > 1: |
|
129 |
view = self.vreg.select_view('list', req, req.etype_rset(etype)) |
|
130 |
url = view.url() |
|
131 |
else: |
|
132 |
url = self.build_url('view', rql='%s X' % etype) |
|
133 |
etypelink = u' <a href="%s">%s</a> (%d)' % ( |
|
134 |
html_escape(url), label, nb) |
|
135 |
yield (label, etypelink, self.add_entity_link(eschema, req)) |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
136 |
|
0 | 137 |
def add_entity_link(self, eschema, req): |
138 |
"""creates a [+] link for adding an entity if user has permission to do so""" |
|
139 |
if not eschema.has_perm(req, 'add'): |
|
140 |
return u'' |
|
141 |
return u'[<a href="%s" title="%s">+</a>]' % ( |
|
142 |
html_escape(self.create_url(eschema.type)), |
|
143 |
self.req.__('add a %s' % eschema)) |
|
144 |
||
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
145 |
|
0 | 146 |
class IndexView(ManageView): |
147 |
id = 'index' |
|
148 |
title = _('index') |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
149 |
|
0 | 150 |
def display_folders(self): |
329
903eb8c4ebd6
don't show folder section on index page when there is now folder yet
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
178
diff
changeset
|
151 |
return 'Folder' in self.schema and self.req.execute('Any COUNT(X) WHERE X is Folder')[0][0] |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
152 |
|
0 | 153 |
|
154 |
||
155 |
class SchemaView(StartupView): |
|
156 |
id = 'schema' |
|
157 |
title = _('application schema') |
|
158 |
||
159 |
def call(self): |
|
160 |
"""display schema information""" |
|
161 |
self.req.add_js('cubicweb.ajax.js') |
|
162 |
self.req.add_css('cubicweb.schema.css') |
|
163 |
withmeta = int(self.req.form.get('withmeta', 0)) |
|
164 |
self.w(u'<img src="%s" alt="%s"/>\n' % ( |
|
165 |
html_escape(self.req.build_url('view', vid='schemagraph', withmeta=withmeta)), |
|
166 |
self.req._("graphical representation of the application'schema"))) |
|
167 |
if withmeta: |
|
168 |
self.w(u'<div><a href="%s">%s</a></div>' % ( |
|
169 |
self.build_url('schema', withmeta=0), |
|
170 |
self.req._('hide meta-data'))) |
|
171 |
else: |
|
172 |
self.w(u'<div><a href="%s">%s</a></div>' % ( |
|
173 |
self.build_url('schema', withmeta=1), |
|
174 |
self.req._('show meta-data'))) |
|
175 |
self.w(u'<div id="detailed_schema"><a href="%s">%s</a></div>' % |
|
176 |
(html_escape(ajax_replace_url('detailed_schema', '', 'schematext', |
|
177 |
skipmeta=int(not withmeta))), |
|
178 |
self.req._('detailed schema view'))) |
|
179 |
||
180 |
||
513
907c18c01c60
while EntityView has been used here?? (changeset 501 by laure)
sylvain.thenault@logilab.fr
parents:
505
diff
changeset
|
181 |
class SchemaUreportsView(StartupView): |
0 | 182 |
id = 'schematext' |
183 |
||
184 |
def call(self): |
|
185 |
from cubicweb.schemaviewer import SchemaViewer |
|
186 |
skipmeta = int(self.req.form.get('skipmeta', True)) |
|
187 |
schema = self.schema |
|
188 |
viewer = SchemaViewer(self.req) |
|
189 |
layout = viewer.visit_schema(schema, display_relations=True, |
|
190 |
skiprels=('is', 'is_instance_of', 'identity', |
|
191 |
'owned_by', 'created_by'), |
|
192 |
skipmeta=skipmeta) |
|
193 |
self.w(ureport_as_html(layout)) |
|
194 |