Merge branch 'apigen'
[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 readDashboard(self, fn):
29         try:
30             template= open("/opt/planetstack/templates/admin/dashboard/%s.html" % fn, "r").read()
31             if (fn=="tenant"):
32                 # fix for tenant view - it writes html to a div called tabs-5
33                 template = '<div id="tabs-5"></div>' + template
34             return template
35         except:
36             return "failed to open %s" % fn
37
38     def multiDashboardView(self, request, context):
39         head_template = self.head_template
40         tail_template = self.tail_template
41
42         body = """
43          <div id="hometabs" >
44          <ul id="suit_form_tabs" class="nav nav-tabs nav-tabs-suit" data-tab-prefix="suit-tab">
45         """
46
47         dashboards = request.user.get_dashboards()
48
49         # customize is a special dashboard they always get
50         customize = DashboardView.objects.filter(name="Customize")
51         if customize:
52             dashboards.append(customize[0])
53
54         for i,view in enumerate(dashboards):
55             body = body + '<li><a href="#dashtab-%d">%s</a></li>\n' % (i, view.name)
56
57         body = body + "</ul>\n"
58
59         for i,view in enumerate(dashboards):
60             url = view.url
61             body = body + '<div id="dashtab-%d">\n' % i
62             if url.startswith("template:"):
63                 fn = url[9:]
64                 body = body + self.readDashboard(fn)
65             body = body + '</div>\n'
66
67         body=body+"</div>\n"
68
69         t = template.Template(head_template + body + self.tail_template)
70
71         response_kwargs = {}
72         response_kwargs.setdefault('content_type', self.content_type)
73         return self.response_class(\r
74             request = request,\r
75             template = t,\r
76             context = context,\r
77             **response_kwargs)
78
79     def singleDashboardView(self, request, name, context):
80         head_template = self.head_template
81         tail_template = self.tail_template
82
83         t = template.Template(head_template + self.readDashboard(name) + 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