]> git.ipfire.org Git - ipfire.org.git/blob - src/web/__init__.py
Move download handlers into new module
[ipfire.org.git] / src / web / __init__.py
1 #/usr/bin/python
2
3 import logging
4 import os.path
5 import tornado.locale
6 import tornado.options
7 import tornado.web
8
9 import ipfire
10
11 from handlers import *
12
13 from . import blog
14 from . import download
15 from . import location
16 from . import ui_modules
17
18 class Application(tornado.web.Application):
19 def __init__(self, config, **kwargs):
20 # Initialize backend
21 self.backend = ipfire.Backend(config)
22
23 settings = {
24 # Do not compress responses
25 "gzip" : False,
26
27 # Enable XSRF cookies
28 "xsrf_cookies" : True,
29
30 # Login
31 "login_url" : "/login",
32
33 # Setup directory structure
34 "static_path" : self.backend.config.get("global", "static_dir"),
35 "template_path" : self.backend.config.get("global", "templates_dir"),
36
37 # UI Modules
38 "ui_methods" : {
39 "format_month_name" : self.format_month_name,
40 },
41 "ui_modules" : {
42 "Menu" : ui_modules.MenuModule,
43
44 # Blog
45 "BlogHistoryNavigation": blog.HistoryNavigationModule,
46 "BlogList" : blog.ListModule,
47 "BlogPost" : blog.PostModule,
48 "BlogPosts" : blog.PostsModule,
49
50 # Download
51 "DownloadButton" : download.ButtonModule,
52
53 # Location
54 "Map" : ui_modules.MapModule,
55
56 # Old modules
57 "LanguageName" : ui_modules.LanguageNameModule,
58
59 "MirrorItem" : ui_modules.MirrorItemModule,
60 "MirrorsTable" : ui_modules.MirrorsTableModule,
61 "NetBootMenuConfig" : ui_modules.NetBootMenuConfigModule,
62 "NetBootMenuHeader" : ui_modules.NetBootMenuHeaderModule,
63 "NetBootMenuSeparator" : ui_modules.NetBootMenuSeparatorModule,
64 "ProgressBar" : ui_modules.ProgressBarModule,
65 "FireinfoDeviceTable" : ui_modules.FireinfoDeviceTableModule,
66 "FireinfoDeviceAndGroupsTable" : ui_modules.FireinfoDeviceAndGroupsTableModule,
67 "FireinfoGeoTable" : ui_modules.FireinfoGeoTableModule,
68 "TalkContact" : ui_modules.TalkContactModule,
69 "TalkCallLog" : ui_modules.TalkCallLogModule,
70 "TalkLines" : ui_modules.TalkLinesModule,
71 "TalkOngoingCalls" : ui_modules.TalkOngoingCallsModule,
72 },
73
74 # Call this when a page wasn't found
75 "default_handler_class" : NotFoundHandler,
76 }
77 settings.update(kwargs)
78
79 tornado.web.Application.__init__(self, **settings)
80
81 authentication_handlers = [
82 (r"/login", LoginHandler),
83 (r"/logout", LogoutHandler),
84 ]
85
86 self.add_handlers(r"(dev|www)\.ipfire\.(at|org)", [
87 # Entry site that lead the user to index
88 (r"/", IndexHandler),
89
90 # Download sites
91 (r"/downloads", tornado.web.RedirectHandler, { "url" : "/download" }),
92 (r"/download", download.IndexHandler),
93 (r"/download/([0-9a-z\-\.]+)", download.ReleaseHandler),
94
95 # Donate
96 (r"/donate", DonateHandler),
97 (r"/donation", tornado.web.RedirectHandler, { "url" : "/donate" }),
98
99 # RSS feed
100 (r"/news.rss", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/feed.xml" }),
101
102 # Redirect news articles to blog
103 (r"/news/(.*)", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/posts/{1}" }),
104
105 # Static Pages
106 (r"/chat", StaticHandler, { "template" : "chat.html" }),
107 (r"/features", StaticHandler, { "template" : "features.html" }),
108 (r"/get-involved", StaticHandler, { "template" : "get-involved.html" }),
109 (r"/get-started", StaticHandler, { "template" : "get-started.html" }),
110 (r"/get-support", StaticHandler, { "template" : "get-support.html" }),
111 (r"/hardware", StaticHandler, { "template" : "hardware.html" }),
112 (r"/legal", StaticHandler, { "template" : "legal.html" }),
113
114 # Handle old pages that have moved elsewhere
115 (r"/imprint", tornado.web.RedirectHandler, { "url" : "/legal" }),
116 (r"/(de|en)/(.*)", LangCompatHandler),
117
118 # Export arbitrary error pages
119 (r"/error/([45][0-9]{2})", ErrorHandler),
120 ])
121
122 # blog.ipfire.org
123 self.add_handlers(r"blog(\.dev)?\.ipfire\.org", [
124 (r"/", blog.IndexHandler),
125 (r"/authors/(\w+)", blog.AuthorHandler),
126 (r"/post/(.*)", blog.PostHandler),
127 (r"/search", blog.SearchHandler),
128 (r"/tags/([0-9a-z\-\.]+)", blog.TagHandler),
129 (r"/years/([0-9]+)", blog.YearHandler),
130
131 # RSS Feed
132 (r"/feed.xml", blog.FeedHandler),
133 ])
134
135 # downloads.ipfire.org
136 self.add_handlers(r"downloads?(\.dev)?\.ipfire\.org", [
137 (r"/", tornado.web.RedirectHandler, { "url" : "https://www.ipfire.org/" }),
138 (r"/(.*)", download.FileHandler),
139 ])
140
141 # mirrors.ipfire.org
142 self.add_handlers(r"mirrors(\.dev)?\.ipfire\.org", [
143 (r"/", MirrorIndexHandler),
144 (r"/mirror/(.*)", MirrorItemHandler),
145 ])
146
147 # planet.ipfire.org
148 self.add_handlers(r"planet(\.dev)?\.ipfire\.org", [
149 (r"/", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/" }),
150 (r"/post/([A-Za-z0-9_-]+)", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/posts/{1}" }),
151 (r"/user/([a-z0-9_-]+)", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/authors/{1}" }),
152
153 # RSS
154 (r"/rss", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/feed.xml" }),
155 (r"/user/([a-z0-9_-]+)/rss", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/authors/{1}.rss" }),
156 (r"/news.rss", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/feed.xml" }),
157 ])
158
159 # fireinfo.ipfire.org
160 self.add_handlers(r"fireinfo(\.dev)?\.ipfire\.org", [
161 (r"/", FireinfoIndexHandler),
162 (r"/device/driver/(.*)", FireinfoDeviceDriverDetail),
163 (r"/device/vendors", FireinfoDeviceVendorsHandler),
164 (r"/device/(pci|usb)/([0-9a-f]{4})", FireinfoDeviceVendorHandler),
165 (r"/device/(pci|usb)/([0-9a-f]{4})/([0-9a-f]{4})", FireinfoDeviceModelHandler),
166
167 # Show profiles
168 (r"/profile/random", FireinfoRandomProfileHandler),
169 (r"/profile/([a-z0-9]{40})", FireinfoProfileDetailHandler),
170
171 # Send profiles.
172 (r"/send/([a-z0-9]+)", FireinfoProfileSendHandler),
173
174 # Stats handlers
175 (r"/statistics", FireinfoStatsHandler),
176 (r"/statistics/processors", FireinfoStatsProcessorsHandler),
177 (r"/statistics/processors/(arm|x86)", FireinfoStatsProcessorDetailHandler),
178 (r"/statistics/geo-locations", FireinfoStatsGeoHandler),
179 (r"/statistics/languages", FireinfoStatsLanguagesHandler),
180 (r"/statistics/memory", FireinfoStatsMemoryHandler),
181 (r"/statistics/networking", FireinfoStatsNetworkingHandler),
182 (r"/statistics/releases", FireinfoStatsReleasesHandler),
183 (r"/statistics/virtualization", FireinfoStatsVirtualHandler),
184
185 # Compat handlers
186 (r"/stats", tornado.web.RedirectHandler, { "url" : "/statistics" }),
187 (r"/stats/cpus", tornado.web.RedirectHandler, { "url" : "/statistics/processors" }),
188 (r"/stats/geo", tornado.web.RedirectHandler, { "url" : "/statistics/geo-locations" }),
189 (r"/stats/network", tornado.web.RedirectHandler, { "url" : "/statistics/networking" }),
190 (r"/stats/oses", tornado.web.RedirectHandler, { "url" : "/statistics/releases" }),
191 (r"/stats/virtual", tornado.web.RedirectHandler, { "url" : "/statistics/virtualization" }),
192 (r"/vendor/(pci|usb)/([0-9a-f]{4})", FireinfoDeviceVendorCompatHandler),
193 (r"/model/(pci|usb)/([0-9a-f]{4})/([0-9a-f]{4})", FireinfoDeviceModelCompatHandler),
194 ])
195
196 # i-use.ipfire.org
197 self.add_handlers(r"i-use(\.dev)?\.ipfire\.org", [
198 (r"/profile/([a-f0-9]{40})/([0-9]+).png", IUseImage),
199 ])
200
201 # boot.ipfire.org
202 BOOT_STATIC_PATH = os.path.join(self.settings["static_path"], "netboot")
203 self.add_handlers(r"boot(\.dev)?\.ipfire\.org", [
204 (r"/", tornado.web.RedirectHandler, { "url" : "https://www.ipfire.org/download" }),
205
206 # Configurations
207 (r"/premenu.cfg", PremenuCfgHandler),
208 (r"/menu.gpxe", MenuGPXEHandler),
209 (r"/menu.cfg", MenuCfgHandler),
210
211 # Static files
212 (r"/(boot\.png|pxelinux\.0|menu\.c32|vesamenu\.c32)",
213 tornado.web.StaticFileHandler, { "path" : BOOT_STATIC_PATH }),
214 ])
215
216 # nopaste.ipfire.org
217 self.add_handlers(r"nopaste(\.dev)?\.ipfire\.org", [
218 (r"/", NopasteCreateHandler),
219 (r"/raw/(.*)", NopasteRawHandler),
220 (r"/view/(.*)", NopasteViewHandler),
221 ] + authentication_handlers)
222
223 # location.ipfire.org
224 self.add_handlers(r"location(\.dev)?\.ipfire\.org", [
225 (r"/", location.IndexHandler),
226 (r"/lookup/(.+)", location.LookupHandler),
227 ])
228
229 # geoip.ipfire.org
230 self.add_handlers(r"geoip(\.dev)?\.ipfire\.org", [
231 (r"/", tornado.web.RedirectHandler, { "url" : "https://location.ipfire.org/" }),
232 ])
233
234 # talk.ipfire.org
235 self.add_handlers(r"talk(\.dev)?\.ipfire\.org", [
236 (r"/", TalkIndexHandler),
237 (r"/conferences", TalkConferencesHandler),
238 (r"/diagnosis", TalkDiagnosisHandler),
239 (r"/hangup/(.*)", TalkHangupChannelHandler),
240 (r"/phonebook/(\w+)", TalkPhonebookAccountHandler),
241 (r"/phonebook", TalkPhonebookHandler),
242 (r"/profile", TalkProfileHandler),
243 ] + authentication_handlers)
244
245 # accounts.ipfire.org
246 self.add_handlers(r"accounts(\.dev)?\.ipfire\.org", [
247 (r"/avatar/(\w+)\.jpg", AccountsAvatarHandler),
248 ])
249
250 # admin.ipfire.org
251 self.add_handlers(r"admin(\.dev)?\.ipfire\.org", [
252 (r"/", AdminIndexHandler),
253 # Accounts
254 (r"/accounts", AdminAccountsHandler),
255 #(r"/accounts/delete/([0-9]+)", AdminAccountsDeleteHandler),
256 #(r"/accounts/edit/([0-9]+)", AdminAccountsEditHandler),
257 # Planet
258 (r"/planet", AdminPlanetHandler),
259 (r"/planet/compose", AdminPlanetComposeHandler),
260 (r"/planet/edit/(.*)", AdminPlanetEditHandler),
261 (r"/planet/publish/(.*)", AdminPlanetPublishHandler),
262 # Mirrors
263 (r"/mirrors", AdminMirrorsHandler),
264 (r"/mirrors/create", AdminMirrorsCreateHandler),
265 (r"/mirrors/delete/([0-9]+)", AdminMirrorsDeleteHandler),
266 (r"/mirrors/edit/([0-9]+)", AdminMirrorsEditHandler),
267 (r"/mirrors/details/([0-9]+)", AdminMirrorsDetailsHandler),
268 (r"/mirrors/update", AdminMirrorsUpdateHandler),
269 # Fireinfo
270 (r"/fireinfo", AdminFireinfoHandler),
271 # Downloads
272 (r"/downloads", AdminDownloadsHandler),
273 (r"/downloads/mirrors", AdminDownloadsMirrorsHandler),
274 # API
275 (r"/api/planet/render", AdminApiPlanetRenderMarkupHandler)
276 ] + authentication_handlers)
277
278 # ipfire.org
279 self.add_handlers(r"ipfire\.org", [
280 (r".*", tornado.web.RedirectHandler, { "url" : "https://www.ipfire.org" })
281 ])
282
283 logging.info("Successfully initialied application")
284
285 def format_month_name(self, handler, month):
286 _ = handler.locale.translate
287
288 if month == 1:
289 return _("January")
290 elif month == 2:
291 return _("February")
292 elif month == 3:
293 return _("March")
294 elif month == 4:
295 return _("April")
296 elif month == 5:
297 return _("May")
298 elif month == 6:
299 return _("June")
300 elif month == 7:
301 return _("July")
302 elif month == 8:
303 return _("August")
304 elif month == 9:
305 return _("September")
306 elif month == 10:
307 return _("October")
308 elif month == 11:
309 return _("November")
310 elif month == 12:
311 return _("December")
312
313 return month