]> git.ipfire.org Git - ipfire.org.git/blame - src/web/__init__.py
fireinfo: Add page for all information about releases
[ipfire.org.git] / src / web / __init__.py
CommitLineData
81675874 1#/usr/bin/python
2
feb02477 3import logging
401827c2 4import itertools
81675874 5import os.path
c01ad253 6import phonenumbers
e96e445b 7import phonenumbers.geocoder
81675874 8import tornado.locale
a49b5422 9import tornado.options
81675874 10import tornado.web
11
a95c2f97 12import ipfire
574a88c7 13import ipfire.countries
feb02477 14
11347e46 15from .handlers import *
81675874 16
08df6527 17from . import auth
12e5de7e 18from . import blog
f301d952 19from . import boot
e77cd04c 20from . import download
96c9bb79 21from . import fireinfo
699a0911 22from . import iuse
f5b01fc2 23from . import location
95483f04 24from . import mirrors
5613b94b 25from . import newsletter
a41085fb 26from . import nopaste
03706893 27from . import people
df6180a5 28from . import ui_modules
12e5de7e 29
81675874 30class Application(tornado.web.Application):
3e7c6ccd
MT
31 def __init__(self, config, **kwargs):
32 # Initialize backend
a95c2f97 33 self.backend = ipfire.Backend(config)
a6dc0bad 34
9ed02e3b
MT
35 settings = {
36 # Do not compress responses
37 "gzip" : False,
38
39 # Enable XSRF cookies
40 "xsrf_cookies" : True,
41
42 # Login
43 "login_url" : "/login",
44
45 # Setup directory structure
46 "static_path" : self.backend.config.get("global", "static_dir"),
47 "template_path" : self.backend.config.get("global", "templates_dir"),
48
49 # UI Modules
50 "ui_methods" : {
574a88c7 51 "format_country_name" : self.format_country_name,
e96e445b
MT
52 "format_month_name" : self.format_month_name,
53 "format_phone_number" : self.format_phone_number,
54 "format_phone_number_to_e164" : self.format_phone_number_to_e164,
55 "format_phone_number_location" : self.format_phone_number_location,
56 "grouper" : grouper,
cc3b928d 57 },
9ed02e3b 58 "ui_modules" : {
f5b01fc2 59 # Blog
7e64f6a3
MT
60 "BlogHistoryNavigation": blog.HistoryNavigationModule,
61 "BlogList" : blog.ListModule,
f91dfcc7 62 "BlogPost" : blog.PostModule,
8a897d25 63 "BlogPosts" : blog.PostsModule,
f91dfcc7 64
93feb275
MT
65 # Boot
66 "BootMenuConfig" : boot.MenuConfigModule,
67 "BootMenuHeader" : boot.MenuHeaderModule,
68 "BootMenuSeparator" : boot.MenuSeparatorModule,
69
f5b01fc2 70 # Location
df6180a5 71 "Map" : ui_modules.MapModule,
f5b01fc2 72
917434b8 73 # Talk
dbb0c109
MT
74 "AccountsList" : people.AccountsListModule,
75 "CDR" : people.CDRModule,
76 "Channels" : people.ChannelsModule,
68ece434 77 "MOS" : people.MOSModule,
b5e2077f 78 "Password" : people.PasswordModule,
dbb0c109 79 "Registrations" : people.RegistrationsModule,
7afd64bb 80 "SIPStatus" : people.SIPStatusModule,
917434b8 81
e1814f16
MT
82 # Nopaste
83 "Code" : nopaste.CodeModule,
84
23f0179e 85 # Fireinfo
df6180a5 86 "ProgressBar" : ui_modules.ProgressBarModule,
96c9bb79
MT
87 "FireinfoDeviceTable" : fireinfo.DeviceTableModule,
88 "FireinfoDeviceAndGroupsTable" : fireinfo.DeviceAndGroupsTableModule,
89 "FireinfoGeoTable" : fireinfo.GeoTableModule,
81675874 90 },
3403dc5e
MT
91
92 # Call this when a page wasn't found
b22bc8e8 93 "default_handler_class" : base.NotFoundHandler,
9ed02e3b 94 }
9068dba1 95 settings.update(kwargs)
5cf160e0 96
ae0228e1
MT
97 tornado.web.Application.__init__(self, **settings)
98
66862195 99 authentication_handlers = [
08df6527
MT
100 (r"/login", auth.LoginHandler),
101 (r"/logout", auth.LogoutHandler),
66862195
MT
102 ]
103
d4dc517c 104 self.add_handlers(r"(dev|www)\.ipfire\.(at|org)", [
940227cb
MT
105 # Entry site that lead the user to index
106 (r"/", IndexHandler),
940227cb 107
81675874 108 # Download sites
60b0917c 109 (r"/downloads", tornado.web.RedirectHandler, { "url" : "/download" }),
e77cd04c
MT
110 (r"/download", download.IndexHandler),
111 (r"/download/([0-9a-z\-\.]+)", download.ReleaseHandler),
940227cb 112
e64ce07e
MT
113 # Donate
114 (r"/donate", DonateHandler),
115 (r"/donation", tornado.web.RedirectHandler, { "url" : "/donate" }),
8d48f4ef 116
5613b94b
MT
117 # Newsletter
118 (r"/newsletter/subscribe", newsletter.SubscribeHandler),
119
de683d7c 120 # RSS feed
f0714277 121 (r"/news.rss", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/feed.xml" }),
7f9dbcc0
MT
122
123 # Redirect news articles to blog
124 (r"/news/(.*)", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/posts/{1}" }),
de683d7c 125
45592df5 126 # Static Pages
84151dbf 127 (r"/chat", StaticHandler, { "template" : "chat.html" }),
45592df5 128 (r"/features", StaticHandler, { "template" : "features.html" }),
45592df5 129 (r"/legal", StaticHandler, { "template" : "legal.html" }),
00026d8b 130 (r"/support", StaticHandler, { "template" : "support.html" }),
45592df5 131
14cd4fa8
MT
132 # Handle old pages that have moved elsewhere
133 (r"/imprint", tornado.web.RedirectHandler, { "url" : "/legal" }),
3808b871 134 (r"/(de|en)/(.*)", LangCompatHandler),
37ed7c3c
MT
135
136 # Export arbitrary error pages
b22bc8e8 137 (r"/error/([45][0-9]{2})", base.ErrorHandler),
baa693a3
MT
138
139 # Block page
b22bc8e8 140 (r"/blocked", base.BlockedHandler),
940227cb
MT
141 ])
142
12e5de7e
MT
143 # blog.ipfire.org
144 self.add_handlers(r"blog(\.dev)?\.ipfire\.org", [
8a897d25 145 (r"/", blog.IndexHandler),
cfc0823a 146 (r"/authors/(\w+)", blog.AuthorHandler),
541c952b 147 (r"/compose", blog.ComposeHandler),
0b342a05 148 (r"/drafts", blog.DraftsHandler),
d17a2624 149 (r"/post/([0-9a-z\-\._]+)", blog.PostHandler),
914238a5 150 (r"/post/([0-9a-z\-\._]+)/delete", blog.DeleteHandler),
d17a2624
MT
151 (r"/post/([0-9a-z\-\._]+)/edit", blog.EditHandler),
152 (r"/post/([0-9a-z\-\._]+)/publish", blog.PublishHandler),
e6b18dce 153 (r"/search", blog.SearchHandler),
8d7487d2 154 (r"/tags/([0-9a-z\-\.]+)", blog.TagHandler),
7e64f6a3 155 (r"/years/([0-9]+)", blog.YearHandler),
f0714277
MT
156
157 # RSS Feed
158 (r"/feed.xml", blog.FeedHandler),
08df6527 159 ] + authentication_handlers)
12e5de7e 160
940227cb 161 # downloads.ipfire.org
80594ae3 162 self.add_handlers(r"downloads?(\.dev)?\.ipfire\.org", [
ed8116c7
MT
163 (r"/", tornado.web.RedirectHandler, { "url" : "https://www.ipfire.org/" }),
164 (r"/(.*)", download.FileHandler),
54af860e 165 ])
940227cb
MT
166
167 # mirrors.ipfire.org
8fceca0a 168 self.add_handlers(r"mirrors(\.dev)?\.ipfire\.org", [
95483f04
MT
169 (r"/", mirrors.IndexHandler),
170 (r"/mirrors/(.*)", mirrors.MirrorHandler),
3808b871 171 ])
940227cb 172
d0d074e0 173 # planet.ipfire.org
7cad1818 174 self.add_handlers(r"planet(\.dev)?\.ipfire\.org", [
3d4ce901
MT
175 (r"/", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/" }),
176 (r"/post/([A-Za-z0-9_-]+)", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/posts/{1}" }),
177 (r"/user/([a-z0-9_-]+)", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/authors/{1}" }),
bcc3ed4d
MT
178
179 # RSS
f0714277 180 (r"/rss", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/feed.xml" }),
3d4ce901 181 (r"/user/([a-z0-9_-]+)/rss", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/authors/{1}.rss" }),
f0714277 182 (r"/news.rss", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/feed.xml" }),
3808b871 183 ])
d0d074e0 184
66862195 185 # fireinfo.ipfire.org
8fceca0a 186 self.add_handlers(r"fireinfo(\.dev)?\.ipfire\.org", [
96c9bb79 187 (r"/", fireinfo.IndexHandler),
8ab37e0b
MT
188
189 # Vendors
190 (r"/vendors", fireinfo.VendorsHandler),
191 (r"/vendors/(pci|usb)/([0-9a-f]{4})", fireinfo.VendorHandler),
66862195 192
1e3b2aad 193 # Driver
0cd21a36 194 (r"/drivers/(.*)", fireinfo.DriverDetail),
1e3b2aad 195
66862195 196 # Show profiles
96c9bb79 197 (r"/profile/random", fireinfo.RandomProfileHandler),
b84b407f 198 (r"/profile/([a-z0-9]{40})", fireinfo.ProfileHandler),
91a446f0 199
ed2e3c1f
MT
200 # Stats
201 (r"/releases", fireinfo.ReleasesHandler),
202
30852a9e 203 # Send profiles.
96c9bb79 204 (r"/send/([a-z0-9]+)", fireinfo.ProfileSendHandler),
66862195
MT
205
206 # Stats handlers
96c9bb79
MT
207 (r"/statistics", fireinfo.StatsHandler),
208 (r"/statistics/processors", fireinfo.StatsProcessorsHandler),
209 (r"/statistics/processors/(arm|x86)", fireinfo.StatsProcessorDetailHandler),
3808b871 210 ])
5cf160e0 211
c37ec602 212 # i-use.ipfire.org
8fceca0a 213 self.add_handlers(r"i-use(\.dev)?\.ipfire\.org", [
e2f2865d 214 (r"/", tornado.web.RedirectHandler, { "url" : "https://www.ipfire.org/" }),
395c1ac0 215 (r"/profile/([a-f0-9]{40})/([0-9]+).png", iuse.ImageHandler),
c37ec602
MT
216 ])
217
8e2e1261
MT
218 # boot.ipfire.org
219 BOOT_STATIC_PATH = os.path.join(self.settings["static_path"], "netboot")
8fceca0a 220 self.add_handlers(r"boot(\.dev)?\.ipfire\.org", [
f301d952 221 (r"/", tornado.web.RedirectHandler, { "url" : "https://wiki.ipfire.org/installation/pxe" }),
8e2e1261
MT
222
223 # Configurations
f301d952
MT
224 (r"/premenu.cfg", boot.PremenuCfgHandler),
225 (r"/menu.gpxe", boot.MenuGPXEHandler),
226 (r"/menu.cfg", boot.MenuCfgHandler),
8e2e1261
MT
227
228 # Static files
37b5c0cf 229 (r"/(boot\.png|pxelinux\.0|menu\.c32|vesamenu\.c32)",
8e2e1261
MT
230 tornado.web.StaticFileHandler, { "path" : BOOT_STATIC_PATH }),
231 ])
232
60024cc8 233 # nopaste.ipfire.org
8fceca0a 234 self.add_handlers(r"nopaste(\.dev)?\.ipfire\.org", [
a41085fb
MT
235 (r"/", nopaste.CreateHandler),
236 (r"/raw/(.*)", nopaste.RawHandler),
237 (r"/view/(.*)", nopaste.ViewHandler),
3808b871 238 ] + authentication_handlers)
60024cc8 239
f5b01fc2
MT
240 # location.ipfire.org
241 self.add_handlers(r"location(\.dev)?\.ipfire\.org", [
242 (r"/", location.IndexHandler),
243 (r"/lookup/(.+)", location.LookupHandler),
244 ])
245
9068dba1 246 # geoip.ipfire.org
8fceca0a 247 self.add_handlers(r"geoip(\.dev)?\.ipfire\.org", [
f5b01fc2 248 (r"/", tornado.web.RedirectHandler, { "url" : "https://location.ipfire.org/" }),
3808b871 249 ])
9068dba1 250
66862195
MT
251 # talk.ipfire.org
252 self.add_handlers(r"talk(\.dev)?\.ipfire\.org", [
786e9ca8
MT
253 (r"/", tornado.web.RedirectHandler, { "url" : "https://people.ipfire.org/" }),
254 ])
66862195 255
03706893
MT
256 # people.ipfire.org
257 self.add_handlers(r"people(\.dev)?\.ipfire\.org", [
786e9ca8 258 (r"/", people.IndexHandler),
30aeccdb 259 (r"/conferences", people.ConferencesHandler),
786e9ca8
MT
260 (r"/search", people.SearchHandler),
261 (r"/users", people.UsersHandler),
262 (r"/users/(\w+)", people.UserHandler),
03706893 263 (r"/users/(\w+)\.jpg", people.AvatarHandler),
68ece434 264 (r"/users/(\w+)/calls/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})", people.CallHandler),
bdaf6b46 265 (r"/users/(\w+)/calls(?:/(\d{4}-\d{2}-\d{2}))?", people.CallsHandler),
e96e445b 266 (r"/users/(\w+)/edit", people.UserEditHandler),
3ea97943 267 (r"/users/(\w+)/passwd", people.UserPasswdHandler),
f4672785 268 (r"/users/(\w+)/ssh-keys", people.SSHKeysIndexHandler),
44b75370
MT
269 (r"/users/(\w+)/ssh-keys/(SHA256\:.*)/delete", people.SSHKeysDeleteHandler),
270 (r"/users/(\w+)/ssh-keys/(SHA256\:.*)", people.SSHKeysDownloadHandler),
0d1fb712 271 (r"/users/(\w+)/ssh-keys/upload", people.SSHKeysUploadHandler),
e0daee8f 272 (r"/users/(\w+)/sip", people.SIPHandler),
786e9ca8 273 ] + authentication_handlers)
2cd9af74 274
ae0228e1 275 # ipfire.org
45592df5 276 self.add_handlers(r"ipfire\.org", [
ba43a892 277 (r".*", tornado.web.RedirectHandler, { "url" : "https://www.ipfire.org" })
5cf160e0 278 ])
3add293a 279
feb02477
MT
280 logging.info("Successfully initialied application")
281
574a88c7
MT
282 def format_country_name(self, handler, country_code):
283 return ipfire.countries.get_name(country_code)
284
cc3b928d
MT
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
401827c2 314
e96e445b
MT
315 def format_phone_number(self, handler, number):
316 if not isinstance(number, phonenumbers.PhoneNumber):
317 try:
01e73b0e 318 number = phonenumbers.parse(number, None)
e96e445b
MT
319 except phonenumbers.phonenumberutil.NumberParseException:
320 return number
c01ad253
MT
321
322 return phonenumbers.format_number(number, phonenumbers.PhoneNumberFormat.INTERNATIONAL)
323
e96e445b
MT
324 def format_phone_number_to_e164(self, handler, number):
325 return phonenumbers.format_number(number, phonenumbers.PhoneNumberFormat.E164)
326
327 def format_phone_number_location(self, handler, number):
328 s = [
329 phonenumbers.geocoder.description_for_number(number, handler.locale.code),
330 phonenumbers.region_code_for_number(number),
331 ]
332
333 return ", ".join((e for e in s if e))
334
401827c2
MT
335
336def grouper(handler, iterator, n):
337 """
338 Returns groups of n from the iterator
339 """
340 i = iter(iterator)
341
342 while True:
343 ret = list(itertools.islice(i, 0, n))
344 if not ret:
345 break
346
347 yield ret