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