]>
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_filemode" : misc
.format_filemode
,
83 "lang" : self
.locale
.code
[:2],
84 "session" : self
.session
,
85 "version" : __version__
,
86 "year" : time
.strftime("%Y"),
91 def write_error(self
, status_code
, exc_info
=None, **kwargs
):
92 if status_code
in (400, 403, 404):
93 error_document
= "errors/error-%s.html" % status_code
95 error_document
= "errors/error.html"
98 status_message
= http
.client
.responses
[status_code
]
100 status_message
= None
102 # Collect more information about the exception if possible.
104 tb
= traceback
.format_exception(*exc_info
)
108 self
.render(error_document
, status_code
=status_code
,
109 status_message
=status_message
, exc_info
=exc_info
, tb
=tb
, **kwargs
)
113 def get_argument_bool(self
, name
):
114 arg
= self
.get_argument(name
, default
=None)
118 def get_argument_int(self
, *args
, **kwargs
):
119 arg
= self
.get_argument(*args
, **kwargs
)
127 except (TypeError, ValueError):
128 raise tornado
.web
.HTTPError(400, "%s is not an integer" % arg
)