]> git.ipfire.org Git - people/ms/westferry.git/blame - src/westferry/handlers/base.py
system: Add scaffolding to more testing
[people/ms/westferry.git] / src / westferry / handlers / base.py
CommitLineData
9877dda9
MT
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
19514d9c 22import functools
9877dda9
MT
23import tornado.web
24
19514d9c 25from .. import ui
615d6335 26from ..i18n import _, N_
a6144752 27
9877dda9
MT
28_handlers = []
29
30class 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
49class BaseHandler(tornado.web.RequestHandler, metaclass=HandlerRegistration):
50 url = None
21a66327 51
a6144752
MT
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
21a66327
MT
57 @property
58 def backend(self):
59 """
60 Shortcut to access the backend
61 """
62 return self.application.backend
63
17eb42fd
MT
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
84759d58 75 from . import system
17eb42fd 76
9231bd0e 77 menu = ui.menus.Menu(self)
17eb42fd
MT
78
79 # Analytics
25a2da75 80 menu.add_handler(analytics.OverviewHandler, title=_("Analytics"))
17eb42fd 81
84759d58
MT
82 # System
83 menu.add_handler(system.SummaryHandler, title=_("System"))
84
17eb42fd
MT
85 # Demo (only in debug mode)
86 if self.backend.debug:
87 submenu = menu.add_menu(_("Demo"))
88
25a2da75
MT
89 submenu.add_handler(demo.OverviewHandler)
90 submenu.add_handler(demo.FormsHandler)
17eb42fd
MT
91
92 return menu
93
19514d9c
MT
94 @functools.cached_property
95 def tabs(self):
96 return ui.tabs.Tabs(self)
97
a6144752
MT
98 def get_template_namespace(self):
99 ns = tornado.web.RequestHandler.get_template_namespace(self)
100
101 # Add some global constants
102 ns.update({
103 "VERSION" : self.backend.version,
104 })
105
106 # Add some more
107 ns.update({
108 "menu" : self.menu,
109 })
110
111 return ns
112
21a66327
MT
113 def get_argument_int(self, name, *args, **kwargs):
114 val = self.get_argument(name, *args, **kwargs)
115
116 if val is None:
117 return
118
119 try:
120 return int(val)
121 except ValueError:
122 raise tornado.web.HTTPError(400,
123 _("Invalid type for '%s', expected integer") % name)
19514d9c
MT
124
125 def get(self):
126 # Render the default view
127 self.render("default.html")
edf0b75a
MT
128
129 def post(self):
130 """
131 This is the default handler which will find the correct form
132 and execute it.
133 """
134 form_id = self.get_argument("form")
135
136 # Find the form
137 form = self.tabs.get_form(form_id)
138 if not form:
139 raise tornado.web.HTTPError(400, "Could not find form with ID '%s'" % form_id)
140
141 # Execute the form action
142 form.execute()