Fixed ssh key error, Now an existing user is not ask for his credit card again
This commit is contained in:
		
					parent
					
						
							
								5861bec4a6
							
						
					
				
			
			
				commit
				
					
						1f10f04a9d
					
				
			
		
					 7 changed files with 155 additions and 17 deletions
				
			
		|  | @ -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): | class VirtualMachinePlan(AssignPermissionsMixin, models.Model): | ||||||
| 
 | 
 | ||||||
|  | @ -196,7 +208,7 @@ class VirtualMachinePlan(AssignPermissionsMixin, models.Model): | ||||||
|         # Get opennebula client |         # Get opennebula client | ||||||
|         opennebula_client = OpenNebulaManager( |         opennebula_client = OpenNebulaManager( | ||||||
|             email=user.email, |             email=user.email, | ||||||
|             password=user.password[:20], |             password=user.password, | ||||||
|         ) |         ) | ||||||
| 
 | 
 | ||||||
|         # Get vm given the id |         # Get vm given the id | ||||||
|  | @ -216,7 +228,7 @@ class VirtualMachinePlan(AssignPermissionsMixin, models.Model): | ||||||
|         # Get opennebula client |         # Get opennebula client | ||||||
|         opennebula_client = OpenNebulaManager( |         opennebula_client = OpenNebulaManager( | ||||||
|             email=user.email, |             email=user.email, | ||||||
|             password=user.password[:20], |             password=user.password, | ||||||
|         ) |         ) | ||||||
| 
 | 
 | ||||||
|         # Get vm pool |         # Get vm pool | ||||||
|  | @ -279,6 +291,12 @@ class HostingOrder(AssignPermissionsMixin, models.Model): | ||||||
|         self.cc_brand = stripe_charge.source.brand |         self.cc_brand = stripe_charge.source.brand | ||||||
|         self.save() |         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): | class UserHostingKey(models.Model): | ||||||
|     user = models.ForeignKey(CustomUser) |     user = models.ForeignKey(CustomUser) | ||||||
|  |  | ||||||
|  | @ -171,6 +171,14 @@ class OpenNebulaManager: | ||||||
| 
 | 
 | ||||||
|         return vm_id |         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): |     def get_vm(self, email, vm_id): | ||||||
|         # Get vm's |         # Get vm's | ||||||
|         vms = self.get_vms(email) |         vms = self.get_vms(email) | ||||||
|  |  | ||||||
|  | @ -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'); |     var $form = $('#payment-form'); | ||||||
|     $form.submit(payWithStripe); |     $form.submit(payWithStripe); | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -8,7 +8,7 @@ | ||||||
| 			<div class="col-xs-12 col-md-4 col-md-offset-2 billing"> | 			<div class="col-xs-12 col-md-4 col-md-offset-2 billing"> | ||||||
| 				<h3><b>Billing Address</b></h3> | 				<h3><b>Billing Address</b></h3> | ||||||
| 				<hr> | 				<hr> | ||||||
| 				<form role="form" id="billing-form" method="post" action="{% url 'hosting:payment' %}" novalidate> | 				<form role="form" id="billing-form" method="post" action="" novalidate> | ||||||
| 	                {% for field in form %} | 	                {% for field in form %} | ||||||
|                         {% csrf_token %} |                         {% csrf_token %} | ||||||
| 	                    {% bootstrap_field field show_label=False type='fields'%} | 	                    {% bootstrap_field field show_label=False type='fields'%} | ||||||
|  | @ -23,6 +23,17 @@ | ||||||
| 				<hr> | 				<hr> | ||||||
| 	            <div> | 	            <div> | ||||||
| 	                <div> | 	                <div> | ||||||
|  |                     {% if credit_card_data.last4 %} | ||||||
|  |                       <form role="form" id="payment-form-with-creditcard"novalidate> | ||||||
|  |                         <h5 class="billing-head">Credit Card</h5> | ||||||
|  |                         <h5 class="membership-lead">Last 4: *****{{credit_card_data.last4}}</h5> | ||||||
|  |                         <h5 class="membership-lead">Type: {{credit_card_data.cc_brand}}</h5> | ||||||
|  |                         <input type="hidden" name="credit_card_needed" value="false"/> | ||||||
|  |                       </form>   | ||||||
|  |                      <button id="payment_button_with_creditcard" class="btn btn-success btn-lg btn-block" type="submit">Submit Payment</button> | ||||||
|  |                     {% else %} | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| 	                    <form role="form" id="payment-form" novalidate> | 	                    <form role="form" id="payment-form" novalidate> | ||||||
| 	                        <div class="row"> | 	                        <div class="row"> | ||||||
| 	                            <div class="col-xs-9 col-md-12"> | 	                            <div class="col-xs-9 col-md-12"> | ||||||
|  | @ -76,6 +87,7 @@ | ||||||
| 
 | 
 | ||||||
|                             |                             | ||||||
| 	                    </form> | 	                    </form> | ||||||
|  |                     {% endif %} | ||||||
| 	                </div> | 	                </div> | ||||||
| 	            </div> | 	            </div> | ||||||
| 	        </div> | 	        </div> | ||||||
|  | @ -110,6 +122,7 @@ | ||||||
| 	</div> | 	</div> | ||||||
| 
 | 
 | ||||||
| </div> | </div> | ||||||
|  | 
 | ||||||
| <!-- stripe key data --> | <!-- stripe key data --> | ||||||
| {% if stripe_key %} | {% if stripe_key %} | ||||||
| <script type="text/javascript">  | <script type="text/javascript">  | ||||||
|  | @ -117,6 +130,13 @@ | ||||||
| </script> | </script> | ||||||
| {%endif%} | {%endif%} | ||||||
| 
 | 
 | ||||||
|  | {% if credit_card_data.last4 and credit_card_data.cc_brand %} | ||||||
|  | <script type="text/javascript">  | ||||||
|  |      (function () {window.hasCreditcard = true;})(); | ||||||
|  | </script> | ||||||
|  | 
 | ||||||
|  | {%endif%} | ||||||
|  | 
 | ||||||
| {%endblock%} | {%endblock%} | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -58,7 +58,7 @@ | ||||||
| 				        {% if private_key %} | 				        {% if private_key %} | ||||||
| 				 		<div class="alert alert-warning"> | 				 		<div class="alert alert-warning"> | ||||||
| 				 			  | 				 			  | ||||||
|   							<strong>{% trans "Warning!"%}</strong>{% 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."%}   |   							<strong>{% trans "Warning!"%}</strong>{% trans "You can view your SSH  private key once. Don't lost your key"%}   | ||||||
| 						</div> | 						</div> | ||||||
| 						<div class="form-group"> | 						<div class="form-group"> | ||||||
| 						  <textarea class="form-control" rows="6" id="ssh_key" type="hidden" style="display:none">{{private_key}}</textarea> | 						  <textarea class="form-control" rows="6" id="ssh_key" type="hidden" style="display:none">{{private_key}}</textarea> | ||||||
|  | @ -101,6 +101,7 @@ | ||||||
| 		// Remove anchor from body | 		// Remove anchor from body | ||||||
| 		document.body.removeChild(a); | 		document.body.removeChild(a); | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
| </script> | </script> | ||||||
| {%endif%} | {%endif%} | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -22,7 +22,7 @@ from stored_messages.api import mark_read | ||||||
| 
 | 
 | ||||||
| from membership.models import CustomUser, StripeCustomer | from membership.models import CustomUser, StripeCustomer | ||||||
| from utils.stripe_utils import StripeUtils | 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.views import PasswordResetViewMixin, PasswordResetConfirmViewMixin, LoginViewMixin | ||||||
| from utils.mailer import BaseEmail | from utils.mailer import BaseEmail | ||||||
| from .models import VirtualMachineType, VirtualMachinePlan, HostingOrder, HostingBill, UserHostingKey | from .models import VirtualMachineType, VirtualMachinePlan, HostingOrder, HostingBill, UserHostingKey | ||||||
|  | @ -160,7 +160,7 @@ class SignupView(CreateView): | ||||||
|     model = CustomUser |     model = CustomUser | ||||||
| 
 | 
 | ||||||
|     def get_success_url(self): |     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 |         return next_url | ||||||
| 
 | 
 | ||||||
|     def form_valid(self, form): |     def form_valid(self, form): | ||||||
|  | @ -226,18 +226,19 @@ class GenerateVMSSHKeysView(LoginRequiredMixin, FormView): | ||||||
|     context_object_name = "virtual_machine" |     context_object_name = "virtual_machine" | ||||||
| 
 | 
 | ||||||
|     def get_context_data(self, **kwargs): |     def get_context_data(self, **kwargs): | ||||||
|         try: |  | ||||||
|             user_key = UserHostingKey.objects.get( |  | ||||||
|                 user=self.request.user |  | ||||||
|             ) |  | ||||||
|         except UserHostingKey.DoesNotExist: |  | ||||||
|             user_key = None |  | ||||||
| 
 |  | ||||||
|         context = super( |         context = super( | ||||||
|             GenerateVMSSHKeysView, |             GenerateVMSSHKeysView, | ||||||
|             self |             self | ||||||
|         ).get_context_data(**kwargs) |         ).get_context_data(**kwargs) | ||||||
| 
 | 
 | ||||||
|  |         try: | ||||||
|  |             user_key = UserHostingKey.objects.get( | ||||||
|  |                 user=self.request.user | ||||||
|  |             ) | ||||||
|  | 
 | ||||||
|  |         except UserHostingKey.DoesNotExist: | ||||||
|  |             user_key = None | ||||||
|  | 
 | ||||||
|         context.update({ |         context.update({ | ||||||
|             'user_key': user_key |             'user_key': user_key | ||||||
|         }) |         }) | ||||||
|  | @ -256,21 +257,75 @@ class GenerateVMSSHKeysView(LoginRequiredMixin, FormView): | ||||||
|         if form.cleaned_data.get('private_key'): |         if form.cleaned_data.get('private_key'): | ||||||
|             context.update({ |             context.update({ | ||||||
|                 'private_key': form.cleaned_data.get('private_key'), |                 '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) |         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): | class PaymentVMView(LoginRequiredMixin, FormView): | ||||||
|     template_name = 'hosting/payment.html' |     template_name = 'hosting/payment.html' | ||||||
|     login_url = reverse_lazy('hosting:login') |     login_url = reverse_lazy('hosting:login') | ||||||
|     form_class = BillingAddressForm |     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): |     def get_context_data(self, **kwargs): | ||||||
|         context = super(PaymentVMView, self).get_context_data(**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({ |         context.update({ | ||||||
|             'stripe_key': settings.STRIPE_API_PUBLIC_KEY |             'stripe_key': settings.STRIPE_API_PUBLIC_KEY | ||||||
|         }) |         }) | ||||||
|  | @ -281,7 +336,12 @@ class PaymentVMView(LoginRequiredMixin, FormView): | ||||||
|         form = self.get_form() |         form = self.get_form() | ||||||
| 
 | 
 | ||||||
|         if form.is_valid(): |         if form.is_valid(): | ||||||
|  | 
 | ||||||
|  |             # Get billing address data | ||||||
|  |             billing_address_data = form.cleaned_data | ||||||
|  | 
 | ||||||
|             context = self.get_context_data() |             context = self.get_context_data() | ||||||
|  | 
 | ||||||
|             specifications = request.session.get('vm_specs') |             specifications = request.session.get('vm_specs') | ||||||
| 
 | 
 | ||||||
|             vm_template = specifications.get('vm_template', 1) |             vm_template = specifications.get('vm_template', 1) | ||||||
|  | @ -318,6 +378,15 @@ class PaymentVMView(LoginRequiredMixin, FormView): | ||||||
|             # Create Billing Address |             # Create Billing Address | ||||||
|             billing_address = form.save() |             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 |             # Create a Hosting Order | ||||||
|             order = HostingOrder.create(vm_plan=plan, customer=customer, |             order = HostingOrder.create(vm_plan=plan, customer=customer, | ||||||
|                                         billing_address=billing_address) |                                         billing_address=billing_address) | ||||||
|  | @ -384,6 +453,7 @@ class OrdersHostingDetailView(PermissionRequiredMixin, LoginRequiredMixin, Detai | ||||||
|     permission_required = ['view_hostingorder'] |     permission_required = ['view_hostingorder'] | ||||||
|     model = HostingOrder |     model = HostingOrder | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
| class OrdersHostingListView(LoginRequiredMixin, ListView): | class OrdersHostingListView(LoginRequiredMixin, ListView): | ||||||
|     template_name = "hosting/orders.html" |     template_name = "hosting/orders.html" | ||||||
|     login_url = reverse_lazy('hosting:login') |     login_url = reverse_lazy('hosting:login') | ||||||
|  | @ -468,7 +538,7 @@ class VirtualMachineView(PermissionRequiredMixin, LoginRequiredMixin, View): | ||||||
|     login_url = reverse_lazy('hosting:login') |     login_url = reverse_lazy('hosting:login') | ||||||
|     # model = VirtualMachinePlan |     # model = VirtualMachinePlan | ||||||
|     # context_object_name = "virtual_machine" |     # context_object_name = "virtual_machine" | ||||||
|     permission_required = ['view_virtualmachineplan', 'cancel_virtualmachineplan'] |     permission_required = [] | ||||||
|     # fields = '__all__' |     # fields = '__all__' | ||||||
| 
 | 
 | ||||||
|     # def get_context_data(self, **kwargs): |     # def get_context_data(self, **kwargs): | ||||||
|  |  | ||||||
|  | @ -100,7 +100,7 @@ class EditCreditCardForm(forms.Form): | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class BillingAddressForm(forms.ModelForm): | class BillingAddressForm(forms.ModelForm): | ||||||
|     token = forms.CharField(widget=forms.HiddenInput()) |     token = forms.CharField(widget=forms.HiddenInput(), required=False) | ||||||
| 
 | 
 | ||||||
|     class Meta: |     class Meta: | ||||||
|         model = BillingAddress |         model = BillingAddress | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue