author | sylvain.thenault@logilab.fr |
Wed, 22 Apr 2009 19:44:15 +0200 | |
branch | tls-sprint |
changeset 1436 | 97774eb6443b |
parent 1149 | 1e19b6ef53a1 |
child 1490 | 6b024694d493 |
permissions | -rw-r--r-- |
0 | 1 |
"""common web configuration for twisted/modpython applications |
2 |
||
3 |
:organization: Logilab |
|
447 | 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 |
||
9 |
import os |
|
1132 | 10 |
from os.path import join, exists, split |
0 | 11 |
|
12 |
from logilab.common.configuration import Method |
|
13 |
from logilab.common.decorators import cached |
|
14 |
||
15 |
from cubicweb.toolsutils import read_config |
|
16 |
from cubicweb.cwconfig import CubicWebConfiguration, register_persistent_options, merge_options |
|
17 |
||
18 |
_ = unicode |
|
19 |
||
20 |
register_persistent_options( ( |
|
21 |
# site-wide only web ui configuration |
|
22 |
('site-title', |
|
23 |
{'type' : 'string', 'default': 'unset title', |
|
24 |
'help': _('site title'), |
|
25 |
'sitewide': True, 'group': 'ui', |
|
26 |
}), |
|
27 |
('main-template', |
|
823
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
447
diff
changeset
|
28 |
{'type' : 'string', 'default': 'main-template', |
0 | 29 |
'help': _('id of main template used to render pages'), |
30 |
'sitewide': True, 'group': 'ui', |
|
31 |
}), |
|
32 |
# user web ui configuration |
|
33 |
('fckeditor', |
|
34 |
{'type' : 'yn', 'default': True, |
|
35 |
'help': _('should html fields being edited using fckeditor (a HTML ' |
|
36 |
'WYSIWYG editor). You should also select text/html as default ' |
|
37 |
'text format to actually get fckeditor.'), |
|
38 |
'group': 'ui', |
|
39 |
}), |
|
40 |
# navigation configuration |
|
41 |
('page-size', |
|
42 |
{'type' : 'int', 'default': 40, |
|
43 |
'help': _('maximum number of objects displayed by page of results'), |
|
44 |
'group': 'navigation', |
|
45 |
}), |
|
46 |
('related-limit', |
|
47 |
{'type' : 'int', 'default': 8, |
|
48 |
'help': _('maximum number of related entities to display in the primary ' |
|
49 |
'view'), |
|
50 |
'group': 'navigation', |
|
51 |
}), |
|
52 |
('combobox-limit', |
|
53 |
{'type' : 'int', 'default': 20, |
|
54 |
'help': _('maximum number of entities to display in related combo box'), |
|
55 |
'group': 'navigation', |
|
56 |
}), |
|
57 |
||
58 |
)) |
|
59 |
||
60 |
||
61 |
class WebConfiguration(CubicWebConfiguration): |
|
62 |
"""the WebConfiguration is a singleton object handling application's |
|
63 |
configuration and preferences |
|
64 |
""" |
|
65 |
cubicweb_vobject_path = CubicWebConfiguration.cubicweb_vobject_path | set(['web/views']) |
|
66 |
cube_vobject_path = CubicWebConfiguration.cube_vobject_path | set(['views']) |
|
67 |
||
68 |
options = merge_options(CubicWebConfiguration.options + ( |
|
69 |
('anonymous-user', |
|
70 |
{'type' : 'string', |
|
71 |
'default': None, |
|
72 |
'help': 'login of the CubicWeb user account to use for anonymous user (if you want to allow anonymous)', |
|
73 |
'group': 'main', 'inputlevel': 1, |
|
74 |
}), |
|
75 |
('anonymous-password', |
|
76 |
{'type' : 'string', |
|
77 |
'default': None, |
|
78 |
'help': 'password of the CubicWeb user account to use for anonymous user, ' |
|
79 |
'if anonymous-user is set', |
|
80 |
'group': 'main', 'inputlevel': 1, |
|
81 |
}), |
|
82 |
('query-log-file', |
|
83 |
{'type' : 'string', |
|
84 |
'default': None, |
|
85 |
'help': 'web application query log file', |
|
86 |
'group': 'main', 'inputlevel': 2, |
|
87 |
}), |
|
88 |
('pyro-application-id', |
|
89 |
{'type' : 'string', |
|
90 |
'default': Method('default_application_id'), |
|
91 |
'help': 'CubicWeb application identifier in the Pyro name server', |
|
92 |
'group': 'pyro-client', 'inputlevel': 1, |
|
93 |
}), |
|
94 |
# web configuration |
|
95 |
('https-url', |
|
96 |
{'type' : 'string', |
|
97 |
'default': None, |
|
98 |
'help': 'web server root url on https. By specifying this option your '\ |
|
99 |
'site can be available as an http and https site. Authenticated users '\ |
|
100 |
'will in this case be authenticated and once done navigate through the '\ |
|
101 |
'https site. IMPORTANTE NOTE: to do this work, you should have your '\ |
|
102 |
'apache redirection include "https" as base url path so cubicweb can '\ |
|
103 |
'differentiate between http vs https access. For instance: \n'\ |
|
104 |
'RewriteRule ^/demo/(.*) http://127.0.0.1:8080/https/$1 [L,P]\n'\ |
|
105 |
'where the cubicweb web server is listening on port 8080.', |
|
106 |
'group': 'main', 'inputlevel': 2, |
|
107 |
}), |
|
108 |
('auth-mode', |
|
109 |
{'type' : 'choice', |
|
110 |
'choices' : ('cookie', 'http'), |
|
111 |
'default': 'cookie', |
|
112 |
'help': 'authentication mode (cookie / http)', |
|
113 |
'group': 'web', 'inputlevel': 1, |
|
114 |
}), |
|
115 |
('realm', |
|
116 |
{'type' : 'string', |
|
117 |
'default': 'cubicweb', |
|
118 |
'help': 'realm to use on HTTP authentication mode', |
|
119 |
'group': 'web', 'inputlevel': 2, |
|
120 |
}), |
|
121 |
('http-session-time', |
|
122 |
{'type' : 'int', |
|
123 |
'default': 0, |
|
124 |
'help': 'duration in seconds for HTTP sessions. 0 mean no expiration. '\ |
|
125 |
'Should be greater than RQL server\'s session-time.', |
|
126 |
'group': 'web', 'inputlevel': 2, |
|
127 |
}), |
|
128 |
('cleanup-session-time', |
|
129 |
{'type' : 'int', |
|
130 |
'default': 43200, |
|
131 |
'help': 'duration in seconds for which unused connections should be '\ |
|
132 |
'closed, to limit memory consumption. This is different from '\ |
|
133 |
'http-session-time since in some cases you may have an unexpired http '\ |
|
134 |
'session (e.g. valid session cookie) which will trigger transparent '\ |
|
135 |
'creation of a new session. In other cases, sessions may never expire \ |
|
136 |
and cause memory leak. Should be smaller than http-session-time, '\ |
|
137 |
'unless it\'s 0. Default to 12 h.', |
|
138 |
'group': 'web', 'inputlevel': 2, |
|
139 |
}), |
|
140 |
('cleanup-anonymous-session-time', |
|
141 |
{'type' : 'int', |
|
142 |
'default': 120, |
|
143 |
'help': 'Same as cleanup-session-time but specific to anonymous '\ |
|
144 |
'sessions. Default to 2 min.', |
|
145 |
'group': 'web', 'inputlevel': 2, |
|
146 |
}), |
|
147 |
('embed-allowed', |
|
148 |
{'type' : 'regexp', |
|
149 |
'default': None, |
|
150 |
'help': 'regular expression matching URLs that may be embeded. \ |
|
151 |
leave it blank if you don\'t want the embedding feature, or set it to ".*" \ |
|
152 |
if you want to allow everything', |
|
153 |
'group': 'web', 'inputlevel': 1, |
|
154 |
}), |
|
155 |
('submit-url', |
|
156 |
{'type' : 'string', |
|
157 |
'default': Method('default_submit_url'), |
|
158 |
'help': ('URL that may be used to report bug in this application ' |
|
159 |
'by direct access to the project\'s (jpl) tracker, ' |
|
160 |
'if you want this feature on. The url should looks like ' |
|
161 |
'http://mytracker.com/view?__linkto=concerns:1234:subject&etype=Ticket&type=bug&vid=creation ' |
|
162 |
'where 1234 should be replaced by the eid of your project in ' |
|
163 |
'the tracker. If you have no idea about what I\'am talking ' |
|
164 |
'about, you should probably let no value for this option.'), |
|
165 |
'group': 'web', 'inputlevel': 2, |
|
166 |
}), |
|
167 |
('submit-mail', |
|
168 |
{'type' : 'string', |
|
169 |
'default': None, |
|
170 |
'help': ('Mail used as recipient to report bug in this application, ' |
|
171 |
'if you want this feature on'), |
|
172 |
'group': 'web', 'inputlevel': 2, |
|
173 |
}), |
|
174 |
||
175 |
('language-negociation', |
|
176 |
{'type' : 'yn', |
|
177 |
'default': True, |
|
178 |
'help': 'use Accept-Language http header to try to set user '\ |
|
179 |
'interface\'s language according to browser defined preferences', |
|
180 |
'group': 'web', 'inputlevel': 2, |
|
181 |
}), |
|
182 |
||
183 |
('print-traceback', |
|
184 |
{'type' : 'yn', |
|
185 |
'default': not CubicWebConfiguration.mode == 'installed', |
|
186 |
'help': 'print the traceback on the error page when an error occured', |
|
187 |
'group': 'web', 'inputlevel': 2, |
|
188 |
}), |
|
189 |
)) |
|
190 |
||
191 |
def default_submit_url(self): |
|
192 |
try: |
|
193 |
cube = self.cubes()[0] |
|
194 |
cubeeid = self.cube_pkginfo(cube).cube_eid |
|
1149 | 195 |
except Exception: |
0 | 196 |
return None |
197 |
if cubeeid: |
|
198 |
return 'http://intranet.logilab.fr/jpl/view?__linkto=concerns:%s:subject&etype=Ticket&type=bug&vid=creation' % cubeeid |
|
199 |
return None |
|
200 |
||
890
3530baff9120
make fckeditor actually optional, fix its config, avoid needs for a link to fckeditor.js
sylvain.thenault@logilab.fr
parents:
823
diff
changeset
|
201 |
def fckeditor_installed(self): |
3530baff9120
make fckeditor actually optional, fix its config, avoid needs for a link to fckeditor.js
sylvain.thenault@logilab.fr
parents:
823
diff
changeset
|
202 |
return exists(self.ext_resources['FCKEDITOR_PATH']) |
3530baff9120
make fckeditor actually optional, fix its config, avoid needs for a link to fckeditor.js
sylvain.thenault@logilab.fr
parents:
823
diff
changeset
|
203 |
|
3530baff9120
make fckeditor actually optional, fix its config, avoid needs for a link to fckeditor.js
sylvain.thenault@logilab.fr
parents:
823
diff
changeset
|
204 |
def eproperty_definitions(self): |
893 | 205 |
for key, pdef in super(WebConfiguration, self).eproperty_definitions(): |
890
3530baff9120
make fckeditor actually optional, fix its config, avoid needs for a link to fckeditor.js
sylvain.thenault@logilab.fr
parents:
823
diff
changeset
|
206 |
if key == 'ui.fckeditor' and not self.fckeditor_installed(): |
3530baff9120
make fckeditor actually optional, fix its config, avoid needs for a link to fckeditor.js
sylvain.thenault@logilab.fr
parents:
823
diff
changeset
|
207 |
continue |
3530baff9120
make fckeditor actually optional, fix its config, avoid needs for a link to fckeditor.js
sylvain.thenault@logilab.fr
parents:
823
diff
changeset
|
208 |
yield key, pdef |
3530baff9120
make fckeditor actually optional, fix its config, avoid needs for a link to fckeditor.js
sylvain.thenault@logilab.fr
parents:
823
diff
changeset
|
209 |
|
0 | 210 |
# method used to connect to the repository: 'inmemory' / 'pyro' |
211 |
# Pyro repository by default |
|
212 |
repo_method = 'pyro' |
|
213 |
||
214 |
# don't use @cached: we want to be able to disable it while this must still |
|
215 |
# be cached |
|
216 |
def repository(self, vreg=None): |
|
217 |
"""return the application's repository object""" |
|
218 |
try: |
|
219 |
return self.__repo |
|
220 |
except AttributeError: |
|
221 |
from cubicweb.dbapi import get_repository |
|
222 |
if self.repo_method == 'inmemory': |
|
223 |
repo = get_repository('inmemory', vreg=vreg, config=self) |
|
224 |
else: |
|
225 |
repo = get_repository('pyro', self['pyro-application-id'], |
|
226 |
config=self) |
|
227 |
self.__repo = repo |
|
228 |
return repo |
|
229 |
||
230 |
def vc_config(self): |
|
231 |
return self.repository().get_versions() |
|
232 |
||
233 |
# mapping to external resources (id -> path) (`external_resources` file) ## |
|
234 |
ext_resources = { |
|
235 |
'FAVICON': 'DATADIR/favicon.ico', |
|
236 |
'LOGO': 'DATADIR/logo.png', |
|
237 |
'RSS_LOGO': 'DATADIR/rss.png', |
|
238 |
'HELP': 'DATADIR/help.png', |
|
239 |
'CALENDAR_ICON': 'DATADIR/calendar.gif', |
|
240 |
'SEARCH_GO':'DATADIR/go.png', |
|
241 |
||
242 |
'FCKEDITOR_PATH': '/usr/share/fckeditor/', |
|
243 |
||
244 |
'IE_STYLESHEETS': ['DATADIR/cubicweb.ie.css'], |
|
245 |
'STYLESHEETS': ['DATADIR/cubicweb.css'], |
|
246 |
'STYLESHEETS_PRINT': ['DATADIR/cubicweb.print.css'], |
|
247 |
||
248 |
'JAVASCRIPTS': ['DATADIR/jquery.js', |
|
30
25ef1dddaab8
round corners are back
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
249 |
'DATADIR/jquery.corner.js', |
25ef1dddaab8
round corners are back
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
250 |
'DATADIR/jquery.json.js', |
0 | 251 |
'DATADIR/cubicweb.compat.js', |
252 |
'DATADIR/cubicweb.python.js', |
|
253 |
'DATADIR/cubicweb.htmlhelpers.js'], |
|
254 |
} |
|
255 |
||
256 |
||
257 |
def anonymous_user(self): |
|
258 |
"""return a login and password to use for anonymous users. None |
|
259 |
may be returned for both if anonymous connections are not allowed |
|
260 |
""" |
|
261 |
try: |
|
262 |
user = self['anonymous-user'] |
|
263 |
passwd = self['anonymous-password'] |
|
264 |
except KeyError: |
|
265 |
user, passwd = None, None |
|
266 |
if user is not None: |
|
267 |
user = unicode(user) |
|
268 |
return user, passwd |
|
269 |
||
270 |
def has_resource(self, rid): |
|
271 |
"""return true if an external resource is defined""" |
|
272 |
return bool(self.ext_resources.get(rid)) |
|
273 |
||
274 |
@cached |
|
275 |
def locate_resource(self, rid): |
|
276 |
"""return the directory where the given resource may be found""" |
|
277 |
return self._fs_locate(rid, 'data') |
|
278 |
||
279 |
@cached |
|
280 |
def locate_doc_file(self, fname): |
|
281 |
"""return the directory where the given resource may be found""" |
|
282 |
return self._fs_locate(fname, 'wdoc') |
|
283 |
||
284 |
def _fs_locate(self, rid, rdirectory): |
|
285 |
"""return the directory where the given resource may be found""" |
|
286 |
path = [self.apphome] + self.cubes_path() + [join(self.shared_dir())] |
|
287 |
for directory in path: |
|
288 |
if exists(join(directory, rdirectory, rid)): |
|
289 |
return join(directory, rdirectory) |
|
290 |
||
291 |
def locate_all_files(self, rid, rdirectory='wdoc'): |
|
292 |
"""return all files corresponding to the given resource""" |
|
293 |
path = [self.apphome] + self.cubes_path() + [join(self.shared_dir())] |
|
294 |
for directory in path: |
|
295 |
fpath = join(directory, rdirectory, rid) |
|
296 |
if exists(fpath): |
|
297 |
yield join(fpath) |
|
298 |
||
299 |
def load_configuration(self): |
|
300 |
"""load application's configuration files""" |
|
301 |
super(WebConfiguration, self).load_configuration() |
|
302 |
# load external resources definition |
|
303 |
self._build_ext_resources() |
|
304 |
self._init_base_url() |
|
305 |
||
306 |
def _init_base_url(self): |
|
307 |
# normalize base url(s) |
|
308 |
baseurl = self['base-url'] |
|
309 |
if baseurl and baseurl[-1] != '/': |
|
310 |
baseurl += '/' |
|
311 |
self.global_set_option('base-url', baseurl) |
|
312 |
httpsurl = self['https-url'] |
|
313 |
if httpsurl and httpsurl[-1] != '/': |
|
314 |
httpsurl += '/' |
|
315 |
self.global_set_option('https-url', httpsurl) |
|
316 |
||
317 |
def _build_ext_resources(self): |
|
318 |
libresourcesfile = join(self.shared_dir(), 'data', 'external_resources') |
|
319 |
self.ext_resources.update(read_config(libresourcesfile)) |
|
320 |
for path in reversed([self.apphome] + self.cubes_path()): |
|
321 |
resourcesfile = join(path, 'data', 'external_resources') |
|
322 |
if exists(resourcesfile): |
|
323 |
self.debug('loading %s', resourcesfile) |
|
324 |
self.ext_resources.update(read_config(resourcesfile)) |
|
252
8cd0c2111783
search external_resources in application home
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
151
diff
changeset
|
325 |
resourcesfile = join(self.apphome, 'external_resources') |
8cd0c2111783
search external_resources in application home
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
151
diff
changeset
|
326 |
if exists(resourcesfile): |
8cd0c2111783
search external_resources in application home
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
151
diff
changeset
|
327 |
self.debug('loading %s', resourcesfile) |
8cd0c2111783
search external_resources in application home
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
151
diff
changeset
|
328 |
self.ext_resources.update(read_config(resourcesfile)) |
0 | 329 |
for resource in ('STYLESHEETS', 'STYLESHEETS_PRINT', |
330 |
'IE_STYLESHEETS', 'JAVASCRIPTS'): |
|
331 |
val = self.ext_resources[resource] |
|
332 |
if isinstance(val, str): |
|
333 |
files = [w.strip() for w in val.split(',') if w.strip()] |
|
334 |
self.ext_resources[resource] = files |
|
151
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
335 |
|
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
336 |
|
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
337 |
# static files handling ################################################### |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
338 |
|
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
339 |
@property |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
340 |
def static_directory(self): |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
341 |
return join(self.appdatahome, 'static') |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
342 |
|
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
343 |
def static_file_exists(self, rpath): |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
344 |
return exists(join(self.static_directory, rpath)) |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
345 |
|
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
346 |
def static_file_open(self, rpath, mode='wb'): |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
347 |
staticdir = self.static_directory |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
348 |
rdir, filename = split(rpath) |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
349 |
if rdir: |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
350 |
staticdir = join(staticdir, rdir) |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
351 |
os.makedirs(staticdir) |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
352 |
return file(join(staticdir, filename), mode) |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
353 |
|
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
354 |
def static_file_add(self, rpath, data): |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
355 |
stream = self.static_file_open(rpath) |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
356 |
stream.write(data) |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
357 |
stream.close() |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
358 |
|
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
359 |
def static_file_del(self, rpath): |
447 | 360 |
if self.static_file_exists(rpath): |
151
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
361 |
os.remove(join(self.static_directory, rpath)) |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
30
diff
changeset
|
362 |