kill server when port is already used
[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_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 myslice.viewutils           import topmenu_items, the_user
13
14 class SliceRequestView (LoginRequiredAutoLogoutView):
15
16     def authority_hrn_initial (self, request):
17         authorities_query = Query.get('authority').filter_by('authority_hrn', 'included', ['ple.inria', 'ple.upmc']).select('name', 'authority_hrn')
18 #        authorities_query = Query.get('authority').select('authority_hrn')
19         authorities = execute_query(request, authorities_query)
20         
21         authority_hrn_tuples = [ (authority['authority_hrn'], authority['name'],) for authority in authorities ]
22         return {'authority_hrn': authority_hrn_tuples}
23
24     # because we inherit LoginRequiredAutoLogoutView that is implemented by redefining 'dispatch'
25     # we cannot redefine dispatch here, or we'd lose LoginRequired and AutoLogout behaviours
26     def post (self, request):
27         
28         # The form has been submitted
29         form = SliceRequestForm(request.POST, initial=self.authority_hrn_initial(request)) 
30
31         if form.is_valid():
32             slice_name      = form.cleaned_data['slice_name']
33             authority_hrn   = form.cleaned_data['authority_hrn']
34             number_of_nodes = form.cleaned_data['number_of_nodes']
35             type_of_nodes   = form.cleaned_data['type_of_nodes']
36             purpose         = form.cleaned_data['purpose']
37             
38             s = PendingSlice(
39                 slice_name      = slice_name,
40                 authority_hrn   = authority_hrn,
41                 number_of_nodes = number_of_nodes,
42                 type_of_nodes   = type_of_nodes,
43                 purpose         = purpose
44             )
45             s.save()
46
47             # All validation rules pass; process data in form.cleaned_data
48             # slice_name, number_of_nodes, type_of_nodes, purpose
49             email = form.cleaned_data['email'] # email of the sender
50             cc_myself = form.cleaned_data['cc_myself']
51
52             # The recipients are the PI of the authority
53             recipients = authority_get_pi_emails(request,authority_hrn)
54             #recipients = ['yasin.upmc@gmail.com','jordan.auge@lip6.fr']
55             if cc_myself:
56                 recipients.append(email)
57             msg = render_to_string('slice-request-email.txt', form.cleaned_data)
58             send_mail("Onelab user %s requested a slice"%email , msg, email, recipients)
59
60             return render(request,'slice-request-ack-view.html') # Redirect after POST
61         else:
62             return self._display (request, form)
63
64     def get (self, request):
65         return self._display (request, SliceRequestForm(initial=self.authority_hrn_initial(request)))
66
67     def _display (self, request, form):
68         return render(request, 'slice-request-view.html', {
69                 'form': form,
70                 'topmenu_items': topmenu_items('Request a slice', request),
71                 'username': the_user (request) 
72                 })
73
74