From 9d2a53f2e275d04f11d0904484d7a10bbacf028d Mon Sep 17 00:00:00 2001 From: Hostea dashboard Date: Tue, 13 Sep 2022 19:57:26 +0530 Subject: [PATCH 1/2] fix: more email project renaming --- billing/management/commands/generate_invoice.py | 2 +- billing/views.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/billing/management/commands/generate_invoice.py b/billing/management/commands/generate_invoice.py index 7b81135..eea0479 100644 --- a/billing/management/commands/generate_invoice.py +++ b/billing/management/commands/generate_invoice.py @@ -59,7 +59,7 @@ class Command(BaseCommand): sender = settings.DEFAULT_FROM_EMAIL send_mail( - subject="[Gna!] Payment receipt your Hostea VM", + subject="[Gna!] Payment receipt your Gna! VM", message=body, from_email=f"No reply Gna!<{sender}>", # TODO read from settings.py recipient_list=[email], diff --git a/billing/views.py b/billing/views.py index 78e6164..ff85bcf 100644 --- a/billing/views.py +++ b/billing/views.py @@ -128,7 +128,7 @@ def payment_success(request, payment_public_id): sender = settings.DEFAULT_FROM_EMAIL send_mail( - subject="[Gna!] Payment receipt your Hostea VM", + subject="[Gna!] Payment receipt your Gna! VM", message=body, from_email=f"No reply Gna!<{sender}>", # TODO read from settings.py recipient_list=[email], From 86fe9a2c59e804fc3023f699aea32c18c44d18e4 Mon Sep 17 00:00:00 2001 From: Hostea dashboard Date: Tue, 13 Sep 2022 19:57:37 +0530 Subject: [PATCH 2/2] feat: alert staff on new instance creation closes: https://gitea.gna.org/Hostea/dashboard/issues/52 --- .../emails/staff-new-instance-alert.txt | 10 +++++++ infrastructure/tests.py | 18 +++++++++++ infrastructure/utils.py | 30 +++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 infrastructure/templates/infrastructure/emails/staff-new-instance-alert.txt diff --git a/infrastructure/templates/infrastructure/emails/staff-new-instance-alert.txt b/infrastructure/templates/infrastructure/emails/staff-new-instance-alert.txt new file mode 100644 index 0000000..4fc491e --- /dev/null +++ b/infrastructure/templates/infrastructure/emails/staff-new-instance-alert.txt @@ -0,0 +1,10 @@ +Hello {{ username }}!, + + +A customer has purchased a new instance. Please find the details below: + +Gitea: {{ gitea_uri }} +Woodpecker CI: {{ woodpecker_uri }} + +Cheers, +Gna! team diff --git a/infrastructure/tests.py b/infrastructure/tests.py index b7d83e7..156f95d 100644 --- a/infrastructure/tests.py +++ b/infrastructure/tests.py @@ -17,6 +17,7 @@ from io import StringIO from django.test import TestCase, Client, override_settings from django.core.management import call_command +from django.core import mail from dash.models import Instance, InstanceConfiguration from accounts.tests import register_util, login_util @@ -37,6 +38,8 @@ class InfraUtilTest(TestCase): def setUp(self): self.username = "infrautil_user" register_util(t=self, username=self.username) + self.user.is_staff = True + self.user.save() create_configurations(t=self) # @override_settings(HOSTEA=infra_custom_config(test_name="test_path_util")) @@ -226,3 +229,18 @@ class InfraUtilTest(TestCase): print(out) self.assertEqual(instance_name in out, True) self.assertEqual(f"Payment not fulfilled for instance: {instance}" in out, True) + + staff_notification = None + for m in mail.outbox: + if "New instance alert" in m.subject: + staff_notification = m + break + self.assertEqual(staff_notification.to[0], self.email) + self.assertEqual( + "[Gna!] New instance alert" in staff_notification.subject, True + ) + self.assertEqual( + "A customer has purchased a new instance. Please find the details below:" + in staff_notification.body, + True, + ) diff --git a/infrastructure/utils.py b/infrastructure/utils.py index 8e370d4..11223f0 100644 --- a/infrastructure/utils.py +++ b/infrastructure/utils.py @@ -24,6 +24,7 @@ from time import sleep from django.utils.crypto import get_random_string from django.template.loader import render_to_string +from django.contrib.auth import get_user_model from django.core.mail import send_mail from django.conf import settings from payments import get_payment_model @@ -83,10 +84,38 @@ def create_vm_if_not_exists(instance: Instance) -> (str, str): """ Create VM utility. Gitea password is returned """ + + def notify_staff(instance: Instance): + infra = Infra() + User = get_user_model() + gitea_uri = Infra.get_gitea_uri(instance=instance) + woodpecker = Infra.get_woodpecker_uri(instance=instance) + + for staff in User.objects.filter(is_staff=True): + ctx = { + "gitea_uri": gitea_uri, + "woodpecker_uri": woodpecker, + "username": staff.username, + } + body = render_to_string( + "infrastructure/emails/staff-new-instance-alert.txt", + context=ctx, + ) + + sender = settings.DEFAULT_FROM_EMAIL + + send_mail( + subject="[Gna!] New instance alert", + message=body, + from_email=f"No reply Gna!<{sender}>", # TODO read from settings.py + recipient_list=[staff.email], + ) + infra = Infra() if not InstanceCreated.objects.filter(instance=instance).exists(): (gitea_password, commit) = infra.add_vm(instance=instance) InstanceCreated.objects.create(instance=instance, created=True) + notify_staff(instance=instance) job = Job.objects.create(instance=instance, job_type=str(JobType.PING)) Worker(job=job).start() return (gitea_password, commit) @@ -95,6 +124,7 @@ def create_vm_if_not_exists(instance: Instance) -> (str, str): infra.translate_size(instance=instance) ): # Worker.init_global() + notify_staff(instance=instance) return infra.add_vm(instance=instance) return None