1 from django.template.loader import render_to_string
2 from django.shortcuts import render
3 from django.core.mail import send_mail
5 from manifold.core.query import Query
6 from manifold.manifoldapi import execute_admin_query, execute_query
8 from portal.models import PendingSlice
9 from portal.actions import authority_get_pi_emails
10 from portal.forms import SliceRequestForm
11 from unfold.loginrequired import LoginRequiredAutoLogoutView
12 from ui.topmenu import topmenu_items, the_user
14 class SliceRequestView (LoginRequiredAutoLogoutView):
16 def authority_hrn_initial (self, request):
17 # Using cache manifold-tables to get the list of authorities
18 authorities_query = Query.get('authority').\
19 select('name', 'authority_hrn')
21 #onelab_enabled_query = Query.get('local:platform').filter_by('platform', '==', 'ple-onelab').filter_by('disabled', '==', 'False')
22 #onelab_enabled = not not execute_admin_query(request, onelab_enabled_query)
24 #authorities_query = Query.get('ple:authority').select('name', 'authority_hrn').filter_by('authority_hrn', 'included', ['ple.inria', 'ple.upmc', 'ple.ibbtple','ple.nitos'])
27 authorities = execute_admin_query(request, authorities_query)
28 authorities = sorted(authorities)
30 authority_hrn_tuples = [ (authority['authority_hrn'], authority['name'] if authority['name'] else authority['authority_hrn'],) for authority in authorities ]
31 print "authority_hrn_tuples=", authority_hrn_tuples
32 return {'authority_hrn': authority_hrn_tuples}
34 # because we inherit LoginRequiredAutoLogoutView that is implemented by redefining 'dispatch'
35 # we cannot redefine dispatch here, or we'd lose LoginRequired and AutoLogout behaviours
36 def post (self, request):
38 # The form has been submitted
39 form = SliceRequestForm(request.POST, initial=self.authority_hrn_initial(request))
42 slice_name = form.cleaned_data['slice_name']
43 authority_hrn = form.cleaned_data['authority_hrn']
44 number_of_nodes = form.cleaned_data['number_of_nodes']
45 type_of_nodes = form.cleaned_data['type_of_nodes']
46 purpose = form.cleaned_data['purpose']
49 slice_name = slice_name,
50 authority_hrn = authority_hrn,
51 number_of_nodes = number_of_nodes,
52 type_of_nodes = type_of_nodes,
57 # All validation rules pass; process data in form.cleaned_data
58 # slice_name, number_of_nodes, type_of_nodes, purpose
59 email = form.cleaned_data['email'] # email of the sender
60 cc_myself = form.cleaned_data['cc_myself']
62 # The recipients are the PI of the authority
63 recipients = authority_get_pi_emails(request, authority_hrn)
66 recipients.append(email)
67 msg = render_to_string('slice-request-email.txt', form.cleaned_data)
68 print "email, msg, email, recipients", email , msg, email, recipients
69 send_mail("Onelab user %s requested a slice"%email , msg, email, recipients)
71 return render(request,'slice-request-ack-view.html') # Redirect after POST
73 return self._display (request, form)
75 def get (self, request):
76 return self._display (request, SliceRequestForm(initial=self.authority_hrn_initial(request)))
78 def _display (self, request, form):
79 return render(request, 'slice-request-view.html', {
81 'topmenu_items': topmenu_items('Request a slice', request),
82 'username': the_user (request)