]>
git.ipfire.org Git - pbs.git/blob - src/web/base.py
10 from .. import __version__
12 from ..decorators
import *
14 class BaseHandler(tornado
.web
.RequestHandler
):
17 return self
.application
.backend
21 return self
.backend
.db
25 # Get the session from the cookie
26 session_id
= self
.get_cookie("session_id", None)
28 # Search for a valid database session
30 return self
.backend
.sessions
.get(session_id
)
32 def get_current_user(self
):
34 return self
.session
.impersonated_user
or self
.session
.user
36 def get_user_locale(self
):
37 # Get the locale from the user settings
39 return self
.current_user
.locale
41 # If no locale was provided, we take what ever the browser requested
42 return self
.get_browser_locale()
45 def current_address(self
):
47 Returns the IP address the request came from.
49 return self
.request
.headers
.get("X-Real-IP") or self
.request
.remote_ip
54 Returns the HTTP user agent
56 return self
.request
.headers
.get("User-Agent", None)
61 return self
.current_user
.timezone
65 def format_date(self
, date
, relative
=True, shorter
=False,
67 # XXX not very precise but working for now.
68 gmt_offset
= self
.timezone
.utcoffset(date
).total_seconds() / -60
70 return self
.locale
.format_date(date
, gmt_offset
=gmt_offset
,
71 relative
=relative
, shorter
=shorter
, full_format
=full_format
)
73 def get_template_namespace(self
):
74 ns
= tornado
.web
.RequestHandler
.get_template_namespace(self
)
77 "backend" : self
.backend
,
78 "hostname" : self
.request
.host
,
79 "format_date" : self
.format_date
,
80 "format_size" : misc
.format_size
,
81 "friendly_time" : misc
.friendly_time
,
82 "format_email" : misc
.format_email
,
83 "format_filemode" : misc
.format_filemode
,
84 "lang" : self
.locale
.code
[:2],
85 "session" : self
.session
,
86 "version" : __version__
,
87 "year" : time
.strftime("%Y"),
92 def write_error(self
, status_code
, exc_info
=None, **kwargs
):
93 if status_code
in (400, 403, 404):
94 error_document
= "errors/error-%s.html" % status_code
96 error_document
= "errors/error.html"
99 status_message
= httplib
.responses
[status_code
]
101 status_message
= None
103 # Collect more information about the exception if possible.
105 tb
= traceback
.format_exception(*exc_info
)
109 self
.render(error_document
, status_code
=status_code
,
110 status_message
=status_message
, exc_info
=exc_info
, tb
=tb
, **kwargs
)