Add SSH form to hosting VM buy flow also

This commit is contained in:
PCoder 2019-06-25 03:10:50 +02:00
parent 85f7d73442
commit 34c917acc2
2 changed files with 60 additions and 2 deletions

View file

@ -198,6 +198,35 @@
{% block submit_btn %} {% block submit_btn %}
<form method="post" id="virtual_machine_create_form"> <form method="post" id="virtual_machine_create_form">
{% csrf_token %} {% 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 %}
<div class="alert alert-warning">
{% for message in messages %}
<span>{{ message }}</span>
{% endfor %}
</div>
{% endif %}
<div class="dashboard-container-head">
<h2 class="dashboard-title-thin"><i class="fa fa-key" aria-hidden="true"></i>&nbsp;{% trans "Add your public SSH key" %}</h2>
</div>
<div class="existing-keys">
{% if keys|length > 0 %}
<div class="existing-keys-title">Existing keys</div>
{% endif %}
{% for key in keys %}
<textarea class="form-control input-no-border" style="width: 100%" readonly rows="6">
{{key}}
</textarea>
<br/>
{% endfor %}
</div>
{% for field in form %}
{% bootstrap_field field %}
{% endfor %}
<div class="row"> <div class="row">
<div class="col-sm-8"> <div class="col-sm-8">
<div class="dcl-place-order-text">{% 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 %}.</div> <div class="dcl-place-order-text">{% 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 %}.</div>

View file

@ -838,13 +838,19 @@ class PaymentVMView(LoginRequiredMixin, FormView):
return self.form_invalid(form) return self.form_invalid(form)
class OrdersHostingDetailView(LoginRequiredMixin, DetailView): class OrdersHostingDetailView(LoginRequiredMixin, DetailView, FormView):
form_class = UserHostingKeyForm
template_name = "hosting/order_detail.html" template_name = "hosting/order_detail.html"
context_object_name = "order" context_object_name = "order"
login_url = reverse_lazy('hosting:login') login_url = reverse_lazy('hosting:login')
permission_required = ['view_hostingorder'] permission_required = ['view_hostingorder']
model = 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): def get_object(self, queryset=None):
order_id = self.kwargs.get('pk') order_id = self.kwargs.get('pk')
try: try:
@ -869,6 +875,8 @@ class OrdersHostingDetailView(LoginRequiredMixin, DetailView):
if self.request.GET.get('page') == 'payment': if self.request.GET.get('page') == 'payment':
context['page_header_text'] = _('Confirm Order') context['page_header_text'] = _('Confirm Order')
context['form'] = UserHostingKeyForm(request=self.request)
context['keys'] = get_all_public_keys(self.request.user)
else: else:
context['page_header_text'] = _('Invoice') context['page_header_text'] = _('Invoice')
if not self.request.user.has_perm( if not self.request.user.has_perm(
@ -994,6 +1002,27 @@ class OrdersHostingDetailView(LoginRequiredMixin, DetailView):
@method_decorator(decorators) @method_decorator(decorators)
def post(self, request): 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': "<br/>".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') template = request.session.get('template')
specs = request.session.get('specs') specs = request.session.get('specs')
stripe_utils = StripeUtils() stripe_utils = StripeUtils()