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