README: create admin account when django prompts [needed for passwrod reset wizard]
[myslice.git] / portal / slicerequestview.py
1 from django.template.loader      import render_to_string
2 from django.shortcuts            import render
3 from django.core.mail            import send_mail
4
5 from manifold.core.query         import Query
6 from manifold.manifoldapi        import execute_admin_query, execute_query
7
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
13
14 class SliceRequestView (LoginRequiredAutoLogoutView):
15
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')
20         
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)
23         #if onelab_enabled:
24         #authorities_query = Query.get('ple:authority').select('name', 'authority_hrn').filter_by('authority_hrn', 'included', ['ple.inria', 'ple.upmc', 'ple.ibbtple','ple.nitos'])
25         # Now using Cache
26
27         authorities = execute_admin_query(request, authorities_query)
28         authorities = sorted(authorities)
29         
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}
33
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):
37         
38         # The form has been submitted
39         form = SliceRequestForm(request.POST, initial=self.authority_hrn_initial(request)) 
40
41         if form.is_valid():
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']
47             
48             s = PendingSlice(
49                 slice_name      = slice_name,
50                 authority_hrn   = authority_hrn,
51                 number_of_nodes = number_of_nodes,
52                 type_of_nodes   = type_of_nodes,
53                 purpose         = purpose
54             )
55             s.save()
56
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']
61
62             # The recipients are the PI of the authority
63             recipients = authority_get_pi_emails(request, authority_hrn)
64
65             if cc_myself:
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)
70
71             return render(request,'slice-request-ack-view.html') # Redirect after POST
72         else:
73             return self._display (request, form)
74
75     def get (self, request):
76         return self._display (request, SliceRequestForm(initial=self.authority_hrn_initial(request)))
77
78     def _display (self, request, form):
79         return render(request, 'slice-request-view.html', {
80                 'form': form,
81                 'topmenu_items': topmenu_items('Request a slice', request),
82                 'username': the_user (request) 
83                 })
84
85