web/views/bookmark.py
changeset 0 b97547f5f1fa
child 631 99f5852f8604
child 852 105893288777
equal deleted inserted replaced
-1:000000000000 0:b97547f5f1fa
       
     1 """Primary view for bookmarks + user's bookmarks box
       
     2 
       
     3 :organization: Logilab
       
     4 :copyright: 2001-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     6 """
       
     7 __docformat__ = "restructuredtext en"
       
     8 
       
     9 from logilab.mtconverter import html_escape
       
    10 
       
    11 from cubicweb import Unauthorized
       
    12 from cubicweb.web.htmlwidgets import BoxWidget, BoxMenu, RawBoxItem
       
    13 from cubicweb.web.box import UserRQLBoxTemplate
       
    14 from cubicweb.web.views.baseviews import PrimaryView
       
    15 
       
    16 
       
    17 class BookmarkPrimaryView(PrimaryView):
       
    18     accepts = ('Bookmark',)
       
    19         
       
    20     def cell_call(self, row, col):
       
    21         """the primary view for bookmark entity"""
       
    22         entity = self.complete_entity(row, col)
       
    23         self.w(u' ')
       
    24         self.w(u"<span class='title'><b>")
       
    25         self.w(u"%s : %s" % (self.req._('Bookmark'), html_escape(entity.title)))
       
    26         self.w(u"</b></span>")
       
    27         self.w(u'<br/><br/><div class="content"><a href="%s">' % (
       
    28             html_escape(entity.actual_url())))
       
    29         self.w(u'</a>')
       
    30         self.w(u'<p>%s%s</p>' % (self.req._('Used by:'), ', '.join(html_escape(u.name())
       
    31                                                                    for u in entity.bookmarked_by)))
       
    32         self.w(u'</div>')
       
    33 
       
    34 
       
    35 class BookmarksBox(UserRQLBoxTemplate):
       
    36     """display a box containing all user's bookmarks"""
       
    37     id = 'bookmarks_box'
       
    38     order = 40
       
    39     title = _('bookmarks')
       
    40     rql = ('Any B,T,P ORDERBY lower(T) '
       
    41            'WHERE B is Bookmark,B title T, B path P, B bookmarked_by U, '
       
    42            'U eid %(x)s')
       
    43     etype = 'Bookmark'
       
    44     rtype = 'bookmarked_by'
       
    45     
       
    46     
       
    47     def call(self, **kwargs):
       
    48         req = self.req
       
    49         ueid = req.user.eid
       
    50         try:
       
    51             rset = req.execute(self.rql, {'x': ueid})
       
    52         except Unauthorized:
       
    53             # can't access to something in the query, forget this box
       
    54             return
       
    55         box = BoxWidget(req._(self.title), self.id)
       
    56         box.listing_class = 'sideBox'
       
    57         rschema = self.schema.rschema(self.rtype)
       
    58         eschema = self.schema.eschema(self.etype)
       
    59         candelete = rschema.has_perm(req, 'delete', toeid=ueid)
       
    60         if candelete:
       
    61             req.add_js( ('cubicweb.ajax.js', 'cubicweb.bookmarks.js') )
       
    62         else:
       
    63             dlink = None
       
    64         for bookmark in rset.entities():
       
    65             label = '<a href="%s">%s</a>' % (html_escape(bookmark.action_url()),
       
    66                                              html_escape(bookmark.title))
       
    67             if candelete:
       
    68                 dlink = u'[<a href="javascript:removeBookmark(%s)" title="%s">-</a>]' % (
       
    69                     bookmark.eid, _('delete this bookmark'))
       
    70                 label = '%s %s' % (dlink, label)
       
    71             box.append(RawBoxItem(label, liclass=u'invisible'))
       
    72         if eschema.has_perm(req, 'add') and rschema.has_perm(req, 'add', toeid=ueid):
       
    73             boxmenu = BoxMenu(req._('manage bookmarks'), liclass=u'invisible')
       
    74             linkto = 'bookmarked_by:%s:subject' % ueid
       
    75             # use a relative path so that we can move the application without
       
    76             # loosing bookmarks
       
    77             path = req.relative_path()
       
    78             url = self.create_url(self.etype, __linkto=linkto, path=path)
       
    79             boxmenu.append(self.mk_action(req._('bookmark this page'), url,
       
    80                                           category='manage', id='bookmark'))
       
    81             if rset:
       
    82                 if req.user.is_in_group('managers'):
       
    83                     bookmarksrql = 'Bookmark B WHERE B bookmarked_by U, U eid %s' % ueid
       
    84                     erset = rset
       
    85                 else:
       
    86                     # we can't edit shared bookmarks we don't own
       
    87                     bookmarksrql = 'Bookmark B WHERE B bookmarked_by U, B owned_by U, U eid %(x)s'
       
    88                     erset = req.execute(bookmarksrql, {'x': ueid}, 'x',
       
    89                                                 build_descr=False)
       
    90                     bookmarksrql %= {'x': ueid}
       
    91                 if erset:
       
    92                     url = self.build_url(vid='muledit', rql=bookmarksrql)
       
    93                     boxmenu.append(self.mk_action(self.req._('edit bookmarks'), url, category='manage'))
       
    94             url = req.user.absolute_url(vid='xaddrelation', rtype='bookmarked_by',
       
    95                                         target='subject')
       
    96             boxmenu.append(self.mk_action(self.req._('pick existing bookmarks'), url, category='manage'))
       
    97             box.append(boxmenu)
       
    98         if not box.is_empty():
       
    99             box.render(self.w)