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