Scott Baker | c7325a4 | 2014-05-30 16:06:46 -0700 | [diff] [blame] | 1 | from view_common import * |
| 2 | |
| 3 | class DashboardWelcomeView(TemplateView): |
| 4 | template_name = 'admin/dashboard/welcome.html' |
| 5 | |
| 6 | def get(self, request, *args, **kwargs): |
| 7 | context = self.get_context_data(**kwargs) |
| 8 | context = getDashboardContext(request.user, context) |
| 9 | return self.render_to_response(context=context) |
| 10 | |
| 11 | class DashboardDynamicView(TemplateView): |
| 12 | head_template = r"""{% extends "admin/dashboard/dashboard_base.html" %} |
| 13 | {% load admin_static %} |
| 14 | {% block content %} |
| 15 | """ |
| 16 | |
Scott Baker | a916aa1 | 2014-11-03 14:35:58 -0800 | [diff] [blame] | 17 | head_wholePage_template = r"""{% extends "admin/wholePage.html" %} |
| 18 | {% load admin_static %} |
| 19 | {% block content %} |
| 20 | """ |
| 21 | |
Scott Baker | c7325a4 | 2014-05-30 16:06:46 -0700 | [diff] [blame] | 22 | tail_template = r"{% endblock %}" |
| 23 | |
| 24 | def get(self, request, name="root", *args, **kwargs): |
| 25 | context = self.get_context_data(**kwargs) |
| 26 | context = getDashboardContext(request.user, context) |
| 27 | |
| 28 | if name=="root": |
| 29 | return self.multiDashboardView(request, context) |
Scott Baker | 731723b | 2014-11-03 15:59:52 -0800 | [diff] [blame] | 30 | elif kwargs.get("wholePage",None): |
Scott Baker | a916aa1 | 2014-11-03 14:35:58 -0800 | [diff] [blame] | 31 | return self.singleFullView(request, name, context) |
Scott Baker | c7325a4 | 2014-05-30 16:06:46 -0700 | [diff] [blame] | 32 | else: |
| 33 | return self.singleDashboardView(request, name, context) |
| 34 | |
Scott Baker | bb53ee8 | 2014-06-18 18:26:45 -0700 | [diff] [blame] | 35 | def readTemplate(self, fn): |
Scott Baker | bbe7c82 | 2014-07-13 16:48:46 -0700 | [diff] [blame] | 36 | TEMPLATE_DIRS = ["/opt/planetstack/templates/admin/dashboard/", |
| 37 | "/opt/planetstack/core/xoslib/dashboards/"] |
| 38 | |
| 39 | for template_dir in TEMPLATE_DIRS: |
| 40 | pathname = os.path.join(template_dir, fn) + ".html" |
| 41 | if os.path.exists(pathname): |
| 42 | break |
| 43 | else: |
| 44 | return "failed to find %s in %s" % (fn, TEMPLATE_DIRS) |
| 45 | |
| 46 | template= open(pathname, "r").read() |
| 47 | if (fn=="tenant"): |
| 48 | # fix for tenant view - it writes html to a div called tabs-5 |
| 49 | template = '<div id="tabs-5"></div>' + template |
| 50 | return template |
Scott Baker | c7325a4 | 2014-05-30 16:06:46 -0700 | [diff] [blame] | 51 | |
Scott Baker | bb53ee8 | 2014-06-18 18:26:45 -0700 | [diff] [blame] | 52 | def embedDashboard(self, url): |
| 53 | if url.startswith("template:"): |
| 54 | fn = url[9:] |
| 55 | return self.readTemplate(fn) |
| 56 | elif url.startswith("http"): |
| 57 | return '<iframe src="%s" width="100%%" height="100%%" style="min-height: 1024px;" frameBorder="0"></iframe>' % url |
Scott Baker | 5576645 | 2014-07-15 10:58:22 -0700 | [diff] [blame] | 58 | else: |
| 59 | return "don't know how to load dashboard %s" % url |
Scott Baker | bb53ee8 | 2014-06-18 18:26:45 -0700 | [diff] [blame] | 60 | |
Scott Baker | c7325a4 | 2014-05-30 16:06:46 -0700 | [diff] [blame] | 61 | def multiDashboardView(self, request, context): |
| 62 | head_template = self.head_template |
| 63 | tail_template = self.tail_template |
| 64 | |
| 65 | body = """ |
| 66 | <div id="hometabs" > |
| 67 | <ul id="suit_form_tabs" class="nav nav-tabs nav-tabs-suit" data-tab-prefix="suit-tab"> |
| 68 | """ |
| 69 | |
| 70 | dashboards = request.user.get_dashboards() |
| 71 | |
| 72 | # customize is a special dashboard they always get |
| 73 | customize = DashboardView.objects.filter(name="Customize") |
| 74 | if customize: |
| 75 | dashboards.append(customize[0]) |
| 76 | |
| 77 | for i,view in enumerate(dashboards): |
Scott Baker | b8f3cab | 2015-01-18 16:33:30 -0800 | [diff] [blame] | 78 | # don't display disabled dashboards |
| 79 | if (not view.enabled): |
| 80 | continue |
Scott Baker | c7325a4 | 2014-05-30 16:06:46 -0700 | [diff] [blame] | 81 | body = body + '<li><a href="#dashtab-%d">%s</a></li>\n' % (i, view.name) |
| 82 | |
| 83 | body = body + "</ul>\n" |
| 84 | |
| 85 | for i,view in enumerate(dashboards): |
Scott Baker | b8f3cab | 2015-01-18 16:33:30 -0800 | [diff] [blame] | 86 | # don't display disabled dashboards |
| 87 | if (not view.enabled): |
| 88 | continue |
| 89 | |
Scott Baker | c7325a4 | 2014-05-30 16:06:46 -0700 | [diff] [blame] | 90 | url = view.url |
| 91 | body = body + '<div id="dashtab-%d">\n' % i |
Scott Baker | 1788599 | 2014-12-19 17:15:59 -0800 | [diff] [blame] | 92 | if (view.controllers.all().count()>0): |
Scott Baker | 9fbad3b | 2015-01-26 22:12:40 -0800 | [diff] [blame^] | 93 | body = body + 'Controller: <select id="dashselect-%d">' % i; |
| 94 | body = body + '<option value="None">(select a controller)</option>'; |
Scott Baker | f0f5073 | 2014-12-23 09:39:06 -0800 | [diff] [blame] | 95 | for j,controllerdashboard in enumerate(view.controllerdashboardviews.all()): |
Scott Baker | 1788599 | 2014-12-19 17:15:59 -0800 | [diff] [blame] | 96 | body = body + '<option value="%d">%s</option>' % (j, controllerdashboard.controller.name) |
Scott Baker | 9fbad3b | 2015-01-26 22:12:40 -0800 | [diff] [blame^] | 97 | body = body + '</select><hr>' |
Scott Baker | 1788599 | 2014-12-19 17:15:59 -0800 | [diff] [blame] | 98 | |
Scott Baker | f0f5073 | 2014-12-23 09:39:06 -0800 | [diff] [blame] | 99 | for j,controllerdashboard in enumerate(view.controllerdashboardviews.all()): |
Scott Baker | 49af8e2 | 2015-01-06 23:00:30 -0800 | [diff] [blame] | 100 | body = body + '<script type="text/template" id="dashtemplate-%d-%d">\n%s\n</script>\n' % (i,j, self.embedDashboard(controllerdashboard.url)); |
| 101 | |
| 102 | body = body + '<div id="dashcontent-%d" class="dashcontent"></div>\n' % i |
Scott Baker | 1788599 | 2014-12-19 17:15:59 -0800 | [diff] [blame] | 103 | |
| 104 | body = body + """<script> |
Scott Baker | 49af8e2 | 2015-01-06 23:00:30 -0800 | [diff] [blame] | 105 | $("#dashselect-%d").change(function() { |
Scott Baker | 1788599 | 2014-12-19 17:15:59 -0800 | [diff] [blame] | 106 | v=$("#dashselect-%d").val(); |
Scott Baker | 9fbad3b | 2015-01-26 22:12:40 -0800 | [diff] [blame^] | 107 | if (v=="None") { |
| 108 | $("#dashcontent-%d").html(""); |
| 109 | return; |
| 110 | } |
Scott Baker | 49af8e2 | 2015-01-06 23:00:30 -0800 | [diff] [blame] | 111 | $("#dashcontent-%d").html( $("#dashtemplate-%d-" + v).html() ); |
Scott Baker | 1788599 | 2014-12-19 17:15:59 -0800 | [diff] [blame] | 112 | }); |
Scott Baker | 9fbad3b | 2015-01-26 22:12:40 -0800 | [diff] [blame^] | 113 | //$("#dashcontent-%d").html( $("#dashtemplate-%d-0").html() ); |
Scott Baker | 1788599 | 2014-12-19 17:15:59 -0800 | [diff] [blame] | 114 | </script> |
Scott Baker | 9fbad3b | 2015-01-26 22:12:40 -0800 | [diff] [blame^] | 115 | """ % (i,i,i,i,i,i,i); |
Scott Baker | 1788599 | 2014-12-19 17:15:59 -0800 | [diff] [blame] | 116 | else: |
| 117 | body = body + self.embedDashboard(url) |
Scott Baker | c7325a4 | 2014-05-30 16:06:46 -0700 | [diff] [blame] | 118 | body = body + '</div>\n' |
| 119 | |
| 120 | body=body+"</div>\n" |
| 121 | |
| 122 | t = template.Template(head_template + body + self.tail_template) |
| 123 | |
| 124 | response_kwargs = {} |
| 125 | response_kwargs.setdefault('content_type', self.content_type) |
| 126 | return self.response_class(
|
| 127 | request = request,
|
| 128 | template = t,
|
| 129 | context = context,
|
| 130 | **response_kwargs) |
| 131 | |
| 132 | def singleDashboardView(self, request, name, context): |
| 133 | head_template = self.head_template |
| 134 | tail_template = self.tail_template |
| 135 | |
Scott Baker | bb53ee8 | 2014-06-18 18:26:45 -0700 | [diff] [blame] | 136 | t = template.Template(head_template + self.readTemplate(name) + self.tail_template) |
Scott Baker | c7325a4 | 2014-05-30 16:06:46 -0700 | [diff] [blame] | 137 | |
| 138 | response_kwargs = {} |
| 139 | response_kwargs.setdefault('content_type', self.content_type) |
| 140 | return self.response_class(
|
| 141 | request = request,
|
| 142 | template = t,
|
| 143 | context = context,
|
| 144 | **response_kwargs) |
| 145 | |
Scott Baker | a916aa1 | 2014-11-03 14:35:58 -0800 | [diff] [blame] | 146 | def singleFullView(self, request, name, context): |
| 147 | head_template = self.head_wholePage_template |
| 148 | tail_template = self.tail_template |
| 149 | |
| 150 | t = template.Template(head_template + self.readTemplate(name) + self.tail_template) |
| 151 | |
| 152 | response_kwargs = {} |
| 153 | response_kwargs.setdefault('content_type', self.content_type) |
| 154 | return self.response_class(
|
| 155 | request = request,
|
| 156 | template = t,
|
| 157 | context = context,
|
| 158 | **response_kwargs) |
| 159 | |