]> git.ipfire.org Git - people/ms/westferry.git/blob - src/westferry/ui/tabs.py
d4b94568bbea89839198594864cc11ef5ab6ecd7
[people/ms/westferry.git] / src / westferry / ui / tabs.py
1 #!/usr/bin/python3
2 ###############################################################################
3 # #
4 # Westferry - The IPFire web user interface #
5 # Copyright (C) 2021 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 uuid
23
24 from . import base
25 from . import forms
26 from . import graphs
27
28 class TabsModule(base.BaseUIModule):
29 def render(self, tabs):
30 return self.render_string("modules/tabs.html", tabs=tabs)
31
32
33 class Tabs(object):
34 def __init__(self, handler, id=None):
35 self.handler = handler
36
37 # Store ID or generate a random one
38 self.id = id or uuid.uuid4()
39
40 self.tabs = {}
41
42 def __getattr__(self, key):
43 try:
44 return self.tabs[key]
45 except KeyError as e:
46 raise AttributeError(key) from e
47
48 def __iter__(self):
49 for id in self.tabs:
50 yield self.tabs[id]
51
52 def add_tab(self, id, *args, **kwargs):
53 # Check if a tab with this ID already exists
54 #if id in self:
55 # raise ValueError("Tab with ID '%s' already exists" % id)
56
57 # Create a new tab
58 self.tabs[id] = tab = Tab(self.handler, id, *args, **kwargs)
59
60 # Return the new tab
61 return tab
62
63 def get_form(self, id):
64 """
65 Returns the form with a matching ID
66 """
67 for tab in self:
68 for item in tab.items:
69 if isinstance(item, forms.Form):
70 if item.id == id:
71 return item
72
73
74 class Tab(object):
75 def __init__(self, handler, id, title):
76 self.handler = handler
77
78 # TODO Check format of ID
79 self.id = id
80
81 self.title = title
82
83 # List to store all items that have been added to this tab
84 self.items = []
85
86 def _add_item(self, cls, *args, **kwargs):
87 item = cls(self.handler, *args, **kwargs)
88 self.items.append(item)
89
90 return item
91
92 def add_form(self, *args, **kwargs):
93 return self._add_item(forms.Form, *args, **kwargs)
94
95 def add_graph(self, *args, **kwargs):
96 return self._add_item(graphs.Graph, *args, **kwargs)