]> git.ipfire.org Git - people/ms/westferry.git/blame - src/westferry/handlers/base.py
Add a simple menu implementation
[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
22import tornado.web
23
a6144752
MT
24N_ = lambda x: x
25
9877dda9
MT
26_handlers = []
27
28class HandlerRegistration(type):
29 def __init__(handler, name, bases, dict):
30 type.__init__(handler, name, bases, dict)
31
32 # The main class from which is inherited is not registered
33 # as a plugin
34 if name.endswith("BaseHandler"):
35 return
36
37 if handler.url is None:
38 raise RuntimeError(_("Handler %s is improperly configured") % handler)
39
40 _handlers.append(handler)
41
42 @staticmethod
43 def get_handlers():
44 return _handlers
45
46
47class BaseHandler(tornado.web.RequestHandler, metaclass=HandlerRegistration):
48 url = None
21a66327 49
a6144752
MT
50 title = N_("No Title")
51
52 # Points to the menu map of the section in which the handler is in
53 menu = None
54
21a66327
MT
55 @property
56 def backend(self):
57 """
58 Shortcut to access the backend
59 """
60 return self.application.backend
61
a6144752
MT
62 def get_template_namespace(self):
63 ns = tornado.web.RequestHandler.get_template_namespace(self)
64
65 # Add some global constants
66 ns.update({
67 "VERSION" : self.backend.version,
68 })
69
70 # Add some more
71 ns.update({
72 "menu" : self.menu,
73 })
74
75 return ns
76
21a66327
MT
77 def get_argument_int(self, name, *args, **kwargs):
78 val = self.get_argument(name, *args, **kwargs)
79
80 if val is None:
81 return
82
83 try:
84 return int(val)
85 except ValueError:
86 raise tornado.web.HTTPError(400,
87 _("Invalid type for '%s', expected integer") % name)