]>
git.ipfire.org Git - people/jschlag/pbs.git/blob - web/handlers_base.py
16 class BaseHandler(tornado
.web
.RequestHandler
):
19 return self
.pakfire
.cache
21 def get_current_user(self
):
22 session_id
= self
.get_cookie("session_id")
26 # Get the session from the database.
27 session
= self
.pakfire
.sessions
.get(session_id
)
29 # Return nothing, if no session was found.
33 # Update the session lifetime.
34 session
.refresh(self
.request
.remote_ip
)
35 self
.set_cookie("session_id", session
.id, expires
=session
.valid_until
)
37 # If the session impersonated a user, we return that one.
38 if session
.impersonated_user
:
39 return session
.impersonated_user
41 # By default, we return the user of this session.
47 return self
.current_user
.session
49 def get_user_locale(self
):
50 # Get the locale from the user settings.
51 if self
.current_user
and self
.current_user
.locale
:
52 locale
= tornado
.locale
.get(self
.current_user
.locale
)
56 # If no locale was provided, we take what ever the browser requested.
57 return self
.get_browser_locale()
60 def remote_address(self
):
62 Returns the IP address the request came from.
64 remote_ips
= self
.request
.remote_ip
.split(", ")
71 return self
.current_user
.timezone
75 def format_date(self
, date
, relative
=True, shorter
=False,
77 # XXX not very precise but working for now.
78 gmt_offset
= self
.timezone
.utcoffset(date
).total_seconds() / -60
80 return self
.locale
.format_date(date
, gmt_offset
=gmt_offset
,
81 relative
=relative
, shorter
=shorter
, full_format
=full_format
)
84 def render_args(self
):
87 session
= self
.current_user
.session
90 "bugtracker" : self
.pakfire
.bugzilla
,
91 "hostname" : self
.request
.host
,
92 "format_date" : self
.format_date
,
93 "format_size" : backend
.misc
.format_size
,
94 "friendly_time" : backend
.misc
.friendly_time
,
95 "format_email" : backend
.misc
.format_email
,
96 "format_filemode" : backend
.misc
.format_filemode
,
97 "lang" : self
.locale
.code
[:2],
98 "pakfire_version" : pakfire
.__version
__,
100 "year" : time
.strftime("%Y"),
105 def render(self
, *args
, **kwargs
):
106 kwargs
.update(self
.render_args
)
107 tornado
.web
.RequestHandler
.render(self
, *args
, **kwargs
)
109 def render_string(self
, *args
, **kwargs
):
110 kwargs
.update(self
.render_args
)
111 return tornado
.web
.RequestHandler
.render_string(self
, *args
, **kwargs
)
113 def get_error_html(self
, status_code
, exception
=None, **kwargs
):
114 error_document
= "errors/error.html"
117 "code" : status_code
,
118 "message" : httplib
.responses
[status_code
],
121 if status_code
in (400, 403, 404):
122 error_document
= "errors/error-%s.html" % status_code
124 # Collect more information about the exception if possible.
126 exception
= traceback
.format_exc()
128 return self
.render_string(error_document
, exception
=exception
, **kwargs
)
132 return self
.application
.pakfire
136 return self
.pakfire
.arches
140 return self
.pakfire
.mirrors
145 Indicates what level of public/non-public things a user
148 if self
.current_user
and self
.current_user
.is_admin():