dashboard/billing/models.py

130 lines
3.9 KiB
Python
Raw Normal View History

# 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 decimal import Decimal
from typing import Iterable
from urllib.parse import urlparse, urlunparse
from django.db import models
from django.urls import reverse
from django.contrib.auth.models import User
from django.conf import settings
from payments import PurchasedItem
from payments.models import BasePayment
from dash.models import InstanceConfiguration, Instance
from accounts.utils import gen_secret
class PaymentModelManager(models.Manager):
def create(self, variant: str, instance: Instance):
owner = instance.owned_by
instance_config = instance.configuration_id
description = f"{instance_config.name} VM {instance.name} rent"
return super().create(
variant=variant,
description=description,
paid_by=owner,
total=instance_config.rent,
delivery=Decimal(0),
tax=Decimal(0),
instance_name=instance.name,
instance_configuration_id=instance_config,
currency="eur",
)
class Payment(BasePayment):
paid_by = models.ForeignKey(User, on_delete=models.CASCADE)
instance_configuration_id = models.ForeignKey(
InstanceConfiguration, on_delete=models.PROTECT
)
instance_name = models.CharField(
"Name of this Instance. Also Serves as subdomain",
max_length=200,
)
public_ref = models.CharField(
"Public referent to the payment record",
unique=True,
max_length=32,
default=gen_secret,
editable=False,
)
date = models.DateTimeField(auto_now_add=True, blank=True)
objects = PaymentModelManager()
vm_deleted = models.BooleanField(default=False, null=False)
def get_failure_url(self) -> str:
url = urlparse(settings.PAYMENT_HOST)
return urlunparse(
(
url.scheme,
url.netloc,
reverse("billing.invoice.fail", args=(self.public_ref,)),
"",
"",
"",
)
)
def get_success_url(self) -> str:
url = urlparse(settings.PAYMENT_HOST)
return urlunparse(
(
url.scheme,
url.netloc,
reverse("billing.invoice.success", args=(self.public_ref,)),
"",
"",
"",
)
)
def get_purchased_items(self) -> Iterable[PurchasedItem]:
# Return items that will be included in this payment.
yield PurchasedItem(
name=self.instance_name,
sku=self.instance_configuration_id.name,
quantity=1,
price=self.price,
currency="EUR",
)
def __str__(self):
return f"[{self.date}][{self.paid_by}][{self.instance_name}] {self.total}"
class Meta:
indexes = [
models.Index(
fields=[
"public_ref",
]
),
models.Index(
fields=[
"paid_by",
"instance_name",
]
),
models.Index(
fields=[
"paid_by",
]
),
]