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