]> git.ipfire.org Git - people/ms/westferry.git/blame - src/westferry/ui/graphs.py
UI: graphs: Add option to hide graph title
[people/ms/westferry.git] / src / westferry / ui / graphs.py
CommitLineData
03f518c7
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 urllib.parse
23
24from . import base
25
26DEFAULT_INTERVAL = "day"
27DEFAULT_FORMAT = "svg"
28
29
30class Graph(object):
31 def __init__(self, handler, template, object_id=None, title=None):
32 self.handler = handler
33 self._title = title
34
35 # Graph identifier
36 self.template = template
37 self.object_id = object_id
38
39 # Set the default interval
40 self.interval = handler.get_argument("interval", DEFAULT_INTERVAL)
41 self.format = handler.get_argument("format", DEFAULT_FORMAT)
42
43 @property
44 def backend(self):
45 """
46 Shortcut to the backend.
47 """
48 return self.handler.backend
49
50 @property
51 def locale(self):
52 """
53 Shortcut to the locale
54 """
55 return self.handler.locale
56
57 @property
58 def graph_info(self):
59 if not hasattr(self, "_graph_info"):
60 self._graph_info = self.backend.graphs.graph_info(self.template,
61 object_id=self.object_id)
62
63 return self._graph_info
64
65 @property
66 def title(self):
67 return self._title or self.graph_info.get("title")
68
69 # Format
70
71 def get_format(self):
72 return self._format
73
74 def set_format(self, format):
75 # TODO check for valid inputs
76 self._format = format
77
78 # Interval
79
80 def get_interval(self):
81 return self._interval
82
83 def set_interval(self, interval):
84 # TODO check for valid inputs
85 self._interval = interval
86
87 interval = property(get_interval, set_interval)
88
89 def make_namespace(self, **kwargs):
90 ns = {
91 "format" : self.format,
92 "interval" : self.interval,
93 }
94 ns.update(kwargs)
95
96 return ns
97
98 def _make_url(self, url, format=None, **kwargs):
99 ns = self.make_namespace(**kwargs)
100
101 url = url % {
102 "format" : format or self.format or DEFAULT_FORMAT,
103 "object_id" : self.object_id,
104 "template" : self.template,
105 }
106
107 query_string = urllib.parse.urlencode(ns)
108 if query_string:
109 url += "?%s" % query_string
110
111 return url
112
113 def make_image_url(self, **kwargs):
114 if self.object_id:
115 url = "/graph/%(template)s/%(object_id)s.%(format)s"
116 else:
117 url = "/graph/%(template)s.%(format)s"
118
119 return self._make_url(url, **kwargs)
120
0ac359f3
MT
121 def make_thumbnail_url(self, **kwargs):
122 if self.object_id:
123 url = "/graph/thumbnail/%(template)s/%(object_id)s.%(format)s"
124 else:
125 url = "/graph/thumbnail/%(template)s.%(format)s"
126
127 return self._make_url(url, **kwargs)
128
03f518c7
MT
129 def make_url(self, **kwargs):
130 if self.object_id:
131 url = "/graph/%(template)s/%(object_id)s"
132 else:
133 url = "/graph/%(template)s"
134
135 return self._make_url(url, **kwargs)
136
137 def generate_graph(self, **kwargs):
138 return self.backend.graphs.generate_graph(self.template,
139 object_id=self.object_id, **kwargs)
140
141
142class GraphBoxModule(base.BaseUIModule):
ba5eadb9
MT
143 def render(self, graph, show_title=True):
144 return self.render_string("modules/graphs/box.html", graph=graph,
145 show_title=show_title)