author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 30 Jun 2009 15:18:07 +0200 | |
branch | stable |
changeset 2203 | efdd3a9f9028 |
parent 2193 | 667b6340bfd4 |
child 2230 | 45d670c00fe1 |
permissions | -rw-r--r-- |
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
1 |
"""Views, forms, actions... for the CubicWeb web client |
0 | 2 |
|
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1719
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1719
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
9 |
|
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
10 |
import os |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
11 |
from tempfile import mktemp |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
12 |
|
0 | 13 |
from rql import nodes |
14 |
||
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
15 |
|
0 | 16 |
def need_table_view(rset, schema): |
17 |
"""return True if we think that a table view is more appropriate than a |
|
18 |
list or primary view to display the given result set |
|
19 |
""" |
|
20 |
rqlst = rset.syntax_tree() |
|
21 |
if len(rqlst.children) > 1: |
|
22 |
# UNION query, use a table |
|
23 |
return True |
|
24 |
selected = rqlst.children[0].selection |
|
25 |
try: |
|
26 |
mainvar = selected[0] |
|
27 |
except AttributeError: |
|
28 |
# not a variable ref, using table view is probably a good option |
|
29 |
return True |
|
30 |
if not (isinstance(mainvar, nodes.VariableRef) or |
|
31 |
(isinstance(mainvar, nodes.Constant) and mainvar.uid)): |
|
32 |
return True |
|
33 |
for i, etype in enumerate(rset.description[0][1:]): |
|
34 |
# etype may be None on outer join |
|
35 |
if etype is None: |
|
36 |
return True |
|
37 |
# check the selected index node is a VariableRef (else we |
|
38 |
# won't detect aggregate function |
|
39 |
if not isinstance(selected[i+1], nodes.VariableRef): |
|
40 |
return True |
|
41 |
# if this is not a final entity |
|
42 |
if not schema.eschema(etype).is_final(): |
|
43 |
return True |
|
44 |
# if this is a final entity not linked to the main variable |
|
45 |
var = selected[i+1].variable |
|
46 |
for vref in var.references(): |
|
47 |
rel = vref.relation() |
|
48 |
if rel is None: |
|
49 |
continue |
|
50 |
if mainvar.is_equivalent(rel.children[0]): |
|
51 |
break |
|
52 |
else: |
|
53 |
return True |
|
54 |
return False |
|
55 |
||
2193
667b6340bfd4
[views] comment the naive content-negotiation algorithm, it breaks FF2 navigation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
56 |
# FIXME: VID_BY_MIMETYPE is unfortunately a bit too naive since |
667b6340bfd4
[views] comment the naive content-negotiation algorithm, it breaks FF2 navigation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
57 |
# some browsers (e.g. FF2) send a bunch of mimetypes in |
667b6340bfd4
[views] comment the naive content-negotiation algorithm, it breaks FF2 navigation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
58 |
# the Accept header, for instance: |
667b6340bfd4
[views] comment the naive content-negotiation algorithm, it breaks FF2 navigation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
59 |
# text/xml,application/xml,application/xhtml+xml,text/html;q=0.9, |
667b6340bfd4
[views] comment the naive content-negotiation algorithm, it breaks FF2 navigation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
60 |
# text/plain;q=0.8,image/png,*/*;q=0.5 |
667b6340bfd4
[views] comment the naive content-negotiation algorithm, it breaks FF2 navigation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
61 |
VID_BY_MIMETYPE = { |
667b6340bfd4
[views] comment the naive content-negotiation algorithm, it breaks FF2 navigation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
62 |
#'text/xml': 'xml', |
667b6340bfd4
[views] comment the naive content-negotiation algorithm, it breaks FF2 navigation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
63 |
# XXX rss, owl... |
667b6340bfd4
[views] comment the naive content-negotiation algorithm, it breaks FF2 navigation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
64 |
} |
0 | 65 |
def vid_from_rset(req, rset, schema): |
66 |
"""given a result set, return a view id""" |
|
67 |
if rset is None: |
|
68 |
return 'index' |
|
1716
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1091
diff
changeset
|
69 |
for mimetype in req.parse_accept_header('Accept'): |
1719
bf26f32c8a72
wrong variable name
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents:
1716
diff
changeset
|
70 |
if mimetype in VID_BY_MIMETYPE: |
bf26f32c8a72
wrong variable name
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents:
1716
diff
changeset
|
71 |
return VID_BY_MIMETYPE[mimetype] |
0 | 72 |
nb_rows = len(rset) |
73 |
# empty resultset |
|
74 |
if nb_rows == 0 : |
|
75 |
return 'noresult' |
|
76 |
# entity result set |
|
77 |
if not schema.eschema(rset.description[0][0]).is_final(): |
|
78 |
if need_table_view(rset, schema): |
|
79 |
return 'table' |
|
80 |
if nb_rows == 1: |
|
81 |
if req.search_state[0] == 'normal': |
|
82 |
return 'primary' |
|
83 |
return 'outofcontext-search' |
|
84 |
return 'list' |
|
85 |
return 'table' |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
86 |
|
1716
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1091
diff
changeset
|
87 |
|
0 | 88 |
def linksearch_select_url(req, rset): |
89 |
"""when searching an entity to create a relation, return an url to select |
|
90 |
entities in the given rset |
|
91 |
""" |
|
92 |
req.add_js( ('cubicweb.ajax.js', 'cubicweb.edition.js') ) |
|
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
|
93 |
target, eid, r_type, searchedtype = req.search_state[1] |
0 | 94 |
if target == 'subject': |
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
|
95 |
id_fmt = '%s:%s:%%s' % (eid, r_type) |
0 | 96 |
else: |
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
|
97 |
id_fmt = '%%s:%s:%s' % (r_type, eid) |
0 | 98 |
triplets = '-'.join(id_fmt % row[0] for row in rset.rows) |
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
|
99 |
return "javascript: selectForAssociation('%s', '%s');" % (triplets, eid) |
1716
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1091
diff
changeset
|
100 |
|
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1091
diff
changeset
|
101 |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
102 |
class TmpFileViewMixin(object): |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
103 |
binary = True |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
104 |
content_type = 'application/octet-stream' |
1716
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1091
diff
changeset
|
105 |
cache_max_age = 60*60*2 # stay in http cache for 2 hours by default |
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1091
diff
changeset
|
106 |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
107 |
def call(self): |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
108 |
self.cell_call() |
1716
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1091
diff
changeset
|
109 |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
110 |
def cell_call(self, row=0, col=0): |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
111 |
self.row, self.col = row, col # in case one need it |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
112 |
tmpfile = mktemp('.png') |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
113 |
try: |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
114 |
self._generate(tmpfile) |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
115 |
self.w(open(tmpfile).read()) |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
116 |
finally: |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
117 |
os.unlink(tmpfile) |