From 34c917acc266edcf8037c238383ad9f75d31e306 Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 25 Jun 2019 03:10:50 +0200 Subject: [PATCH] Add SSH form to hosting VM buy flow also --- hosting/templates/hosting/order_detail.html | 29 ++++++++++++++++++ hosting/views.py | 33 +++++++++++++++++++-- 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/hosting/templates/hosting/order_detail.html b/hosting/templates/hosting/order_detail.html index 4a62e9fa..7ab79378 100644 --- a/hosting/templates/hosting/order_detail.html +++ b/hosting/templates/hosting/order_detail.html @@ -198,6 +198,35 @@ {% block submit_btn %}
{% csrf_token %} + {% comment %} + We are in VM buy flow and we want user to click the "Place order" button. + At this point, we also want the user to input the SSH key for the VM. + {% endcomment %} + + {% if messages %} +
+ {% for message in messages %} + {{ message }} + {% endfor %} +
+ {% endif %} +
+

 {% trans "Add your public SSH key" %}

+
+
+ {% if keys|length > 0 %} +
Existing keys
+ {% endif %} + {% for key in keys %} + +
+ {% endfor %} +
+ {% for field in form %} + {% bootstrap_field field %} + {% endfor %}
{% blocktrans with vm_price=vm.total_price|floatformat:2|intcomma %}By clicking "Place order" this plan will charge your credit card account with {{ vm_price }} CHF/month{% endblocktrans %}.
diff --git a/hosting/views.py b/hosting/views.py index 2ef5383d..33a8748e 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -838,13 +838,19 @@ class PaymentVMView(LoginRequiredMixin, FormView): return self.form_invalid(form) -class OrdersHostingDetailView(LoginRequiredMixin, DetailView): +class OrdersHostingDetailView(LoginRequiredMixin, DetailView, FormView): + form_class = UserHostingKeyForm template_name = "hosting/order_detail.html" context_object_name = "order" login_url = reverse_lazy('hosting:login') permission_required = ['view_hostingorder'] model = HostingOrder + def get_form_kwargs(self): + kwargs = super(OrdersHostingDetailView, self).get_form_kwargs() + kwargs.update({'request': self.request}) + return kwargs + def get_object(self, queryset=None): order_id = self.kwargs.get('pk') try: @@ -869,6 +875,8 @@ class OrdersHostingDetailView(LoginRequiredMixin, DetailView): if self.request.GET.get('page') == 'payment': context['page_header_text'] = _('Confirm Order') + context['form'] = UserHostingKeyForm(request=self.request) + context['keys'] = get_all_public_keys(self.request.user) else: context['page_header_text'] = _('Invoice') if not self.request.user.has_perm( @@ -994,6 +1002,27 @@ class OrdersHostingDetailView(LoginRequiredMixin, DetailView): @method_decorator(decorators) def post(self, request): + # Check ssh public key and then proceed + form = self.get_form() + required = 'add_ssh' in self.request.POST + + # SSH key is required only if the user doesn't have an existing + # key + if len(get_all_public_keys(self.request.user)) == 0: + form.fields['name'].required = required + form.fields['public_key'].required = required + if not form.is_valid(): + response = { + 'status': False, + 'msg_title': str(_('SSH key related error occurred')), + 'msg_body': "
".join([str(v) for k,v in form.errors.items()]), + } + return JsonResponse(response) + + # We have a valid SSH key from the user, save it in opennebula and db + # and proceed further + form.save() + template = request.session.get('template') specs = request.session.get('specs') stripe_utils = StripeUtils() @@ -1641,7 +1670,7 @@ class VirtualMachineView(LoginRequiredMixin, View): "manager.delete_vm returned False. Hence, error making " "xml-rpc call to delete vm failed." ) - response['text'] = str(_('Error terminating VM ')) + str(vm.id) + response['text'] = str(_('Error terminating VM')) + str(vm.id) else: for t in range(15): try: