]>
git.ipfire.org Git - people/shoehn/ipfire.org.git/blob - webapp/handlers_base.py
3 from __future__
import division
15 class BaseHandler(tornado
.web
.RequestHandler
):
18 def get_account(self
, uid
):
19 # Find the name of the author
20 return self
.accounts
.find(uid
)
22 def get_user_locale(self
):
23 # The planet is always in english.
24 if self
.request
.host
== "planet.ipfire.org":
25 return tornado
.locale
.get("en_US")
27 # Otherwise take the browser locale.
28 return self
.get_browser_locale()
31 def render_args(self
):
32 today
= datetime
.date
.today()
34 # Get the requested host and remove the development prefix
35 hostname
= self
.request
.host
.replace(".dev.", ".")
38 "format_size" : backend
.util
.format_size
,
39 "format_time" : backend
.util
.format_time
,
40 "hostname" : hostname
,
41 "lang" : self
.locale
.code
[:2],
42 "rss_url" : self
.rss_url
,
46 def render(self
, *args
, **_kwargs
):
47 kwargs
= self
.render_args
48 kwargs
.update(_kwargs
)
49 tornado
.web
.RequestHandler
.render(self
, *args
, **kwargs
)
51 def render_string(self
, *args
, **_kwargs
):
52 kwargs
= self
.render_args
53 kwargs
.update(_kwargs
)
54 return tornado
.web
.RequestHandler
.render_string(self
, *args
, **kwargs
)
56 def write_error(self
, status_code
, **kwargs
):
57 if status_code
in (404, 500):
60 "exception" : kwargs
.get("exception", None),
61 "message" : httplib
.responses
[status_code
],
63 self
.render("error-%s.html" % status_code
, **render_args
)
65 return tornado
.web
.RequestHandler
.write_error(self
, status_code
, **kwargs
)
67 def static_url(self
, path
, static
=True):
68 ret
= tornado
.web
.RequestHandler
.static_url(self
, path
)
70 if self
.settings
.get("debug", False):
73 elif self
.request
.host
== "admin.ipfire.org":
77 return "http://static.ipfire.org%s" % ret
81 def get_remote_ip(self
):
82 # Fix for clients behind a proxy that sends "X-Forwarded-For".
83 remote_ips
= self
.request
.remote_ip
.split(", ")
84 for remote_ip
in remote_ips
:
86 addr
= ipaddr
.IPAddress(remote_ip
)
88 # Skip invalid IP addresses.
91 # Check if the given IP address is from a
98 def get_remote_location(self
):
99 if not hasattr(self
, "__remote_location"):
100 remote_ip
= self
.get_remote_ip()
102 self
.__remote
_location
= self
.geoip
.get_location(remote_ip
)
104 return self
.__remote
_location
106 def get_argument_date(self
, arg
, *args
, **kwargs
):
107 value
= self
.get_argument(arg
, *args
, **kwargs
)
112 return dateutil
.parser
.parse(value
)
114 raise tornado
.web
.HTTPError(400)
118 def get_current_user(self
):
119 session_id
= self
.get_cookie("session_id")
123 # Get account from the session object
124 account
= self
.backend
.accounts
.get_by_session(session_id
, self
.request
.host
)
126 # If the account was not found or the session was not valid
127 # any more, we will remove the cookie.
129 self
.clear_cookie("session_id")
133 def login(self
, username
, password
):
135 account
= self
.backend
.accounts
.find_account(username
)
137 logging
.warning(401, "unknown account: %s" % username
)
141 if not account
.check_password(password
):
142 logging
.warning("invalid password for %s" % account
)
145 # User has logged in, create a session
146 session_id
, session_expires
= self
.backend
.accounts
.create_session(account
,
149 # Check if a new session was created
151 logging
.warning("Could not create session")
154 # Send session cookie to the client
155 self
.set_cookie("session_id", session_id
,
156 domain
=self
.request
.host
, expires
=session_expires
)
161 session_id
= self
.get_cookie("session_id")
166 success
= self
.backend
.accounts
.destroy_session(session_id
, self
.request
.host
)
168 self
.clear_cookie("session_id")
172 return self
.application
.backend
176 return self
.backend
.db
179 def advertisements(self
):
180 return self
.backend
.advertisements
184 return self
.backend
.accounts
188 return self
.backend
.downloads
192 return self
.backend
.fireinfo
196 return self
.backend
.iuse
200 return self
.backend
.memcache
204 return self
.backend
.mirrors
208 return self
.backend
.netboot
212 return self
.backend
.news
216 return self
.backend
.settings
220 return self
.backend
.releases
224 return self
.backend
.geoip
228 return self
.backend
.talk
232 return self
.backend
.tracker
236 return self
.backend
.planet
240 return self
.backend
.wishlist
243 class LoginHandler(BaseHandler
):
245 self
.render("auth/login.html")
248 username
= self
.get_argument("username")
249 password
= self
.get_argument("password")
251 if not self
.login(username
, password
):
252 raise tornado
.web
.HTTPError(401)
254 next
= self
.get_argument("next", "/")
255 return self
.redirect(next
)
258 class LogoutHandler(BaseHandler
):
262 # Get back to the start page