remove vm creation to util function
This commit is contained in:
		
					parent
					
						
							
								fae1c7fbeb
							
						
					
				
			
			
				commit
				
					
						3debf34118
					
				
			
		
					 3 changed files with 82 additions and 118 deletions
				
			
		|  | @ -1,5 +1,10 @@ | ||||||
| from django.contrib.sites.models import Site | from django.contrib.sites.models import Site | ||||||
| 
 | 
 | ||||||
|  | from datacenterlight.tasks import create_vm_task | ||||||
|  | from hosting.models import HostingOrder, HostingBill | ||||||
|  | from membership.models import StripeCustomer | ||||||
|  | from utils.forms import UserBillingAddressForm | ||||||
|  | from utils.models import BillingAddress | ||||||
| from .cms_models import CMSIntegration | from .cms_models import CMSIntegration | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | @ -12,3 +17,57 @@ def get_cms_integration(name): | ||||||
|     except CMSIntegration.DoesNotExist: |     except CMSIntegration.DoesNotExist: | ||||||
|         cms_integration = CMSIntegration.objects.get(name=name, domain=None) |         cms_integration = CMSIntegration.objects.get(name=name, domain=None) | ||||||
|     return cms_integration |     return cms_integration | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def create_vm(billing_address_data, stripe_customer_id, specs, | ||||||
|  |               stripe_subscription_obj, card_details_dict, request, | ||||||
|  |               vm_template_id, template, user): | ||||||
|  |     billing_address = BillingAddress( | ||||||
|  |         cardholder_name=billing_address_data['cardholder_name'], | ||||||
|  |         street_address=billing_address_data['street_address'], | ||||||
|  |         city=billing_address_data['city'], | ||||||
|  |         postal_code=billing_address_data['postal_code'], | ||||||
|  |         country=billing_address_data['country'] | ||||||
|  |     ) | ||||||
|  |     billing_address.save() | ||||||
|  | 
 | ||||||
|  |     customer = StripeCustomer.objects.filter(id=stripe_customer_id).first() | ||||||
|  | 
 | ||||||
|  |     # Create a Hosting Order with vm_id = 0, we shall set it later in | ||||||
|  |     # celery task once the VM instance is up and running | ||||||
|  |     order = HostingOrder.create( | ||||||
|  |         price=specs['price'], | ||||||
|  |         vm_id=0, | ||||||
|  |         customer=customer, | ||||||
|  |         billing_address=billing_address | ||||||
|  |     ) | ||||||
|  | 
 | ||||||
|  |     # Create a Hosting Bill | ||||||
|  |     HostingBill.create(customer=customer, billing_address=billing_address) | ||||||
|  | 
 | ||||||
|  |     # 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() | ||||||
|  | 
 | ||||||
|  |     # Associate an order with a stripe subscription | ||||||
|  |     order.set_subscription_id( | ||||||
|  |         stripe_subscription_obj.id, card_details_dict | ||||||
|  |     ) | ||||||
|  | 
 | ||||||
|  |     # If the Stripe payment succeeds, set order status approved | ||||||
|  |     order.set_approved() | ||||||
|  | 
 | ||||||
|  |     create_vm_task.delay(vm_template_id, user, specs, template, order.id) | ||||||
|  | 
 | ||||||
|  |     for session_var in ['specs', 'template', 'billing_address', | ||||||
|  |                         'billing_address_data', | ||||||
|  |                         'token', 'customer']: | ||||||
|  |         if session_var in request.session: | ||||||
|  |             del request.session[session_var] | ||||||
|  |  | ||||||
|  | @ -7,7 +7,7 @@ from django.contrib import messages | ||||||
| from django.contrib.auth import login, authenticate | from django.contrib.auth import login, authenticate | ||||||
| from django.core.exceptions import ValidationError | from django.core.exceptions import ValidationError | ||||||
| from django.core.urlresolvers import reverse | from django.core.urlresolvers import reverse | ||||||
| from django.http import HttpResponseRedirect, HttpResponse | from django.http import HttpResponseRedirect, HttpResponse, JsonResponse | ||||||
| from django.shortcuts import render | from django.shortcuts import render | ||||||
| from django.utils.translation import get_language, ugettext_lazy as _ | from django.utils.translation import get_language, ugettext_lazy as _ | ||||||
| from django.views.decorators.cache import cache_control | from django.views.decorators.cache import cache_control | ||||||
|  | @ -27,7 +27,7 @@ from utils.stripe_utils import StripeUtils | ||||||
| from utils.tasks import send_plain_email_task | from utils.tasks import send_plain_email_task | ||||||
| from .forms import ContactForm | from .forms import ContactForm | ||||||
| from .models import VMTemplate | from .models import VMTemplate | ||||||
| from .utils import get_cms_integration | from .utils import get_cms_integration, create_vm | ||||||
| 
 | 
 | ||||||
| logger = logging.getLogger(__name__) | logger = logging.getLogger(__name__) | ||||||
| 
 | 
 | ||||||
|  | @ -390,8 +390,8 @@ class OrderConfirmationView(DetailView): | ||||||
|                       ' On close of this popup, you will be redirected back to' |                       ' On close of this popup, you will be redirected back to' | ||||||
|                       ' the payment page.')) |                       ' the payment page.')) | ||||||
|             } |             } | ||||||
|             return HttpResponse(json.dumps(response), |             return JsonResponse(response) | ||||||
|                                 content_type="application/json") | 
 | ||||||
|         card_details_dict = card_details.get('response_object') |         card_details_dict = card_details.get('response_object') | ||||||
|         cpu = specs.get('cpu') |         cpu = specs.get('cpu') | ||||||
|         memory = specs.get('memory') |         memory = specs.get('memory') | ||||||
|  | @ -431,8 +431,7 @@ class OrderConfirmationView(DetailView): | ||||||
|                       ' On close of this popup, you will be redirected back to' |                       ' On close of this popup, you will be redirected back to' | ||||||
|                       ' the payment page.')) |                       ' the payment page.')) | ||||||
|             } |             } | ||||||
|             return HttpResponse(json.dumps(response), |             return JsonResponse(response) | ||||||
|                                 content_type="application/json") |  | ||||||
| 
 | 
 | ||||||
|         # Create user if the user is not logged in and if he is not already |         # Create user if the user is not logged in and if he is not already | ||||||
|         # registered |         # registered | ||||||
|  | @ -487,53 +486,11 @@ class OrderConfirmationView(DetailView): | ||||||
|             'language': get_language(), |             'language': get_language(), | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         billing_address = BillingAddress( |         create_vm( | ||||||
|             cardholder_name=billing_address_data['cardholder_name'], |             billing_address_data, stripe_customer_id, specs, | ||||||
|             street_address=billing_address_data['street_address'], |             stripe_subscription_obj, card_details_dict, request, | ||||||
|             city=billing_address_data['city'], |             vm_template_id, template, user | ||||||
|             postal_code=billing_address_data['postal_code'], |  | ||||||
|             country=billing_address_data['country'] |  | ||||||
|         ) |         ) | ||||||
|         billing_address.save() |  | ||||||
| 
 |  | ||||||
|         customer = StripeCustomer.objects.filter(id=stripe_customer_id).first() |  | ||||||
| 
 |  | ||||||
|         # Create a Hosting Order with vm_id = 0, we shall set it later in |  | ||||||
|         # celery task once the VM instance is up and running |  | ||||||
|         order = HostingOrder.create( |  | ||||||
|             price=specs['price'], |  | ||||||
|             vm_id=0, |  | ||||||
|             customer=customer, |  | ||||||
|             billing_address=billing_address |  | ||||||
|         ) |  | ||||||
| 
 |  | ||||||
|         # Create a Hosting Bill |  | ||||||
|         HostingBill.create(customer=customer, billing_address=billing_address) |  | ||||||
| 
 |  | ||||||
|         # 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() |  | ||||||
| 
 |  | ||||||
|         # Associate an order with a stripe subscription |  | ||||||
|         order.set_subscription_id( |  | ||||||
|             stripe_subscription_obj.id, card_details_dict |  | ||||||
|         ) |  | ||||||
| 
 |  | ||||||
|         # If the Stripe payment succeeds, set order status approved |  | ||||||
|         order.set_approved() |  | ||||||
| 
 |  | ||||||
|         create_vm_task.delay(vm_template_id, user, specs, template, order.id) |  | ||||||
|         for session_var in ['specs', 'template', 'billing_address', |  | ||||||
|                             'billing_address_data', |  | ||||||
|                             'token', 'customer']: |  | ||||||
|             if session_var in request.session: |  | ||||||
|                 del request.session[session_var] |  | ||||||
| 
 | 
 | ||||||
|         response = { |         response = { | ||||||
|             'status': True, |             'status': True, | ||||||
|  | @ -549,5 +506,4 @@ class OrderConfirmationView(DetailView): | ||||||
|                   ' it is ready.')) |                   ' it is ready.')) | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         return HttpResponse(json.dumps(response), |         return JsonResponse(response) | ||||||
|                             content_type="application/json") |  | ||||||
|  |  | ||||||
|  | @ -1,4 +1,3 @@ | ||||||
| import json |  | ||||||
| import logging | import logging | ||||||
| import uuid | import uuid | ||||||
| from datetime import datetime | from datetime import datetime | ||||||
|  | @ -12,7 +11,9 @@ from django.contrib.auth.tokens import default_token_generator | ||||||
| from django.core.exceptions import ValidationError | from django.core.exceptions import ValidationError | ||||||
| from django.core.files.base import ContentFile | from django.core.files.base import ContentFile | ||||||
| from django.core.urlresolvers import reverse_lazy, reverse | from django.core.urlresolvers import reverse_lazy, reverse | ||||||
| from django.http import Http404, HttpResponseRedirect, HttpResponse | from django.http import ( | ||||||
|  |     Http404, HttpResponseRedirect, HttpResponse, JsonResponse | ||||||
|  | ) | ||||||
| from django.shortcuts import redirect, render | from django.shortcuts import redirect, render | ||||||
| from django.utils.http import urlsafe_base64_decode | from django.utils.http import urlsafe_base64_decode | ||||||
| from django.utils.safestring import mark_safe | from django.utils.safestring import mark_safe | ||||||
|  | @ -31,7 +32,7 @@ from stored_messages.models import Message | ||||||
| from stored_messages.settings import stored_messages_settings | from stored_messages.settings import stored_messages_settings | ||||||
| 
 | 
 | ||||||
| from datacenterlight.models import VMTemplate | from datacenterlight.models import VMTemplate | ||||||
| from datacenterlight.tasks import create_vm_task | from datacenterlight.utils import create_vm | ||||||
| from membership.models import CustomUser, StripeCustomer | from membership.models import CustomUser, StripeCustomer | ||||||
| from opennebula_api.models import OpenNebulaManager | from opennebula_api.models import OpenNebulaManager | ||||||
| from opennebula_api.serializers import ( | from opennebula_api.serializers import ( | ||||||
|  | @ -44,7 +45,6 @@ from utils.forms import ( | ||||||
| ) | ) | ||||||
| from utils.hosting_utils import get_vm_price | from utils.hosting_utils import get_vm_price | ||||||
| from utils.mailer import BaseEmail | from utils.mailer import BaseEmail | ||||||
| from utils.models import BillingAddress |  | ||||||
| from utils.stripe_utils import StripeUtils | from utils.stripe_utils import StripeUtils | ||||||
| from utils.tasks import send_plain_email_task | from utils.tasks import send_plain_email_task | ||||||
| from utils.views import ( | from utils.views import ( | ||||||
|  | @ -873,8 +873,8 @@ class OrdersHostingDetailView(LoginRequiredMixin, DetailView): | ||||||
|                       ' On close of this popup, you will be redirected back to' |                       ' On close of this popup, you will be redirected back to' | ||||||
|                       ' the payment page.')) |                       ' the payment page.')) | ||||||
|             } |             } | ||||||
|             return HttpResponse(json.dumps(response), |             return JsonResponse(response) | ||||||
|                                 content_type="application/json") | 
 | ||||||
|         user = { |         user = { | ||||||
|             'name': self.request.user.name, |             'name': self.request.user.name, | ||||||
|             'email': self.request.user.email, |             'email': self.request.user.email, | ||||||
|  | @ -884,55 +884,11 @@ class OrdersHostingDetailView(LoginRequiredMixin, DetailView): | ||||||
|             'language': get_language(), |             'language': get_language(), | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         billing_address = BillingAddress( |         create_vm( | ||||||
|             cardholder_name=billing_address_data['cardholder_name'], |             billing_address_data, stripe_customer_id, specs, | ||||||
|             street_address=billing_address_data['street_address'], |             stripe_subscription_obj, card_details_dict, request, | ||||||
|             city=billing_address_data['city'], |             vm_template_id, template, user | ||||||
|             postal_code=billing_address_data['postal_code'], |  | ||||||
|             country=billing_address_data['country'] |  | ||||||
|         ) |         ) | ||||||
|         billing_address.save() |  | ||||||
| 
 |  | ||||||
|         customer = StripeCustomer.objects.filter(id=stripe_customer_id).first() |  | ||||||
| 
 |  | ||||||
|         # Create a Hosting Order with vm_id = 0, we shall set it later in |  | ||||||
|         # celery task once the VM instance is up and running |  | ||||||
|         order = HostingOrder.create( |  | ||||||
|             price=specs['price'], |  | ||||||
|             vm_id=0, |  | ||||||
|             customer=customer, |  | ||||||
|             billing_address=billing_address |  | ||||||
|         ) |  | ||||||
| 
 |  | ||||||
|         # Create a Hosting Bill |  | ||||||
|         HostingBill.create(customer=customer, billing_address=billing_address) |  | ||||||
| 
 |  | ||||||
|         # 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() |  | ||||||
| 
 |  | ||||||
|         # Associate an order with a stripe subscription |  | ||||||
|         order.set_subscription_id( |  | ||||||
|             stripe_subscription_obj.id, card_details_dict |  | ||||||
|         ) |  | ||||||
| 
 |  | ||||||
|         # If the Stripe payment succeeds, set order status approved |  | ||||||
|         order.set_approved() |  | ||||||
| 
 |  | ||||||
|         create_vm_task.delay(vm_template_id, user, specs, template, order.id) |  | ||||||
| 
 |  | ||||||
|         for session_var in ['specs', 'template', 'billing_address', |  | ||||||
|                             'billing_address_data', |  | ||||||
|                             'token', 'customer']: |  | ||||||
|             if session_var in request.session: |  | ||||||
|                 del request.session[session_var] |  | ||||||
| 
 | 
 | ||||||
|         response = { |         response = { | ||||||
|             'status': True, |             'status': True, | ||||||
|  | @ -944,8 +900,7 @@ class OrdersHostingDetailView(LoginRequiredMixin, DetailView): | ||||||
|                   ' it is ready.')) |                   ' it is ready.')) | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         return HttpResponse(json.dumps(response), |         return JsonResponse(response) | ||||||
|                             content_type="application/json") |  | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class OrdersHostingListView(LoginRequiredMixin, ListView): | class OrdersHostingListView(LoginRequiredMixin, ListView): | ||||||
|  | @ -1128,10 +1083,7 @@ class VirtualMachineView(LoginRequiredMixin, View): | ||||||
|                 for m in storage: |                 for m in storage: | ||||||
|                     pass |                     pass | ||||||
|                 storage.used = True |                 storage.used = True | ||||||
|                 return HttpResponse( |                 return JsonResponse({'text': ugettext('Terminated')}) | ||||||
|                     json.dumps({'text': ugettext('Terminated')}), |  | ||||||
|                     content_type="application/json" |  | ||||||
|                 ) |  | ||||||
|             else: |             else: | ||||||
|                 return redirect(reverse('hosting:virtual_machines')) |                 return redirect(reverse('hosting:virtual_machines')) | ||||||
|         elif self.request.is_ajax(): |         elif self.request.is_ajax(): | ||||||
|  | @ -1262,10 +1214,7 @@ class VirtualMachineView(LoginRequiredMixin, View): | ||||||
|                 ["%s=%s" % (k, v) for (k, v) in admin_email_body.items()]), |                 ["%s=%s" % (k, v) for (k, v) in admin_email_body.items()]), | ||||||
|         } |         } | ||||||
|         send_plain_email_task.delay(email_to_admin_data) |         send_plain_email_task.delay(email_to_admin_data) | ||||||
|         return HttpResponse( |         return JsonResponse(response) | ||||||
|             json.dumps(response), |  | ||||||
|             content_type="application/json" |  | ||||||
|         ) |  | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class HostingBillListView(PermissionRequiredMixin, LoginRequiredMixin, | class HostingBillListView(PermissionRequiredMixin, LoginRequiredMixin, | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue