]>
git.ipfire.org Git - ipfire.org.git/blob - www/webapp/handlers_base.py
3 from __future__
import division
13 units
= ["B", "k", "M", "G"]
16 while b
>= 1024 and unit_pointer
< len(units
):
20 return "%.1f%s" % (b
, units
[unit_pointer
])
23 class BaseHandler(tornado
.web
.RequestHandler
):
26 def get_account(self
, uid
):
27 # Find the name of the author
28 return self
.accounts
.find(uid
)
30 def get_supported_locales(self
):
31 for l
in tornado
.locale
.get_supported_locales(None):
32 yield tornado
.locale
.get(l
)
34 def valid_locale(self
, locale
):
38 for l
in self
.get_supported_locales():
39 if l
.code
.startswith(locale
):
44 def get_query_locale(self
):
45 locale
= self
.get_argument("locale", None)
50 if self
.valid_locale(locale
):
54 locale
= self
.get_query_locale()
56 self
.set_cookie("locale", locale
)
58 def get_user_locale(self
):
59 default_locale
= tornado
.locale
.get("en_US")
61 # The planet is always in english.
62 if self
.request
.host
== "planet.ipfire.org":
65 # Get the locale from the query.
66 locale
= self
.get_query_locale()
68 # Read the locale from the cookies.
69 locale
= self
.get_cookie("locale", None)
72 locale
= self
.get_browser_locale().code
74 for l
in self
.get_supported_locales():
75 if l
.code
.startswith(locale
):
81 def render_args(self
):
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"),
90 def render(self
, *args
, **_kwargs
):
91 kwargs
= self
.render_args
92 kwargs
.update(_kwargs
)
93 tornado
.web
.RequestHandler
.render(self
, *args
, **kwargs
)
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
)
100 def get_error_html(self
, status_code
, **kwargs
):
101 if status_code
in (404, 500):
103 "code" : status_code
,
104 "exception" : kwargs
.get("exception", None),
105 "message" : httplib
.responses
[status_code
],
107 return self
.render_string("error-%s.html" % status_code
, **render_args
)
109 return tornado
.web
.RequestHandler
.get_error_html(self
, status_code
, **kwargs
)
111 def static_url(self
, path
, static
=True):
112 ret
= tornado
.web
.RequestHandler
.static_url(self
, path
)
114 if self
.settings
.get("debug", False):
118 return "http://static.ipfire.org%s" % ret
124 return backend
.Accounts()
128 return backend
.Banners()
132 return backend
.Memcached()
136 return backend
.Mirrors()
140 return backend
.News()
144 return backend
.Config()
148 return backend
.Releases()
152 return backend
.Banners()
156 return backend
.GeoIP()
160 return backend
.Tracker()
164 return backend
.Planet()
168 return backend
.Wishlist()