Production version March 2015
[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 import urllib2,json
21
22 class ExperimentView (FreeAccessView, ThemeView):
23     template_name = 'slice-tab-experiment.html'
24
25     def get (self, request, slicename, state=None):
26   
27         username = self.request.user    
28         
29         split_slicename = slicename.split('.')
30         ple_slicename = split_slicename[0] + '8' + split_slicename[1] + '_' + split_slicename[2]
31
32         query_current_resources = Query.get('slice').select('resource').filter_by('slice_hrn','==',slicename)
33         current_resources = execute_query(request, query_current_resources)
34
35         ple_resource_list=[]
36         nitos_resource_list=[]
37         nitos_paris_resource_list=[]
38         iotlab_resource_list=[]
39         try:
40             for resources in current_resources:
41                 list_res = resources['resource']
42                 #print "list_b4"
43                 #print list_res
44                 for res in list_res:
45                     split_list = res.split('+') # split the resource urn
46                     #print "list_after"
47                     #print split_list
48                     if [s for s in split_list if 'ple' in s]: # find ple resources
49                         res_hrn = split_list[-1] # last element is resource hrn
50                         ple_resource_list.append(res_hrn)
51                     if [s for s in split_list if 'omf:paris.fit-nitos.fr' in s]: # find nitos_paris resources
52                         res_hrn = split_list[-1] # last element is resource hrn
53                         nitos_paris_resource_list.append(res_hrn)
54                     if [s for s in split_list if 'iotlab' in s]: # find iotlab resources
55                         res_hrn = split_list[-1] # last element is resource hrn
56                         iotlab_resource_list.append(res_hrn)
57                     if [s for s in split_list if 'omf:nitos.indoor' in s]: # find nitos_indoor resources
58                         res_hrn = split_list[-1] # last element is resource hrn
59                         nitos_resource_list.append(res_hrn)
60                     if [s for s in split_list if 'omf:nitos.outdoor' in s]: # find nitos_outdoor resources
61                         res_hrn = split_list[-1] # last element is resource hrn
62                         nitos_resource_list.append(res_hrn)
63
64
65         except Exception,e:
66             print "Exception in slicetabexperiment.py in OneLab resource search %s" % e
67         
68         #print "list of ple res hrns"
69         #print ple_resource_list
70         #print "list of nit_paris res hrns"
71         #print nitos_paris_resource_list
72         #print "list of iotLab res hrns"
73         #print iotlab_resource_list
74         #print "list of nitos res hrns"
75         #print nitos_resource_list
76
77         all_users = list() 
78         #get all  iotlab users
79         try:
80             engine = ConfigEngine()
81             userData = "Basic " + (engine.iotlab_admin_user() + ":" + engine.iotlab_admin_password()).encode("base64").rstrip()
82             req = urllib2.Request(engine.iotlab_url())
83             req.add_header('Accept', 'application/json')
84             req.add_header("Content-type", "application/x-www-form-urlencoded")
85             req.add_header('Authorization', userData)
86             # make the request and print the results
87             res = urllib2.urlopen(req)
88             all_users = json.load(res) 
89         except urllib2.URLError as e:
90             print "There is a problem in getting iotlab users %s" % e.reason
91        
92
93         #getting the login from email
94         #initial value  no-account == contact_admin
95         iot_login = 'contact_admin'
96         username = str(username)
97         for user in all_users:
98             if user['email'] == username:
99                 iot_login = user['login']
100             
101         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))
102