1 # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
3 # |
|
4 # This file is part of CubicWeb. |
|
5 # |
|
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
|
7 # terms of the GNU Lesser General Public License as published by the Free |
|
8 # Software Foundation, either version 2.1 of the License, or (at your option) |
|
9 # any later version. |
|
10 # |
|
11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
|
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
|
14 # details. |
|
15 # |
|
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/>. |
|
18 """Primary view for bookmarks + user's bookmarks box""" |
|
19 |
|
20 __docformat__ = "restructuredtext en" |
|
21 from cubicweb import _ |
|
22 |
|
23 from logilab.mtconverter import xml_escape |
|
24 |
|
25 from cubicweb import Unauthorized |
|
26 from cubicweb.predicates import is_instance, one_line_rset |
|
27 from cubicweb.web import action, component, htmlwidgets, formwidgets as fw |
|
28 from cubicweb.web.views import uicfg, primary |
|
29 from cubicweb.web.views.ajaxcontroller import ajaxfunc |
|
30 |
|
31 _abaa = uicfg.actionbox_appearsin_addmenu |
|
32 _abaa.tag_subject_of(('*', 'bookmarked_by', '*'), False) |
|
33 _abaa.tag_object_of(('*', 'bookmarked_by', '*'), False) |
|
34 |
|
35 _afs = uicfg.autoform_section |
|
36 _afs.tag_object_of(('*', 'bookmarked_by', 'CWUser'), 'main', 'metadata') |
|
37 _afs.tag_attribute(('Bookmark', 'path'), 'main', 'attributes') |
|
38 _afs.tag_attribute(('Bookmark', 'path'), 'muledit', 'attributes') |
|
39 |
|
40 _affk = uicfg.autoform_field_kwargs |
|
41 _affk.tag_attribute(('Bookmark', 'path'), {'widget': fw.EditableURLWidget}) |
|
42 |
|
43 |
|
44 class FollowAction(action.Action): |
|
45 __regid__ = 'follow' |
|
46 __select__ = one_line_rset() & is_instance('Bookmark') |
|
47 |
|
48 title = _('follow') |
|
49 category = 'mainactions' |
|
50 |
|
51 def url(self): |
|
52 return self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0).actual_url() |
|
53 |
|
54 |
|
55 class BookmarkPrimaryView(primary.PrimaryView): |
|
56 __select__ = is_instance('Bookmark') |
|
57 |
|
58 def cell_call(self, row, col): |
|
59 """the primary view for bookmark entity""" |
|
60 entity = self.cw_rset.complete_entity(row, col) |
|
61 self.w(u' ') |
|
62 self.w(u"<span class='title'><b>") |
|
63 self.w(u"%s : %s" % (self._cw._('Bookmark'), xml_escape(entity.title))) |
|
64 self.w(u"</b></span>") |
|
65 self.w(u'<br/><br/><div class="content"><a href="%s">' % ( |
|
66 xml_escape(entity.actual_url()))) |
|
67 self.w(u'</a>') |
|
68 self.w(u'<p>%s%s</p>' % (self._cw._('Used by:'), ', '.join(xml_escape(u.name()) |
|
69 for u in entity.bookmarked_by))) |
|
70 self.w(u'</div>') |
|
71 |
|
72 |
|
73 class BookmarksBox(component.CtxComponent): |
|
74 """display a box containing all user's bookmarks""" |
|
75 __regid__ = 'bookmarks_box' |
|
76 |
|
77 title = _('bookmarks') |
|
78 order = 40 |
|
79 rql = ('Any B,T,P ORDERBY lower(T) ' |
|
80 'WHERE B is Bookmark,B title T, B path P, B bookmarked_by U, ' |
|
81 'U eid %(x)s') |
|
82 |
|
83 def init_rendering(self): |
|
84 ueid = self._cw.user.eid |
|
85 self.bookmarks_rset = self._cw.execute(self.rql, {'x': ueid}) |
|
86 rschema = self._cw.vreg.schema.rschema('bookmarked_by') |
|
87 eschema = self._cw.vreg.schema.eschema('Bookmark') |
|
88 self.can_delete = rschema.has_perm(self._cw, 'delete', toeid=ueid) |
|
89 self.can_edit = (eschema.has_perm(self._cw, 'add') and |
|
90 rschema.has_perm(self._cw, 'add', toeid=ueid)) |
|
91 if not self.bookmarks_rset and not self.can_edit: |
|
92 raise component.EmptyComponent() |
|
93 self.items = [] |
|
94 |
|
95 def render_body(self, w): |
|
96 ueid = self._cw.user.eid |
|
97 req = self._cw |
|
98 if self.can_delete: |
|
99 req.add_js('cubicweb.ajax.js') |
|
100 for bookmark in self.bookmarks_rset.entities(): |
|
101 label = self.link(bookmark.title, bookmark.action_url()) |
|
102 if self.can_delete: |
|
103 dlink = u'[<a class="action" href="javascript:removeBookmark(%s)" title="%s">-</a>]' % ( |
|
104 bookmark.eid, req._('delete this bookmark')) |
|
105 label = '<div>%s %s</div>' % (dlink, label) |
|
106 self.append(label) |
|
107 if self.can_edit: |
|
108 menu = htmlwidgets.BoxMenu(req._('manage bookmarks')) |
|
109 linkto = 'bookmarked_by:%s:subject' % ueid |
|
110 # use a relative path so that we can move the instance without |
|
111 # loosing bookmarks |
|
112 path = req.relative_path() |
|
113 # XXX if vtitle specified in params, extract it and use it as |
|
114 # default value for bookmark's title |
|
115 url = req.vreg['etypes'].etype_class('Bookmark').cw_create_url( |
|
116 req, __linkto=linkto, path=path) |
|
117 menu.append(self.link(req._('bookmark this page'), url)) |
|
118 if self.bookmarks_rset: |
|
119 if req.user.is_in_group('managers'): |
|
120 bookmarksrql = 'Bookmark B WHERE B bookmarked_by U, U eid %s' % ueid |
|
121 erset = self.bookmarks_rset |
|
122 else: |
|
123 # we can't edit shared bookmarks we don't own |
|
124 bookmarksrql = 'Bookmark B WHERE B bookmarked_by U, B owned_by U, U eid %(x)s' |
|
125 erset = req.execute(bookmarksrql, {'x': ueid}, |
|
126 build_descr=False) |
|
127 bookmarksrql %= {'x': ueid} |
|
128 if erset: |
|
129 url = req.build_url(vid='muledit', rql=bookmarksrql) |
|
130 menu.append(self.link(req._('edit bookmarks'), url)) |
|
131 url = req.user.absolute_url(vid='xaddrelation', rtype='bookmarked_by', |
|
132 target='subject') |
|
133 menu.append(self.link(req._('pick existing bookmarks'), url)) |
|
134 self.append(menu) |
|
135 self.render_items(w) |
|
136 |
|
137 @ajaxfunc |
|
138 def delete_bookmark(self, beid): |
|
139 rql = 'DELETE B bookmarked_by U WHERE B eid %(b)s, U eid %(u)s' |
|
140 self._cw.execute(rql, {'b': int(beid), 'u' : self._cw.user.eid}) |
|