Reorganize code
This commit is contained in:
parent
7d683e1257
commit
96de92d312
1 changed files with 45 additions and 52 deletions
|
@ -42,6 +42,7 @@ 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.stripe_utils import StripeUtils
|
from utils.stripe_utils import StripeUtils
|
||||||
|
from utils.tasks import send_plain_email_task
|
||||||
from utils.views import (
|
from utils.views import (
|
||||||
PasswordResetViewMixin, PasswordResetConfirmViewMixin, LoginViewMixin,
|
PasswordResetViewMixin, PasswordResetConfirmViewMixin, LoginViewMixin,
|
||||||
ResendActivationLinkViewMixin
|
ResendActivationLinkViewMixin
|
||||||
|
@ -1055,23 +1056,26 @@ class VirtualMachineView(LoginRequiredMixin, View):
|
||||||
|
|
||||||
def post(self, request, *args, **kwargs):
|
def post(self, request, *args, **kwargs):
|
||||||
response = {'status': False}
|
response = {'status': False}
|
||||||
|
admin_email_body = {}
|
||||||
owner = self.request.user
|
owner = self.request.user
|
||||||
vm = self.get_object()
|
vm = self.get_object()
|
||||||
|
|
||||||
opennebula_vm_id = self.kwargs.get('pk')
|
manager = OpenNebulaManager(
|
||||||
|
email=owner.email,
|
||||||
# Cancel subscription
|
password=owner.password
|
||||||
stripe_utils = StripeUtils()
|
|
||||||
error_msg_subject = (
|
|
||||||
'Error canceling subscription for '
|
|
||||||
'{user} and vm id {vm_id}'.format(
|
|
||||||
user=owner.email,
|
|
||||||
vm_id=opennebula_vm_id
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
try:
|
||||||
|
vm_data = VirtualMachineSerializer(manager.get_vm(vm.id)).data
|
||||||
|
vm_name = vm_data.get('name')
|
||||||
|
except WrongIdError as wrong_id_err:
|
||||||
|
logger.error(str(wrong_id_err))
|
||||||
|
return redirect(reverse('hosting:virtual_machines'))
|
||||||
|
|
||||||
|
# Cancel Stripe subscription
|
||||||
|
stripe_utils = StripeUtils()
|
||||||
try:
|
try:
|
||||||
hosting_order = HostingOrder.objects.get(
|
hosting_order = HostingOrder.objects.get(
|
||||||
vm_id=opennebula_vm_id
|
vm_id=vm.id
|
||||||
)
|
)
|
||||||
result = stripe_utils.unsubscribe_customer(
|
result = stripe_utils.unsubscribe_customer(
|
||||||
subscription_id=hosting_order.subscription_id
|
subscription_id=hosting_order.subscription_id
|
||||||
|
@ -1081,43 +1085,20 @@ class VirtualMachineView(LoginRequiredMixin, View):
|
||||||
if (stripe_subscription_obj is None or
|
if (stripe_subscription_obj is None or
|
||||||
stripe_subscription_obj.status != 'canceled'):
|
stripe_subscription_obj.status != 'canceled'):
|
||||||
error_msg = result.get('error')
|
error_msg = result.get('error')
|
||||||
logger.error(error_msg_subject)
|
logger.error(
|
||||||
|
'Error canceling subscription for {user} and vm id '
|
||||||
|
'{vm_id}'.format(user=owner.email, vm_id=vm.id)
|
||||||
|
)
|
||||||
logger.error(error_msg)
|
logger.error(error_msg)
|
||||||
email_data = {
|
admin_email_body['stripe_error_msg'] = error_msg
|
||||||
'subject': error_msg_subject,
|
|
||||||
'from_email': settings.DCL_SUPPORT_FROM_ADDRESS,
|
|
||||||
'to': settings.DCL_ERROR_EMAILS_TO_LIST,
|
|
||||||
'body': error_msg,
|
|
||||||
}
|
|
||||||
email = EmailMessage(**email_data)
|
|
||||||
email.send()
|
|
||||||
except HostingOrder.DoesNotExist:
|
except HostingOrder.DoesNotExist:
|
||||||
error_msg = (
|
error_msg = (
|
||||||
"HostingOrder corresponding to vm_id={vm_id} does"
|
"HostingOrder corresponding to vm_id={vm_id} does"
|
||||||
"not exist. Hence, can not find subscription to "
|
"not exist. Hence, can not find subscription to "
|
||||||
"cancel ".format(vm_id=opennebula_vm_id)
|
"cancel ".format(vm_id=vm.id)
|
||||||
)
|
)
|
||||||
logger.error(error_msg)
|
logger.error(error_msg)
|
||||||
email_data = {
|
admin_email_body['stripe_error_msg'] = error_msg
|
||||||
'subject': error_msg_subject,
|
|
||||||
'from_email': settings.DCL_SUPPORT_FROM_ADDRESS,
|
|
||||||
'to': settings.DCL_ERROR_EMAILS_TO_LIST,
|
|
||||||
'body': error_msg,
|
|
||||||
}
|
|
||||||
email = EmailMessage(**email_data)
|
|
||||||
email.send()
|
|
||||||
|
|
||||||
manager = OpenNebulaManager(
|
|
||||||
email=owner.email,
|
|
||||||
password=owner.password
|
|
||||||
)
|
|
||||||
|
|
||||||
try:
|
|
||||||
vm_data = VirtualMachineSerializer(manager.get_vm(vm.id)).data
|
|
||||||
vm_name = vm_data.get('name')
|
|
||||||
except WrongIdError as wrong_id_err:
|
|
||||||
logger.error(str(wrong_id_err))
|
|
||||||
return redirect(reverse('hosting:virtual_machines'))
|
|
||||||
|
|
||||||
terminated = manager.delete_vm(vm.id)
|
terminated = manager.delete_vm(vm.id)
|
||||||
|
|
||||||
|
@ -1126,26 +1107,24 @@ class VirtualMachineView(LoginRequiredMixin, View):
|
||||||
"manager.delete_vm returned False. Hence, error making "
|
"manager.delete_vm returned False. Hence, error making "
|
||||||
"xml-rpc call to delete vm failed."
|
"xml-rpc call to delete vm failed."
|
||||||
)
|
)
|
||||||
response['text'] = ugettext(
|
response['text'] = ugettext('Error terminating VM') + vm.id
|
||||||
'Error terminating VM') + opennebula_vm_id
|
|
||||||
else:
|
else:
|
||||||
for t in range(15):
|
for t in range(15):
|
||||||
try:
|
try:
|
||||||
manager.get_vm(opennebula_vm_id)
|
manager.get_vm(vm.id)
|
||||||
except WrongIdError:
|
except WrongIdError:
|
||||||
response['status'] = True
|
response['status'] = True
|
||||||
response['text'] = ugettext('Terminated')
|
response['text'] = ugettext('Terminated')
|
||||||
vm_detail_obj = VMDetail.objects.filter(
|
vm_detail_obj = VMDetail.objects.filter(
|
||||||
vm_id=opennebula_vm_id
|
vm_id=vm.id
|
||||||
).first()
|
).first()
|
||||||
vm_detail_obj.terminated_at = datetime.utcnow()
|
vm_detail_obj.terminated_at = datetime.utcnow()
|
||||||
vm_detail_obj.save()
|
vm_detail_obj.save()
|
||||||
except BaseException as base_exception:
|
except BaseException as base_exception:
|
||||||
logger.error(
|
logger.error(
|
||||||
"manager.get_vm returned exception: {details}. Hence, "
|
"manager.get_vm({vm_id}) returned exception: "
|
||||||
"the vm with id {vm_id} is no more accessible".format(
|
"{details}.".format(
|
||||||
details=str(base_exception),
|
details=str(base_exception), vm_id=vm.id
|
||||||
vm_id=opennebula_vm_id
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
break
|
break
|
||||||
|
@ -1153,10 +1132,12 @@ class VirtualMachineView(LoginRequiredMixin, View):
|
||||||
sleep(2)
|
sleep(2)
|
||||||
context = {
|
context = {
|
||||||
'vm_name': vm_name,
|
'vm_name': vm_name,
|
||||||
'base_url': "{0}://{1}".format(self.request.scheme,
|
'base_url': "{0}://{1}".format(
|
||||||
self.request.get_host()),
|
self.request.scheme, self.request.get_host()
|
||||||
|
),
|
||||||
'page_header': _('Virtual Machine %(vm_name)s Cancelled') % {
|
'page_header': _('Virtual Machine %(vm_name)s Cancelled') % {
|
||||||
'vm_name': vm_name}
|
'vm_name': vm_name
|
||||||
|
}
|
||||||
}
|
}
|
||||||
email_data = {
|
email_data = {
|
||||||
'subject': context['page_header'],
|
'subject': context['page_header'],
|
||||||
|
@ -1168,6 +1149,18 @@ class VirtualMachineView(LoginRequiredMixin, View):
|
||||||
}
|
}
|
||||||
email = BaseEmail(**email_data)
|
email = BaseEmail(**email_data)
|
||||||
email.send()
|
email.send()
|
||||||
|
admin_email_body.update(response)
|
||||||
|
email_to_admin_data = {
|
||||||
|
'subject': "Deleted Subscription for VM: {vm_id} and "
|
||||||
|
"user: {user}".format(
|
||||||
|
vm_id=vm.id, user=owner.email
|
||||||
|
),
|
||||||
|
'from_email': settings.DCL_SUPPORT_FROM_ADDRESS,
|
||||||
|
'to': ['info@ungleich.ch'],
|
||||||
|
'body': "\n".join(
|
||||||
|
["%s=%s" % (k, v) for (k, v) in admin_email_body]),
|
||||||
|
}
|
||||||
|
send_plain_email_task.delay(email_to_admin_data)
|
||||||
return HttpResponse(
|
return HttpResponse(
|
||||||
json.dumps(response),
|
json.dumps(response),
|
||||||
content_type="application/json"
|
content_type="application/json"
|
||||||
|
|
Loading…
Reference in a new issue