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