]>
git.ipfire.org Git - ipfire.org.git/blob - www/webapp/handlers.py
14 import tornado
.httpclient
18 from banners
import banners
19 from helpers
import size
22 from releases
import releases
30 class BaseHandler(tornado
.web
.RequestHandler
):
31 def get_user_locale(self
):
32 uri
= self
.request
.uri
.split("/")
34 for lang
in tornado
.locale
.get_supported_locales(None):
35 if lang
[:2] == uri
[1]:
36 return tornado
.locale
.get(lang
)
39 def render_args(self
):
41 "banner" : banners
.get(),
42 "lang" : self
.locale
.code
[:2],
43 "langs" : [l
[:2] for l
in tornado
.locale
.get_supported_locales(None)],
44 "lang_link" : self
.lang_link
,
46 "title" : "no title given",
47 "server" : self
.request
.host
.replace("ipfire", "<span>ipfire</span>"),
48 "uri" : self
.request
.uri
,
49 "year" : time
.strftime("%Y"),
52 def render(self
, *args
, **kwargs
):
53 nargs
= self
.render_args
55 nargs
["title"] = "%s - %s" % (self
.request
.host
, nargs
["title"])
56 tornado
.web
.RequestHandler
.render(self
, *args
, **nargs
)
59 return "/%s/%s" % (self
.locale
.code
[:2], s
)
61 def lang_link(self
, lang
):
62 return "/%s/%s" % (lang
, self
.request
.uri
[4:])
64 def get_error_html(self
, status_code
, **kwargs
):
65 if status_code
in (404, 500):
66 render_args
= self
.render_args
69 "exception" : kwargs
.get("exception", None),
70 "message" : httplib
.responses
[status_code
],
72 return self
.render_string("error-%s.html" % status_code
, **render_args
)
74 return tornado
.web
.RequestHandler
.get_error_html(self
, status_code
, **kwargs
)
78 return self
.application
.hash_db
80 class MainHandler(BaseHandler
):
82 lang
= self
.locale
.code
[:2]
83 self
.redirect("/%s/index" % (lang
))
86 class DownloadHandler(BaseHandler
):
88 self
.render("downloads.html", release
=releases
.latest
)
91 class DownloadAllHandler(BaseHandler
):
93 self
.render("downloads-all.html", releases
=releases
)
96 class DownloadDevelopmentHandler(BaseHandler
):
98 self
.render("downloads-development.html", releases
=releases
)
101 class DownloadTorrentHandler(BaseHandler
):
102 tracker_url
= "http://tracker.ipfire.org:6969/stats?format=txt&mode=tpbs"
104 @tornado.web
.asynchronous
106 http
= tornado
.httpclient
.AsyncHTTPClient()
107 http
.fetch(self
.tracker_url
, callback
=self
.async_callback(self
.on_response
))
109 def on_response(self
, response
):
110 torrents
= releases
.torrents
112 if response
.code
== 200:
113 for line
in response
.body
.split("\n"):
114 if not line
: continue
115 hash, seeds
, peers
= line
.split(":")
122 self
.render("downloads-torrents.html",
125 request_time
=response
.request_time
,
126 tracker
=urlparse
.urlparse(response
.request
.url
).netloc
)
129 class StaticHandler(BaseHandler
):
131 def static_path(self
):
132 return os
.path
.join(self
.application
.settings
["template_path"], "static")
135 def static_files(self
):
137 for filename
in os
.listdir(self
.static_path
):
138 if filename
.endswith(".html"):
142 def get(self
, name
=None):
143 name
= "%s.html" % name
145 if not name
in self
.static_files
:
146 raise tornado
.web
.HTTPError(404)
148 self
.render("static/%s" % name
)
151 class IndexHandler(BaseHandler
):
153 self
.render("index.html", news
=news
)
156 class NewsHandler(BaseHandler
):
158 self
.render("news.html", news
=news
)
161 class BuildHandler(BaseHandler
):
169 for build
in builds
.find():
170 if (time
.time() - float(build
.get("date"))) < 12*60*60:
171 self
.builds
["<12h"].append(build
)
172 elif (time
.time() - float(build
.get("date"))) < 24*60*60:
173 self
.builds
[">12h"].append(build
)
175 self
.builds
[">24h"].append(build
)
177 for l
in self
.builds
.values():
181 self
.render("builds.html", builds
=self
.builds
)
184 class UrielBaseHandler(BaseHandler
):
185 #db = uriel.Database()
188 class UrielHandler(UrielBaseHandler
):
193 class ApiClusterInfoHandler(BaseHandler
):
195 id = self
.get_argument("id", "null")
197 c
= cluster
.Cluster(info
["cluster"]["hostname"])
199 self
.write(simplejson
.dumps({
203 "error" : "null", }))
207 class TranslationHandler(BaseHandler
):
209 self
.render("translations.html", projects
=translations
.projects
)
212 class SourceHandler(BaseHandler
):
214 source_path
= "/srv/sources"
217 for dir, subdirs
, files
in os
.walk(source_path
):
221 if file in [f
["name"] for f
in fileobjects
]:
224 hash = self
.hash_db
.get_hash(file)
227 hash = "0000000000000000000000000000000000000000"
230 "dir" : dir[len(source_path
)+1:],
233 "size" : size(os
.path
.getsize(os
.path
.join(source_path
, dir, file))),
236 fileobjects
.sort(key
=operator
.itemgetter("name"))
238 self
.render("sources.html", files
=fileobjects
)
241 class SourceDownloadHandler(BaseHandler
):
242 def head(self
, path
):
243 self
.get(path
, include_body
=False)
245 def get(self
, path
, include_body
=True):
246 source_path
= "/srv/sources"
248 path
= os
.path
.abspath(os
.path
.join(source_path
, path
[1:]))
250 if not path
.startswith(source_path
):
251 raise tornado
.web
.HTTPError(403)
252 if not os
.path
.exists(path
):
253 raise tornado
.web
.HTTPError(404)
255 stat_result
= os
.stat(path
)
256 modified
= datetime
.datetime
.fromtimestamp(stat_result
[stat
.ST_MTIME
])
258 self
.set_header("Last-Modified", modified
)
259 self
.set_header("Content-Length", stat_result
[stat
.ST_SIZE
])
261 mime_type
, encoding
= mimetypes
.guess_type(path
)
263 self
.set_header("Content-Type", mime_type
)
265 hash = self
.hash_db
.get_hash(path
)
267 self
.set_header("X-Hash-Sha1", "%s" % hash)
271 file = open(path
, "r")
273 self
.write(file.read())