]>
git.ipfire.org Git - people/shoehn/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
.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.", ".")
37 def render_args(self
):
38 today
= datetime
.date
.today()
41 "format_size" : backend
.util
.format_size
,
42 "format_time" : backend
.util
.format_time
,
43 "hostname" : self
.hostname
,
44 "lang" : self
.locale
.code
[:2],
45 "rss_url" : self
.rss_url
,
49 def render(self
, *args
, **_kwargs
):
50 kwargs
= self
.render_args
51 kwargs
.update(_kwargs
)
52 tornado
.web
.RequestHandler
.render(self
, *args
, **kwargs
)
54 def render_string(self
, *args
, **_kwargs
):
55 kwargs
= self
.render_args
56 kwargs
.update(_kwargs
)
57 return tornado
.web
.RequestHandler
.render_string(self
, *args
, **kwargs
)
59 def write_error(self
, status_code
, **kwargs
):
60 if status_code
in (404, 500):
63 "exception" : kwargs
.get("exception", None),
64 "message" : httplib
.responses
[status_code
],
66 self
.render("error-%s.html" % status_code
, **render_args
)
68 return tornado
.web
.RequestHandler
.write_error(self
, status_code
, **kwargs
)
70 def static_url(self
, path
, static
=True):
71 ret
= tornado
.web
.RequestHandler
.static_url(self
, path
)
73 if self
.settings
.get("debug", False):
76 elif self
.request
.host
== "admin.ipfire.org":
80 return "http://static.ipfire.org%s" % ret
84 def get_remote_ip(self
):
85 # Fix for clients behind a proxy that sends "X-Forwarded-For".
86 remote_ips
= self
.request
.remote_ip
.split(", ")
87 for remote_ip
in remote_ips
:
89 addr
= ipaddr
.IPAddress(remote_ip
)
91 # Skip invalid IP addresses.
94 # Check if the given IP address is from a
101 def get_remote_location(self
):
102 if not hasattr(self
, "__remote_location"):
103 remote_ip
= self
.get_remote_ip()
105 self
.__remote
_location
= self
.geoip
.get_location(remote_ip
)
107 return self
.__remote
_location
109 def get_argument_date(self
, arg
, *args
, **kwargs
):
110 value
= self
.get_argument(arg
, *args
, **kwargs
)
115 return dateutil
.parser
.parse(value
)
117 raise tornado
.web
.HTTPError(400)
121 def get_current_user(self
):
122 session_id
= self
.get_cookie("session_id")
126 # Get account from the session object
127 account
= self
.backend
.accounts
.get_by_session(session_id
, self
.request
.host
)
129 # If the account was not found or the session was not valid
130 # any more, we will remove the cookie.
132 self
.clear_cookie("session_id")
136 def login(self
, username
, password
):
138 account
= self
.backend
.accounts
.find_account(username
)
140 logging
.warning(401, "unknown account: %s" % username
)
144 if not account
.check_password(password
):
145 logging
.warning("invalid password for %s" % account
)
148 # User has logged in, create a session
149 session_id
, session_expires
= self
.backend
.accounts
.create_session(account
,
152 # Check if a new session was created
154 logging
.warning("Could not create session")
157 # Send session cookie to the client
158 self
.set_cookie("session_id", session_id
,
159 domain
=self
.request
.host
, expires
=session_expires
)
164 session_id
= self
.get_cookie("session_id")
169 success
= self
.backend
.accounts
.destroy_session(session_id
, self
.request
.host
)
171 self
.clear_cookie("session_id")
175 return self
.application
.backend
179 return self
.backend
.db
182 def advertisements(self
):
183 return self
.backend
.advertisements
187 return self
.backend
.accounts
191 return self
.backend
.downloads
195 return self
.backend
.fireinfo
199 return self
.backend
.iuse
203 return self
.backend
.memcache
207 return self
.backend
.mirrors
211 return self
.backend
.netboot
215 return self
.backend
.news
219 return self
.backend
.settings
223 return self
.backend
.releases
227 return self
.backend
.geoip
231 return self
.backend
.talk
235 return self
.backend
.tracker
239 return self
.backend
.planet
243 return self
.backend
.wishlist
246 class LoginHandler(BaseHandler
):
248 self
.render("auth/login.html")
251 username
= self
.get_argument("username")
252 password
= self
.get_argument("password")
254 if not self
.login(username
, password
):
255 raise tornado
.web
.HTTPError(401)
257 next
= self
.get_argument("next", "/")
258 return self
.redirect(next
)
261 class LogoutHandler(BaseHandler
):
265 # Get back to the start page