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