comparison roundup/cgi/client.py @ 3427:198fe87b0254

add language detection (patch [SF#1360321])
author Alexander Smishlajev <a1s@users.sourceforge.net>
date Sat, 03 Dec 2005 09:35:06 +0000
parents 8c113ca4a5fb
children 8e3c0b88afad
comparison
equal deleted inserted replaced
3426:52f89836d05b 3427:198fe87b0254
1 # $Id: client.py,v 1.216 2005-07-18 02:19:40 richard Exp $ 1 # $Id: client.py,v 1.217 2005-12-03 09:35:06 a1s Exp $
2 2
3 """WWW request handler (also used in the stand-alone server). 3 """WWW request handler (also used in the stand-alone server).
4 """ 4 """
5 __docformat__ = 'restructuredtext' 5 __docformat__ = 'restructuredtext'
6 6
13 from roundup.cgi.actions import * 13 from roundup.cgi.actions import *
14 from roundup.exceptions import * 14 from roundup.exceptions import *
15 from roundup.cgi.exceptions import * 15 from roundup.cgi.exceptions import *
16 from roundup.cgi.form_parser import FormParser 16 from roundup.cgi.form_parser import FormParser
17 from roundup.mailer import Mailer, MessageSendError 17 from roundup.mailer import Mailer, MessageSendError
18 from roundup.cgi import accept_language
18 19
19 def initialiseSecurity(security): 20 def initialiseSecurity(security):
20 '''Create some Permissions and Roles on the security object 21 '''Create some Permissions and Roles on the security object
21 22
22 This function is directly invoked by security.Security.__init__() 23 This function is directly invoked by security.Security.__init__()
194 def inner_main(self): 195 def inner_main(self):
195 '''Process a request. 196 '''Process a request.
196 197
197 The most common requests are handled like so: 198 The most common requests are handled like so:
198 199
199 1. figure out who we are, defaulting to the "anonymous" user 200 1. look for charset and language preferences, set up user locale
201 see determine_charset, determine_language
202 2. figure out who we are, defaulting to the "anonymous" user
200 see determine_user 203 see determine_user
201 2. figure out what the request is for - the context 204 3. figure out what the request is for - the context
202 see determine_context 205 see determine_context
203 3. handle any requested action (item edit, search, ...) 206 4. handle any requested action (item edit, search, ...)
204 see handle_action 207 see handle_action
205 4. render a template, resulting in HTML output 208 5. render a template, resulting in HTML output
206 209
207 In some situations, exceptions occur: 210 In some situations, exceptions occur:
208 211
209 - HTTP Redirect (generally raised by an action) 212 - HTTP Redirect (generally raised by an action)
210 - SendFile (generally raised by determine_context) 213 - SendFile (generally raised by determine_context)
223 ''' 226 '''
224 self.ok_message = [] 227 self.ok_message = []
225 self.error_message = [] 228 self.error_message = []
226 try: 229 try:
227 self.determine_charset() 230 self.determine_charset()
231 self.determine_language()
228 232
229 # make sure we're identified (even anonymously) 233 # make sure we're identified (even anonymously)
230 self.determine_user() 234 self.determine_user()
231 235
232 # figure out the context and desired content template 236 # figure out the context and desired content template
329 If no charset requested by client, use storage charset (utf-8). 333 If no charset requested by client, use storage charset (utf-8).
330 334
331 If the charset is found, and differs from the storage charset, 335 If the charset is found, and differs from the storage charset,
332 recode all form fields of type 'text/plain' 336 recode all form fields of type 'text/plain'
333 """ 337 """
334
335 # look for client charset 338 # look for client charset
336 charset_parameter = 0 339 charset_parameter = 0
337 if self.form.has_key('@charset'): 340 if self.form.has_key('@charset'):
338 charset = self.form['@charset'].value 341 charset = self.form['@charset'].value
339 if charset.lower() == "none": 342 if charset.lower() == "none":
384 except UnicodeError: 387 except UnicodeError:
385 continue 388 continue
386 value = re_charref.sub(_decode_charref, value) 389 value = re_charref.sub(_decode_charref, value)
387 field.value = encoder(value)[0] 390 field.value = encoder(value)[0]
388 391
392 def determine_language(self):
393 """Determine the language"""
394 # look for language parameter
395 # then for language cookie
396 # last for the Accept-Language header
397 if self.form.has_key("@language"):
398 language = self.form["@language"].value
399 if language.lower() == "none":
400 language = ""
401 self.add_cookie("roundup_language", language)
402 elif self.cookie.has_key("roundup_language"):
403 language = self.cookie["roundup_language"].value
404 elif self.instance.config["WEB_USE_BROWSER_LANGUAGE"]:
405 hal = self.env['HTTP_ACCEPT_LANGUAGE']
406 language = accept_language.parse(hal)
407 else:
408 language = ""
409
410 self.language = language
411 if language:
412 self.setTranslator(TranslationService.get_translation(
413 language,
414 tracker_home=self.instance.config["TRACKER_HOME"]))
415
389 def determine_user(self): 416 def determine_user(self):
390 ''' Determine who the user is 417 """Determine who the user is"""
391 '''
392 # determine the uid to use 418 # determine the uid to use
393 self.opendb('admin') 419 self.opendb('admin')
394 420
395 # make sure we have the session Class 421 # make sure we have the session Class
396 self.clean_sessions() 422 self.clean_sessions()
458 484
459 # reopen the database as the correct user 485 # reopen the database as the correct user
460 self.opendb(self.user) 486 self.opendb(self.user)
461 487
462 def opendb(self, username): 488 def opendb(self, username):
463 ''' Open the database and set the current user. 489 """Open the database and set the current user.
464 490
465 Opens a database once. On subsequent calls only the user is set on 491 Opens a database once. On subsequent calls only the user is set on
466 the database object the instance.optimize is set. If we are in 492 the database object the instance.optimize is set. If we are in
467 "Development Mode" (cf. roundup_server) then the database is always 493 "Development Mode" (cf. roundup_server) then the database is always
468 re-opened. 494 re-opened.
469 ''' 495 """
470 # don't do anything if the db is open and the user has not changed 496 # don't do anything if the db is open and the user has not changed
471 if hasattr(self, 'db') and self.db.isCurrentUser(username): 497 if hasattr(self, 'db') and self.db.isCurrentUser(username):
472 return 498 return
473 499
474 # open the database or only set the user 500 # open the database or only set the user

Roundup Issue Tracker: http://roundup-tracker.org/