backport default into stable: stable is now cw 3.8
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr## This file is part of CubicWeb.## CubicWeb is free software: you can redistribute it and/or modify it under the# terms of the GNU Lesser General Public License as published by the Free# Software Foundation, either version 2.1 of the License, or (at your option)# any later version.## logilab-common is distributed in the hope that it will be useful, but WITHOUT# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more# details.## You should have received a copy of the GNU Lesser General Public License along# with CubicWeb. If not, see <http://www.gnu.org/licenses/>."""Primary view for bookmarks + user's bookmarks box"""__docformat__="restructuredtext en"fromlogilab.mtconverterimportxml_escapefromcubicwebimportUnauthorizedfromcubicweb.selectorsimportimplements,one_line_rsetfromcubicweb.web.htmlwidgetsimportBoxWidget,BoxMenu,RawBoxItemfromcubicweb.webimportaction,box,uicfg,formwidgetsasfwfromcubicweb.web.viewsimportprimary_abaa=uicfg.actionbox_appearsin_addmenu_abaa.tag_subject_of(('*','bookmarked_by','*'),False)_abaa.tag_object_of(('*','bookmarked_by','*'),False)_afs=uicfg.autoform_section_afs.tag_object_of(('*','bookmarked_by','CWUser'),'main','metadata')_afs.tag_attribute(('Bookmark','path'),'main','attributes')_afs.tag_attribute(('Bookmark','path'),'muledit','attributes')_affk=uicfg.autoform_field_kwargs_affk.tag_attribute(('Bookmark','path'),{'widget':fw.EditableURLWidget})classFollowAction(action.Action):__regid__='follow'__select__=one_line_rset()&implements('Bookmark')title=_('follow')category='mainactions'defurl(self):returnself.cw_rset.get_entity(self.cw_rowor0,self.cw_color0).actual_url()classBookmarkPrimaryView(primary.PrimaryView):__select__=implements('Bookmark')defcell_call(self,row,col):"""the primary view for bookmark entity"""entity=self.cw_rset.complete_entity(row,col)self.w(u' ')self.w(u"<span class='title'><b>")self.w(u"%s : %s"%(self._cw._('Bookmark'),xml_escape(entity.title)))self.w(u"</b></span>")self.w(u'<br/><br/><div class="content"><a href="%s">'%(xml_escape(entity.actual_url())))self.w(u'</a>')self.w(u'<p>%s%s</p>'%(self._cw._('Used by:'),', '.join(xml_escape(u.name())foruinentity.bookmarked_by)))self.w(u'</div>')classBookmarksBox(box.UserRQLBoxTemplate):"""display a box containing all user's bookmarks"""__regid__='bookmarks_box'order=40title=_('bookmarks')rql=('Any B,T,P ORDERBY lower(T) ''WHERE B is Bookmark,B title T, B path P, B bookmarked_by U, ''U eid %(x)s')etype='Bookmark'rtype='bookmarked_by'defcall(self,**kwargs):req=self._cwueid=req.user.eidtry:rset=req.execute(self.rql,{'x':ueid})exceptUnauthorized:# can't access to something in the query, forget this boxreturnbox=BoxWidget(req._(self.title),self.__regid__)box.listing_class='sideBox'rschema=self._cw.vreg.schema.rschema(self.rtype)eschema=self._cw.vreg.schema.eschema(self.etype)candelete=rschema.has_perm(req,'delete',toeid=ueid)ifcandelete:req.add_js(('cubicweb.ajax.js','cubicweb.bookmarks.js'))else:dlink=Noneforbookmarkinrset.entities():label='<a href="%s">%s</a>'%(xml_escape(bookmark.action_url()),xml_escape(bookmark.title))ifcandelete:dlink=u'[<a href="javascript:removeBookmark(%s)" title="%s">-</a>]'%(bookmark.eid,_('delete this bookmark'))label='%s%s'%(dlink,label)box.append(RawBoxItem(label))ifeschema.has_perm(req,'add')andrschema.has_perm(req,'add',toeid=ueid):boxmenu=BoxMenu(req._('manage bookmarks'))linkto='bookmarked_by:%s:subject'%ueid# use a relative path so that we can move the instance without# loosing bookmarkspath=req.relative_path()# XXX if vtitle specified in params, extract it and use it as default value# for bookmark's titleurl=self.create_url(self.etype,__linkto=linkto,path=path)boxmenu.append(self.mk_action(req._('bookmark this page'),url,category='manage',id='bookmark'))ifrset:ifreq.user.is_in_group('managers'):bookmarksrql='Bookmark B WHERE B bookmarked_by U, U eid %s'%ueiderset=rsetelse:# we can't edit shared bookmarks we don't ownbookmarksrql='Bookmark B WHERE B bookmarked_by U, B owned_by U, U eid %(x)s'erset=req.execute(bookmarksrql,{'x':ueid},build_descr=False)bookmarksrql%={'x':ueid}iferset:url=self._cw.build_url(vid='muledit',rql=bookmarksrql)boxmenu.append(self.mk_action(self._cw._('edit bookmarks'),url,category='manage'))url=req.user.absolute_url(vid='xaddrelation',rtype='bookmarked_by',target='subject')boxmenu.append(self.mk_action(self._cw._('pick existing bookmarks'),url,category='manage'))box.append(boxmenu)ifnotbox.is_empty():box.render(self.w)