]> git.ipfire.org Git - ipfire.org.git/blob - webapp/handlers_base.py
Drop German translation
[ipfire.org.git] / webapp / handlers_base.py
1 #!/usr/bin/python
2
3 from __future__ import division
4
5 import datetime
6 import dateutil.parser
7 import httplib
8 import ipaddr
9 import logging
10 import time
11 import tornado.locale
12 import tornado.web
13
14 import backend.util
15
16 class BaseHandler(tornado.web.RequestHandler):
17 rss_url = None
18
19 def get_account(self, uid):
20 # Find the name of the author
21 return self.accounts.find(uid)
22
23 @property
24 def hostname(self):
25 # Remove the development prefix
26 return self.request.host.replace(".dev.", ".")
27
28 @property
29 def ssl_params(self):
30 return {
31 "ssl_cipher" : self.request.headers.get("X-Https-Cipher", None),
32 "ssl_protocol" : self.request.headers.get("X-Https-Protocol", None),
33 }
34
35 @property
36 def render_args(self):
37 today = datetime.date.today()
38
39 ret = {
40 "format_size" : backend.util.format_size,
41 "format_time" : backend.util.format_time,
42 "hostname" : self.hostname,
43 "rss_url" : self.rss_url,
44 "year" : today.year,
45 }
46 ret.update(self.ssl_params)
47
48 return ret
49
50 def render(self, *args, **_kwargs):
51 kwargs = self.render_args
52 kwargs.update(_kwargs)
53 tornado.web.RequestHandler.render(self, *args, **kwargs)
54
55 def render_string(self, *args, **_kwargs):
56 kwargs = self.render_args
57 kwargs.update(_kwargs)
58 return tornado.web.RequestHandler.render_string(self, *args, **kwargs)
59
60 def write_error(self, status_code, **kwargs):
61 if status_code in (404, 500):
62 render_args = ({
63 "code" : status_code,
64 "exception" : kwargs.get("exception", None),
65 "message" : httplib.responses[status_code],
66 })
67 self.render("error-%s.html" % status_code, **render_args)
68 else:
69 return tornado.web.RequestHandler.write_error(self, status_code, **kwargs)
70
71 def static_url(self, path, static=True):
72 ret = tornado.web.RequestHandler.static_url(self, path)
73
74 if self.settings.get("debug", False):
75 return ret
76
77 if static:
78 return "//static.ipfire.org%s" % ret
79
80 return ret
81
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
86 for remote_ip in remote_ips:
87 try:
88 addr = ipaddr.IPAddress(remote_ip)
89 except ValueError:
90 # Skip invalid IP addresses.
91 continue
92
93 # Check if the given IP address is from a
94 # private network.
95 if addr.is_private:
96 continue
97
98 return remote_ip
99
100 # Return the last IP if nothing else worked
101 return remote_ips.pop()
102
103 def get_remote_location(self):
104 if not hasattr(self, "__remote_location"):
105 remote_ip = self.get_remote_ip()
106
107 self.__remote_location = self.geoip.get_location(remote_ip)
108
109 return self.__remote_location
110
111 def get_argument_date(self, arg, *args, **kwargs):
112 value = self.get_argument(arg, *args, **kwargs)
113 if value is None:
114 return
115
116 try:
117 return dateutil.parser.parse(value)
118 except ValueError:
119 raise tornado.web.HTTPError(400)
120
121 # Login stuff
122
123 def get_current_user(self):
124 session_id = self.get_cookie("session_id")
125 if not session_id:
126 return
127
128 # Get account from the session object
129 account = self.backend.accounts.get_by_session(session_id, self.request.host)
130
131 # If the account was not found or the session was not valid
132 # any more, we will remove the cookie.
133 if not account:
134 self.clear_cookie("session_id")
135
136 return account
137
138 def login(self, username, password):
139 # Find account
140 account = self.backend.accounts.find_account(username)
141 if not account:
142 logging.warning(401, "unknown account: %s" % username)
143 return False
144
145 # Check credentials
146 if not account.check_password(password):
147 logging.warning("invalid password for %s" % account)
148 return False
149
150 # User has logged in, create a session
151 session_id, session_expires = self.backend.accounts.create_session(account,
152 self.request.host)
153
154 # Check if a new session was created
155 if not session_id:
156 logging.warning("Could not create session")
157 return False
158
159 # Send session cookie to the client
160 self.set_cookie("session_id", session_id,
161 domain=self.request.host, expires=session_expires)
162
163 return True
164
165 def logout(self):
166 session_id = self.get_cookie("session_id")
167
168 if not session_id:
169 return
170
171 success = self.backend.accounts.destroy_session(session_id, self.request.host)
172 if success:
173 self.clear_cookie("session_id")
174
175 @property
176 def backend(self):
177 return self.application.backend
178
179 @property
180 def db(self):
181 return self.backend.db
182
183 @property
184 def advertisements(self):
185 return self.backend.advertisements
186
187 @property
188 def accounts(self):
189 return self.backend.accounts
190
191 @property
192 def downloads(self):
193 return self.backend.downloads
194
195 @property
196 def fireinfo(self):
197 return self.backend.fireinfo
198
199 @property
200 def iuse(self):
201 return self.backend.iuse
202
203 @property
204 def memcached(self):
205 return self.backend.memcache
206
207 @property
208 def mirrors(self):
209 return self.backend.mirrors
210
211 @property
212 def netboot(self):
213 return self.backend.netboot
214
215 @property
216 def news(self):
217 return self.backend.news
218
219 @property
220 def config(self):
221 return self.backend.settings
222
223 @property
224 def releases(self):
225 return self.backend.releases
226
227 @property
228 def geoip(self):
229 return self.backend.geoip
230
231 @property
232 def talk(self):
233 return self.backend.talk
234
235 @property
236 def tracker(self):
237 return self.backend.tracker
238
239 @property
240 def planet(self):
241 return self.backend.planet
242
243
244 class LoginHandler(BaseHandler):
245 def get(self):
246 self.render("auth/login.html")
247
248 def post(self):
249 username = self.get_argument("username")
250 password = self.get_argument("password")
251
252 if not self.login(username, password):
253 raise tornado.web.HTTPError(401)
254
255 next = self.get_argument("next", "/")
256 return self.redirect(next)
257
258
259 class LogoutHandler(BaseHandler):
260 def get(self):
261 self.logout()
262
263 # Get back to the start page
264 self.redirect("/")