]>
git.ipfire.org Git - people/jschlag/pbs.git/blob - web/handlers_base.py
11 class BaseHandler(tornado
.web
.RequestHandler
):
12 def get_current_user(self
):
13 user
= self
.get_secure_cookie("user")
17 return self
.pakfire
.users
.get_by_name(user
)
19 def get_user_locale(self
):
20 DEFAULT_LOCALE
= tornado
.locale
.get("en_US")
22 [tornado
.locale
.get(l
) for l
in tornado
.locale
.get_supported_locales(None)]
24 # One can append "?locale=de" to mostly and URI on the site and
25 # another output that guessed.
26 locale
= self
.get_argument("locale", None)
28 locale
= tornado
.locale
.get(locale
)
29 for locale
in ALLOWED_LOCALES
:
32 # Get the locale from the user settings.
33 if self
.current_user
and self
.current_user
.locale
:
34 locale
= tornado
.locale
.get(self
.current_user
.locale
)
35 if locale
in ALLOWED_LOCALES
:
38 # If no locale was provided we guess what the browser sends us
39 locale
= self
.get_browser_locale()
40 if locale
in ALLOWED_LOCALES
:
43 # If no one of the cases above worked we use our default locale
47 def render_args(self
):
49 "hostname" : self
.request
.host
,
50 "friendly_size" : backend
.misc
.friendly_size
,
51 "lang" : self
.locale
.code
[:2],
52 "year" : time
.strftime("%Y"),
55 def render(self
, *args
, **kwargs
):
56 kwargs
.update(self
.render_args
)
57 tornado
.web
.RequestHandler
.render(self
, *args
, **kwargs
)
59 def render_string(self
, *args
, **kwargs
):
60 kwargs
.update(self
.render_args
)
61 return tornado
.web
.RequestHandler
.render_string(self
, *args
, **kwargs
)
63 def get_error_html(self
, status_code
, **kwargs
):
64 if status_code
in (404, 500):
67 "exception" : kwargs
.get("exception", None),
68 "message" : httplib
.responses
[status_code
],
70 return self
.render_string("error-%s.html" % status_code
, **render_args
)
72 return tornado
.web
.RequestHandler
.get_error_html(self
, status_code
, **kwargs
)
76 return self
.application
.pakfire