]>
git.ipfire.org Git - people/shoehn/ipfire.org.git/blob - webapp/handlers_base.py
3 from __future__
import division
14 units
= ["B", "k", "M", "G"]
17 while b
>= 1024 and unit_pointer
< len(units
):
21 return "%.1f%s" % (b
, units
[unit_pointer
])
24 class BaseHandler(tornado
.web
.RequestHandler
):
27 def get_account(self
, uid
):
28 # Find the name of the author
29 return self
.accounts
.find(uid
)
31 def get_query_locale(self
):
32 locale
= self
.get_argument("locale", None)
37 return tornado
.locale
.get(locale
)
40 locale
= self
.get_query_locale()
42 self
.set_cookie("locale", locale
.code
)
44 def get_user_locale(self
):
45 # The planet is always in english.
46 if self
.request
.host
== "planet.ipfire.org":
47 return tornado
.locale
.get("en_US")
49 # Get the locale from the query.
50 locale
= self
.get_query_locale()
54 # Read the locale from the cookies.
55 locale
= self
.get_cookie("locale", None)
57 return tornado
.locale
.get(locale
)
59 # Otherwise take the browser locale.
60 return self
.get_browser_locale()
63 def render_args(self
):
64 today
= datetime
.date
.today()
67 "format_size" : format_size
,
68 "hostname" : self
.request
.host
,
69 "lang" : self
.locale
.code
[:2],
70 "rss_url" : self
.rss_url
,
74 def render(self
, *args
, **_kwargs
):
75 kwargs
= self
.render_args
76 kwargs
.update(_kwargs
)
77 tornado
.web
.RequestHandler
.render(self
, *args
, **kwargs
)
79 def render_string(self
, *args
, **_kwargs
):
80 kwargs
= self
.render_args
81 kwargs
.update(_kwargs
)
82 return tornado
.web
.RequestHandler
.render_string(self
, *args
, **kwargs
)
84 def write_error(self
, status_code
, **kwargs
):
85 if status_code
in (404, 500):
88 "exception" : kwargs
.get("exception", None),
89 "message" : httplib
.responses
[status_code
],
91 self
.render("error-%s.html" % status_code
, **render_args
)
93 return tornado
.web
.RequestHandler
.write_error(self
, status_code
, **kwargs
)
95 def static_url(self
, path
, static
=True):
96 ret
= tornado
.web
.RequestHandler
.static_url(self
, path
)
98 if self
.settings
.get("debug", False):
101 elif self
.request
.host
== "admin.ipfire.org":
105 return "http://static.ipfire.org%s" % ret
111 return self
.application
.backend
114 def advertisements(self
):
115 return backend
.Advertisements()
119 return self
.backend
.accounts
123 return backend
.Banners()
127 return backend
.Memcached()
131 return backend
.Mirrors()
135 return backend
.News()
139 return backend
.Config()
143 return backend
.Releases()
147 return backend
.Banners()
151 return backend
.GeoIP()
155 return backend
.Tracker()
159 return self
.backend
.planet
163 return backend
.Wishlist()