]>
git.ipfire.org Git - ipfire.org.git/blob - 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_query_locale(self
):
31 locale
= self
.get_argument("locale", None)
36 return tornado
.locale
.get(locale
)
39 locale
= self
.get_query_locale()
41 self
.set_cookie("locale", locale
.code
)
43 def get_user_locale(self
):
44 # The planet is always in english.
45 if self
.request
.host
== "planet.ipfire.org":
46 return tornado
.locale
.get("en_US")
48 # Get the locale from the query.
49 locale
= self
.get_query_locale()
53 # Read the locale from the cookies.
54 locale
= self
.get_cookie("locale", None)
56 return tornado
.locale
.get(locale
)
58 # Otherwise take the browser locale.
59 return self
.get_browser_locale()
62 def render_args(self
):
64 "format_size" : format_size
,
65 "hostname" : self
.request
.host
,
66 "lang" : self
.locale
.code
[:2],
67 "rss_url" : self
.rss_url
,
68 "year" : time
.strftime("%Y"),
71 def render(self
, *args
, **_kwargs
):
72 kwargs
= self
.render_args
73 kwargs
.update(_kwargs
)
74 tornado
.web
.RequestHandler
.render(self
, *args
, **kwargs
)
76 def render_string(self
, *args
, **_kwargs
):
77 kwargs
= self
.render_args
78 kwargs
.update(_kwargs
)
79 return tornado
.web
.RequestHandler
.render_string(self
, *args
, **kwargs
)
81 def write_error(self
, status_code
, **kwargs
):
82 if status_code
in (404, 500):
85 "exception" : kwargs
.get("exception", None),
86 "message" : httplib
.responses
[status_code
],
88 self
.render("error-%s.html" % status_code
, **render_args
)
90 return tornado
.web
.RequestHandler
.write_error(self
, status_code
, **kwargs
)
92 def static_url(self
, path
, static
=True):
93 ret
= tornado
.web
.RequestHandler
.static_url(self
, path
)
95 if self
.settings
.get("debug", False):
99 return "http://static.ipfire.org%s" % ret
104 def advertisements(self
):
105 return backend
.Advertisements()
109 return backend
.Accounts()
113 return backend
.Banners()
117 return backend
.Memcached()
121 return backend
.Mirrors()
125 return backend
.News()
129 return backend
.Config()
133 return backend
.Releases()
137 return backend
.Banners()
141 return backend
.GeoIP()
145 return backend
.Tracker()
149 return backend
.Planet()
153 return backend
.Wishlist()