]>
git.ipfire.org Git - ipfire.org.git/blob - webapp/handlers_base.py
b784fa2244a6dfc24a4addb26089adf6940373ba
3 from __future__
import division
16 class BaseHandler(tornado
.web
.RequestHandler
):
19 def get_account(self
, uid
):
20 # Find the name of the author
21 return self
.accounts
.find(uid
)
23 def get_user_locale(self
):
24 # The planet is always in english.
25 if self
.hostname
== "planet.ipfire.org":
26 return tornado
.locale
.get("en_US")
28 # Otherwise take the browser locale.
29 return self
.get_browser_locale()
33 # Remove the development prefix
34 return self
.request
.host
.replace(".dev.", ".")
39 "ssl_cipher" : self
.request
.headers
.get("X-Https-Cipher", None),
40 "ssl_protocol" : self
.request
.headers
.get("X-Https-Protocol", None),
44 def render_args(self
):
45 today
= datetime
.date
.today()
48 "format_size" : backend
.util
.format_size
,
49 "format_time" : backend
.util
.format_time
,
50 "hostname" : self
.hostname
,
51 "lang" : self
.locale
.code
[:2],
52 "rss_url" : self
.rss_url
,
55 ret
.update(self
.ssl_params
)
59 def render(self
, *args
, **_kwargs
):
60 kwargs
= self
.render_args
61 kwargs
.update(_kwargs
)
62 tornado
.web
.RequestHandler
.render(self
, *args
, **kwargs
)
64 def render_string(self
, *args
, **_kwargs
):
65 kwargs
= self
.render_args
66 kwargs
.update(_kwargs
)
67 return tornado
.web
.RequestHandler
.render_string(self
, *args
, **kwargs
)
69 def write_error(self
, status_code
, **kwargs
):
70 if status_code
in (404, 500):
73 "exception" : kwargs
.get("exception", None),
74 "message" : httplib
.responses
[status_code
],
76 self
.render("error-%s.html" % status_code
, **render_args
)
78 return tornado
.web
.RequestHandler
.write_error(self
, status_code
, **kwargs
)
80 def static_url(self
, path
, static
=True):
81 ret
= tornado
.web
.RequestHandler
.static_url(self
, path
)
83 if self
.settings
.get("debug", False):
87 return "//static.ipfire.org%s" % ret
91 def get_remote_ip(self
):
92 # Fix for clients behind a proxy that sends "X-Forwarded-For".
93 remote_ips
= self
.request
.remote_ip
.split(", ")
95 for remote_ip
in remote_ips
:
97 addr
= ipaddr
.IPAddress(remote_ip
)
99 # Skip invalid IP addresses.
102 # Check if the given IP address is from a
109 # Return the last IP if nothing else worked
110 return remote_ips
.pop()
112 def get_remote_location(self
):
113 if not hasattr(self
, "__remote_location"):
114 remote_ip
= self
.get_remote_ip()
116 self
.__remote
_location
= self
.geoip
.get_location(remote_ip
)
118 return self
.__remote
_location
120 def get_argument_date(self
, arg
, *args
, **kwargs
):
121 value
= self
.get_argument(arg
, *args
, **kwargs
)
126 return dateutil
.parser
.parse(value
)
128 raise tornado
.web
.HTTPError(400)
132 def get_current_user(self
):
133 session_id
= self
.get_cookie("session_id")
137 # Get account from the session object
138 account
= self
.backend
.accounts
.get_by_session(session_id
, self
.request
.host
)
140 # If the account was not found or the session was not valid
141 # any more, we will remove the cookie.
143 self
.clear_cookie("session_id")
147 def login(self
, username
, password
):
149 account
= self
.backend
.accounts
.find_account(username
)
151 logging
.warning(401, "unknown account: %s" % username
)
155 if not account
.check_password(password
):
156 logging
.warning("invalid password for %s" % account
)
159 # User has logged in, create a session
160 session_id
, session_expires
= self
.backend
.accounts
.create_session(account
,
163 # Check if a new session was created
165 logging
.warning("Could not create session")
168 # Send session cookie to the client
169 self
.set_cookie("session_id", session_id
,
170 domain
=self
.request
.host
, expires
=session_expires
)
175 session_id
= self
.get_cookie("session_id")
180 success
= self
.backend
.accounts
.destroy_session(session_id
, self
.request
.host
)
182 self
.clear_cookie("session_id")
186 return self
.application
.backend
190 return self
.backend
.db
193 def advertisements(self
):
194 return self
.backend
.advertisements
198 return self
.backend
.accounts
202 return self
.backend
.downloads
206 return self
.backend
.fireinfo
210 return self
.backend
.iuse
214 return self
.backend
.memcache
218 return self
.backend
.mirrors
222 return self
.backend
.netboot
226 return self
.backend
.news
230 return self
.backend
.settings
234 return self
.backend
.releases
238 return self
.backend
.geoip
242 return self
.backend
.talk
246 return self
.backend
.tracker
250 return self
.backend
.planet
253 class LoginHandler(BaseHandler
):
255 self
.render("auth/login.html")
258 username
= self
.get_argument("username")
259 password
= self
.get_argument("password")
261 if not self
.login(username
, password
):
262 raise tornado
.web
.HTTPError(401)
264 next
= self
.get_argument("next", "/")
265 return self
.redirect(next
)
268 class LogoutHandler(BaseHandler
):
272 # Get back to the start page