2022-07-07 20:16:17 +00:00
|
|
|
# Copyright © 2022 Aravinth Manivannan <realaravinth@batsense.net>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.conf import settings
|
2022-09-12 11:23:43 +00:00
|
|
|
from django.core.mail import send_mail
|
|
|
|
from django.template.loader import render_to_string
|
2022-07-07 20:16:17 +00:00
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
|
|
|
|
from oauth2_provider.models import get_application_model
|
|
|
|
from oauth2_provider.generators import generate_client_id, generate_client_secret
|
|
|
|
|
|
|
|
from accounts.utils import gen_secret
|
|
|
|
from dash.models import Instance
|
2022-09-04 09:01:28 +00:00
|
|
|
from infrastructure.models import InstanceCreated
|
2022-09-12 11:23:43 +00:00
|
|
|
from billing.utils import generate_invoice, payment_fullfilled, get_invoice_link
|
2022-07-07 20:16:17 +00:00
|
|
|
|
|
|
|
Application = get_application_model()
|
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = "Generate invoices, should be run from cronjob scheduled for daily execution"
|
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
instances = Instance.objects.all()
|
|
|
|
if instances:
|
2022-09-04 09:01:28 +00:00
|
|
|
for paid_instance in InstanceCreated.objects.all():
|
|
|
|
self.stdout.write(f"Found instance: {paid_instance.instance}")
|
|
|
|
if not payment_fullfilled(instance=paid_instance.instance):
|
|
|
|
self.stdout.write(
|
|
|
|
f"Payment not fulfilled for instance: {paid_instance.instance}"
|
|
|
|
)
|
|
|
|
payment = generate_invoice(instance=paid_instance.instance)
|
2022-09-12 11:23:43 +00:00
|
|
|
owner = paid_instance.instance.owned_by
|
|
|
|
ctx = {
|
|
|
|
"username": owner.username,
|
|
|
|
"payment": payment,
|
|
|
|
"link": get_invoice_link(payment=payment),
|
|
|
|
}
|
|
|
|
|
|
|
|
body = render_to_string(
|
|
|
|
"billing/emails/renew-subscription.txt",
|
|
|
|
context=ctx,
|
|
|
|
)
|
|
|
|
|
|
|
|
email = owner.email
|
|
|
|
sender = settings.DEFAULT_FROM_EMAIL
|
|
|
|
|
|
|
|
send_mail(
|
2022-09-13 14:27:26 +00:00
|
|
|
subject="[Gna!] Payment receipt your Gna! VM",
|
2022-09-12 11:23:43 +00:00
|
|
|
message=body,
|
|
|
|
from_email=f"No reply Gna!<{sender}>", # TODO read from settings.py
|
|
|
|
recipient_list=[email],
|
|
|
|
)
|
|
|
|
|
2022-07-07 20:16:17 +00:00
|
|
|
else:
|
|
|
|
self.stdout.write("No instances available")
|