Merge remote-tracking branch 'mainRepo/master' into feature/6561/invoices-webhook

This commit is contained in:
PCoder 2019-06-12 06:25:47 +02:00
commit 4147b8f891
40 changed files with 1094 additions and 142 deletions

View file

@ -28,13 +28,16 @@ from django.views.generic import (
)
from guardian.mixins import PermissionRequiredMixin
from oca.pool import WrongIdError
from rest_framework.renderers import JSONRenderer
from rest_framework.response import Response
from rest_framework.views import APIView
from stored_messages.api import mark_read
from stored_messages.models import Message
from stored_messages.settings import stored_messages_settings
from datacenterlight.cms_models import DCLCalculatorPluginModel
from datacenterlight.models import VMTemplate, VMPricing
from datacenterlight.utils import create_vm, get_cms_integration
from datacenterlight.utils import create_vm, get_cms_integration, check_otp
from hosting.models import UserCardDetail
from membership.models import CustomUser, StripeCustomer
from opennebula_api.models import OpenNebulaManager
@ -66,9 +69,12 @@ from .models import (
logger = logging.getLogger(__name__)
CONNECTION_ERROR = "Your VMs cannot be displayed at the moment due to a \
backend connection error. please try again in a few \
minutes."
decorators = [never_cache]
@ -1185,19 +1191,26 @@ class InvoiceListView(LoginRequiredMixin, ListView):
customer__user=self.request.user
)
ips_dict = {}
line_items_dict = {}
line_item_period_dict = {}
for mhb in mhbs:
try:
vm_detail = VMDetail.objects.get(vm_id=mhb.order.vm_id)
ips_dict[mhb.invoice_number] = [vm_detail.ipv6, vm_detail.ipv4]
line_items_dict[mhb.invoice_number] = HostingBillLineItem.objects.filter(monthly_hosting_bill=mhb)
all_line_items = HostingBillLineItem.objects.filter(monthly_hosting_bill=mhb)
for line_item in all_line_items:
if line_item.get_item_detail_str() != "":
line_item_period_dict[mhb.invoice_number] = {
"period_start": line_item.period_start,
"period_end": line_item.period_end
}
break
except VMDetail.DoesNotExist as dne:
ips_dict[mhb.invoice_number] = ['--']
logger.debug("VMDetail for {} doesn't exist".format(
mhb.order.vm_id
))
context['line_items'] = line_items_dict
context['ips'] = ips_dict
context['period'] = line_item_period_dict
return context
def get_queryset(self):
@ -1295,8 +1308,8 @@ class InvoiceDetailView(LoginRequiredMixin, DetailView):
# fallback to get it from the infrastructure
try:
manager = OpenNebulaManager(
email=self.request.email,
password=self.request.password
email=self.request.user.email,
password=self.request.user.password
)
vm = manager.get_vm(vm_id)
context['vm'] = VirtualMachineSerializer(vm).data
@ -1335,6 +1348,9 @@ class InvoiceDetailView(LoginRequiredMixin, DetailView):
context['total_in_chf'] = obj.total_in_chf()
context['invoice_number'] = obj.invoice_number
context['discount_on_stripe'] = obj.discount_in_chf()
if obj.lines_data_count > 1:
# special case, we pass the details of each of the line items
context['line_items'] = obj.hostingbilllineitem_set.all()
return context
else:
raise Http404
@ -1771,3 +1787,39 @@ def forbidden_view(request, exception=None, reason=''):
'again.')
messages.add_message(request, messages.ERROR, err_msg)
return HttpResponseRedirect(request.get_full_path())
class CheckUserVM(APIView):
renderer_classes = (JSONRenderer, )
def get(self, request):
try:
email = request.data['email']
ip = request.data['ip']
user = request.data['user']
realm = request.data['realm']
token = request.data['token']
if realm != settings.READ_VM_REALM:
return Response("User not allowed", 403)
response = check_otp(user, realm, token)
if response != 200:
return Response('Invalid token', 403)
manager = OpenNebulaManager()
# not the best way to lookup vms by ip
# TODO: make this optimal
vms = manager.get_vms()
users_vms = [vm for vm in vms if vm.uname == email]
if len(users_vms) == 0:
return Response('No VM found with the given email address',
404)
for vm in users_vms:
for nic in vm.template.nics:
if hasattr(nic, 'ip6_global'):
if nic.ip6_global == ip:
return Response('success', 200)
elif hasattr(nic, 'ip'):
if nic.ip == ip:
return Response('success', 200)
return Response('No VM found matching the ip address provided', 404)
except KeyError:
return Response('Not enough data provided', 400)