author | sylvain.thenault@logilab.fr |
Tue, 17 Feb 2009 13:38:30 +0100 | |
branch | tls-sprint |
changeset 655 | ca3c4992c7d1 |
parent 652 | 603c782dc092 |
child 658 | b5c73b5cdc68 |
permissions | -rw-r--r-- |
0 | 1 |
"""abstract component class and base components definition for CubicWeb web client |
2 |
||
3 |
:organization: Logilab |
|
640
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
593
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
6 |
""" |
|
7 |
__docformat__ = "restructuredtext en" |
|
8 |
||
655 | 9 |
from cubicweb.selectors import ( |
10 |
paginated_rset, one_line_rset, primary_view, match_context_prop, |
|
11 |
condition_compat, accepts_compat, has_relation_compat) |
|
12 |
from cubicweb.common.appobject import Component, SingletonComponent, ComponentMixIn |
|
0 | 13 |
from cubicweb.common.utils import merge_dicts |
655 | 14 |
from cubicweb.common.view import View |
0 | 15 |
from cubicweb.common.registerers import action_registerer |
16 |
from cubicweb.common.uilib import html_escape |
|
17 |
||
18 |
_ = unicode |
|
19 |
||
20 |
||
655 | 21 |
class EntityVComponent(ComponentMixIn, View): |
0 | 22 |
"""abstract base class for additinal components displayed in content |
23 |
headers and footer according to: |
|
24 |
|
|
25 |
* the displayed entity's type |
|
26 |
* a context (currently 'header' or 'footer') |
|
27 |
||
28 |
it should be configured using .accepts, .etype, .rtype, .target and |
|
29 |
.context class attributes |
|
30 |
""" |
|
31 |
||
32 |
__registry__ = 'contentnavigation' |
|
33 |
__registerer__ = action_registerer |
|
655 | 34 |
__selectors__ = (one_line_rset, primary_view, match_context_prop,) |
35 |
registered = accepts_compat(has_relation_compat(condition_compat(View.registered.im_func))) |
|
0 | 36 |
|
37 |
property_defs = { |
|
38 |
_('visible'): dict(type='Boolean', default=True, |
|
39 |
help=_('display the box or not')), |
|
40 |
_('order'): dict(type='Int', default=99, |
|
41 |
help=_('display order of the component')), |
|
42 |
_('context'): dict(type='String', default='header', |
|
43 |
vocabulary=(_('navtop'), _('navbottom'), |
|
44 |
_('navcontenttop'), _('navcontentbottom')), |
|
45 |
#vocabulary=(_('header'), _('incontext'), _('footer')), |
|
46 |
help=_('context where this component should be displayed')), |
|
47 |
_('htmlclass'):dict(type='String', default='mainRelated', |
|
48 |
help=_('html class of the component')), |
|
49 |
} |
|
50 |
||
51 |
context = 'navcontentbottom' # 'footer' | 'header' | 'incontext' |
|
52 |
||
53 |
def call(self, view): |
|
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
54 |
return self.cell_call(0, 0, view) |
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
55 |
|
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
56 |
def cell_call(self, row, col, view): |
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
57 |
raise NotImplementedError() |
0 | 58 |
|
59 |
||
655 | 60 |
class NavigationComponent(ComponentMixIn, View): |
0 | 61 |
"""abstract base class for navigation components""" |
655 | 62 |
id = 'navigation' |
237
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
63 |
__selectors__ = (paginated_rset,) |
655 | 64 |
|
0 | 65 |
page_size_property = 'navigation.page-size' |
66 |
start_param = '__start' |
|
67 |
stop_param = '__stop' |
|
68 |
page_link_templ = u'<span class="slice"><a href="%s" title="%s">%s</a></span>' |
|
69 |
selected_page_link_templ = u'<span class="selectedSlice"><a href="%s" title="%s">%s</a></span>' |
|
70 |
previous_page_link_templ = next_page_link_templ = page_link_templ |
|
71 |
no_previous_page_link = no_next_page_link = u'' |
|
589
e1e100276f59
add page_size argument
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
529
diff
changeset
|
72 |
|
e1e100276f59
add page_size argument
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
529
diff
changeset
|
73 |
@classmethod |
e1e100276f59
add page_size argument
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
529
diff
changeset
|
74 |
def selected(cls, req, rset, row=None, col=None, page_size=None, **kwargs): |
e1e100276f59
add page_size argument
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
529
diff
changeset
|
75 |
"""by default web app objects are usually instantiated on |
e1e100276f59
add page_size argument
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
529
diff
changeset
|
76 |
selection according to a request, a result set, and optional |
e1e100276f59
add page_size argument
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
529
diff
changeset
|
77 |
row and col |
e1e100276f59
add page_size argument
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
529
diff
changeset
|
78 |
""" |
e1e100276f59
add page_size argument
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
529
diff
changeset
|
79 |
instance = super(NavigationComponent, cls).selected(req, rset, row, col, **kwargs) |
e1e100276f59
add page_size argument
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
529
diff
changeset
|
80 |
if page_size is not None: |
e1e100276f59
add page_size argument
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
529
diff
changeset
|
81 |
instance.page_size = page_size |
593
6f6549780096
page_size can be given in url attributes
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
589
diff
changeset
|
82 |
elif 'page_size' in req.form: |
6f6549780096
page_size can be given in url attributes
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
589
diff
changeset
|
83 |
instance.page_size = int(req.form['page_size']) |
589
e1e100276f59
add page_size argument
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
529
diff
changeset
|
84 |
return instance |
0 | 85 |
|
86 |
def __init__(self, req, rset): |
|
87 |
super(NavigationComponent, self).__init__(req, rset) |
|
88 |
self.starting_from = 0 |
|
89 |
self.total = rset.rowcount |
|
90 |
||
91 |
def get_page_size(self): |
|
92 |
try: |
|
93 |
return self._page_size |
|
94 |
except AttributeError: |
|
95 |
self._page_size = self.req.property_value(self.page_size_property) |
|
96 |
return self._page_size |
|
97 |
||
98 |
def set_page_size(self, page_size): |
|
99 |
self._page_size = page_size |
|
100 |
||
101 |
page_size = property(get_page_size, set_page_size) |
|
102 |
||
103 |
def page_boundaries(self): |
|
104 |
try: |
|
105 |
stop = int(self.req.form[self.stop_param]) + 1 |
|
106 |
start = int(self.req.form[self.start_param]) |
|
107 |
except KeyError: |
|
108 |
start, stop = 0, self.page_size |
|
109 |
self.starting_from = start |
|
110 |
return start, stop |
|
111 |
||
112 |
def clean_params(self, params): |
|
113 |
if self.start_param in params: |
|
114 |
del params[self.start_param] |
|
115 |
if self.stop_param in params: |
|
116 |
del params[self.stop_param] |
|
117 |
||
118 |
def page_link(self, path, params, start, stop, content): |
|
119 |
url = self.build_url(path, **merge_dicts(params, {self.start_param : start, |
|
120 |
self.stop_param : stop,})) |
|
121 |
url = html_escape(url) |
|
122 |
if start == self.starting_from: |
|
123 |
return self.selected_page_link_templ % (url, content, content) |
|
124 |
return self.page_link_templ % (url, content, content) |
|
125 |
||
126 |
def previous_link(self, params, content='<<', title=_('previous_results')): |
|
127 |
start = self.starting_from |
|
128 |
if not start : |
|
129 |
return self.no_previous_page_link |
|
130 |
start = max(0, start - self.page_size) |
|
131 |
stop = start + self.page_size - 1 |
|
132 |
url = self.build_url(**merge_dicts(params, {self.start_param : start, |
|
133 |
self.stop_param : stop,})) |
|
134 |
url = html_escape(url) |
|
135 |
return self.previous_page_link_templ % (url, title, content) |
|
136 |
||
137 |
def next_link(self, params, content='>>', title=_('next_results')): |
|
138 |
start = self.starting_from + self.page_size |
|
139 |
if start >= self.total: |
|
140 |
return self.no_next_page_link |
|
141 |
stop = start + self.page_size - 1 |
|
142 |
url = self.build_url(**merge_dicts(params, {self.start_param : start, |
|
143 |
self.stop_param : stop,})) |
|
144 |
url = html_escape(url) |
|
145 |
return self.next_page_link_templ % (url, title, content) |
|
146 |
||
147 |
||
148 |
class RelatedObjectsVComponent(EntityVComponent): |
|
149 |
"""a section to display some related entities""" |
|
150 |
vid = 'list' |
|
151 |
||
152 |
def rql(self): |
|
655 | 153 |
"""override this method if you want to use a custom rql query""" |
0 | 154 |
return None |
155 |
||
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
156 |
def cell_call(self, row, col, view=None): |
0 | 157 |
rql = self.rql() |
158 |
if rql is None: |
|
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
159 |
entity = self.rset.get_entity(row, col) |
0 | 160 |
if self.target == 'object': |
161 |
role = 'subject' |
|
162 |
else: |
|
163 |
role = 'object' |
|
164 |
rset = entity.related(self.rtype, role) |
|
165 |
else: |
|
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
166 |
eid = self.rset[row][col] |
0 | 167 |
rset = self.req.execute(self.rql(), {'x': eid}, 'x') |
168 |
if not rset.rowcount: |
|
169 |
return |
|
170 |
self.w(u'<div class="%s">' % self.div_class()) |
|
171 |
self.wview(self.vid, rset, title=self.req._(self.title).capitalize()) |
|
172 |
self.w(u'</div>') |