9eaaea70cc3b22e3b06a8f101fa827ac0b9c4cf0
[unfold.git] / portal / slicetabexperiment.py
1 from __future__ import print_function
2
3 # this somehow is not used anymore - should it not be ?
4 from django.core.context_processors import csrf
5 from django.http import HttpResponseRedirect
6 from django.contrib.auth import authenticate, login, logout
7 from django.template import RequestContext
8 from django.shortcuts import render_to_response
9 from django.shortcuts import render
10
11 from unfold.loginrequired import FreeAccessView
12
13 from manifold.core.query        import Query
14 from manifoldapi.manifoldapi    import execute_query
15 from manifoldapi.manifoldresult import ManifoldResult
16 from ui.topmenu import topmenu_items, the_user
17 from myslice.configengine import ConfigEngine
18
19 from myslice.theme import ThemeView
20 from myslice.configengine import ConfigEngine
21
22 from sfa.planetlab.plxrn import hash_loginbase
23
24 import urllib2,json
25
26 class ExperimentView (FreeAccessView, ThemeView):
27     template_name = 'slice-tab-experiment.html'
28
29     def get (self, request, slicename, state=None):
30   
31         username = self.request.user    
32         
33         query_current_resources = Query.get('slice').select('resource','parent_authority').filter_by('slice_hrn','==',slicename)
34         current_resources = execute_query(request, query_current_resources)
35
36         parent_authority = current_resources[0]['parent_authority']
37         
38         split_slicename = slicename.split('.')
39         ple_slicename = hash_loginbase(parent_authority) + '_' + split_slicename[-1]
40
41         ple_resource_list=[]
42         nitos_resource_list=[]
43         nitos_paris_resource_list=[]
44         iotlab_resource_list=[]
45         try:
46             for resources in current_resources:
47                 list_res = resources['resource']
48                 #print "list_b4"
49                 #print list_res
50                 for res in list_res:
51                     split_list = res.split('+') # split the resource urn
52                     #print "list_after"
53                     #print split_list
54                     if [s for s in split_list if 'ple' in s]: # find ple resources
55                         res_hrn = split_list[-1] # last element is resource hrn
56                         ple_resource_list.append(res_hrn)
57                     if [s for s in split_list if 'omf:paris.fit-nitos.fr' in s]: # find nitos_paris resources
58                         res_hrn = split_list[-1] # last element is resource hrn
59                         nitos_paris_resource_list.append(res_hrn)
60                     if [s for s in split_list if 'iotlab' in s]: # find iotlab resources
61                         res_hrn = split_list[-1] # last element is resource hrn
62                         iotlab_resource_list.append(res_hrn)
63                     if [s for s in split_list if 'omf:nitos.indoor' in s]: # find nitos_indoor resources
64                         res_hrn = split_list[-1] # last element is resource hrn
65                         nitos_resource_list.append(res_hrn)
66                     if [s for s in split_list if 'omf:nitos.outdoor' in s]: # find nitos_outdoor resources
67                         res_hrn = split_list[-1] # last element is resource hrn
68                         nitos_resource_list.append(res_hrn)
69
70
71         except Exception,e:
72             print("Exception in slicetabexperiment.py in OneLab resource search %s" % e)
73         
74         #print "list of ple res hrns"
75         #print ple_resource_list
76         #print "list of nit_paris res hrns"
77         #print nitos_paris_resource_list
78         #print "list of iotLab res hrns"
79         #print iotlab_resource_list
80         #print "list of nitos res hrns"
81         #print nitos_resource_list
82
83         all_users = list() 
84         #get all  iotlab users
85         all_users = list() 
86         try:
87             engine = ConfigEngine()
88             userData = "Basic " + (engine.iotlab_admin_user() + ":" + engine.iotlab_admin_password()).encode("base64").rstrip()
89             req = urllib2.Request(engine.iotlab_url())
90             req.add_header('Accept', 'application/json')
91             req.add_header("Content-type", "application/x-www-form-urlencoded")
92             req.add_header('Authorization', userData)
93             # make the request and print the results
94             res = urllib2.urlopen(req)
95             all_users = json.load(res) 
96         except urllib2.URLError as e:
97             print("There is a problem in getting iotlab users %s" % e.reason)
98        
99
100         #getting the login from email
101         #initial value  no-account == contact_admin
102         iot_login = 'contact_admin'
103         username = str(username)
104         for user in all_users:
105             if user['email'] == username:
106                 iot_login = user['login']
107             
108         return render_to_response(self.template, { 'theme' : self.theme,'slicename':slicename, 'ple_slicename':ple_slicename, 'username':username, 'ple_resources':ple_resource_list, 'nitos_resources': nitos_resource_list, 'nitos_paris_resources':nitos_paris_resource_list, 'iotlab_resources':iotlab_resource_list, 'iot_login':iot_login }, context_instance=RequestContext(request))
109