]>
git.ipfire.org Git - people/shoehn/ipfire.org.git/blob - webapp/handlers_base.py
a52aacf9da2646b39e852d1c01be163fc9c99962
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(", ")
88 for remote_ip
in remote_ips
:
90 addr
= ipaddr
.IPAddress(remote_ip
)
92 # Skip invalid IP addresses.
95 # Check if the given IP address is from a
102 # Return the last IP if nothing else worked
103 return remote_ips
.pop()
105 def get_remote_location(self
):
106 if not hasattr(self
, "__remote_location"):
107 remote_ip
= self
.get_remote_ip()
109 self
.__remote
_location
= self
.geoip
.get_location(remote_ip
)
111 return self
.__remote
_location
113 def get_argument_date(self
, arg
, *args
, **kwargs
):
114 value
= self
.get_argument(arg
, *args
, **kwargs
)
119 return dateutil
.parser
.parse(value
)
121 raise tornado
.web
.HTTPError(400)
125 def get_current_user(self
):
126 session_id
= self
.get_cookie("session_id")
130 # Get account from the session object
131 account
= self
.backend
.accounts
.get_by_session(session_id
, self
.request
.host
)
133 # If the account was not found or the session was not valid
134 # any more, we will remove the cookie.
136 self
.clear_cookie("session_id")
140 def login(self
, username
, password
):
142 account
= self
.backend
.accounts
.find_account(username
)
144 logging
.warning(401, "unknown account: %s" % username
)
148 if not account
.check_password(password
):
149 logging
.warning("invalid password for %s" % account
)
152 # User has logged in, create a session
153 session_id
, session_expires
= self
.backend
.accounts
.create_session(account
,
156 # Check if a new session was created
158 logging
.warning("Could not create session")
161 # Send session cookie to the client
162 self
.set_cookie("session_id", session_id
,
163 domain
=self
.request
.host
, expires
=session_expires
)
168 session_id
= self
.get_cookie("session_id")
173 success
= self
.backend
.accounts
.destroy_session(session_id
, self
.request
.host
)
175 self
.clear_cookie("session_id")
179 return self
.application
.backend
183 return self
.backend
.db
186 def advertisements(self
):
187 return self
.backend
.advertisements
191 return self
.backend
.accounts
195 return self
.backend
.downloads
199 return self
.backend
.fireinfo
203 return self
.backend
.iuse
207 return self
.backend
.memcache
211 return self
.backend
.mirrors
215 return self
.backend
.netboot
219 return self
.backend
.news
223 return self
.backend
.settings
227 return self
.backend
.releases
231 return self
.backend
.geoip
235 return self
.backend
.talk
239 return self
.backend
.tracker
243 return self
.backend
.planet
247 return self
.backend
.wishlist
250 class LoginHandler(BaseHandler
):
252 self
.render("auth/login.html")
255 username
= self
.get_argument("username")
256 password
= self
.get_argument("password")
258 if not self
.login(username
, password
):
259 raise tornado
.web
.HTTPError(401)
261 next
= self
.get_argument("next", "/")
262 return self
.redirect(next
)
265 class LogoutHandler(BaseHandler
):
269 # Get back to the start page