"""Set of HTML startup views. A startup view is global, e.g. doesn'tapply to a result set.:organization: Logilab:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses"""__docformat__="restructuredtext en"_=unicodefromlogilab.common.textutilsimportunormalizefromlogilab.mtconverterimportxml_escapefromcubicweb.viewimportStartupViewfromcubicweb.selectorsimportmatch_user_groups,implementsfromcubicweb.schemaimportdisplay_namefromcubicweb.webimportajax_replace_url,uicfg,httpcacheclassManageView(StartupView):__regid__='manage'title=_('manage')http_cache_manager=httpcache.EtagHTTPCacheManageradd_etype_links=()defdisplay_folders(self):returnFalsedefcall(self,**kwargs):"""The default view representing the instance's management"""self._cw.add_css('cubicweb.manageview.css')self.w(u'<div>\n')ifnotself.display_folders():self._main_index()else:self.w(u'<table><tr>\n')self.w(u'<td style="width:40%">')self._main_index()self.w(u'</td><td style="width:60%">')self.folders()self.w(u'</td>')self.w(u'</tr></table>\n')self.w(u'</div>\n')def_main_index(self):req=self._cwmanager=req.user.matching_groups('managers')ifnotmanagerand'Card'inself._cw.vreg.schema:rset=self._cw.execute('Card X WHERE X wikiid "index"')else:rset=Noneifrset:self.wview('inlined',rset,row=0)else:self.entities()self.w(u'<div class="hr"> </div>')self.startup_views()ifmanagerand'Card'inself._cw.vreg.schema:self.w(u'<div class="hr"> </div>')ifrset:href=rset.get_entity(0,0).absolute_url(vid='edition')label=self._cw._('edit the index page')else:href=req.build_url('view',vid='creation',etype='Card',wikiid='index')label=self._cw._('create an index page')self.w(u'<br/><a href="%s">%s</a>\n'%(xml_escape(href),label))deffolders(self):self.w(u'<h4>%s</h4>\n'%self._cw._('Browse by category'))self._cw.vreg['views'].select('tree',self._cw).render(w=self.w)defcreate_links(self):self.w(u'<ul class="createLink">')foretypeinself.add_etype_links:eschema=self.schema.eschema(etype)ifeschema.has_perm(self.req,'add'):self.w(u'<li><a href="%s">%s</a></li>'%(self.req.build_url('add/%s'%eschema),self.req.__('add a %s'%eschema).capitalize()))self.w(u'</ul>')defstartup_views(self):self.w(u'<h4>%s</h4>\n'%self._cw._('Startup views'))self.startupviews_table()defstartupviews_table(self):forvinself._cw.vreg['views'].possible_views(self._cw,None):ifv.category!='startupview'orv.__regid__in('index','tree','manage'):continueself.w('<p><a href="%s">%s</a></p>'%(xml_escape(v.url()),xml_escape(self._cw._(v.title).capitalize())))defentities(self):schema=self._cw.vreg.schemaself.w(u'<h4>%s</h4>\n'%self._cw._('The repository holds the following entities'))manager=self._cw.user.matching_groups('managers')self.w(u'<table class="startup">')ifmanager:self.w(u'<tr><th colspan="4">%s</th></tr>\n'%self._cw._('application entities'))self.entity_types_table(eschemaforeschemainschema.entities()ifuicfg.indexview_etype_section.get(eschema)=='application')ifmanager:self.w(u'<tr><th colspan="4">%s</th></tr>\n'%self._cw._('system entities'))self.entity_types_table(eschemaforeschemainschema.entities()ifuicfg.indexview_etype_section.get(eschema)=='system')if'CWAttribute'inschema:# check schema supportself.w(u'<tr><th colspan="4">%s</th></tr>\n'%self._cw._('schema entities'))self.entity_types_table(eschemaforeschemainschema.entities()ifuicfg.indexview_etype_section.get(eschema)=='schema')self.w(u'</table>')defentity_types_table(self,eschemas):newline=0infos=sorted(self.entity_types(eschemas),key=lambda(l,a,e):unormalize(l))q,r=divmod(len(infos),2)ifr:infos.append((None,' ',' '))infos=zip(infos[:q+r],infos[q+r:])for(_,etypelink,addlink),(_,etypelink2,addlink2)ininfos:self.w(u'<tr>\n')self.w(u'<td class="addcol">%s</td><td>%s</td>\n'%(addlink,etypelink))self.w(u'<td class="addcol">%s</td><td>%s</td>\n'%(addlink2,etypelink2))self.w(u'</tr>\n')defentity_types(self,eschemas):"""return a list of formatted links to get a list of entities of a each entity's types """req=self._cwforeschemaineschemas:ifeschema.finalornoteschema.may_have_permission('read',req):continueetype=eschema.typelabel=display_name(req,etype,'plural')nb=req.execute('Any COUNT(X) WHERE X is %s'%etype)[0][0]url=self._cw.build_url(etype)etypelink=u' <a href="%s">%s</a> (%d)'%(xml_escape(url),label,nb)yield(label,etypelink,self.add_entity_link(eschema,req))defadd_entity_link(self,eschema,req):"""creates a [+] link for adding an entity if user has permission to do so"""ifnoteschema.has_perm(req,'add'):returnu''returnu'[<a href="%s" title="%s">+</a>]'%(xml_escape(self.create_url(eschema.type)),self._cw.__('add a %s'%eschema))classIndexView(ManageView):__regid__='index'title=_('view_index')defdisplay_folders(self):return'Folder'inself._cw.vreg.schemaandself._cw.execute('Any COUNT(X) WHERE X is Folder')[0][0]