]> git.ipfire.org Git - people/shoehn/ipfire.org.git/blobdiff - www/webapp/handlers_base.py
Website update.
[people/shoehn/ipfire.org.git] / www / webapp / handlers_base.py
index e78cb6b270acd898e502f271f5024a808e51442f..209d9b6289a8d11635c2eee98455a3156d0072c2 100644 (file)
@@ -1,5 +1,7 @@
 #!/usr/bin/python
 
+from __future__ import division
+
 import httplib
 import time
 import tornado.locale
@@ -7,6 +9,17 @@ import tornado.web
 
 import backend
 
+def format_size(b):
+       units = ["B", "k", "M", "G"]
+       unit_pointer = 0
+
+       while b >= 1024 and unit_pointer < len(units):
+               b /= 1024
+               unit_pointer += 1
+
+       return "%.1f%s" % (b, units[unit_pointer])
+
+
 class BaseHandler(tornado.web.RequestHandler):
        rss_url = None
 
@@ -14,36 +27,60 @@ class BaseHandler(tornado.web.RequestHandler):
                # Find the name of the author
                return self.accounts.find(uid)
 
-       def get_user_locale(self):
-               DEFAULT_LOCALE = tornado.locale.get("en_US")
-               ALLOWED_LOCALES = \
-                       [tornado.locale.get(l) for l in tornado.locale.get_supported_locales(None)]
+       def get_supported_locales(self):
+               for l in tornado.locale.get_supported_locales(None):
+                       yield tornado.locale.get(l)
+
+       def valid_locale(self, locale):
+               if not locale:
+                       return False
+
+               for l in self.get_supported_locales():
+                       if l.code.startswith(locale):
+                               return True
 
-               # One can append "?locale=de" to mostly and URI on the site and
-               # another output that guessed.
+               return False
+
+       def get_query_locale(self):
                locale = self.get_argument("locale", None)
+
+               if locale is None:
+                       return
+
+               if self.valid_locale(locale):
+                       return locale
+
+       def prepare(self):
+               locale = self.get_query_locale()
                if locale:
-                       for l in ALLOWED_LOCALES:
-                               if not l.code.startswith(locale):
-                                       continue
+                       self.set_cookie("locale", locale)
 
-                               return l
+       def get_user_locale(self):
+               default_locale = tornado.locale.get("en_US")
 
                # The planet is always in english.
                if self.request.host == "planet.ipfire.org":
-                       return DEFAULT_LOCALE
+                       return default_locale
 
-               # If no locale was provided we guess what the browser sends us
-               locale = self.get_browser_locale()
-               if locale in ALLOWED_LOCALES:
-                       return locale
+               # Get the locale from the query.
+               locale = self.get_query_locale()
+               if not locale:
+                       # Read the locale from the cookies.
+                       locale = self.get_cookie("locale", None)
+
+                       if not locale:
+                               locale = self.get_browser_locale().code
+
+               for l in self.get_supported_locales():
+                       if l.code.startswith(locale):
+                               return l
 
-               # If no one of the cases above worked we use our default locale
-               return DEFAULT_LOCALE
+               return default_locale
 
        @property
        def render_args(self):
                return {
+                       "format_size" : format_size,
                        "hostname" : self.request.host,
                        "lang" : self.locale.code[:2],
                        "rss_url" : self.rss_url,