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