author | Nicolas Chauvat <nicolas.chauvat@logilab.fr> |
Mon, 27 Jul 2009 15:19:01 +0200 | |
changeset 2524 | 2d0c04c8cbe4 |
parent 2488 | cd007fc4f2ea |
child 2650 | 18aec79ec3a3 |
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 |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1739
diff
changeset
|
5 |
:copyright: 2001-2009 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): |
|
21 |
id = '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 |
8370be19afd7
configurable entity types tables
sylvain.thenault@logilab.fr
parents:
1544
diff
changeset
|
24 |
|
8370be19afd7
configurable entity types tables
sylvain.thenault@logilab.fr
parents:
1544
diff
changeset
|
25 |
@classmethod |
8370be19afd7
configurable entity types tables
sylvain.thenault@logilab.fr
parents:
1544
diff
changeset
|
26 |
def vreg_initialization_completed(cls): |
8370be19afd7
configurable entity types tables
sylvain.thenault@logilab.fr
parents:
1544
diff
changeset
|
27 |
for eschema in cls.schema.entities(): |
8370be19afd7
configurable entity types tables
sylvain.thenault@logilab.fr
parents:
1544
diff
changeset
|
28 |
if eschema.schema_entity(): |
1739 | 29 |
uicfg.indexview_etype_section.setdefault(eschema, 'schema') |
1631
8370be19afd7
configurable entity types tables
sylvain.thenault@logilab.fr
parents:
1544
diff
changeset
|
30 |
elif eschema.is_subobject(strict=True): |
1739 | 31 |
uicfg.indexview_etype_section.setdefault(eschema, 'subobject') |
1631
8370be19afd7
configurable entity types tables
sylvain.thenault@logilab.fr
parents:
1544
diff
changeset
|
32 |
else: |
1739 | 33 |
uicfg.indexview_etype_section.setdefault(eschema, 'application') |
0 | 34 |
|
35 |
def display_folders(self): |
|
36 |
return False |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
37 |
|
0 | 38 |
def call(self, **kwargs): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2436
diff
changeset
|
39 |
"""The default view representing the instance's management""" |
853
4f6cd02bd71f
Use specific css files directly in views code
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
513
diff
changeset
|
40 |
self.req.add_css('cubicweb.manageview.css') |
0 | 41 |
self.w(u'<div>\n') |
42 |
if not self.display_folders(): |
|
43 |
self._main_index() |
|
44 |
else: |
|
45 |
self.w(u'<table><tr>\n') |
|
46 |
self.w(u'<td style="width:40%">') |
|
47 |
self._main_index() |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
48 |
self.w(u'</td><td style="width:60%">') |
0 | 49 |
self.folders() |
50 |
self.w(u'</td>') |
|
51 |
self.w(u'</tr></table>\n') |
|
52 |
self.w(u'</div>\n') |
|
53 |
||
54 |
def _main_index(self): |
|
55 |
req = self.req |
|
56 |
manager = req.user.matching_groups('managers') |
|
57 |
if not manager and 'Card' in self.schema: |
|
58 |
rset = self.req.execute('Card X WHERE X wikiid "index"') |
|
59 |
else: |
|
60 |
rset = None |
|
61 |
if rset: |
|
62 |
self.wview('inlined', rset, row=0) |
|
63 |
else: |
|
64 |
self.entities() |
|
65 |
self.w(u'<div class="hr"> </div>') |
|
66 |
self.startup_views() |
|
67 |
if manager and 'Card' in self.schema: |
|
68 |
self.w(u'<div class="hr"> </div>') |
|
69 |
if rset: |
|
70 |
href = rset.get_entity(0, 0).absolute_url(vid='edition') |
|
71 |
label = self.req._('edit the index page') |
|
72 |
else: |
|
73 |
href = req.build_url('view', vid='creation', etype='Card', wikiid='index') |
|
74 |
label = self.req._('create an index page') |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2265
diff
changeset
|
75 |
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
|
76 |
|
0 | 77 |
def folders(self): |
78 |
self.w(u'<h4>%s</h4>\n' % self.req._('Browse by category')) |
|
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2017
diff
changeset
|
79 |
self.vreg.select('views', 'tree', self.req).render(w=self.w) |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
80 |
|
0 | 81 |
def startup_views(self): |
82 |
self.w(u'<h4>%s</h4>\n' % self.req._('Startup views')) |
|
83 |
self.startupviews_table() |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
84 |
|
0 | 85 |
def startupviews_table(self): |
86 |
for v in self.vreg.possible_views(self.req, None): |
|
87 |
if v.category != 'startupview' or v.id in ('index', 'tree', 'manage'): |
|
88 |
continue |
|
89 |
self.w('<p><a href="%s">%s</a></p>' % ( |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2265
diff
changeset
|
90 |
xml_escape(v.url()), xml_escape(self.req._(v.title).capitalize()))) |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
91 |
|
0 | 92 |
def entities(self): |
93 |
schema = self.schema |
|
94 |
self.w(u'<h4>%s</h4>\n' % self.req._('The repository holds the following entities')) |
|
95 |
manager = self.req.user.matching_groups('managers') |
|
96 |
self.w(u'<table class="startup">') |
|
97 |
if manager: |
|
98 |
self.w(u'<tr><th colspan="4">%s</th></tr>\n' % self.req._('application entities')) |
|
99 |
self.entity_types_table(eschema for eschema in schema.entities() |
|
1739 | 100 |
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
|
101 |
if manager: |
0 | 102 |
self.w(u'<tr><th colspan="4">%s</th></tr>\n' % self.req._('system entities')) |
103 |
self.entity_types_table(eschema for eschema in schema.entities() |
|
1739 | 104 |
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
|
105 |
if 'CWAttribute' in schema: # check schema support |
0 | 106 |
self.w(u'<tr><th colspan="4">%s</th></tr>\n' % self.req._('schema entities')) |
1631
8370be19afd7
configurable entity types tables
sylvain.thenault@logilab.fr
parents:
1544
diff
changeset
|
107 |
self.entity_types_table(eschema for eschema in schema.entities() |
1739 | 108 |
if uicfg.indexview_etype_section.get(eschema) == 'schema') |
0 | 109 |
self.w(u'</table>') |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
110 |
|
0 | 111 |
def entity_types_table(self, eschemas): |
112 |
newline = 0 |
|
113 |
infos = sorted(self.entity_types(eschemas), |
|
114 |
key=lambda (l,a,e):unormalize(l)) |
|
115 |
q, r = divmod(len(infos), 2) |
|
116 |
if r: |
|
117 |
infos.append( (None, ' ', ' ') ) |
|
118 |
infos = zip(infos[:q+r], infos[q+r:]) |
|
119 |
for (_, etypelink, addlink), (_, etypelink2, addlink2) in infos: |
|
120 |
self.w(u'<tr>\n') |
|
121 |
self.w(u'<td class="addcol">%s</td><td>%s</td>\n' % (addlink, etypelink)) |
|
122 |
self.w(u'<td class="addcol">%s</td><td>%s</td>\n' % (addlink2, etypelink2)) |
|
123 |
self.w(u'</tr>\n') |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
124 |
|
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
125 |
|
0 | 126 |
def entity_types(self, eschemas): |
127 |
"""return a list of formatted links to get a list of entities of |
|
128 |
a each entity's types |
|
129 |
""" |
|
130 |
req = self.req |
|
131 |
for eschema in eschemas: |
|
132 |
if eschema.is_final() or (not eschema.has_perm(req, 'read') and |
|
133 |
not eschema.has_local_role('read')): |
|
134 |
continue |
|
135 |
etype = eschema.type |
|
136 |
label = display_name(req, etype, 'plural') |
|
137 |
nb = req.execute('Any COUNT(X) WHERE X is %s' % etype)[0][0] |
|
2488
cd007fc4f2ea
should be enough. Nicer isn't it?
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
138 |
url = self.build_url(etype) |
0 | 139 |
etypelink = u' <a href="%s">%s</a> (%d)' % ( |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2265
diff
changeset
|
140 |
xml_escape(url), label, nb) |
0 | 141 |
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
|
142 |
|
0 | 143 |
def add_entity_link(self, eschema, req): |
144 |
"""creates a [+] link for adding an entity if user has permission to do so""" |
|
145 |
if not eschema.has_perm(req, 'add'): |
|
146 |
return u'' |
|
147 |
return u'[<a href="%s" title="%s">+</a>]' % ( |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2265
diff
changeset
|
148 |
xml_escape(self.create_url(eschema.type)), |
0 | 149 |
self.req.__('add a %s' % eschema)) |
150 |
||
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
151 |
|
0 | 152 |
class IndexView(ManageView): |
153 |
id = 'index' |
|
2368
b41aef0e63a7
use msgid, update i18n
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2312
diff
changeset
|
154 |
title = _('view_index') |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
155 |
|
0 | 156 |
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
|
157 |
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
|
158 |