]> git.ipfire.org Git - ipfire.org.git/blame - www/webapp/handlers_base.py
www: Use a better caching for all sites.
[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
MT
31
32 # If no locale was provided we guess what the browser sends us
33 locale = self.get_browser_locale()
34 if locale in ALLOWED_LOCALES:
35 return locale
36
37 # If no one of the cases above worked we use our default locale
38 return DEFAULT_LOCALE
39
40 @property
41 def render_args(self):
42 return {
43 "hostname" : self.request.host,
44 "lang" : self.locale.code[:2],
de683d7c 45 "rss_url" : self.rss_url,
940227cb
MT
46 "year" : time.strftime("%Y"),
47 }
48
bcc3ed4d
MT
49 def render(self, *args, **_kwargs):
50 kwargs = self.render_args
51 kwargs.update(_kwargs)
940227cb
MT
52 tornado.web.RequestHandler.render(self, *args, **kwargs)
53
bcc3ed4d
MT
54 def render_string(self, *args, **_kwargs):
55 kwargs = self.render_args
56 kwargs.update(_kwargs)
940227cb
MT
57 return tornado.web.RequestHandler.render_string(self, *args, **kwargs)
58
59 def get_error_html(self, status_code, **kwargs):
60 if status_code in (404, 500):
61 render_args = ({
62 "code" : status_code,
63 "exception" : kwargs.get("exception", None),
64 "message" : httplib.responses[status_code],
65 })
66 return self.render_string("error-%s.html" % status_code, **render_args)
67 else:
68 return tornado.web.RequestHandler.get_error_html(self, status_code, **kwargs)
69
3d540c40
MT
70 def static_url(self, path, static=True):
71 ret = tornado.web.RequestHandler.static_url(self, path)
72
73 if static:
74 return "http://static.ipfire.org%s" % ret
75
76 return ret
77
940227cb
MT
78 @property
79 def accounts(self):
80 return backend.Accounts()
81
82 @property
83 def banners(self):
84 return backend.Banners()
85
b3250465
MT
86 @property
87 def memcached(self):
88 return backend.Memcached()
89
940227cb
MT
90 @property
91 def mirrors(self):
92 return backend.Mirrors()
93
94 @property
95 def news(self):
96 return backend.News()
97
98 @property
99 def config(self):
100 return backend.Config()
101
102 @property
103 def releases(self):
104 return backend.Releases()
105
106 @property
107 def banners(self):
108 return backend.Banners()
109
110 @property
111 def geoip(self):
112 return backend.GeoIP()
113
114 @property
115 def tracker(self):
116 return backend.Tracker()
bcc3ed4d
MT
117
118 @property
119 def planet(self):
120 return backend.Planet()