Merge branch 'master' of git://git.planet-lab.org/plstackapi
[plstackapi.git] / planetstack / core / dashboard / views / home.py
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
17     tail_template = r"{% endblock %}"
18
19     def get(self, request, name="root", *args, **kwargs):
20         context = self.get_context_data(**kwargs)
21         context = getDashboardContext(request.user, context)
22
23         if name=="root":
24             return self.multiDashboardView(request, context)
25         else:
26             return self.singleDashboardView(request, name, context)
27
28     def readTemplate(self, fn):
29         TEMPLATE_DIRS = ["/opt/planetstack/templates/admin/dashboard/",
30                          "/opt/planetstack/core/xoslib/dashboards/"]
31
32         for template_dir in TEMPLATE_DIRS:
33             pathname = os.path.join(template_dir, fn) + ".html"
34             if os.path.exists(pathname):
35                 break
36         else:
37             return "failed to find %s in %s" % (fn, TEMPLATE_DIRS)
38
39         template= open(pathname, "r").read()
40         if (fn=="tenant"):
41             # fix for tenant view - it writes html to a div called tabs-5
42             template = '<div id="tabs-5"></div>' + template
43         return template
44
45     def embedDashboard(self, url):
46         if url.startswith("template:"):
47             fn = url[9:]
48             return self.readTemplate(fn)
49         elif url.startswith("http"):
50             return '<iframe src="%s" width="100%%" height="100%%" style="min-height: 1024px;" frameBorder="0"></iframe>' % url
51         else:
52             return "don't know how to load dashboard %s" % url
53
54     def multiDashboardView(self, request, context):
55         head_template = self.head_template
56         tail_template = self.tail_template
57
58         body = """
59          <div id="hometabs" >
60          <ul id="suit_form_tabs" class="nav nav-tabs nav-tabs-suit" data-tab-prefix="suit-tab">
61         """
62
63         dashboards = request.user.get_dashboards()
64
65         # customize is a special dashboard they always get
66         customize = DashboardView.objects.filter(name="Customize")
67         if customize:
68             dashboards.append(customize[0])
69
70         for i,view in enumerate(dashboards):
71             body = body + '<li><a href="#dashtab-%d">%s</a></li>\n' % (i, view.name)
72
73         body = body + "</ul>\n"
74
75         for i,view in enumerate(dashboards):
76             url = view.url
77             body = body + '<div id="dashtab-%d">\n' % i
78             body = body + self.embedDashboard(url)
79             body = body + '</div>\n'
80
81         body=body+"</div>\n"
82
83         t = template.Template(head_template + body + self.tail_template)
84
85         response_kwargs = {}
86         response_kwargs.setdefault('content_type', self.content_type)
87         return self.response_class(\r
88             request = request,\r
89             template = t,\r
90             context = context,\r
91             **response_kwargs)
92
93     def singleDashboardView(self, request, name, context):
94         head_template = self.head_template
95         tail_template = self.tail_template
96
97         t = template.Template(head_template + self.readTemplate(name) + self.tail_template)
98
99         response_kwargs = {}
100         response_kwargs.setdefault('content_type', self.content_type)
101         return self.response_class(\r
102             request = request,\r
103             template = t,\r
104             context = context,\r
105             **response_kwargs)
106