author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 09 Feb 2010 12:54:44 +0100 | |
changeset 4518 | 5690bf6d6081 |
parent 4252 | 6c4f109c2b03 |
child 5421 | 8167de96c523 |
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 |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
5 |
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 6 |
: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:
1739
diff
changeset
|
7 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 8 |
""" |
9 |
__docformat__ = "restructuredtext en" |
|
1631
8370be19afd7
configurable entity types tables
sylvain.thenault@logilab.fr
parents:
1544
diff
changeset
|
10 |
_ = unicode |
0 | 11 |
|
1132 | 12 |
from logilab.common.textutils import unormalize |
2387
ea1defea9636
[views] fix some crashers (schema still does not render properly, might be yams side)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2381
diff
changeset
|
13 |
from logilab.mtconverter import xml_escape |
0 | 14 |
|
1532
111c52e0022f
duh? remaining __selectors__, probably a wrong merge
sylvain.thenault@logilab.fr
parents:
1523
diff
changeset
|
15 |
from cubicweb.view import StartupView |
1998
12040c090aa4
[views] change schema views: remove view eschema and use tabs for cwetype
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1977
diff
changeset
|
16 |
from cubicweb.selectors import match_user_groups, implements |
2436
44b2eea35efa
move all schema related views where they belong (eg not startup), fix schema graph views
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2387
diff
changeset
|
17 |
from cubicweb.schema import display_name |
1635
866563e2d0fc
don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents:
1631
diff
changeset
|
18 |
from cubicweb.web import ajax_replace_url, uicfg, httpcache |
0 | 19 |
|
20 |
class ManageView(StartupView): |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3023
diff
changeset
|
21 |
__regid__ = 'manage' |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
22 |
title = _('manage') |
1631
8370be19afd7
configurable entity types tables
sylvain.thenault@logilab.fr
parents:
1544
diff
changeset
|
23 |
http_cache_manager = httpcache.EtagHTTPCacheManager |
3500
ab10d1cb53ff
[ui api] backport create_links from forge so it may be reused
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
24 |
add_etype_links = () |
1631
8370be19afd7
configurable entity types tables
sylvain.thenault@logilab.fr
parents:
1544
diff
changeset
|
25 |
|
0 | 26 |
def display_folders(self): |
27 |
return False |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
28 |
|
0 | 29 |
def call(self, **kwargs): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2436
diff
changeset
|
30 |
"""The default view representing the instance's management""" |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
31 |
self._cw.add_css('cubicweb.manageview.css') |
0 | 32 |
self.w(u'<div>\n') |
33 |
if not self.display_folders(): |
|
34 |
self._main_index() |
|
35 |
else: |
|
36 |
self.w(u'<table><tr>\n') |
|
37 |
self.w(u'<td style="width:40%">') |
|
38 |
self._main_index() |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
39 |
self.w(u'</td><td style="width:60%">') |
0 | 40 |
self.folders() |
41 |
self.w(u'</td>') |
|
42 |
self.w(u'</tr></table>\n') |
|
43 |
self.w(u'</div>\n') |
|
44 |
||
45 |
def _main_index(self): |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
46 |
req = self._cw |
0 | 47 |
manager = req.user.matching_groups('managers') |
4045
f4a52abb6f4f
cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3890
diff
changeset
|
48 |
if not manager and 'Card' in self._cw.vreg.schema: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
49 |
rset = self._cw.execute('Card X WHERE X wikiid "index"') |
0 | 50 |
else: |
51 |
rset = None |
|
52 |
if rset: |
|
53 |
self.wview('inlined', rset, row=0) |
|
54 |
else: |
|
55 |
self.entities() |
|
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2780
diff
changeset
|
56 |
self.w(u'<div class="hr"> </div>') |
0 | 57 |
self.startup_views() |
4045
f4a52abb6f4f
cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3890
diff
changeset
|
58 |
if manager and 'Card' in self._cw.vreg.schema: |
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2780
diff
changeset
|
59 |
self.w(u'<div class="hr"> </div>') |
0 | 60 |
if rset: |
61 |
href = rset.get_entity(0, 0).absolute_url(vid='edition') |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
62 |
label = self._cw._('edit the index page') |
0 | 63 |
else: |
64 |
href = req.build_url('view', vid='creation', etype='Card', wikiid='index') |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
65 |
label = self._cw._('create an index page') |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2265
diff
changeset
|
66 |
self.w(u'<br/><a href="%s">%s</a>\n' % (xml_escape(href), label)) |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
67 |
|
0 | 68 |
def folders(self): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
69 |
self.w(u'<h4>%s</h4>\n' % self._cw._('Browse by category')) |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
70 |
self._cw.vreg['views'].select('tree', self._cw).render(w=self.w) |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
71 |
|
3500
ab10d1cb53ff
[ui api] backport create_links from forge so it may be reused
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
72 |
def create_links(self): |
ab10d1cb53ff
[ui api] backport create_links from forge so it may be reused
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
73 |
self.w(u'<ul class="createLink">') |
ab10d1cb53ff
[ui api] backport create_links from forge so it may be reused
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
74 |
for etype in self.add_etype_links: |
ab10d1cb53ff
[ui api] backport create_links from forge so it may be reused
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
75 |
eschema = self.schema.eschema(etype) |
4183
b5aa030bb2f9
use ._cw instead of .req (reintroduced by merge of stable)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4144
diff
changeset
|
76 |
if eschema.has_perm(self._cw, 'add'): |
3500
ab10d1cb53ff
[ui api] backport create_links from forge so it may be reused
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
77 |
self.w(u'<li><a href="%s">%s</a></li>' % ( |
4183
b5aa030bb2f9
use ._cw instead of .req (reintroduced by merge of stable)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4144
diff
changeset
|
78 |
self._cw.build_url('add/%s' % eschema), |
b5aa030bb2f9
use ._cw instead of .req (reintroduced by merge of stable)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4144
diff
changeset
|
79 |
self._cw.__('add a %s' % eschema).capitalize())) |
3500
ab10d1cb53ff
[ui api] backport create_links from forge so it may be reused
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
80 |
self.w(u'</ul>') |
ab10d1cb53ff
[ui api] backport create_links from forge so it may be reused
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
81 |
|
0 | 82 |
def startup_views(self): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
83 |
self.w(u'<h4>%s</h4>\n' % self._cw._('Startup views')) |
0 | 84 |
self.startupviews_table() |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
85 |
|
0 | 86 |
def startupviews_table(self): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
87 |
for v in self._cw.vreg['views'].possible_views(self._cw, None): |
3457
0924d0d08d60
[api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
88 |
if v.category != 'startupview' or v.__regid__ in ('index', 'tree', 'manage'): |
0 | 89 |
continue |
90 |
self.w('<p><a href="%s">%s</a></p>' % ( |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
91 |
xml_escape(v.url()), xml_escape(self._cw._(v.title).capitalize()))) |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
92 |
|
0 | 93 |
def entities(self): |
4045
f4a52abb6f4f
cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3890
diff
changeset
|
94 |
schema = self._cw.vreg.schema |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
95 |
self.w(u'<h4>%s</h4>\n' % self._cw._('The repository holds the following entities')) |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
96 |
manager = self._cw.user.matching_groups('managers') |
0 | 97 |
self.w(u'<table class="startup">') |
98 |
if manager: |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
99 |
self.w(u'<tr><th colspan="4">%s</th></tr>\n' % self._cw._('application entities')) |
0 | 100 |
self.entity_types_table(eschema for eschema in schema.entities() |
1739 | 101 |
if uicfg.indexview_etype_section.get(eschema) == 'application') |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
102 |
if manager: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
103 |
self.w(u'<tr><th colspan="4">%s</th></tr>\n' % self._cw._('system entities')) |
0 | 104 |
self.entity_types_table(eschema for eschema in schema.entities() |
1739 | 105 |
if uicfg.indexview_etype_section.get(eschema) == 'system') |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
106 |
if 'CWAttribute' in schema: # check schema support |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
107 |
self.w(u'<tr><th colspan="4">%s</th></tr>\n' % self._cw._('schema entities')) |
1631
8370be19afd7
configurable entity types tables
sylvain.thenault@logilab.fr
parents:
1544
diff
changeset
|
108 |
self.entity_types_table(eschema for eschema in schema.entities() |
1739 | 109 |
if uicfg.indexview_etype_section.get(eschema) == 'schema') |
0 | 110 |
self.w(u'</table>') |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
111 |
|
0 | 112 |
def entity_types_table(self, eschemas): |
113 |
newline = 0 |
|
114 |
infos = sorted(self.entity_types(eschemas), |
|
115 |
key=lambda (l,a,e):unormalize(l)) |
|
116 |
q, r = divmod(len(infos), 2) |
|
117 |
if r: |
|
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2780
diff
changeset
|
118 |
infos.append( (None, ' ', ' ') ) |
0 | 119 |
infos = zip(infos[:q+r], infos[q+r:]) |
120 |
for (_, etypelink, addlink), (_, etypelink2, addlink2) in infos: |
|
121 |
self.w(u'<tr>\n') |
|
122 |
self.w(u'<td class="addcol">%s</td><td>%s</td>\n' % (addlink, etypelink)) |
|
123 |
self.w(u'<td class="addcol">%s</td><td>%s</td>\n' % (addlink2, etypelink2)) |
|
124 |
self.w(u'</tr>\n') |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
125 |
|
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
126 |
|
0 | 127 |
def entity_types(self, eschemas): |
128 |
"""return a list of formatted links to get a list of entities of |
|
129 |
a each entity's types |
|
130 |
""" |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
131 |
req = self._cw |
0 | 132 |
for eschema in eschemas: |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
133 |
if eschema.final or not eschema.may_have_permission('read', req): |
0 | 134 |
continue |
135 |
etype = eschema.type |
|
136 |
nb = req.execute('Any COUNT(X) WHERE X is %s' % etype)[0][0] |
|
4144 | 137 |
if nb > 1: |
138 |
label = display_name(req, etype, 'plural') |
|
139 |
else: |
|
140 |
label = display_name(req, etype) |
|
0 | 141 |
nb = req.execute('Any COUNT(X) WHERE X is %s' % etype)[0][0] |
3460
e4843535db25
[api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3457
diff
changeset
|
142 |
url = self._cw.build_url(etype) |
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2780
diff
changeset
|
143 |
etypelink = u' <a href="%s">%s</a> (%d)' % ( |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2265
diff
changeset
|
144 |
xml_escape(url), label, nb) |
0 | 145 |
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
|
146 |
|
0 | 147 |
def add_entity_link(self, eschema, req): |
148 |
"""creates a [+] link for adding an entity if user has permission to do so""" |
|
149 |
if not eschema.has_perm(req, 'add'): |
|
150 |
return u'' |
|
151 |
return u'[<a href="%s" title="%s">+</a>]' % ( |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2265
diff
changeset
|
152 |
xml_escape(self.create_url(eschema.type)), |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
153 |
self._cw.__('add a %s' % eschema)) |
0 | 154 |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
155 |
|
0 | 156 |
class IndexView(ManageView): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3023
diff
changeset
|
157 |
__regid__ = 'index' |
2368
b41aef0e63a7
use msgid, update i18n
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2312
diff
changeset
|
158 |
title = _('view_index') |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
159 |
|
0 | 160 |
def display_folders(self): |
4045
f4a52abb6f4f
cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3890
diff
changeset
|
161 |
return 'Folder' in self._cw.vreg.schema and self._cw.execute('Any COUNT(X) WHERE X is Folder')[0][0] |
2748
d2fcf19bfb34
F [registry views] add _registry startup view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2687
diff
changeset
|
162 |