From 2e05ebbf6731035dc838e565a8ded63aaa29bc85 Mon Sep 17 00:00:00 2001 From: Amal Elshihaby Date: Mon, 16 Aug 2021 10:46:59 +0200 Subject: [PATCH] Test Full cycle and fix some issues --- matrixhosting/models.py | 26 +++++++++++--- matrixhosting/signals.py | 19 +--------- .../templates/matrixhosting/base.html | 28 +++++++++++++++ .../templates/matrixhosting/bills.html | 2 -- .../templates/matrixhosting/cards.html | 2 -- .../matrixhosting/includes/_navbar.html | 6 ++-- .../templates/matrixhosting/index.html | 2 +- .../templates/matrixhosting/instances.html | 24 ++++++++----- .../matrixhosting/order_confirmation.html | 4 +-- .../matrixhosting/order_details.html | 27 -------------- .../matrixhosting/order_success.html | 6 ++-- .../templates/matrixhosting/orders.html | 3 -- .../templates/matrixhosting/payments.html | 2 -- matrixhosting/tests.py | 23 +++++++++++- matrixhosting/utils.py | 13 +++++-- matrixhosting/views.py | 35 +++++++++++-------- uncloud/.env | 2 +- uncloud_pay/models.py | 1 - uncloud_pay/views.py | 4 ++- 19 files changed, 131 insertions(+), 98 deletions(-) diff --git a/matrixhosting/models.py b/matrixhosting/models.py index a995d99..1cabb0f 100644 --- a/matrixhosting/models.py +++ b/matrixhosting/models.py @@ -11,7 +11,7 @@ from django.conf import settings from django.contrib.auth import get_user_model from django.template.loader import render_to_string -from uncloud_pay.models import Order +from uncloud_pay.models import Order, BillRecord # Initialize logger. @@ -71,6 +71,24 @@ class VMInstance(models.Model): def __str__(self): return f"{self.id}-{self.order}" - def delete_for_bill(self, bill): - #TODO delete related instances - return True \ No newline at end of file + @classmethod + def delete_for_bill(cls, bill): + bill_records = BillRecord.objects.filter(bill=bill) + for record in bill_records: + instances = VMInstance.objects.filter(order=record.order) + for instance in instances: + instance.delete() + return True + + @classmethod + def create_instance(cls, order): + machine = cls.objects.filter(order=order).first() + if not machine: + order_config = json.loads(order.config) + isOpenRegistration = order_config.get('is_open_registration', False) + instance_config = {'cpuCores': order_config['cores'], 'ram': order_config['memory'], 'storage': order_config['storage'], + 'matrixDomain': order_config['matrix_domain'], 'homeserverDomain': order_config['homeserver_domain'], + 'webClientDomain': order_config['webclient_domain'], 'isOpenRegistration': isOpenRegistration} + cls.objects.create(owner=order.owner, order=order, vm_name=order_config['homeserver_domain'], + homeserver_domain=order_config['homeserver_domain'],webclient_domain=order_config['webclient_domain'], + config=instance_config) \ No newline at end of file diff --git a/matrixhosting/signals.py b/matrixhosting/signals.py index bdee9c7..139597f 100644 --- a/matrixhosting/signals.py +++ b/matrixhosting/signals.py @@ -1,19 +1,2 @@ -import json -from matrixhosting.models import VMInstance -from uncloud_pay.models import Order -from django.db.models.signals import post_save -from django.dispatch import receiver -@receiver(post_save, sender=Order) -def create_instance(sender, instance, created, **kwargs): - if not created: - return - machine = VMInstance.objects.filter(order=instance).first() - if not machine: - order_config = json.loads(instance.config) - instance_config = {'cpuCores': order_config['cores'], 'ram': order_config['memory'], 'storage': order_config['storage'], - 'matrixDomain': order_config['matrix_domain'], 'homeserverDomain': order_config['homeserver_domain'], - 'webClientDomain': order_config['webclient_domain'], 'isOpenRegistration': order_config['is_open_registration']} - VMInstance.objects.create(owner=instance.owner, order=instance, vm_name=order_config['homeserver_domain'], - homeserver_domain=order_config['homeserver_domain'],webclient_domain=order_config['webclient_domain'], - config=instance_config) \ No newline at end of file + diff --git a/matrixhosting/templates/matrixhosting/base.html b/matrixhosting/templates/matrixhosting/base.html index 011e71e..57d7a49 100644 --- a/matrixhosting/templates/matrixhosting/base.html +++ b/matrixhosting/templates/matrixhosting/base.html @@ -44,7 +44,35 @@
{% block navbar %} {% include "matrixhosting/includes/_navbar.html" with transparent_header=transparent_header %} {%endblock %} + {% block main %} +
+ + {% if messages %} +
+ {% for message in messages %} + {% if 'error' in message.tags %} + + {% else %} + + {% endif %} + {% endfor %} +
+ {% endif %} {% block content %} {% endblock %} +
+ {% endblock %} {% include "matrixhosting/includes/_footer.html" %}
diff --git a/matrixhosting/templates/matrixhosting/bills.html b/matrixhosting/templates/matrixhosting/bills.html index f0f31ec..c85b237 100644 --- a/matrixhosting/templates/matrixhosting/bills.html +++ b/matrixhosting/templates/matrixhosting/bills.html @@ -5,7 +5,6 @@ {% block title %} Bills {% endblock %} {% block content %} -
@@ -182,7 +181,6 @@
-
{% endblock %} {% block js_extra %} diff --git a/matrixhosting/templates/matrixhosting/includes/_navbar.html b/matrixhosting/templates/matrixhosting/includes/_navbar.html index f59c589..78032aa 100644 --- a/matrixhosting/templates/matrixhosting/includes/_navbar.html +++ b/matrixhosting/templates/matrixhosting/includes/_navbar.html @@ -18,6 +18,7 @@ ============================== --> {% url 'matrix:index' as index_url %} {% url 'matrix:orders' as orders_url %} + {% url 'matrix:instances' as instances_url %} {% url 'matrix:billing' as payments_url %}