From 7114aa9ee7ecf6e245c58b55c73eac455a6be53b Mon Sep 17 00:00:00 2001 From: Loic Baron Date: Thu, 16 Jan 2014 17:36:37 +0100 Subject: [PATCH] Autocomplete on Slice Request and Registration --- portal/registrationview.py | 20 ++-- portal/slicerequestview.py | 141 +++++++++++++----------- portal/templates/registration_view.html | 6 +- portal/views.py | 2 +- 4 files changed, 93 insertions(+), 76 deletions(-) diff --git a/portal/registrationview.py b/portal/registrationview.py index 5c48726d..b3633bf0 100644 --- a/portal/registrationview.py +++ b/portal/registrationview.py @@ -102,7 +102,7 @@ class RegistrationView (FreeAccessView): # private_key = ''.join(private_key.split()) # public_key = "ssh-rsa " + public_key # Saving to DB - keypair = '{"user_public_key":'+ public_key + ', "user_private_key":'+ private_key + ', "user_hrn":"'+ user_hrn + '"}' + account_config = '{"user_public_key":'+ public_key + ', "user_private_key":'+ private_key + ', "user_hrn":"'+ user_hrn + '"}' auth_type = 'managed' #keypair = re.sub("\r", "", keypair) #keypair = re.sub("\n", "\\n", keypair) @@ -117,10 +117,10 @@ class RegistrationView (FreeAccessView): file_extension = os.path.splitext(file_name)[1] allowed_extension = ['.pub','.txt'] if file_extension in allowed_extension and re.search(r'ssh-rsa',file_content): - keypair = '{"user_public_key":"'+ file_content + '", "user_hrn":"'+ user_hrn +'"}' - keypair = re.sub("\r", "", keypair) - keypair = re.sub("\n", "\\n",keypair) - keypair = ''.join(keypair.split()) + account_config = '{"user_public_key":"'+ file_content + '", "user_hrn":"'+ user_hrn +'"}' + account_config = re.sub("\r", "", account_config) + account_config = re.sub("\n", "\\n",account_config) + account_config = ''.join(account_config.split()) auth_type = 'user' # for sending email public_key = file_content @@ -141,19 +141,19 @@ class RegistrationView (FreeAccessView): #login = reg_login, email = reg_email, password = request.POST['password'], - keypair = keypair, + keypair = account_config, ) b.save() # saves the user to django auth_user table [needed for password reset] user = User.objects.create_user(reg_email, reg_email, request.POST['password']) #creating user to manifold local:user - config = '{"firstname":"'+ reg_fname + '", "lastname":"'+ reg_lname + '", "authority":"'+ reg_auth + '"}' - user_params = {'email': reg_email, 'password': request.POST['password'], 'config': config} + user_config = '{"firstname":"'+ reg_fname + '", "lastname":"'+ reg_lname + '", "authority":"'+ reg_auth + '"}' + user_params = {'email': reg_email, 'password': request.POST['password'], 'config': user_config} manifold_add_user(request,user_params) #creating local:account in manifold user_id = user_detail['user_id']+1 # the user_id for the newly created user in local:user - user_params = {'platform_id': 5, 'user_id': user_id, 'auth_type': auth_type, 'config': keypair} - manifold_add_account(request,user_params) + account_params = {'platform_id': 5, 'user_id': user_id, 'auth_type': auth_type, 'config': account_config} + manifold_add_account(request,account_params) # Send email ctx = { diff --git a/portal/slicerequestview.py b/portal/slicerequestview.py index 0c4a5bed..ee947386 100644 --- a/portal/slicerequestview.py +++ b/portal/slicerequestview.py @@ -2,6 +2,8 @@ from django.template.loader import render_to_string from django.shortcuts import render from django.core.mail import send_mail +from unfold.page import Page + from manifold.core.query import Query from manifold.manifoldapi import execute_admin_query, execute_query @@ -9,77 +11,92 @@ from portal.models import PendingSlice from portal.actions import authority_get_pi_emails from portal.forms import SliceRequestForm from unfold.loginrequired import LoginRequiredAutoLogoutView -from ui.topmenu import topmenu_items, the_user +from ui.topmenu import topmenu_items_live, the_user class SliceRequestView (LoginRequiredAutoLogoutView): - - def authority_hrn_initial (self, request): - # Using cache manifold-tables to get the list of authorities - authorities_query = Query.get('authority').\ - select('name', 'authority_hrn') - - #onelab_enabled_query = Query.get('local:platform').filter_by('platform', '==', 'ple-onelab').filter_by('disabled', '==', 'False') - #onelab_enabled = not not execute_admin_query(request, onelab_enabled_query) - #if onelab_enabled: - #authorities_query = Query.get('ple:authority').select('name', 'authority_hrn').filter_by('authority_hrn', 'included', ['ple.inria', 'ple.upmc', 'ple.ibbtple','ple.nitos']) - # Now using Cache - - authorities = execute_admin_query(request, authorities_query) - authorities = sorted(authorities) - - authority_hrn_tuples = [ (authority['authority_hrn'], authority['name'] if authority['name'] else authority['authority_hrn'],) for authority in authorities ] - print "authority_hrn_tuples=", authority_hrn_tuples - return {'authority_hrn': authority_hrn_tuples} + def __init__ (self): + self.user_email = '' + self.errors = [] # because we inherit LoginRequiredAutoLogoutView that is implemented by redefining 'dispatch' # we cannot redefine dispatch here, or we'd lose LoginRequired and AutoLogout behaviours def post (self, request): - - # The form has been submitted - form = SliceRequestForm(request.POST, initial=self.authority_hrn_initial(request)) - - if form.is_valid(): - slice_name = form.cleaned_data['slice_name'] - authority_hrn = form.cleaned_data['authority_hrn'] - number_of_nodes = form.cleaned_data['number_of_nodes'] - type_of_nodes = form.cleaned_data['type_of_nodes'] - purpose = form.cleaned_data['purpose'] - - s = PendingSlice( - slice_name = slice_name, - authority_hrn = authority_hrn, - number_of_nodes = number_of_nodes, - type_of_nodes = type_of_nodes, - purpose = purpose - ) - s.save() + return self.get_or_post (request, 'POST') - # All validation rules pass; process data in form.cleaned_data - # slice_name, number_of_nodes, type_of_nodes, purpose - email = form.cleaned_data['email'] # email of the sender - cc_myself = form.cleaned_data['cc_myself'] - - # The recipients are the PI of the authority - recipients = authority_get_pi_emails(request, authority_hrn) - - if cc_myself: - recipients.append(email) - msg = render_to_string('slice-request-email.txt', form.cleaned_data) - print "email, msg, email, recipients", email , msg, email, recipients - send_mail("Onelab user %s requested a slice"%email , msg, email, recipients) + def get (self, request): + return self.get_or_post (request, 'GET') - return render(request,'slice-request-ack-view.html') # Redirect after POST - else: - return self._display (request, form) + def get_or_post (self, request, method): + # Using cache manifold-tables to get the list of authorities faster + authorities_query = Query.get('authority').select('name', 'authority_hrn') + authorities = execute_admin_query(request, authorities_query) + authorities = sorted(authorities) - def get (self, request): - return self._display (request, SliceRequestForm(initial=self.authority_hrn_initial(request))) + user_query = Query().get('local:user').select('email') + user_email = execute_query(self.request, user_query) + self.user_email = user_email[0].get('email') - def _display (self, request, form): - return render(request, 'slice-request-view.html', { - 'form': form, - 'topmenu_items': topmenu_items('Request a slice', request), - 'username': the_user (request) - }) + page = Page(request) + page.add_css_files ( [ "http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" ] ) + if method == 'POST': + self.errors = [] + + # The form has been submitted + slice_name = request.POST.get('slice_name', '') + authority_hrn = request.POST.get('authority_hrn', '') + number_of_nodes = request.POST.get('number_of_nodes', '') + purpose = request.POST.get('purpose', '') + email = self.user_email + cc_myself = True + + if (authority_hrn is None or authority_hrn == ''): + self.errors.append('Please, select an authority') + # What kind of slice name is valid? + if (slice_name is None or slice_name == ''): + self.errors.append('Slice Name is mandatory') + + if (purpose is None or purpose == ''): + self.errors.append('Purpose is mandatory') + + if not self.errors: + ctx = { + 'email': email, + 'slice_name': slice_name, + 'authority_hrn': authority_hrn, + 'number_of_nodes': number_of_nodes, + 'purpose': purpose, + } + s = PendingSlice( + slice_name = slice_name, + authority_hrn = authority_hrn, + number_of_nodes = number_of_nodes, + purpose = purpose + ) + s.save() + + # The recipients are the PI of the authority + recipients = authority_get_pi_emails(request, authority_hrn) + + if cc_myself: + recipients.append(email) + msg = render_to_string('slice-request-email.txt', ctx) + #print "email, msg, email, recipients", email , msg, email, recipients + send_mail("Onelab user %s requested a slice"%email , msg, email, recipients) + + return render(request,'slice-request-ack-view.html') # Redirect after POST + + template_env = { + 'topmenu_items': topmenu_items_live('Request a slice', page), + 'errors': self.errors, + 'slice_name': request.POST.get('slice_name', ''), + 'authority_hrn': request.POST.get('authority_hrn', ''), + 'number_of_nodes': request.POST.get('number_of_nodes', ''), + 'purpose': request.POST.get('purpose', ''), + 'email': self.user_email, + 'cc_myself': True, + 'authorities': authorities, + } + template_env.update(page.prelude_env ()) + return render(request, 'slicerequest_view.html',template_env) diff --git a/portal/templates/registration_view.html b/portal/templates/registration_view.html index 5ce34592..b14e2d28 100644 --- a/portal/templates/registration_view.html +++ b/portal/templates/registration_view.html @@ -34,10 +34,10 @@

Enter your last name

- +
- +

An authority responsible for vetting your account

@@ -114,7 +114,7 @@ jQuery(document).ready(function(){ {value:"",label:"No authority found !!!"} {% endif %} ]; - $( "#auth_list" ).autocomplete({ + $( "#authority_hrn" ).autocomplete({ source: availableTags, select: function( event, ui ) {console.log(jQuery(this))} }); diff --git a/portal/views.py b/portal/views.py index 3a719074..8194ed88 100644 --- a/portal/views.py +++ b/portal/views.py @@ -393,7 +393,7 @@ class ValidatePendingView(FreeAccessView): # more general variables expected in the template context['title'] = 'Test view that combines various plugins' # the menu items on the top - context['topmenu_items'] = topmenu_items('Validation', self.request) + context['topmenu_items'] = topmenu_items_live('Validation', page) # so we can sho who is logged context['username'] = the_user(self.request) -- 2.43.0