blob: b7fbdd9c071e7b9ba8a627170c3970bcd6720892 [file] [log] [blame]
srikanth116e6e82014-08-19 07:22:37 -07001#
2# Copyright (c) 2013 Big Switch Networks, Inc.
3#
4# Licensed under the Eclipse Public License, Version 1.0 (the
5# "License"); you may not use this file except in compliance with the
6# License. You may obtain a copy of the License at
7#
8# http://www.eclipse.org/legal/epl-v10.html
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13# implied. See the License for the specific language governing
14# permissions and limitations under the License.
15#
16
17# Views for the core controller UI
18#
19
20from django.shortcuts import render_to_response
21from django.utils import simplejson
22from django.http import HttpResponse
23from django.contrib.auth.decorators import login_required
24from django.contrib.auth.models import User
25from django.template import RequestContext
26from sdncon.apploader import AppLoader, AppLister
27from sdncon.clusterAdmin.utils import conditionally, isCloudBuild
28from sdncon.clusterAdmin.models import Customer, Cluster, CustomerUser
29import os
30
31JSON_CONTENT_TYPE = 'application/json'
32
33# --- View for the root page of any application
34@conditionally(login_required, isCloudBuild())
35def show_application_tabs(request, app):
36 clusterlist = []
37 cus = CustomerUser.objects.all()
38 for cu in cus:
39 if cu.user.username == request.user.username:
40 for cluster in cu.customer.cluster_set.all():
41 clusterlist.append({'customer': cluster.customer.customername,
42 'clustername': cluster.clustername,
43 'clusterid': unicode(cluster)})
44
45 return render_to_response('coreui/templates/showapp.html',
46 {'apps':[x for x in AppLoader.getApps() if not hasattr(x,'invisible')],'currentapp':app,'tabs':AppLoader.getApp(app).tabs,'clusterlist':clusterlist, 'isCloudBuild': isCloudBuild()},
47 context_instance=RequestContext(request))
48
49# --- Generic Views
50def embedded_datatable(request, model, options=None, skip_columns=[]):
51
52 model_name = model.__name__
53 title = model_name + " Table"
54 tablename = model_name+"-embed"
55 table_data = Switch.objects.all()
56
57 # Find headers
58 table_headers = []
59 for field in model._meta.local_fields:
60 table_headers.append(field.name)
61 print table_headers
62
63 # Skip unwanted columns
64 for c in skip_columns:
65 if c in table_headers:
66 table_headers.remove(c)
67
68 # Populate data
69 table_data = []
70 for instance in model.objects.all():
71 table_row = []
72 for field in table_headers:
73 value = instance.__dict__.get(field)
74 table_row.append(value)
75 table_data.append(table_row)
76
77 # Beautify Headers
78 ##TODO ALEX query model info directly, don't use model info list then remove import and delete file
79 #print model_name
80 #if model_name.lower() in model_info_list.model_info_dict:
81 # print "yes"
82 # table_headers_c = []
83 # field_dict = model_info_list.model_info_dict[model_name.lower()]["fields"]
84 # for h in table_headers:
85 # h = h.replace("_","-")
86 # print h, field_dict
87 # if h in field_dict:
88 # table_headers_c.append(field_dict[h]['verbose_name'])
89 # else:
90 # table_headers_c.append(h)
91 # table_headers = table_headers_c
92
93 return render_to_response('coreui/templates/datatable-embed.html', {
94 'tabletitle': title,
95 'tablename' : tablename,
96 'tableheaders' : table_headers,
97 'tabledata' : table_data,
98 'dataTableOptions': options
99 })
100
101def embedded_datatable_from_model(request, model, options=None, skip_columns=[]):
102
103 model_name = model.__name__
104 title = model_name + " Table"
105 tablename = model_name+"-embed"
106 table_data = Switch.objects.all()
107
108 # Find headers
109 table_headers = []
110 for field in model._meta.local_fields:
111 table_headers.append(field.name)
112 print table_headers
113
114 # Skip unwanted columns
115 for c in skip_columns:
116 if c in table_headers:
117 table_headers.remove(c)
118
119 # Populate data
120 table_data = []
121 for instance in model.objects.all():
122 table_row = []
123 for field in table_headers:
124 value = instance.__dict__.get(field)
125 table_row.append(value)
126 table_data.append(table_row)
127
128 # Beautify Headers
129 print model_name
130 if model_name.lower() in model_info_list.model_info_dict:
131 print "yes"
132 table_headers_c = []
133 field_dict = model_info_list.model_info_dict[model_name.lower()]["fields"]
134 for h in table_headers:
135 h = h.replace("_","-")
136 print h, field_dict
137 if h in field_dict:
138 if 'verbose_name' in field_dict[h]:
139 table_headers_c.append(field_dict[h]['verbose_name'])
140 else:
141 table_headers_c.append(h)
142 else:
143 table_headers_c.append(h)
144 table_headers = table_headers_c
145
146 return render_to_response('coreui/templates/datatable-embed.html', {
147 'tabletitle': title,
148 'tablename' : tablename,
149 'tableheaders' : table_headers,
150 'tabledata' : table_data,
151 'dataTableOptions': options
152 })
153