]> git.ipfire.org Git - ipfire.org.git/blame - webapp/__init__.py
planet: Add yearly summary.
[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),
121 ] + static_handlers)
122
d0d074e0
MT
123 # planet.ipfire.org
124 self.add_handlers(r"planet\.ipfire\.org", [
940227cb 125 (r"/", PlanetMainHandler),
d0d074e0 126 (r"/post/([A-Za-z0-9_-]+)", PlanetPostingHandler),
27066195 127 (r"/user/([a-z0-9_-]+)", PlanetUserHandler),
2bdd073f 128 (r"/search", PlanetSearchHandler),
cc3b928d 129 (r"/year/(\d+)", PlanetYearHandler),
bcc3ed4d
MT
130
131 # RSS
132 (r"/rss", RSSPlanetAllHandler),
133 (r"/user/([a-z0-9_-]+)/rss", RSSPlanetUserHandler),
d0d074e0
MT
134 ] + static_handlers)
135
940227cb 136 # stasy.ipfire.org
60024cc8 137 self.add_handlers(r"fireinfo\.ipfire\.org", [
940227cb 138 (r"/", StasyIndexHandler),
91a446f0
MT
139 (r"/profile/([a-z0-9]{40})", StasyProfileDetailHandler),
140 (r"/vendor/(pci|usb)/([0-9a-f]{4})", StasyStatsVendorDetail),
141 (r"/model/(pci|usb)/([0-9a-f]{4})/([0-9a-f]{4})", StasyStatsModelDetail),
142
30852a9e
MT
143 # Send profiles.
144 (r"/send/([a-z0-9]+)", StasyProfileSendHandler),
145
91a446f0
MT
146 # Stats handlers
147 (r"/stats", StasyStatsHandler),
148 (r"/stats/cpus", StasyStatsCPUHandler),
149 (r"/stats/cpuflags", StasyStatsCPUFlagsHandler),
150 (r"/stats/geo", StasyStatsGeoHandler),
151 (r"/stats/memory", StasyStatsMemoryHandler),
54af860e 152 (r"/stats/network", StasyStatsNetworkHandler),
91a446f0
MT
153 (r"/stats/oses", StasyStatsOSesHandler),
154 (r"/stats/virtual", StasyStatsVirtualHandler),
ae0228e1 155 ] + static_handlers)
5cf160e0 156
c37ec602
MT
157 # i-use.ipfire.org
158 self.add_handlers(r"i-use\.ipfire\.org", [
159 (r"/profile/([a-f0-9]{40})/([0-9]+).png", IUseImage),
160 ])
161
5cf160e0 162 # tracker.ipfire.org
940227cb
MT
163 self.add_handlers(r"(torrent|tracker)\.ipfire\.org", [
164 (r"/", TrackerIndexHandler),
344a3d62 165 (r"/announce.*", TrackerAnnounceHandler),
e2afbd6a 166 (r"/scrape", TrackerScrapeHandler),
940227cb 167 (r"/torrent/([0-9a-f]+)", TrackerDetailHandler),
fadcfd00
MT
168 (r"/([0-9a-f]{40})", TrackerDetailHandler),
169 (r"/([0-9a-f]{40})/download", TrackerDownloadHandler),
ae0228e1
MT
170 ] + static_handlers)
171
8e2e1261
MT
172 # boot.ipfire.org
173 BOOT_STATIC_PATH = os.path.join(self.settings["static_path"], "netboot")
174 self.add_handlers(r"boot\.ipfire\.org", [
175 (r"/", tornado.web.RedirectHandler, { "url" : "http://www.ipfire.org/download" }),
176
177 # Configurations
178 (r"/menu.gpxe", MenuGPXEHandler),
179 (r"/menu.cfg", MenuCfgHandler),
180 (r"/config/([0-9]+)/boot.gpxe", BootGPXEHandler),
181
182 # Static files
183 (r"/(boot.png|custom.gpxe|premenu.cfg|vesamenu.c32|menu.c32)",
184 tornado.web.StaticFileHandler, { "path" : BOOT_STATIC_PATH }),
185 ])
186
60024cc8
MT
187 # nopaste.ipfire.org
188 self.add_handlers(r"nopaste\.ipfire\.org", [
189 (r"/", NopasteIndexHandler),
190 (r"/([\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12})", NopasteEntryHandler),
191 ] + static_handlers)
192
7771acea
MT
193 # wishlist.ipfire.org
194 self.add_handlers(r"wishlist\.ipfire\.org", [
195 (r"/", WishlistIndexHandler),
1bf8d482 196 (r"/closed", WishlistClosedHandler),
7771acea
MT
197 (r"/wish/(.*)/donate", WishDonateHandler),
198 (r"/wish/(.*)", WishHandler),
199 (r"/terms", WishlistTermsHandler),
200 ] + static_handlers)
201
d0d074e0
MT
202 # admin.ipfire.org
203 self.add_handlers(r"admin\.ipfire\.org", [
204 (r"/", AdminIndexHandler),
940227cb
MT
205 (r"/login", AdminLoginHandler),
206 (r"/logout", AdminLogoutHandler),
d0d074e0
MT
207 # Accounts
208 (r"/accounts", AdminAccountsHandler),
940227cb
MT
209 #(r"/accounts/delete/([0-9]+)", AdminAccountsDeleteHandler),
210 #(r"/accounts/edit/([0-9]+)", AdminAccountsEditHandler),
d0d074e0
MT
211 # Planet
212 (r"/planet", AdminPlanetHandler),
213 (r"/planet/compose", AdminPlanetComposeHandler),
214 (r"/planet/edit/([0-9]+)", AdminPlanetEditHandler),
940227cb
MT
215 # Mirrors
216 (r"/mirrors", AdminMirrorsHandler),
217 (r"/mirrors/create", AdminMirrorsCreateHandler),
218 (r"/mirrors/delete/([0-9]+)", AdminMirrorsDeleteHandler),
219 (r"/mirrors/edit/([0-9]+)", AdminMirrorsEditHandler),
220 (r"/mirrors/details/([0-9]+)", AdminMirrorsDetailsHandler),
221 (r"/mirrors/update", AdminMirrorsUpdateHandler),
71e1ece7
MT
222 # Fireinfo
223 (r"/fireinfo/stats", AdminFireinfoStatsHandler),
60024cc8
MT
224 # Downloads
225 (r"/downloads", AdminDownloadsHandler),
226 (r"/downloads/mirrors", AdminDownloadsMirrorsHandler),
d0d074e0
MT
227 # API
228 (r"/api/planet/render", AdminApiPlanetRenderMarkupHandler)
229 ] + static_handlers)
230
ae0228e1 231 # ipfire.org
3add293a 232 self.add_handlers(r".*", [
ae0228e1 233 (r".*", tornado.web.RedirectHandler, { "url" : "http://www.ipfire.org" })
5cf160e0 234 ])
3add293a 235
feb02477
MT
236 logging.info("Successfully initialied application")
237
238 self.__running = True
239
3add293a 240 def __del__(self):
feb02477
MT
241 logging.info("Shutting down application")
242
feb02477
MT
243 @property
244 def ioloop(self):
245 return tornado.ioloop.IOLoop.instance()
246
940227cb 247 def shutdown(self, *args):
feb02477
MT
248 logging.debug("Caught shutdown signal")
249 self.ioloop.stop()
250
251 self.__running = False
252
253 def run(self, port=8001):
254 logging.debug("Going to background")
255
256 http_server = tornado.httpserver.HTTPServer(self, xheaders=True)
feb02477 257
940227cb
MT
258 # If we are not running in debug mode, we can actually run multiple
259 # frontends to get best performance out of our service.
260 if not self.settings["debug"]:
261 http_server.bind(port)
e6d36e0a 262 http_server.start(num_processes=multiprocessing.cpu_count())
940227cb
MT
263 else:
264 http_server.listen(port)
de683d7c
MT
265
266 # All requests should be done after 30 seconds or they will be killed.
267 self.ioloop.set_blocking_log_threshold(30)
feb02477 268
940227cb 269 self.ioloop.start()
feb02477
MT
270
271 def reload(self):
272 logging.debug("Caught reload signal")
cc3b928d
MT
273
274 def format_month_name(self, handler, month):
275 _ = handler.locale.translate
276
277 if month == 1:
278 return _("January")
279 elif month == 2:
280 return _("February")
281 elif month == 3:
282 return _("March")
283 elif month == 4:
284 return _("April")
285 elif month == 5:
286 return _("May")
287 elif month == 6:
288 return _("June")
289 elif month == 7:
290 return _("July")
291 elif month == 8:
292 return _("August")
293 elif month == 9:
294 return _("September")
295 elif month == 10:
296 return _("October")
297 elif month == 11:
298 return _("November")
299 elif month == 12:
300 return _("December")
301
302 return month