From 1f10f04a9d971ed1db7ad6183659ef777c382251 Mon Sep 17 00:00:00 2001 From: Levi Date: Thu, 11 May 2017 00:11:33 -0500 Subject: [PATCH] Fixed ssh key error, Now an existing user is not ask for his credit card again --- hosting/models.py | 22 ++++- hosting/opennebula_functions.py | 8 ++ hosting/static/hosting/js/payment.js | 21 +++++ hosting/templates/hosting/payment.html | 22 ++++- .../hosting/virtual_machine_key.html | 3 +- hosting/views.py | 94 ++++++++++++++++--- utils/forms.py | 2 +- 7 files changed, 155 insertions(+), 17 deletions(-) diff --git a/hosting/models.py b/hosting/models.py index b59b7f5a..9fa4a100 100644 --- a/hosting/models.py +++ b/hosting/models.py @@ -91,6 +91,18 @@ class VirtualMachineType(models.Model): } + @classmethod + def get_vm_templates(self, user): + opennebula_client = OpenNebulaManager( + email=user.email, + password=user.password, + ) + + templates = opennebula_client.get_vm_templates() + for template in templates: + print(OpenNebulaManager.parse_vm(template)) + return templates + class VirtualMachinePlan(AssignPermissionsMixin, models.Model): @@ -196,7 +208,7 @@ class VirtualMachinePlan(AssignPermissionsMixin, models.Model): # Get opennebula client opennebula_client = OpenNebulaManager( email=user.email, - password=user.password[:20], + password=user.password, ) # Get vm given the id @@ -216,7 +228,7 @@ class VirtualMachinePlan(AssignPermissionsMixin, models.Model): # Get opennebula client opennebula_client = OpenNebulaManager( email=user.email, - password=user.password[:20], + password=user.password, ) # Get vm pool @@ -279,6 +291,12 @@ class HostingOrder(AssignPermissionsMixin, models.Model): self.cc_brand = stripe_charge.source.brand self.save() + def get_cc_data(self): + return { + 'last4': self.last4, + 'cc_brand': self.cc_brand, + } if self.last4 and self.cc_brand else None + class UserHostingKey(models.Model): user = models.ForeignKey(CustomUser) diff --git a/hosting/opennebula_functions.py b/hosting/opennebula_functions.py index a00bb037..d9421d48 100644 --- a/hosting/opennebula_functions.py +++ b/hosting/opennebula_functions.py @@ -171,6 +171,14 @@ class OpenNebulaManager: return vm_id + def get_vm_templates(self): + template_pool = oca.VmTemplatePool(self.oneadmin_client) + template_pool.info() + a = template_pool[0] + import pdb + pdb.set_trace() + return template_pool + def get_vm(self, email, vm_id): # Get vm's vms = self.get_vms(email) diff --git a/hosting/static/hosting/js/payment.js b/hosting/static/hosting/js/payment.js index c75e3d48..d06a5a3d 100644 --- a/hosting/static/hosting/js/payment.js +++ b/hosting/static/hosting/js/payment.js @@ -25,6 +25,27 @@ $( document ).ready(function() { }); + var hasCreditcard = window.hasCreditcard || false; + console.log("has creditcard", hasCreditcard); + // hasCreditcard= true; + + var submit_form_btn = $('#payment_button_with_creditcard'); + submit_form_btn.on('click', submit_payment); + + + function submit_payment(e){ + e.preventDefault(); + console.log("creditcard sdasd"); + // if (hasCreditcard) { + $('#billing-form').submit(); + console.log("has creditcard2"); + // } + + // $form.submit(); + } + + + var $form = $('#payment-form'); $form.submit(payWithStripe); diff --git a/hosting/templates/hosting/payment.html b/hosting/templates/hosting/payment.html index b0a09812..90da3870 100644 --- a/hosting/templates/hosting/payment.html +++ b/hosting/templates/hosting/payment.html @@ -8,7 +8,7 @@

Billing Address


-
+ {% for field in form %} {% csrf_token %} {% bootstrap_field field show_label=False type='fields'%} @@ -23,6 +23,17 @@
+ {% if credit_card_data.last4 %} + +
Credit Card
+
Last 4: *****{{credit_card_data.last4}}
+
Type: {{credit_card_data.cc_brand}}
+ + + + {% else %} + +
@@ -76,6 +87,7 @@ + {% endif %}
@@ -110,6 +122,7 @@
+ {% if stripe_key %} {%endif%} +{% if credit_card_data.last4 and credit_card_data.cc_brand %} + + +{%endif%} + {%endblock%} diff --git a/hosting/templates/hosting/virtual_machine_key.html b/hosting/templates/hosting/virtual_machine_key.html index 8a0221ae..0062e90b 100644 --- a/hosting/templates/hosting/virtual_machine_key.html +++ b/hosting/templates/hosting/virtual_machine_key.html @@ -58,7 +58,7 @@ {% if private_key %}
- {% trans "Warning!"%}{% trans "You can view your SSH private key once. Copy it or if it wasn't downloaded automatically, just click on Download to start it."%} + {% trans "Warning!"%}{% trans "You can view your SSH private key once. Don't lost your key"%}
@@ -101,6 +101,7 @@ // Remove anchor from body document.body.removeChild(a); + {%endif%} diff --git a/hosting/views.py b/hosting/views.py index cf1cf879..f5be0fb4 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -22,7 +22,7 @@ from stored_messages.api import mark_read from membership.models import CustomUser, StripeCustomer from utils.stripe_utils import StripeUtils -from utils.forms import BillingAddressForm, PasswordResetRequestForm +from utils.forms import BillingAddressForm, PasswordResetRequestForm, UserBillingAddressForm from utils.views import PasswordResetViewMixin, PasswordResetConfirmViewMixin, LoginViewMixin from utils.mailer import BaseEmail from .models import VirtualMachineType, VirtualMachinePlan, HostingOrder, HostingBill, UserHostingKey @@ -160,7 +160,7 @@ class SignupView(CreateView): model = CustomUser def get_success_url(self): - next_url = self.request.session.get('next', reverse_lazy('hosting:signup')) + next_url = self.request.session.get('next', reverse_lazy('hosting:virtual_machines')) return next_url def form_valid(self, form): @@ -226,18 +226,19 @@ class GenerateVMSSHKeysView(LoginRequiredMixin, FormView): context_object_name = "virtual_machine" def get_context_data(self, **kwargs): - try: - user_key = UserHostingKey.objects.get( - user=self.request.user - ) - except UserHostingKey.DoesNotExist: - user_key = None - context = super( GenerateVMSSHKeysView, self ).get_context_data(**kwargs) + try: + user_key = UserHostingKey.objects.get( + user=self.request.user + ) + + except UserHostingKey.DoesNotExist: + user_key = None + context.update({ 'user_key': user_key }) @@ -256,21 +257,75 @@ class GenerateVMSSHKeysView(LoginRequiredMixin, FormView): if form.cleaned_data.get('private_key'): context.update({ 'private_key': form.cleaned_data.get('private_key'), - 'key_name': form.cleaned_data.get('name') + 'key_name': form.cleaned_data.get('name'), + 'form': UserHostingKeyForm(request=self.request) }) - # print("form", form.cleaned_data) + del(context['form']) + context.update({ + 'form': form + }) + form = UserHostingKeyForm(request=self.request) + print("context", context) + + # return HttpResponseRedirect(reverse('hosting:key_pair')) return render(self.request, self.template_name, context) + def post(self, request, *args, **kwargs): + + try: + UserHostingKey.objects.get( + user=self.request.user + ) + return HttpResponseRedirect(reverse('hosting:key_pair')) + + except UserHostingKey.DoesNotExist: + pass + + form = self.get_form() + if form.is_valid(): + return self.form_valid(form) + else: + return self.form_invalid(form) + class PaymentVMView(LoginRequiredMixin, FormView): template_name = 'hosting/payment.html' login_url = reverse_lazy('hosting:login') form_class = BillingAddressForm + def get_form_kwargs(self): + current_billing_address = self.request.user.billing_addresses.first() + form_kwargs = super(PaymentVMView, self).get_form_kwargs() + if not current_billing_address: + return form_kwargs + + form_kwargs.update({ + 'initial': { + 'street_address': current_billing_address.street_address, + 'city': current_billing_address.city, + 'postal_code': current_billing_address.postal_code, + 'country': current_billing_address.country, + } + }) + return form_kwargs + def get_context_data(self, **kwargs): context = super(PaymentVMView, self).get_context_data(**kwargs) + # Get user + user = self.request.user + + # Get user last order + last_hosting_order = HostingOrder.objects.filter(customer__user=user).last() + + # If user has already an hosting order, get the credit card data from it + if last_hosting_order: + credit_card_data = last_hosting_order.get_cc_data() + context.update({ + 'credit_card_data': credit_card_data if credit_card_data else None, + }) + context.update({ 'stripe_key': settings.STRIPE_API_PUBLIC_KEY }) @@ -281,7 +336,12 @@ class PaymentVMView(LoginRequiredMixin, FormView): form = self.get_form() if form.is_valid(): + + # Get billing address data + billing_address_data = form.cleaned_data + context = self.get_context_data() + specifications = request.session.get('vm_specs') vm_template = specifications.get('vm_template', 1) @@ -318,6 +378,15 @@ class PaymentVMView(LoginRequiredMixin, FormView): # Create Billing Address billing_address = form.save() + # Create Billing Address for User if he does not have one + if not customer.user.billing_addresses.count(): + billing_address_data.update({ + 'user': customer.user.id + }) + billing_address_user_form = UserBillingAddressForm(billing_address_data) + billing_address_user_form.is_valid() + billing_address_user_form.save() + # Create a Hosting Order order = HostingOrder.create(vm_plan=plan, customer=customer, billing_address=billing_address) @@ -384,6 +453,7 @@ class OrdersHostingDetailView(PermissionRequiredMixin, LoginRequiredMixin, Detai permission_required = ['view_hostingorder'] model = HostingOrder + class OrdersHostingListView(LoginRequiredMixin, ListView): template_name = "hosting/orders.html" login_url = reverse_lazy('hosting:login') @@ -468,7 +538,7 @@ class VirtualMachineView(PermissionRequiredMixin, LoginRequiredMixin, View): login_url = reverse_lazy('hosting:login') # model = VirtualMachinePlan # context_object_name = "virtual_machine" - permission_required = ['view_virtualmachineplan', 'cancel_virtualmachineplan'] + permission_required = [] # fields = '__all__' # def get_context_data(self, **kwargs): diff --git a/utils/forms.py b/utils/forms.py index dd6f7a85..c50b1a0b 100644 --- a/utils/forms.py +++ b/utils/forms.py @@ -100,7 +100,7 @@ class EditCreditCardForm(forms.Form): class BillingAddressForm(forms.ModelForm): - token = forms.CharField(widget=forms.HiddenInput()) + token = forms.CharField(widget=forms.HiddenInput(), required=False) class Meta: model = BillingAddress