myslice.ini possible themes = onelab, fed4fire, fibre, smartfire
[myslice.git] / portal / views.py
1 # -*- coding: utf-8 -*-
2 #
3 # portal/views.py: views for the portal application
4 # This file is part of the Manifold project.
5 #
6 # Authors:
7 #   Jordan AugĂ© <jordan.auge@lip6.fr>
8 #   Mohammed Yasin Rahman <mohammed-yasin.rahman@lip6.fr>
9 #   Loic Baron <loic.baron@lip6.fr>
10 # Copyright 2013, UPMC Sorbonne UniversitĂ©s / LIP6
11 #
12 # This program is free software; you can redistribute it and/or modify it under
13 # the terms of the GNU General Public License as published by the Free Software
14 # Foundation; either version 3, or (at your option) any later version.
15
16 # This program is distributed in the hope that it will be useful, but WITHOUT
17 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
19 # details.
20
21 # You should have received a copy of the GNU General Public License along with
22 # this program; see the file COPYING.  If not, write to the Free Software
23 # Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
25 import json
26
27 from django.http                import HttpResponseRedirect, HttpResponse
28 from django.shortcuts           import render
29 from django.template.loader     import render_to_string
30
31 from unfold.loginrequired       import FreeAccessView
32 from ui.topmenu                 import topmenu_items_live, the_user
33
34 from portal.event               import Event
35 # presview is put in observation for now
36 #from plugins.pres_view          import PresView
37 from plugins.raw                import Raw
38
39 # these seem totally unused for now
40 #from portal.util                import RegistrationView, ActivationView
41
42 from portal.models              import PendingUser, PendingSlice
43 from portal.actions             import get_requests
44 from manifold.manifoldapi       import execute_query
45 from manifold.core.query        import Query
46 from unfold.page                import Page
47
48 # NOTE
49 # initially all the portal views were defined in this single file
50 # all the other ones have now migrated into separate classes/files for more convenience
51 # I'm leaving these ones here for now as I could not exactly figure what the purpose was 
52 # (i.e. what the correct name should be, as presviewview was a bit cryptic)
53 class PresViewView(FreeAccessView):
54     template_name = "view-unfold1.html"
55
56     def get_context_data(self, **kwargs):
57
58         page = Page(self.request)
59
60 #        pres_view = PresView(page = page)
61         pres_view = Raw(page = page,html="<h1>PresView needs to be integrated</h1>")
62
63         context = super(PresViewView, self).get_context_data(**kwargs)
64
65         #context['ALL_STATIC'] = "all_static"
66         context['unfold_main'] = pres_view.render(self.request)
67
68         # XXX This is repeated in all pages
69         # more general variables expected in the template
70         context['title'] = 'Test view that combines various plugins'
71         # the menu items on the top
72         context['topmenu_items'] = topmenu_items_live('PresView', page)
73         # so we can sho who is logged
74         context['username'] = the_user(self.request)
75
76         prelude_env = page.prelude_env()
77         context.update(prelude_env)
78
79         return context
80
81 def json_me(config_file,type):
82     json_answer = ''
83     for ligne in config_file:
84         if not ligne.startswith('#'):
85             args = ligne.split(';')
86             json_answer += str('{ "name": "' + args[0] + '" ,"id":"' + args[1]  + '" ,"descriptif":"' + args[2]+'"')
87             if type!="dynamic":
88                 json_answer += str(',"contraints":')
89                 if args[3]=="":
90                     json_answer += str('""')
91                 else:
92                     json_answer += str(args[3])
93             json_answer += str('},')
94     return json_answer[:-1]
95
96
97 DIR = '/var/myslice/'
98 STATIC = '%s/config_method_static' % DIR
99 DYNAMIC = '%s/config_method_dynamic' % DIR
100 ANIMATION = '%s/config_method_animation' % DIR
101
102 def pres_view_methods(request, type):
103
104     if type ==None:
105         return 0
106     elif type =="static":
107         config = open(STATIC, "r")
108         json_answer = str('{ "options": [')
109         json_answer += str(json_me(config,"static"))
110         json_answer += str('] }')
111         config.close()
112     elif type =="dynamic":
113         config = open(DYNAMIC, "r")
114         json_answer = str('{ "options": [')
115         json_answer += str(json_me(config,"dynamic"))
116         json_answer += str('] }')
117         config.close()
118     elif type =="animation":
119         config = open(ANIMATION, "r")
120         json_answer = str('{ "options": [')
121         json_answer += str(json_me(config,"animation"))
122         json_answer += str('] }')
123         config.close()
124     elif type =="all":
125         config = open(STATIC, "r")
126         json_answer = str('{ "static": [')
127         json_answer += str(json_me(config,"static"))
128         json_answer += str('],')
129         json_answer += str('"dynamic": [')
130         config.close()
131         config = open(DYNAMIC, "r")
132         json_answer += str(json_me(config,"dynamic"))
133         json_answer += str('],')
134         json_answer += str('"animation": [')
135         config.close()
136         config = open(ANIMATION, "r")
137         json_answer += str(json_me(config,"animation"))
138         json_answer += str('] }')
139         config.close()
140     else:
141         return 0
142     return HttpResponse (json_answer, mimetype="application/json")
143
144 def pres_view_animation(request, constraints, id):
145
146 # sites crees depuis 2008
147 # static.py?contraints=']date_created':1262325600&id='name_id"'
148
149     # method = request.getvalue('method') #ex : GetSites
150     #constraints = "']date_created':1262325600"
151     #id = "2"
152
153     if id == None:
154         return 0
155
156     # method = 'GetSites'#request.getvalue('method') #ex : GetSites
157     # constraints = {}#request.getvalue('constraints') // nul = {}
158     # response_field = "'site_id','name','date_created'"#request.getvalue('response_field')
159
160     config_file = open(ANIMATION, "r")
161     for ligne in config_file:
162         if not ligne.startswith('#'):
163             ligne = ligne.split('\n')
164             first = ligne[0].split(';')
165             if (str(first[1]) == str(id)):
166                 save = first
167     config_file.close()
168
169     #Les print_method, print_option sont definis par le client (js)
170     #Les animations acceptent que les connexions anonymous
171     # args = "postmsg;animation;;;anonymous;https://www.planet-lab.eu/PLCAPI/;"
172     args = ";;"+str(save[8])+";"+str(save[9])+";anonymous;"+str(save[5])+";"+str(save[6])+";{"+str(constraints)+"};"+str(save[7])+";"
173
174
175     #Creation d'un objet event
176     event = Event(args)
177     cmd = [{"params": {
178             "data": {
179                 "print_options": event.print_options,
180                 "print_method": event.print_method,
181                 "message": event.data
182             }
183         }
184     }]
185
186     json_answer = json.dumps(cmd)
187     return HttpResponse (json_answer, mimetype="application/json")
188
189 def pres_view_static(request, constraints, id):
190     #constraints = "']date_created':1262325600"
191     #id = "2"
192
193     # method = 'GetSites'#request.getvalue('method') #ex : GetSites
194     # constraints = {}#request.getvalue('constraints') // nul = {}
195     # response_field = "'site_id','name','date_created'"#request.getvalue('response_field')
196
197     config_file = open(STATIC, "r")
198     for ligne in config_file:
199         if not ligne.startswith('#'):
200             ligne = ligne.split('\n')
201             first = ligne[0].split(';')
202             if (str(first[1]) == str(id)):
203                 save = first
204     config_file.close()
205
206     #Les print_method, print_option sont definis par le client (js)
207     #Les animations acceptent que les connexions anonymous
208     # args = "postmsg;animation;;;anonymous;https://www.planet-lab.eu/PLCAPI/;"
209     args = ";;"+str(save[8])+";"+str(save[9])+";anonymous;"+str(save[5])+";"+str(save[6])+";{"+str(constraints)+"};"+str(save[7])+";"
210
211
212     #Creation d'un objet event
213     event = Event(args)
214     cmd = [{"params": {
215             "data": {
216                 "print_options": event.print_options,
217                 "print_method": event.print_method,
218                 "message": event.data
219             }
220         }
221     }]
222
223     json_answer = json.dumps(cmd)
224     return HttpResponse (json_answer, mimetype="application/json")