Institution: generic template
[unfold.git] / portal / templates / institution.html
1 {% extends "layout_wide.html" %}
2
3 {% block head %} 
4 <script type="text/javascript" src="{{STATIC_URL}}/js/institution.js"></script>
5 <script type="text/javascript" src="{{STATIC_URL}}js/requests.js"></script>
6 {% endblock head %}
7
8 {% block content %}
9 <div class="container">
10         <div class="row">
11                 <div class="col-md-12">
12                          <div class="breadcrumbs">
13                 {% if project %}
14                                 Management &nbsp;>&nbsp; Project: <span id="authority_name">{{user_details.parent_authority}}</span>
15                 {% else %}
16                                 Management &nbsp;>&nbsp; Institution: <span id="authority_name">{{user_details.parent_authority}}</span>
17                 {% endif %}
18                          </div>
19                 </div>
20         </div>
21 </div>
22 <div class="container">
23         <div class="row">
24                 <div class="col-md-12">
25                         <ul class="nav nav-tabs nav-section">
26                                 <li class="active"><a href="#about">About</a></li>
27                                 <li><a href="#users">Users</a></li>
28                 {% if not project %}
29                                 <li><a href="#projects">Projects</a></li>
30                 {% else %}
31                                 <li><a href="#slices">Slices</a></li>
32                 {% endif %}
33                                 <li><a href="#requests">Requests</a></li>
34                         </ul>
35             </div>
36         </div>
37 </div>
38 <div class="container tab-content">
39         <div class="tab-pane active row" id="about">
40         <div class="col-md-12 el">
41         <h2>{{user_details.parent_authority}}</h2>
42         </div>
43         </div>
44         
45         <div class="tab-pane row" id="users" data-authority="{{user_details.parent_authority}}">
46                 <div class="col-md-12 el">
47                         <div id="user-tab-loading" style="padding-bottom:10px;"><img src="{{ STATIC_URL }}img/loading.gif" alt="Loading Slices" /></div>
48                                 <div id="user-tab-loaded" style="display:none;">
49                                 <table id="user-tab" class="table">
50                                         <tr>
51                                         <th>+/-</th>
52                             {% if not project %}
53                                         <th>Email</th>
54                             {% endif %}
55                                         <th>User hrn</th>
56                             <!--
57                                         <th>First name</th>
58                                         <th>Last name</th>
59                                         <th>Enabled</th>
60                             -->
61                                         </tr>
62                                 </table>
63                                 
64                         </div>
65                         {%if  pi %}     
66                         <div>
67                 {% if project %}
68                                 <button id="removepi" type="button" class="btn btn-danger"><span class="glyphicon glyphicon-remove"></span> Remove from project</button>
69                 {% else %}
70                                 <button id="makepi" type="button" class="btn btn-primary"><span class="glyphicon glyphicon-arrow-up"></span> Upgrade to PI</button>
71                                 <button id="removepi" type="button" class="btn"><span class="glyphicon glyphicon-arrow-down"></span> Downgrade to  user</button>
72                                 <button id="deleteusers" type="button" class="btn btn-danger"><span class="glyphicon glyphicon-remove"></span> Delete selected users</button>
73                 {% endif %}
74                         </div>
75                         {% endif %}
76                 </div>
77         </div>
78
79     {% if not project %}
80         <div class="tab-pane row" id="projects" data-authority="{{user_details.parent_authority}}">
81                 <div class="col-md-12 el">
82                         <div id="project-tab-loading" style="padding-bottom:10px;"><img src="{{ STATIC_URL }}img/loading.gif" alt="Loading Projects" /></div>
83                                 <div id="project-tab-loaded" style="display:none;">
84                                 <table id="project-tab" class="table">
85                                         <tr>
86                                         <th>+/-</th>
87                                         <th>Project hrn</th>
88                                         </tr>
89                                 </table>
90                                 
91                         </div>
92                         <div>
93                 {% if  pi %}
94                         <button id="createproject" type="button" class="btn btn-default"><span class="glyphicon glyphicon-plus"></span> Create project</button>
95                         <button id="deleteprojects" type="button" class="btn btn-danger"><span class="glyphicon glyphicon-remove"></span> Delete selected projects</button>
96                         {% else %}
97                         <button id="createproject" type="button" class="btn btn-default"><span class="glyphicon glyphicon-plus"></span> Request project</button>
98                         {% endif %}
99                         </div>
100                 </div>
101         </div>
102    {% endif %}
103         <div class="tab-pane row" id="slices">
104                 <div class="col-md-12 el">
105             <div id="slice-tab-loading" style="padding-bottom:10px;"><img src="{{ STATIC_URL }}img/loading.gif" alt="Loading Slices" /></div>
106             <div id="slice-tab-loaded" style="display:none;">
107                 <table id="slice-tab" class="table">
108                     <tr>
109                         <th>+/-</th>
110                         <th>Slice hrn</th>
111                         <th>Users</th>
112                         <th>Url</th>
113                         <!-- <th>nodes</th> -->
114                         <th>Creation</th>
115                     </tr>
116                 </table>                        
117             </div>
118         <div>
119                 {% if pi %}
120                         <button id="createslice" type="button" class="btn btn-default"><span class="glyphicon glyphicon-plus"></span> Create slice</button>
121             <button id="renewslices" type="button" class="btn btn-primary"><span class="glyphicon glyphicon-refresh"></span> Renew Slices</button>
122             <button id="deleteslices" type="button" class="btn btn-danger"><span class="glyphicon glyphicon-remove"></span> Delete Slices</button>
123                         {% else %}
124                         <button id="createslice" type="button" class="btn btn-default"><span class="glyphicon glyphicon-plus"></span> Request slice</button>
125                         {% endif %}
126         </div>
127            </div>
128         </div>
129         <div class="tab-pane row" id="requests">
130         <div id="spinner" style="padding-top:40px; padding-left:40px;"></div>
131         </div>
132 </div>
133 <script>
134 $(document).ready(function() {
135     {% if person %}
136     {% if user_details.parent_authority %}
137         $('#spinner').spin();
138         
139         $.post("/rest/myslice:slice/",{'fields':['slice_hrn','users','url','slice_date_created'],'filters':{'parent_authority':'{{user_details.parent_authority}}'}}, function( data ) {
140             var list_slices = [];
141             var table_slices = [];
142             /* "slice_hrn", "slice_description", "slice_type", "parent_authority", "created", "nodes", "slice_url", "slice_last_updated", "users", "slice_urn", "slice_expires" */
143             $.each( data, function( key, val ) {
144                 list_slices.push( "<li><a href=\"portal/slice/"+val.slice_hrn+"\">" + val.slice_hrn + "</a></li>" );
145                 if(val.nodes=="undefined" || val.nodes==null){
146                     nodes_length=0;
147                 }else{
148                     nodes_length=val.nodes.length;
149                 }
150                 if(val.users=="undefined" || val.users==null){
151                     users_length=0;
152                 }else{
153                     users_length=val.users.length;
154                 }
155
156                 if(val.url=="undefined" || val.url==null){
157                     slice_url="";
158                 }else{
159                     slice_url="<a href='"+val.url+"' target='_blank'>"+val.url+"</a>";
160                 }
161                 
162                 slice_row = "<tr id='"+val.slice_hrn+"'>";
163                 slice_row += "<td><input type='checkbox' class='slice' id='"+val.slice_hrn+"'><div id='"+val.slice_hrn+"-loading' style='display:none;padding-left:3px;'><img src='{{ STATIC_URL }}img/loading.gif'></div></td>";
164
165                 // filter links to slices, only those that the user has credentials for
166                 s = myslice.user.slices;
167                 if(s.length > 0 && $.inArray(val.slice_hrn, myslice.user.slices)!=-1){
168                     slice_row += "<td><a href='/slice/"+val.slice_hrn+"'>" + val.slice_hrn + "</a></td>";
169                 }else{
170                     slice_row += "<td>" + val.slice_hrn + "</td>";
171                 }
172                 slice_row += "<td>"+users_length+"</td>";
173                 slice_row += "<td>"+slice_url+"</td>";
174                 //slice_row += "<td>"+nodes_length+"</td>";
175                 slice_row += "<td>"+val.slice_date_created+"</td>";
176                 slice_row += "</tr>";
177                 table_slices.push(slice_row);
178                 
179             });
180            
181             /* $("div#slice-list").html($( "<ul/>", { html: list_slices.join( "" ) })); */
182             $("table#slice-tab tr:last").after(table_slices.join( "" ));
183             $("div#slice-tab-loaded").css("display","block");
184             $("div#slice-tab-loading").css("display","none");
185         });
186                 
187         {% if project %}
188                 $.post("/rest/myslice:authority/",{'fields':['pi_users'],'filters':{'authority_hrn':'{{user_details.parent_authority}}'}}, function( data ) {
189            
190             var table_users = [];
191             $.each( data[0].pi_users, function( key, val ) {
192                 user_row = "<tr id='"+val+"'>";
193                 user_row += "<td><input type='checkbox' class='user' id='"+val+"'><div id='"+val+"-loading' style='display:none;padding-left:3px;'><img src='{{ STATIC_URL }}img/loading.gif'></div></td>";
194                 user_row += "<td>"+val+"</td>";
195                 user_row += "</tr>";
196                 table_users.push(user_row);
197             });
198             $("table#user-tab tr:last").after(table_users.join( "" ));
199             $("div#user-tab-loaded").css("display","block");
200             $("div#user-tab-loading").css("display","none");
201
202         });
203         {% else %}
204         $.post("/rest/myslice:user/",{'filters':{'parent_authority':'{{user_details.parent_authority}}'}}, function( data ) {
205             var list_users = [];
206             var table_users = [];
207                     /* Available fields
208                     user_gid, user_enabled, slices, pi_authorities, keys, parent_authority, user_first_name,
209                     user_urn, user_last_name, user_phone, user_hrn, user_email, user_type
210                     */
211             $.each( data, function( key, val ) {
212                 list_users.push( "<li><a href=\"portal/user/"+val.user_email+"\">" + val.user_email + "</a></li>" );
213                 user_row = "<tr id='"+val.user_hrn+"'>";
214                 user_row += "<td><input type='checkbox' class='user' id='"+val.user_hrn+"' data-email='"+val.user_email+"'><div id='"+val.user_hrn+"-loading' style='display:none;padding-left:3px;'><img src='{{ STATIC_URL }}img/loading.gif'></div></td>";
215                 user_row += "<td>"+val.user_email+"</td>";
216                 user_row += "<td>"+val.user_hrn+"</td>";
217                 /*
218                 user_row += "<td>"+val.user_first_name+"</td>";
219                 user_row += "<td>"+val.user_last_name+"</td>";
220                             user_row += "<td>"+val.user_enabled+"</td>";
221                 */
222                 user_row += "</tr>";
223                 table_users.push(user_row);
224             });
225             $("table#user-tab tr:last").after(table_users.join( "" ));
226             $("div#user-tab-loaded").css("display","block");
227             $("div#user-tab-loading").css("display","none");
228         });
229                 $.post("/rest/myslice:authority/",{'fields':['authority_hrn','pi_users'],'filters':{'authority_hrn':'CONTAINS{{user_details.parent_authority}}'}}, function( data ) {
230            
231             var table_projects = [];
232             $.each( data, function( key, val ) {
233                 project_row = "<tr id='"+val.authority_hrn+"'>";
234                 project_row += "<td><input type='checkbox' class='project' id='"+val.authority_hrn+"'><div id='"+val.authority_hrn+"-loading' style='display:none;padding-left:3px;'><img src='{{ STATIC_URL }}img/loading.gif'></div></td>";
235
236                 // filter links to projects, only those that the user has credentials for
237                 p = myslice.projects;
238                 if(p.length > 0 && $.inArray(val.authority_hrn, p)!=-1){
239                     project_row += "<td><a href='/portal/project/"+val.authority_hrn+"'>"+val.authority_hrn+"</a></td>";
240                 }else{
241                     project_row += "<td>"+val.authority_hrn+"</td>";
242                 }
243                 project_row += "</tr>";
244                 table_projects.push(project_row);
245             });
246             $("table#project-tab tr:last").after(table_projects.join( "" ));
247             $("div#project-tab-loaded").css("display","block");
248             $("div#project-tab-loading").css("display","none");
249
250         });
251
252         {% endif %}
253     {% endif %}
254     {% endif %}
255
256 }); // End document.ready
257
258 $(document).ready(function() {
259         $('.nav-tabs a').click(function (e) {
260                 e.preventDefault();
261                 $(this).tab('show');
262         var id = $(this).attr('href').substr(1);
263         if ((id == 'requests')){
264                 $("#" + id).load('/management/' + id);
265         }
266         /*
267         if ((id == 'requests') || (id == 'about'))
268                 $("#" + id).load('/management/' + id);
269         */
270         });
271         var hash = window.location.hash;
272         if (hash) {
273                 $('.nav-tabs a[href='+hash+']').click();
274         } else {
275                 $('.nav-tabs a[href=#about]').click();
276         }
277
278     //upgrade users to PI
279     $('button#makepi').click(function() {
280         spin_all();
281         var flag = false;
282         var pi_users = [];
283         $.post("/rest/myslice:authority/",{'filters':{'authority_hrn': '{{user_details.parent_authority}}'}}, function( data ) {
284             pi_users = data[0]["pi_users"];
285             $('input:checkbox.user').each(function (index) {
286                 if(this.checked){
287                     var record_id = this.id;
288                     pi_users.push(record_id)
289                     $.post("/credentials/clear/",{'emails':[this.dataset['email']]}, function(data) {
290                     }); // post credentials clear
291                     flag = true;                
292                 }
293             });
294             if(flag == true){
295                 $.post("/update/myslice:authority/",{'filters':{'authority_hrn':'{{user_details.parent_authority}}'},'params':{'pi_users':pi_users}}, function(data) {
296                     if(data.success){
297                         mysliceAlert('Success: User rights added to: {{user_details.parent_authority}}','success', true);
298                     }else{
299                         mysliceAlert('Rest Error for: '+data.error,'warning', true);
300                         //alert("Rest Error for "+record_id+": "+data.error);
301                     }
302                     unspin_all();
303                 });
304             }
305         });
306     });
307 //downgrade to user [in projects:remove from project]
308     $('button#removepi').click(function() {
309         spin_all();
310         var flag = false;
311         var pi_users = [];
312         $.post("/rest/myslice:authority/",{'filters':{'authority_hrn': '{{user_details.parent_authority}}'}}, function( data ) {
313             pi_users = data[0]["pi_users"];
314                         // http://stackoverflow.com/questions/9792927/javascript-array-search-and-remove-string
315             $('input:checkbox.user').each(function (index) {
316                 if(this.checked){
317                     var record_id = this.id;
318                                         for (var i=pi_users.length-1; i>=0; i--) {
319                                         if (pi_users[i] === record_id) {
320                                                 pi_users.splice(i, 1);
321                             $.post("/credentials/clear/",{'emails':[this.dataset['email']]}, function(data) {
322                             }); // post credentials clear
323                             flag = true;                
324                                         }
325                                         }
326                 }
327             });
328             if(flag == true){
329                 $.post("/update/myslice:authority/",{'filters':{'authority_hrn':'{{user_details.parent_authority}}'},'params':{'pi_users':pi_users}}, function(data) {
330                     if(data.success){
331                         mysliceAlert('Success: User rights removed from: {{user_details.parent_authority}}','success', true);
332                         {% if project %}
333                         // remove the user from the list of pis of the project displayed
334                         $('input:checkbox.user').each(function (index) {
335                            if(this.checked){
336                                var record_id = this.id;
337                                $('tr[id="'+record_id+'"]').fadeOut("slow");
338                                $('tr[id="'+record_id+'"]').remove();
339                             }
340
341                         });
342                         {% endif %}
343                     }else{
344                         mysliceAlert('Rest Error for: '+data.error,'warning', true);
345                         //alert("Rest Error for "+record_id+": "+data.error);
346                     }
347                     unspin_all();
348                 });
349             }else{
350                 mysliceAlert('No action: User had no rights on: {{user_details.parent_authority}}','success', true);
351                 unspin_all();
352             }
353         });
354     });
355 });
356
357
358
359 </script>
360 {% endblock %}