]> git.ipfire.org Git - ipfire.org.git/blame - www/webapp/handlers_base.py
Rework the /about page on www.ipfire.org.
[ipfire.org.git] / www / webapp / handlers_base.py
CommitLineData
940227cb
MT
1#!/usr/bin/python
2
3import httplib
4import time
5import tornado.locale
6import tornado.web
7
8import backend
9
10class BaseHandler(tornado.web.RequestHandler):
de683d7c
MT
11 rss_url = None
12
940227cb
MT
13 def get_account(self, uid):
14 # Find the name of the author
15 return self.accounts.find(uid)
16
17 def get_user_locale(self):
18 DEFAULT_LOCALE = tornado.locale.get("en_US")
19 ALLOWED_LOCALES = \
20 [tornado.locale.get(l) for l in tornado.locale.get_supported_locales(None)]
21
22 # One can append "?locale=de" to mostly and URI on the site and
23 # another output that guessed.
24 locale = self.get_argument("locale", None)
25 if locale:
54af860e
MT
26 for l in ALLOWED_LOCALES:
27 if not l.code.startswith(locale):
28 continue
29
30 return l
940227cb 31
45c763aa
MT
32 # The planet is always in english.
33 if self.request.host == "planet.ipfire.org":
34 return DEFAULT_LOCALE
35
940227cb
MT
36 # If no locale was provided we guess what the browser sends us
37 locale = self.get_browser_locale()
38 if locale in ALLOWED_LOCALES:
39 return locale
40
41 # If no one of the cases above worked we use our default locale
42 return DEFAULT_LOCALE
43
44 @property
45 def render_args(self):
46 return {
47 "hostname" : self.request.host,
48 "lang" : self.locale.code[:2],
de683d7c 49 "rss_url" : self.rss_url,
940227cb
MT
50 "year" : time.strftime("%Y"),
51 }
52
bcc3ed4d
MT
53 def render(self, *args, **_kwargs):
54 kwargs = self.render_args
55 kwargs.update(_kwargs)
940227cb
MT
56 tornado.web.RequestHandler.render(self, *args, **kwargs)
57
bcc3ed4d
MT
58 def render_string(self, *args, **_kwargs):
59 kwargs = self.render_args
60 kwargs.update(_kwargs)
940227cb
MT
61 return tornado.web.RequestHandler.render_string(self, *args, **kwargs)
62
63 def get_error_html(self, status_code, **kwargs):
64 if status_code in (404, 500):
65 render_args = ({
66 "code" : status_code,
67 "exception" : kwargs.get("exception", None),
68 "message" : httplib.responses[status_code],
69 })
70 return self.render_string("error-%s.html" % status_code, **render_args)
71 else:
72 return tornado.web.RequestHandler.get_error_html(self, status_code, **kwargs)
73
3d540c40
MT
74 def static_url(self, path, static=True):
75 ret = tornado.web.RequestHandler.static_url(self, path)
76
54b8df1a
MT
77 if self.settings.get("debug", False):
78 return ret
79
3d540c40
MT
80 if static:
81 return "http://static.ipfire.org%s" % ret
82
83 return ret
84
940227cb
MT
85 @property
86 def accounts(self):
87 return backend.Accounts()
88
89 @property
90 def banners(self):
91 return backend.Banners()
92
b3250465
MT
93 @property
94 def memcached(self):
95 return backend.Memcached()
96
940227cb
MT
97 @property
98 def mirrors(self):
99 return backend.Mirrors()
100
101 @property
102 def news(self):
103 return backend.News()
104
105 @property
106 def config(self):
107 return backend.Config()
108
109 @property
110 def releases(self):
111 return backend.Releases()
112
113 @property
114 def banners(self):
115 return backend.Banners()
116
117 @property
118 def geoip(self):
119 return backend.GeoIP()
120
121 @property
122 def tracker(self):
123 return backend.Tracker()
bcc3ed4d
MT
124
125 @property
126 def planet(self):
127 return backend.Planet()