author | sylvain.thenault@logilab.fr |
Tue, 07 Apr 2009 19:51:35 +0200 | |
branch | tls-sprint |
changeset 1269 | 94a5c30575c0 |
parent 1091 | b5e253c0dd13 |
child 1716 | b12d9e22bac3 |
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 |
|
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
|
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" |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
8 |
|
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
9 |
import os |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
10 |
from tempfile import mktemp |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
11 |
|
0 | 12 |
from rql import nodes |
13 |
||
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
14 |
|
0 | 15 |
def need_table_view(rset, schema): |
16 |
"""return True if we think that a table view is more appropriate than a |
|
17 |
list or primary view to display the given result set |
|
18 |
""" |
|
19 |
rqlst = rset.syntax_tree() |
|
20 |
if len(rqlst.children) > 1: |
|
21 |
# UNION query, use a table |
|
22 |
return True |
|
23 |
selected = rqlst.children[0].selection |
|
24 |
try: |
|
25 |
mainvar = selected[0] |
|
26 |
except AttributeError: |
|
27 |
# not a variable ref, using table view is probably a good option |
|
28 |
return True |
|
29 |
if not (isinstance(mainvar, nodes.VariableRef) or |
|
30 |
(isinstance(mainvar, nodes.Constant) and mainvar.uid)): |
|
31 |
return True |
|
32 |
for i, etype in enumerate(rset.description[0][1:]): |
|
33 |
# etype may be None on outer join |
|
34 |
if etype is None: |
|
35 |
return True |
|
36 |
# check the selected index node is a VariableRef (else we |
|
37 |
# won't detect aggregate function |
|
38 |
if not isinstance(selected[i+1], nodes.VariableRef): |
|
39 |
return True |
|
40 |
# if this is not a final entity |
|
41 |
if not schema.eschema(etype).is_final(): |
|
42 |
return True |
|
43 |
# if this is a final entity not linked to the main variable |
|
44 |
var = selected[i+1].variable |
|
45 |
for vref in var.references(): |
|
46 |
rel = vref.relation() |
|
47 |
if rel is None: |
|
48 |
continue |
|
49 |
if mainvar.is_equivalent(rel.children[0]): |
|
50 |
break |
|
51 |
else: |
|
52 |
return True |
|
53 |
return False |
|
54 |
||
55 |
||
56 |
def vid_from_rset(req, rset, schema): |
|
57 |
"""given a result set, return a view id""" |
|
58 |
if rset is None: |
|
59 |
return 'index' |
|
60 |
nb_rows = len(rset) |
|
61 |
# empty resultset |
|
62 |
if nb_rows == 0 : |
|
63 |
return 'noresult' |
|
64 |
# entity result set |
|
65 |
if not schema.eschema(rset.description[0][0]).is_final(): |
|
66 |
if need_table_view(rset, schema): |
|
67 |
return 'table' |
|
68 |
if nb_rows == 1: |
|
69 |
if req.search_state[0] == 'normal': |
|
70 |
return 'primary' |
|
71 |
return 'outofcontext-search' |
|
72 |
return 'list' |
|
73 |
return 'table' |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
74 |
|
0 | 75 |
|
76 |
def linksearch_select_url(req, rset): |
|
77 |
"""when searching an entity to create a relation, return an url to select |
|
78 |
entities in the given rset |
|
79 |
""" |
|
80 |
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
|
81 |
target, eid, r_type, searchedtype = req.search_state[1] |
0 | 82 |
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
|
83 |
id_fmt = '%s:%s:%%s' % (eid, r_type) |
0 | 84 |
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
|
85 |
id_fmt = '%%s:%s:%s' % (r_type, eid) |
0 | 86 |
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
|
87 |
return "javascript: selectForAssociation('%s', '%s');" % (triplets, eid) |
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
88 |
|
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
89 |
|
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
90 |
class TmpFileViewMixin(object): |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
91 |
binary = True |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
92 |
content_type = 'application/octet-stream' |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
93 |
cache_max_age = 60*60*2 # stay in http cache for 2 hours by default |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
94 |
|
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
95 |
def call(self): |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
96 |
self.cell_call() |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
97 |
|
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
98 |
def cell_call(self, row=0, col=0): |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
99 |
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
|
100 |
tmpfile = mktemp('.png') |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
101 |
try: |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
102 |
self._generate(tmpfile) |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
103 |
self.w(open(tmpfile).read()) |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
104 |
finally: |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
105 |
os.unlink(tmpfile) |