web/views/bookmark.py
changeset 6140 65a619eb31c4
parent 6039 6e84db1b3e44
child 6141 b8287e54b528
equal deleted inserted replaced
6139:f76599a96238 6140:65a619eb31c4
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
    14 # details.
    14 # details.
    15 #
    15 #
    16 # You should have received a copy of the GNU Lesser General Public License along
    16 # You should have received a copy of the GNU Lesser General Public License along
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    18 """Primary view for bookmarks + user's bookmarks box
    18 """Primary view for bookmarks + user's bookmarks box"""
    19 
    19 
    20 """
       
    21 __docformat__ = "restructuredtext en"
    20 __docformat__ = "restructuredtext en"
    22 
    21 
    23 from logilab.mtconverter import xml_escape
    22 from logilab.mtconverter import xml_escape
    24 
    23 
    25 from cubicweb import Unauthorized
    24 from cubicweb import Unauthorized
    68         self.w(u'<p>%s%s</p>' % (self._cw._('Used by:'), ', '.join(xml_escape(u.name())
    67         self.w(u'<p>%s%s</p>' % (self._cw._('Used by:'), ', '.join(xml_escape(u.name())
    69                                                                    for u in entity.bookmarked_by)))
    68                                                                    for u in entity.bookmarked_by)))
    70         self.w(u'</div>')
    69         self.w(u'</div>')
    71 
    70 
    72 
    71 
    73 class BookmarksBox(box.UserRQLBoxTemplate):
    72 class BookmarksBox(box.Box):
    74     """display a box containing all user's bookmarks"""
    73     """display a box containing all user's bookmarks"""
    75     __regid__ = 'bookmarks_box'
    74     __regid__ = 'bookmarks_box'
       
    75 
       
    76     title = _('bookmarks')
    76     order = 40
    77     order = 40
    77     title = _('bookmarks')
       
    78     rql = ('Any B,T,P ORDERBY lower(T) '
    78     rql = ('Any B,T,P ORDERBY lower(T) '
    79            'WHERE B is Bookmark,B title T, B path P, B bookmarked_by U, '
    79            'WHERE B is Bookmark,B title T, B path P, B bookmarked_by U, '
    80            'U eid %(x)s')
    80            'U eid %(x)s')
    81     etype = 'Bookmark'
       
    82     rtype = 'bookmarked_by'
       
    83 
    81 
       
    82     def init_rendering(self):
       
    83         ueid = self._cw.user.eid
       
    84         self.bookmarks_rset = self._cw.execute(self.rql, {'x': ueid})
       
    85         rschema = self._cw.vreg.schema.rschema('bookmarked_by')
       
    86         eschema = self._cw.vreg.schema.eschema('Bookmark')
       
    87         self.can_delete = rschema.has_perm(self._cw, 'delete', toeid=ueid)
       
    88         self.can_edit = (eschema.has_perm(self._cw, 'add') and
       
    89                          rschema.has_perm(self._cw, 'add', toeid=ueid))
       
    90         if not self.bookmarks_rset and not self.can_edit:
       
    91             raise box.EmptyComponent()
       
    92         self.items = []
    84 
    93 
    85     def call(self, **kwargs):
    94     def render_body(self, w):
       
    95         ueid = self._cw.user.eid
    86         req = self._cw
    96         req = self._cw
    87         ueid = req.user.eid
    97         if self.can_delete:
    88         try:
       
    89             rset = req.execute(self.rql, {'x': ueid})
       
    90         except Unauthorized:
       
    91             # can't access to something in the query, forget this box
       
    92             return
       
    93         box = BoxWidget(req._(self.title), self.__regid__)
       
    94         box.listing_class = 'sideBox'
       
    95         rschema = self._cw.vreg.schema.rschema(self.rtype)
       
    96         eschema = self._cw.vreg.schema.eschema(self.etype)
       
    97         candelete = rschema.has_perm(req, 'delete', toeid=ueid)
       
    98         if candelete:
       
    99             req.add_js('cubicweb.ajax.js')
    98             req.add_js('cubicweb.ajax.js')
   100         else:
    99         for bookmark in self.bookmarks_rset.entities():
   101             dlink = None
   100             label = self.build_link(bookmark.title, bookmark.action_url())
   102         for bookmark in rset.entities():
   101             if self.can_delete:
   103             label = '<a href="%s">%s</a>' % (xml_escape(bookmark.action_url()),
       
   104                                              xml_escape(bookmark.title))
       
   105             if candelete:
       
   106                 dlink = u'[<a href="javascript:removeBookmark(%s)" title="%s">-</a>]' % (
   102                 dlink = u'[<a href="javascript:removeBookmark(%s)" title="%s">-</a>]' % (
   107                     bookmark.eid, _('delete this bookmark'))
   103                     bookmark.eid, _('delete this bookmark'))
   108                 label = '%s %s' % (dlink, label)
   104                 label = '<div>%s %s</div>' % (dlink, label)
   109             box.append(RawBoxItem(label))
   105             self.append(label)
   110         if eschema.has_perm(req, 'add') and rschema.has_perm(req, 'add', toeid=ueid):
   106         if self.can_edit:
   111             boxmenu = BoxMenu(req._('manage bookmarks'))
   107             menu = BoxMenu(req._('manage bookmarks'))
   112             linkto = 'bookmarked_by:%s:subject' % ueid
   108             linkto = 'bookmarked_by:%s:subject' % ueid
   113             # use a relative path so that we can move the instance without
   109             # use a relative path so that we can move the instance without
   114             # loosing bookmarks
   110             # loosing bookmarks
   115             path = req.relative_path()
   111             path = req.relative_path()
   116             # XXX if vtitle specified in params, extract it and use it as default value
   112             # XXX if vtitle specified in params, extract it and use it as
   117             # for bookmark's title
   113             # default value for bookmark's title
   118             url = req.vreg['etypes'].etype_class('Bookmark').cw_create_url(
   114             url = req.vreg['etypes'].etype_class('Bookmark').cw_create_url(
   119                 req, __linkto=linkto, path=path)
   115                 req, __linkto=linkto, path=path)
   120             boxmenu.append(self.mk_action(req._('bookmark this page'), url,
   116             menu.append(self.build_link(req._('bookmark this page'), url))
   121                                           category='manage', id='bookmark'))
   117             if self.bookmarks_rset:
   122             if rset:
       
   123                 if req.user.is_in_group('managers'):
   118                 if req.user.is_in_group('managers'):
   124                     bookmarksrql = 'Bookmark B WHERE B bookmarked_by U, U eid %s' % ueid
   119                     bookmarksrql = 'Bookmark B WHERE B bookmarked_by U, U eid %s' % ueid
   125                     erset = rset
   120                     erset = self.bookmarks_rset
   126                 else:
   121                 else:
   127                     # we can't edit shared bookmarks we don't own
   122                     # we can't edit shared bookmarks we don't own
   128                     bookmarksrql = 'Bookmark B WHERE B bookmarked_by U, B owned_by U, U eid %(x)s'
   123                     bookmarksrql = 'Bookmark B WHERE B bookmarked_by U, B owned_by U, U eid %(x)s'
   129                     erset = req.execute(bookmarksrql, {'x': ueid},
   124                     erset = req.execute(bookmarksrql, {'x': ueid},
   130                                         build_descr=False)
   125                                         build_descr=False)
   131                     bookmarksrql %= {'x': ueid}
   126                     bookmarksrql %= {'x': ueid}
   132                 if erset:
   127                 if erset:
   133                     url = self._cw.build_url(vid='muledit', rql=bookmarksrql)
   128                     url = req.build_url(vid='muledit', rql=bookmarksrql)
   134                     boxmenu.append(self.mk_action(self._cw._('edit bookmarks'), url, category='manage'))
   129                     menu.append(self.build_link(req._('edit bookmarks'), url))
   135             url = req.user.absolute_url(vid='xaddrelation', rtype='bookmarked_by',
   130             url = req.user.absolute_url(vid='xaddrelation', rtype='bookmarked_by',
   136                                         target='subject')
   131                                         target='subject')
   137             boxmenu.append(self.mk_action(self._cw._('pick existing bookmarks'), url, category='manage'))
   132             menu.append(self.build_link(req._('pick existing bookmarks'), url))
   138             box.append(boxmenu)
   133             self.append(menu)
   139         if not box.is_empty():
   134         self.render_items(w)
   140             box.render(self.w)