]> git.ipfire.org Git - people/ms/westferry.git/blob - src/westferry/handlers/base.py
UI: Rename menu to menus
[people/ms/westferry.git] / src / westferry / handlers / base.py
1 #!/usr/bin/python3
2 ###############################################################################
3 # #
4 # Westferry - The IPFire web user interface #
5 # Copyright (C) 2015 IPFire development team #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 import functools
23 import tornado.web
24
25 from .. import ui
26 from ..i18n import _, N_
27
28 _handlers = []
29
30 class HandlerRegistration(type):
31 def __init__(handler, name, bases, dict):
32 type.__init__(handler, name, bases, dict)
33
34 # The main class from which is inherited is not registered
35 # as a plugin
36 if name.endswith("BaseHandler"):
37 return
38
39 if handler.url is None:
40 raise RuntimeError(_("Handler %s is improperly configured") % handler)
41
42 _handlers.append(handler)
43
44 @staticmethod
45 def get_handlers():
46 return _handlers
47
48
49 class BaseHandler(tornado.web.RequestHandler, metaclass=HandlerRegistration):
50 url = None
51
52 title = N_("No Title")
53
54 # Points to the menu map of the section in which the handler is in
55 menu = None
56
57 @property
58 def backend(self):
59 """
60 Shortcut to access the backend
61 """
62 return self.application.backend
63
64 @property
65 def topmenu(self):
66 """
67 Creates the default menu in to the top navigation
68 """
69 _ = self.locale.translate
70
71 # XXX This is ugly, but since this file declares the base handler,
72 # recursive imports fail
73 from . import analytics
74 from . import demo
75
76 menu = ui.menus.Menu(self)
77
78 # Analytics
79 menu.add_handler(analytics.AnalyticsOverviewHandler, title=_("Analytics"))
80
81 # Demo (only in debug mode)
82 if self.backend.debug:
83 submenu = menu.add_menu(_("Demo"))
84
85 submenu.add_handler(demo.DemoOverviewHandler)
86 submenu.add_handler(demo.DemoFormsHandler)
87
88 return menu
89
90 @functools.cached_property
91 def tabs(self):
92 return ui.tabs.Tabs(self)
93
94 def get_template_namespace(self):
95 ns = tornado.web.RequestHandler.get_template_namespace(self)
96
97 # Add some global constants
98 ns.update({
99 "VERSION" : self.backend.version,
100 })
101
102 # Add some more
103 ns.update({
104 "menu" : self.menu,
105 })
106
107 return ns
108
109 def get_argument_int(self, name, *args, **kwargs):
110 val = self.get_argument(name, *args, **kwargs)
111
112 if val is None:
113 return
114
115 try:
116 return int(val)
117 except ValueError:
118 raise tornado.web.HTTPError(400,
119 _("Invalid type for '%s', expected integer") % name)
120
121 def get(self):
122 # Render the default view
123 self.render("default.html")
124
125 def post(self):
126 """
127 This is the default handler which will find the correct form
128 and execute it.
129 """
130 form_id = self.get_argument("form")
131
132 # Find the form
133 form = self.tabs.get_form(form_id)
134 if not form:
135 raise tornado.web.HTTPError(400, "Could not find form with ID '%s'" % form_id)
136
137 # Execute the form action
138 form.execute()