]>
git.ipfire.org Git - ipfire.org.git/blob - webapp/handlers_base.py
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
.request
.host
== "planet.ipfire.org":
26 return tornado
.locale
.get("en_US")
28 # Otherwise take the browser locale.
29 return self
.get_browser_locale()
32 def render_args(self
):
33 today
= datetime
.date
.today()
35 # Get the requested host and remove the development prefix
36 hostname
= self
.request
.host
.replace(".dev.", ".")
39 "format_size" : backend
.util
.format_size
,
40 "format_time" : backend
.util
.format_time
,
41 "hostname" : hostname
,
42 "lang" : self
.locale
.code
[:2],
43 "rss_url" : self
.rss_url
,
47 def render(self
, *args
, **_kwargs
):
48 kwargs
= self
.render_args
49 kwargs
.update(_kwargs
)
50 tornado
.web
.RequestHandler
.render(self
, *args
, **kwargs
)
52 def render_string(self
, *args
, **_kwargs
):
53 kwargs
= self
.render_args
54 kwargs
.update(_kwargs
)
55 return tornado
.web
.RequestHandler
.render_string(self
, *args
, **kwargs
)
57 def write_error(self
, status_code
, **kwargs
):
58 if status_code
in (404, 500):
61 "exception" : kwargs
.get("exception", None),
62 "message" : httplib
.responses
[status_code
],
64 self
.render("error-%s.html" % status_code
, **render_args
)
66 return tornado
.web
.RequestHandler
.write_error(self
, status_code
, **kwargs
)
68 def static_url(self
, path
, static
=True):
69 ret
= tornado
.web
.RequestHandler
.static_url(self
, path
)
71 if self
.settings
.get("debug", False):
74 elif self
.request
.host
== "admin.ipfire.org":
78 return "http://static.ipfire.org%s" % ret
82 def get_remote_ip(self
):
83 # Fix for clients behind a proxy that sends "X-Forwarded-For".
84 remote_ips
= self
.request
.remote_ip
.split(", ")
85 for remote_ip
in remote_ips
:
87 addr
= ipaddr
.IPAddress(remote_ip
)
89 # Skip invalid IP addresses.
92 # Check if the given IP address is from a
99 def get_remote_location(self
):
100 if not hasattr(self
, "__remote_location"):
101 remote_ip
= self
.get_remote_ip()
103 self
.__remote
_location
= self
.geoip
.get_location(remote_ip
)
105 return self
.__remote
_location
107 def get_argument_date(self
, arg
, *args
, **kwargs
):
108 value
= self
.get_argument(arg
, *args
, **kwargs
)
113 return dateutil
.parser
.parse(value
)
115 raise tornado
.web
.HTTPError(400)
119 def get_current_user(self
):
120 session_id
= self
.get_cookie("session_id")
124 # Get account from the session object
125 account
= self
.backend
.accounts
.get_by_session(session_id
, self
.request
.host
)
127 # If the account was not found or the session was not valid
128 # any more, we will remove the cookie.
130 self
.clear_cookie("session_id")
134 def login(self
, username
, password
):
136 account
= self
.backend
.accounts
.find_account(username
)
138 logging
.warning(401, "unknown account: %s" % username
)
142 if not account
.check_password(password
):
143 logging
.warning("invalid password for %s" % account
)
146 # User has logged in, create a session
147 session_id
, session_expires
= self
.backend
.accounts
.create_session(account
,
150 # Check if a new session was created
152 logging
.warning("Could not create session")
155 # Send session cookie to the client
156 self
.set_cookie("session_id", session_id
,
157 domain
=self
.request
.host
, expires
=session_expires
)
162 session_id
= self
.get_cookie("session_id")
167 success
= self
.backend
.accounts
.destroy_session(session_id
, self
.request
.host
)
169 self
.clear_cookie("session_id")
173 return self
.application
.backend
177 return self
.backend
.db
180 def advertisements(self
):
181 return self
.backend
.advertisements
185 return self
.backend
.accounts
189 return self
.backend
.downloads
193 return self
.backend
.fireinfo
197 return self
.backend
.iuse
201 return self
.backend
.memcache
205 return self
.backend
.mirrors
209 return self
.backend
.netboot
213 return self
.backend
.news
217 return self
.backend
.settings
221 return self
.backend
.releases
225 return self
.backend
.geoip
229 return self
.backend
.talk
233 return self
.backend
.tracker
237 return self
.backend
.planet
241 return self
.backend
.wishlist
244 class LoginHandler(BaseHandler
):
246 self
.render("auth/login.html")
249 username
= self
.get_argument("username")
250 password
= self
.get_argument("password")
252 if not self
.login(username
, password
):
253 raise tornado
.web
.HTTPError(401)
255 next
= self
.get_argument("next", "/")
256 return self
.redirect(next
)
259 class LogoutHandler(BaseHandler
):
263 # Get back to the start page