author | Rémi Cardona <remi.cardona@logilab.fr> |
Wed, 20 May 2015 16:33:01 +0200 | |
changeset 10426 | ce213d6858f1 |
parent 9700 | da7d341cca76 |
child 10582 | bc2f6f0d7433 |
permissions | -rw-r--r-- |
7806
aa30c665bd06
[refactoring] introduce add_etype_button function to properly create button to add a new entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6377
diff
changeset
|
1 |
# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4494
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4494
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4494
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4494
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4494
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4494
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4494
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4494
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4494
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4494
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4494
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4494
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4494
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4494
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4494
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
6377
3bb415310d4f
[schema] introduce some new sets categorizing entity/relation types and benefits from them where possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6276
diff
changeset
|
18 |
"""Views, forms, actions... for the CubicWeb web client""" |
0 | 19 |
|
20 |
__docformat__ = "restructuredtext en" |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
21 |
|
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
22 |
import os |
3122 | 23 |
import sys |
2446
440cb4ea7e5c
[refactor] #342855: replace uses of (deprecated) mktemp
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2230
diff
changeset
|
24 |
import tempfile |
7806
aa30c665bd06
[refactoring] introduce add_etype_button function to properly create button to add a new entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6377
diff
changeset
|
25 |
|
0 | 26 |
from rql import nodes |
7806
aa30c665bd06
[refactoring] introduce add_etype_button function to properly create button to add a new entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6377
diff
changeset
|
27 |
from logilab.mtconverter import xml_escape |
9383
7b3fa5a7a5a3
[views/dot] use an inlined svg for schema and workflow graphs
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7806
diff
changeset
|
28 |
from logilab.common.deprecation import class_deprecated |
0 | 29 |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
30 |
|
0 | 31 |
def need_table_view(rset, schema): |
32 |
"""return True if we think that a table view is more appropriate than a |
|
33 |
list or primary view to display the given result set |
|
34 |
""" |
|
35 |
rqlst = rset.syntax_tree() |
|
36 |
if len(rqlst.children) > 1: |
|
37 |
# UNION query, use a table |
|
38 |
return True |
|
39 |
selected = rqlst.children[0].selection |
|
40 |
try: |
|
41 |
mainvar = selected[0] |
|
42 |
except AttributeError: |
|
43 |
# not a variable ref, using table view is probably a good option |
|
44 |
return True |
|
45 |
if not (isinstance(mainvar, nodes.VariableRef) or |
|
46 |
(isinstance(mainvar, nodes.Constant) and mainvar.uid)): |
|
47 |
return True |
|
48 |
for i, etype in enumerate(rset.description[0][1:]): |
|
49 |
# etype may be None on outer join |
|
50 |
if etype is None: |
|
51 |
return True |
|
52 |
# check the selected index node is a VariableRef (else we |
|
53 |
# won't detect aggregate function |
|
54 |
if not isinstance(selected[i+1], nodes.VariableRef): |
|
55 |
return True |
|
56 |
# if this is not a final entity |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3145
diff
changeset
|
57 |
if not schema.eschema(etype).final: |
0 | 58 |
return True |
59 |
# if this is a final entity not linked to the main variable |
|
60 |
var = selected[i+1].variable |
|
61 |
for vref in var.references(): |
|
62 |
rel = vref.relation() |
|
63 |
if rel is None: |
|
64 |
continue |
|
65 |
if mainvar.is_equivalent(rel.children[0]): |
|
66 |
break |
|
67 |
else: |
|
68 |
return True |
|
69 |
return False |
|
70 |
||
2193
667b6340bfd4
[views] comment the naive content-negotiation algorithm, it breaks FF2 navigation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
71 |
# 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
|
72 |
# 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
|
73 |
# 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
|
74 |
# 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
|
75 |
# 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
|
76 |
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
|
77 |
#'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
|
78 |
# 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
|
79 |
} |
0 | 80 |
def vid_from_rset(req, rset, schema): |
81 |
"""given a result set, return a view id""" |
|
82 |
if rset is None: |
|
83 |
return 'index' |
|
1716
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1091
diff
changeset
|
84 |
for mimetype in req.parse_accept_header('Accept'): |
1719
bf26f32c8a72
wrong variable name
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents:
1716
diff
changeset
|
85 |
if mimetype in VID_BY_MIMETYPE: |
bf26f32c8a72
wrong variable name
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents:
1716
diff
changeset
|
86 |
return VID_BY_MIMETYPE[mimetype] |
0 | 87 |
nb_rows = len(rset) |
88 |
# empty resultset |
|
6276 | 89 |
if nb_rows == 0: |
0 | 90 |
return 'noresult' |
91 |
# entity result set |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3145
diff
changeset
|
92 |
if not schema.eschema(rset.description[0][0]).final: |
0 | 93 |
if need_table_view(rset, schema): |
94 |
return 'table' |
|
95 |
if nb_rows == 1: |
|
96 |
if req.search_state[0] == 'normal': |
|
97 |
return 'primary' |
|
98 |
return 'outofcontext-search' |
|
2491 | 99 |
if len(rset.column_types(0)) == 1: |
4494
ccb7fce7297b
AdaptedList -> SameETypeList
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
100 |
return 'sameetypelist' |
0 | 101 |
return 'list' |
102 |
return 'table' |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
103 |
|
1716
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1091
diff
changeset
|
104 |
|
0 | 105 |
def linksearch_select_url(req, rset): |
9700 | 106 |
"""when searching an entity to create a relation, return a URL to select |
0 | 107 |
entities in the given rset |
108 |
""" |
|
109 |
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
|
110 |
target, eid, r_type, searchedtype = req.search_state[1] |
0 | 111 |
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
|
112 |
id_fmt = '%s:%s:%%s' % (eid, r_type) |
0 | 113 |
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
|
114 |
id_fmt = '%%s:%s:%s' % (r_type, eid) |
0 | 115 |
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
|
116 |
return "javascript: selectForAssociation('%s', '%s');" % (triplets, eid) |
1716
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1091
diff
changeset
|
117 |
|
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1091
diff
changeset
|
118 |
|
7806
aa30c665bd06
[refactoring] introduce add_etype_button function to properly create button to add a new entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6377
diff
changeset
|
119 |
def add_etype_button(req, etype, csscls='addButton right', **urlkwargs): |
aa30c665bd06
[refactoring] introduce add_etype_button function to properly create button to add a new entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6377
diff
changeset
|
120 |
vreg = req.vreg |
aa30c665bd06
[refactoring] introduce add_etype_button function to properly create button to add a new entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6377
diff
changeset
|
121 |
eschema = vreg.schema.eschema(etype) |
aa30c665bd06
[refactoring] introduce add_etype_button function to properly create button to add a new entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6377
diff
changeset
|
122 |
if eschema.has_perm(req, 'add'): |
aa30c665bd06
[refactoring] introduce add_etype_button function to properly create button to add a new entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6377
diff
changeset
|
123 |
url = vreg['etypes'].etype_class(etype).cw_create_url(req, **urlkwargs) |
aa30c665bd06
[refactoring] introduce add_etype_button function to properly create button to add a new entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6377
diff
changeset
|
124 |
return u'<a href="%s" class="%s">%s</a>' % ( |
aa30c665bd06
[refactoring] introduce add_etype_button function to properly create button to add a new entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6377
diff
changeset
|
125 |
xml_escape(url), csscls, req.__('New %s' % etype)) |
aa30c665bd06
[refactoring] introduce add_etype_button function to properly create button to add a new entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6377
diff
changeset
|
126 |
return u'' |
aa30c665bd06
[refactoring] introduce add_etype_button function to properly create button to add a new entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6377
diff
changeset
|
127 |
|
aa30c665bd06
[refactoring] introduce add_etype_button function to properly create button to add a new entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6377
diff
changeset
|
128 |
|
9383
7b3fa5a7a5a3
[views/dot] use an inlined svg for schema and workflow graphs
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7806
diff
changeset
|
129 |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
130 |
class TmpFileViewMixin(object): |
9383
7b3fa5a7a5a3
[views/dot] use an inlined svg for schema and workflow graphs
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7806
diff
changeset
|
131 |
__metaclass__ = class_deprecated |
7b3fa5a7a5a3
[views/dot] use an inlined svg for schema and workflow graphs
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7806
diff
changeset
|
132 |
__deprecation_warning__ = '[3.18] %(cls)s is deprecated' |
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
133 |
binary = True |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
134 |
content_type = 'application/octet-stream' |
1716
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1091
diff
changeset
|
135 |
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
|
136 |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
137 |
def call(self): |
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
138 |
self.cell_call() |
1716
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1091
diff
changeset
|
139 |
|
1091
b5e253c0dd13
a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
140 |
def cell_call(self, row=0, col=0): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3145
diff
changeset
|
141 |
self.cw_row, self.cw_col = row, col # in case one needs it |
3145 | 142 |
fd, tmpfile = tempfile.mkstemp('.png') |
143 |
os.close(fd) |
|
144 |
self._generate(tmpfile) |
|
145 |
self.w(open(tmpfile, 'rb').read()) |
|
146 |
os.unlink(tmpfile) |