]> git.ipfire.org Git - people/shoehn/ipfire.org.git/blame - webapp/__init__.py
wishlist: Also show items with high priority on front page.
[people/shoehn/ipfire.org.git] / webapp / __init__.py
CommitLineData
81675874 1#/usr/bin/python
2
feb02477 3import logging
e6d36e0a 4import multiprocessing
81675874 5import os.path
8ccd2ff0 6import simplejson
feb02477 7import tornado.httpserver
81675874 8import tornado.locale
81675874 9import tornado.web
10
18e60079
MT
11from tornado.options import options
12
940227cb 13import backend
feb02477 14
8ccd2ff0
MT
15from handlers import *
16from ui_modules import *
81675874 17
18BASEDIR = os.path.join(os.path.dirname(__file__), "..")
19
60024cc8
MT
20# Load translations.
21tornado.locale.load_gettext_translations(os.path.join(BASEDIR, "translations"), "webapp")
81675874 22
23class Application(tornado.web.Application):
24 def __init__(self):
81675874 25 settings = dict(
26 cookie_secret = "aXBmaXJlY29va2llc2VjcmV0Cg==",
18e60079 27 debug = options.debug,
81675874 28 gzip = True,
d0d074e0 29 login_url = "/login",
81675874 30 template_path = os.path.join(BASEDIR, "templates"),
cc3b928d
MT
31 ui_methods = {
32 "format_month_name" : self.format_month_name,
33 },
81675874 34 ui_modules = {
a0048e66 35 "Advertisement" : AdvertisementModule,
7771acea 36 "DonationBox" : DonationBoxModule,
60024cc8 37 "DownloadButton" : DownloadButtonModule,
81675874 38 "Menu" : MenuModule,
940227cb 39 "MirrorItem" : MirrorItemModule,
0673d1b0 40 "MirrorsTable" : MirrorsTableModule,
81675874 41 "NewsItem" : NewsItemModule,
940227cb 42 "NewsLine" : NewsLineModule,
7771acea
MT
43 "NewsTable" : NewsTableModule,
44 "NewsYearNavigation": NewsYearNavigationModule,
d0d074e0 45 "PlanetEntry" : PlanetEntryModule,
81675874 46 "ReleaseItem" : ReleaseItemModule,
47 "SidebarBanner" : SidebarBannerModule,
81675874 48 "SidebarRelease" : SidebarReleaseModule,
372efc19 49 "StasyTable" : StasyTableModule,
62c3fa48 50 "StasyCPUCoreTable" : StasyCPUCoreTableModule,
91a446f0 51 "StasyDeviceTable" : StasyDeviceTableModule,
638e9782 52 "StasyGeoTable" : StasyGeoTableModule,
940227cb 53 "TrackerPeerList": TrackerPeerListModule,
7771acea
MT
54 "Wish" : WishModule,
55 "Wishlist" : WishlistModule,
81675874 56 },
57 xsrf_cookies = True,
58 )
5cf160e0 59
ae0228e1
MT
60 tornado.web.Application.__init__(self, **settings)
61
62 self.settings["static_path"] = static_path = os.path.join(BASEDIR, "static")
63 static_handlers = [
64 (r"/static/(.*)", tornado.web.StaticFileHandler, dict(path = static_path)),
65 (r"/(favicon\.ico)", tornado.web.StaticFileHandler, dict(path = static_path)),
66 (r"/(robots\.txt)", tornado.web.StaticFileHandler, dict(path = static_path)),
67 ]
68
d4dc517c 69 self.add_handlers(r"(dev|www)\.ipfire\.(at|org)", [
940227cb
MT
70 # Entry site that lead the user to index
71 (r"/", IndexHandler),
72 (r"/index\.?(s?html?)?", RootHandler),
73
74 # Handle news items
940227cb 75 (r"/news", NewsIndexHandler),
7771acea 76 (r"/news/year/([0-9]*)", NewsYearHandler),
940227cb
MT
77 (r"/news/(.*)", NewsItemHandler),
78 (r"/author/(.*)", NewsAuthorHandler),
79
81675874 80 # Download sites
60024cc8
MT
81 (r"/download", DownloadHandler),
82 (r"/downloads", tornado.web.RedirectHandler, { "url" : "/download" }),
940227cb 83
8d48f4ef
MT
84 # Handle old pages that have moved elsewhere
85 (r"/screenshots", tornado.web.RedirectHandler, { "url" : "/about" }),
60024cc8
MT
86 (r"/about", tornado.web.RedirectHandler, { "url" : "/features" }),
87 (r"/support", tornado.web.RedirectHandler, { "url" : "/getinvolved" }),
88
89 (r"/donate", tornado.web.RedirectHandler, { "url" : "/donation" }),
8d48f4ef 90
de683d7c 91 # RSS feed
bcc3ed4d 92 (r"/news.rss", RSSNewsHandler),
de683d7c
MT
93
94 # Redirection for bookmarks, etc.
940227cb
MT
95 (r"/(de|en)/(.*)", LangCompatHandler)
96
97 ] + static_handlers + [
81675874 98 # Always the last rule
940227cb
MT
99 (r"/(.*)", StaticHandler),
100 ])
101
940227cb 102 # downloads.ipfire.org
2db02063 103 self.add_handlers(r"downloads?\.ipfire\.org", [
940227cb
MT
104 (r"/", DownloadsIndexHandler),
105 (r"/latest", DownloadsLatestHandler),
106 (r"/release/([0-9]+)", DownloadsReleaseHandler),
107 (r"/older", DownloadsOlderHandler),
108 (r"/development", DownloadsDevelopmentHandler),
109 (r"/mirrors", tornado.web.RedirectHandler, { "url" : "http://mirrors.ipfire.org/" }),
110 (r"/source", tornado.web.RedirectHandler, { "url" : "http://source.ipfire.org/" }),
60024cc8 111 (r"/download-splash", DownloadSplashHandler),
54af860e 112 ] + static_handlers + [
edd297c4 113 (r"/(iso|torrent)/(.*)", DownloadCompatHandler),
54af860e
MT
114 (r"/(.*)", DownloadFileHandler),
115 ])
940227cb
MT
116
117 # mirrors.ipfire.org
118 self.add_handlers(r"mirrors\.ipfire\.org", [
119 (r"/", MirrorIndexHandler),
120 (r"/mirror/([0-9]+)", MirrorItemHandler),
0a003782 121 (r"/lists/pakfire2", MirrorListPakfire2Handler),
940227cb
MT
122 ] + static_handlers)
123
d0d074e0
MT
124 # planet.ipfire.org
125 self.add_handlers(r"planet\.ipfire\.org", [
940227cb 126 (r"/", PlanetMainHandler),
d0d074e0 127 (r"/post/([A-Za-z0-9_-]+)", PlanetPostingHandler),
27066195 128 (r"/user/([a-z0-9_-]+)", PlanetUserHandler),
2bdd073f 129 (r"/search", PlanetSearchHandler),
cc3b928d 130 (r"/year/(\d+)", PlanetYearHandler),
bcc3ed4d 131
a3e3c96d 132 # API
8876f3df 133 (r"/api/planet/search/autocomplete", PlanetAPISearchAutocomplete),
bcc3ed4d
MT
134
135 # RSS
136 (r"/rss", RSSPlanetAllHandler),
137 (r"/user/([a-z0-9_-]+)/rss", RSSPlanetUserHandler),
20ccd136 138 (r"/news.rss", tornado.web.RedirectHandler, { "url" : "/rss" }),
d0d074e0
MT
139 ] + static_handlers)
140
940227cb 141 # stasy.ipfire.org
60024cc8 142 self.add_handlers(r"fireinfo\.ipfire\.org", [
940227cb 143 (r"/", StasyIndexHandler),
91a446f0
MT
144 (r"/profile/([a-z0-9]{40})", StasyProfileDetailHandler),
145 (r"/vendor/(pci|usb)/([0-9a-f]{4})", StasyStatsVendorDetail),
146 (r"/model/(pci|usb)/([0-9a-f]{4})/([0-9a-f]{4})", StasyStatsModelDetail),
147
30852a9e
MT
148 # Send profiles.
149 (r"/send/([a-z0-9]+)", StasyProfileSendHandler),
150
91a446f0
MT
151 # Stats handlers
152 (r"/stats", StasyStatsHandler),
153 (r"/stats/cpus", StasyStatsCPUHandler),
154 (r"/stats/cpuflags", StasyStatsCPUFlagsHandler),
155 (r"/stats/geo", StasyStatsGeoHandler),
156 (r"/stats/memory", StasyStatsMemoryHandler),
54af860e 157 (r"/stats/network", StasyStatsNetworkHandler),
91a446f0
MT
158 (r"/stats/oses", StasyStatsOSesHandler),
159 (r"/stats/virtual", StasyStatsVirtualHandler),
ae0228e1 160 ] + static_handlers)
5cf160e0 161
c37ec602
MT
162 # i-use.ipfire.org
163 self.add_handlers(r"i-use\.ipfire\.org", [
164 (r"/profile/([a-f0-9]{40})/([0-9]+).png", IUseImage),
165 ])
166
5cf160e0 167 # tracker.ipfire.org
940227cb
MT
168 self.add_handlers(r"(torrent|tracker)\.ipfire\.org", [
169 (r"/", TrackerIndexHandler),
344a3d62 170 (r"/announce.*", TrackerAnnounceHandler),
e2afbd6a 171 (r"/scrape", TrackerScrapeHandler),
940227cb 172 (r"/torrent/([0-9a-f]+)", TrackerDetailHandler),
fadcfd00
MT
173 (r"/([0-9a-f]{40})", TrackerDetailHandler),
174 (r"/([0-9a-f]{40})/download", TrackerDownloadHandler),
ae0228e1
MT
175 ] + static_handlers)
176
8e2e1261
MT
177 # boot.ipfire.org
178 BOOT_STATIC_PATH = os.path.join(self.settings["static_path"], "netboot")
179 self.add_handlers(r"boot\.ipfire\.org", [
180 (r"/", tornado.web.RedirectHandler, { "url" : "http://www.ipfire.org/download" }),
181
182 # Configurations
183 (r"/menu.gpxe", MenuGPXEHandler),
184 (r"/menu.cfg", MenuCfgHandler),
8e2e1261
MT
185
186 # Static files
e847f85c 187 (r"/(boot\.png|premenu\.cfg|pxelinux\.0|menu\.c32|vesamenu\.c32)",
8e2e1261
MT
188 tornado.web.StaticFileHandler, { "path" : BOOT_STATIC_PATH }),
189 ])
190
60024cc8
MT
191 # nopaste.ipfire.org
192 self.add_handlers(r"nopaste\.ipfire\.org", [
193 (r"/", NopasteIndexHandler),
194 (r"/([\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12})", NopasteEntryHandler),
195 ] + static_handlers)
196
7771acea
MT
197 # wishlist.ipfire.org
198 self.add_handlers(r"wishlist\.ipfire\.org", [
199 (r"/", WishlistIndexHandler),
1bf8d482 200 (r"/closed", WishlistClosedHandler),
7771acea
MT
201 (r"/wish/(.*)/donate", WishDonateHandler),
202 (r"/wish/(.*)", WishHandler),
203 (r"/terms", WishlistTermsHandler),
204 ] + static_handlers)
205
d0d074e0
MT
206 # admin.ipfire.org
207 self.add_handlers(r"admin\.ipfire\.org", [
208 (r"/", AdminIndexHandler),
940227cb
MT
209 (r"/login", AdminLoginHandler),
210 (r"/logout", AdminLogoutHandler),
d0d074e0
MT
211 # Accounts
212 (r"/accounts", AdminAccountsHandler),
940227cb
MT
213 #(r"/accounts/delete/([0-9]+)", AdminAccountsDeleteHandler),
214 #(r"/accounts/edit/([0-9]+)", AdminAccountsEditHandler),
d0d074e0
MT
215 # Planet
216 (r"/planet", AdminPlanetHandler),
217 (r"/planet/compose", AdminPlanetComposeHandler),
218 (r"/planet/edit/([0-9]+)", AdminPlanetEditHandler),
940227cb
MT
219 # Mirrors
220 (r"/mirrors", AdminMirrorsHandler),
221 (r"/mirrors/create", AdminMirrorsCreateHandler),
222 (r"/mirrors/delete/([0-9]+)", AdminMirrorsDeleteHandler),
223 (r"/mirrors/edit/([0-9]+)", AdminMirrorsEditHandler),
224 (r"/mirrors/details/([0-9]+)", AdminMirrorsDetailsHandler),
225 (r"/mirrors/update", AdminMirrorsUpdateHandler),
71e1ece7
MT
226 # Fireinfo
227 (r"/fireinfo/stats", AdminFireinfoStatsHandler),
60024cc8
MT
228 # Downloads
229 (r"/downloads", AdminDownloadsHandler),
230 (r"/downloads/mirrors", AdminDownloadsMirrorsHandler),
d0d074e0 231 # API
8876f3df 232 (r"/api/planet/search/autocomplete", PlanetAPISearchAutocomplete),
d0d074e0
MT
233 (r"/api/planet/render", AdminApiPlanetRenderMarkupHandler)
234 ] + static_handlers)
235
ae0228e1 236 # ipfire.org
3add293a 237 self.add_handlers(r".*", [
ae0228e1 238 (r".*", tornado.web.RedirectHandler, { "url" : "http://www.ipfire.org" })
5cf160e0 239 ])
3add293a 240
feb02477
MT
241 logging.info("Successfully initialied application")
242
243 self.__running = True
244
3add293a 245 def __del__(self):
feb02477
MT
246 logging.info("Shutting down application")
247
feb02477
MT
248 @property
249 def ioloop(self):
250 return tornado.ioloop.IOLoop.instance()
251
940227cb 252 def shutdown(self, *args):
feb02477
MT
253 logging.debug("Caught shutdown signal")
254 self.ioloop.stop()
255
256 self.__running = False
257
258 def run(self, port=8001):
259 logging.debug("Going to background")
260
261 http_server = tornado.httpserver.HTTPServer(self, xheaders=True)
feb02477 262
cef37a4a
MT
263 num_processes = multiprocessing.cpu_count() / 2
264
940227cb
MT
265 # If we are not running in debug mode, we can actually run multiple
266 # frontends to get best performance out of our service.
267 if not self.settings["debug"]:
268 http_server.bind(port)
cef37a4a 269 http_server.start(num_processes=num_processes)
940227cb
MT
270 else:
271 http_server.listen(port)
de683d7c
MT
272
273 # All requests should be done after 30 seconds or they will be killed.
274 self.ioloop.set_blocking_log_threshold(30)
feb02477 275
940227cb 276 self.ioloop.start()
feb02477
MT
277
278 def reload(self):
279 logging.debug("Caught reload signal")
cc3b928d
MT
280
281 def format_month_name(self, handler, month):
282 _ = handler.locale.translate
283
284 if month == 1:
285 return _("January")
286 elif month == 2:
287 return _("February")
288 elif month == 3:
289 return _("March")
290 elif month == 4:
291 return _("April")
292 elif month == 5:
293 return _("May")
294 elif month == 6:
295 return _("June")
296 elif month == 7:
297 return _("July")
298 elif month == 8:
299 return _("August")
300 elif month == 9:
301 return _("September")
302 elif month == 10:
303 return _("October")
304 elif month == 11:
305 return _("November")
306 elif month == 12:
307 return _("December")
308
309 return month