]> git.ipfire.org Git - ipfire.org.git/blob - www/webapp/handlers_base.py
Remove obsolete pakfire CGI scripts.
[ipfire.org.git] / www / webapp / handlers_base.py
1 #!/usr/bin/python
2
3 from __future__ import division
4
5 import httplib
6 import time
7 import tornado.locale
8 import tornado.web
9
10 import backend
11
12 def format_size(b):
13 units = ["B", "k", "M", "G"]
14 unit_pointer = 0
15
16 while b >= 1024 and unit_pointer < len(units):
17 b /= 1024
18 unit_pointer += 1
19
20 return "%.1f%s" % (b, units[unit_pointer])
21
22
23 class BaseHandler(tornado.web.RequestHandler):
24 rss_url = None
25
26 def get_account(self, uid):
27 # Find the name of the author
28 return self.accounts.find(uid)
29
30 def get_supported_locales(self):
31 for l in tornado.locale.get_supported_locales(None):
32 yield tornado.locale.get(l)
33
34 def valid_locale(self, locale):
35 if not locale:
36 return False
37
38 for l in self.get_supported_locales():
39 if l.code.startswith(locale):
40 return True
41
42 return False
43
44 def get_query_locale(self):
45 locale = self.get_argument("locale", None)
46
47 if locale is None:
48 return
49
50 if self.valid_locale(locale):
51 return locale
52
53 def prepare(self):
54 locale = self.get_query_locale()
55 if locale:
56 self.set_cookie("locale", locale)
57
58 def get_user_locale(self):
59 default_locale = tornado.locale.get("en_US")
60
61 # The planet is always in english.
62 if self.request.host == "planet.ipfire.org":
63 return default_locale
64
65 # Get the locale from the query.
66 locale = self.get_query_locale()
67 if not locale:
68 # Read the locale from the cookies.
69 locale = self.get_cookie("locale", None)
70
71 if not locale:
72 locale = self.get_browser_locale().code
73
74 for l in self.get_supported_locales():
75 if l.code.startswith(locale):
76 return l
77
78 return default_locale
79
80 @property
81 def render_args(self):
82 return {
83 "format_size" : format_size,
84 "hostname" : self.request.host,
85 "lang" : self.locale.code[:2],
86 "rss_url" : self.rss_url,
87 "year" : time.strftime("%Y"),
88 }
89
90 def render(self, *args, **_kwargs):
91 kwargs = self.render_args
92 kwargs.update(_kwargs)
93 tornado.web.RequestHandler.render(self, *args, **kwargs)
94
95 def render_string(self, *args, **_kwargs):
96 kwargs = self.render_args
97 kwargs.update(_kwargs)
98 return tornado.web.RequestHandler.render_string(self, *args, **kwargs)
99
100 def get_error_html(self, status_code, **kwargs):
101 if status_code in (404, 500):
102 render_args = ({
103 "code" : status_code,
104 "exception" : kwargs.get("exception", None),
105 "message" : httplib.responses[status_code],
106 })
107 return self.render_string("error-%s.html" % status_code, **render_args)
108 else:
109 return tornado.web.RequestHandler.get_error_html(self, status_code, **kwargs)
110
111 def static_url(self, path, static=True):
112 ret = tornado.web.RequestHandler.static_url(self, path)
113
114 if self.settings.get("debug", False):
115 return ret
116
117 if static:
118 return "http://static.ipfire.org%s" % ret
119
120 return ret
121
122 @property
123 def advertisements(self):
124 return backend.Advertisements()
125
126 @property
127 def accounts(self):
128 return backend.Accounts()
129
130 @property
131 def banners(self):
132 return backend.Banners()
133
134 @property
135 def memcached(self):
136 return backend.Memcached()
137
138 @property
139 def mirrors(self):
140 return backend.Mirrors()
141
142 @property
143 def news(self):
144 return backend.News()
145
146 @property
147 def config(self):
148 return backend.Config()
149
150 @property
151 def releases(self):
152 return backend.Releases()
153
154 @property
155 def banners(self):
156 return backend.Banners()
157
158 @property
159 def geoip(self):
160 return backend.GeoIP()
161
162 @property
163 def tracker(self):
164 return backend.Tracker()
165
166 @property
167 def planet(self):
168 return backend.Planet()
169
170 @property
171 def wishlist(self):
172 return backend.Wishlist()