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