author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Tue, 17 Feb 2009 21:29:58 +0100 | |
branch | tls-sprint |
changeset 692 | 800592b8d39b |
parent 631 | 99f5852f8604 |
child 742 | 99115e029dca |
permissions | -rw-r--r-- |
0 | 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 |
|
692
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
631
diff
changeset
|
12 |
from cubicweb.selectors import implements |
0 | 13 |
from cubicweb.web.htmlwidgets import BoxWidget, BoxMenu, RawBoxItem |
631
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
14 |
from cubicweb.web.action import Action |
0 | 15 |
from cubicweb.web.box import UserRQLBoxTemplate |
16 |
from cubicweb.web.views.baseviews import PrimaryView |
|
17 |
||
18 |
||
631
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
19 |
class FollowAction(Action): |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
20 |
id = 'follow' |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
21 |
__selectors__ = (implements('Bookmark'),) |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
22 |
|
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
23 |
title = _('follow') |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
24 |
category = 'mainactions' |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
25 |
|
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
26 |
def url(self): |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
27 |
return self.rset.get_entity(self.row or 0, self.col or 0).actual_url() |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
28 |
|
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
29 |
|
0 | 30 |
class BookmarkPrimaryView(PrimaryView): |
631
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
31 |
__selectors__ = (implements('Bookmark'),) |
0 | 32 |
|
33 |
def cell_call(self, row, col): |
|
34 |
"""the primary view for bookmark entity""" |
|
35 |
entity = self.complete_entity(row, col) |
|
36 |
self.w(u' ') |
|
37 |
self.w(u"<span class='title'><b>") |
|
38 |
self.w(u"%s : %s" % (self.req._('Bookmark'), html_escape(entity.title))) |
|
39 |
self.w(u"</b></span>") |
|
40 |
self.w(u'<br/><br/><div class="content"><a href="%s">' % ( |
|
41 |
html_escape(entity.actual_url()))) |
|
42 |
self.w(u'</a>') |
|
43 |
self.w(u'<p>%s%s</p>' % (self.req._('Used by:'), ', '.join(html_escape(u.name()) |
|
44 |
for u in entity.bookmarked_by))) |
|
45 |
self.w(u'</div>') |
|
46 |
||
47 |
||
48 |
class BookmarksBox(UserRQLBoxTemplate): |
|
49 |
"""display a box containing all user's bookmarks""" |
|
50 |
id = 'bookmarks_box' |
|
51 |
order = 40 |
|
52 |
title = _('bookmarks') |
|
53 |
rql = ('Any B,T,P ORDERBY lower(T) ' |
|
54 |
'WHERE B is Bookmark,B title T, B path P, B bookmarked_by U, ' |
|
55 |
'U eid %(x)s') |
|
56 |
etype = 'Bookmark' |
|
57 |
rtype = 'bookmarked_by' |
|
58 |
||
59 |
||
60 |
def call(self, **kwargs): |
|
61 |
req = self.req |
|
62 |
ueid = req.user.eid |
|
63 |
try: |
|
64 |
rset = req.execute(self.rql, {'x': ueid}) |
|
65 |
except Unauthorized: |
|
66 |
# can't access to something in the query, forget this box |
|
67 |
return |
|
68 |
box = BoxWidget(req._(self.title), self.id) |
|
69 |
box.listing_class = 'sideBox' |
|
70 |
rschema = self.schema.rschema(self.rtype) |
|
71 |
eschema = self.schema.eschema(self.etype) |
|
72 |
candelete = rschema.has_perm(req, 'delete', toeid=ueid) |
|
73 |
if candelete: |
|
74 |
req.add_js( ('cubicweb.ajax.js', 'cubicweb.bookmarks.js') ) |
|
75 |
else: |
|
76 |
dlink = None |
|
77 |
for bookmark in rset.entities(): |
|
78 |
label = '<a href="%s">%s</a>' % (html_escape(bookmark.action_url()), |
|
79 |
html_escape(bookmark.title)) |
|
80 |
if candelete: |
|
81 |
dlink = u'[<a href="javascript:removeBookmark(%s)" title="%s">-</a>]' % ( |
|
82 |
bookmark.eid, _('delete this bookmark')) |
|
83 |
label = '%s %s' % (dlink, label) |
|
84 |
box.append(RawBoxItem(label, liclass=u'invisible')) |
|
85 |
if eschema.has_perm(req, 'add') and rschema.has_perm(req, 'add', toeid=ueid): |
|
86 |
boxmenu = BoxMenu(req._('manage bookmarks'), liclass=u'invisible') |
|
87 |
linkto = 'bookmarked_by:%s:subject' % ueid |
|
88 |
# use a relative path so that we can move the application without |
|
89 |
# loosing bookmarks |
|
90 |
path = req.relative_path() |
|
91 |
url = self.create_url(self.etype, __linkto=linkto, path=path) |
|
92 |
boxmenu.append(self.mk_action(req._('bookmark this page'), url, |
|
93 |
category='manage', id='bookmark')) |
|
94 |
if rset: |
|
95 |
if req.user.is_in_group('managers'): |
|
96 |
bookmarksrql = 'Bookmark B WHERE B bookmarked_by U, U eid %s' % ueid |
|
97 |
erset = rset |
|
98 |
else: |
|
99 |
# we can't edit shared bookmarks we don't own |
|
100 |
bookmarksrql = 'Bookmark B WHERE B bookmarked_by U, B owned_by U, U eid %(x)s' |
|
101 |
erset = req.execute(bookmarksrql, {'x': ueid}, 'x', |
|
102 |
build_descr=False) |
|
103 |
bookmarksrql %= {'x': ueid} |
|
104 |
if erset: |
|
105 |
url = self.build_url(vid='muledit', rql=bookmarksrql) |
|
106 |
boxmenu.append(self.mk_action(self.req._('edit bookmarks'), url, category='manage')) |
|
107 |
url = req.user.absolute_url(vid='xaddrelation', rtype='bookmarked_by', |
|
108 |
target='subject') |
|
109 |
boxmenu.append(self.mk_action(self.req._('pick existing bookmarks'), url, category='manage')) |
|
110 |
box.append(boxmenu) |
|
111 |
if not box.is_empty(): |
|
112 |
box.render(self.w) |