]>
git.ipfire.org Git - people/jschlag/pbs.git/blob - src/web/handlers_base.py
13 from .. import __version__
15 from ..decorators
import *
17 class BaseHandler(tornado
.web
.RequestHandler
):
20 return self
.application
.backend
24 return self
.backend
.db
28 # Get the session from the cookie
29 session_id
= self
.get_cookie("session_id", None)
31 # Search for a valid database session
33 session
= self
.backend
.sessions
.get(session_id
)
35 # Found a valid session
39 def get_current_user(self
):
41 return self
.session
.impersonated_user
or self
.session
.user
43 def get_user_locale(self
):
44 # Get the locale from the user settings.
45 if self
.current_user
and self
.current_user
.locale
:
46 locale
= tornado
.locale
.get(self
.current_user
.locale
)
50 # If no locale was provided, we take what ever the browser requested.
51 return self
.get_browser_locale()
54 def current_address(self
):
56 Returns the IP address the request came from.
58 return self
.request
.headers
.get("X-Real-IP") or self
.request
.remote_ip
63 Returns the HTTP user agent
65 return self
.request
.headers
.get("User-Agent", None)
70 return self
.current_user
.timezone
74 def format_date(self
, date
, relative
=True, shorter
=False,
76 # XXX not very precise but working for now.
77 gmt_offset
= self
.timezone
.utcoffset(date
).total_seconds() / -60
79 return self
.locale
.format_date(date
, gmt_offset
=gmt_offset
,
80 relative
=relative
, shorter
=shorter
, full_format
=full_format
)
82 def get_template_namespace(self
):
83 ns
= tornado
.web
.RequestHandler
.get_template_namespace(self
)
86 "bugtracker" : self
.pakfire
.bugzilla
,
87 "hostname" : self
.request
.host
,
88 "format_date" : self
.format_date
,
89 "format_size" : misc
.format_size
,
90 "friendly_time" : misc
.friendly_time
,
91 "format_email" : misc
.format_email
,
92 "format_filemode" : misc
.format_filemode
,
93 "lang" : self
.locale
.code
[:2],
94 "session" : self
.session
,
95 "version" : __version__
,
96 "year" : time
.strftime("%Y"),
101 def get_error_html(self
, status_code
, exception
=None, **kwargs
):
102 error_document
= "errors/error.html"
105 "code" : status_code
,
106 "message" : httplib
.responses
[status_code
],
109 if status_code
in (400, 403, 404):
110 error_document
= "errors/error-%s.html" % status_code
112 # Collect more information about the exception if possible.
114 exception
= traceback
.format_exc()
116 return self
.render_string(error_document
, exception
=exception
, **kwargs
)
124 return self
.pakfire
.mirrors
129 Indicates what level of public/non-public things a user
132 if self
.current_user
and self
.current_user
.is_admin():