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