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