appcat price model
This commit is contained in:
parent
f14cc0e39e
commit
3a0cc248a7
3 changed files with 448 additions and 13 deletions
|
@ -10,6 +10,7 @@ from .models import (
|
|||
Category,
|
||||
CloudProvider,
|
||||
ComputePlan,
|
||||
ComputePlanPrice,
|
||||
ConsultingPartner,
|
||||
ExternalLink,
|
||||
ExternalLinkOffering,
|
||||
|
@ -18,6 +19,9 @@ from .models import (
|
|||
ReusableText,
|
||||
Service,
|
||||
ServiceOffering,
|
||||
VSHNAppCatBaseFee,
|
||||
VSHNAppCatPrice,
|
||||
VSHNAppCatUnitRate,
|
||||
WebsiteFaq,
|
||||
)
|
||||
|
||||
|
@ -72,10 +76,10 @@ class CategoryAdmin(admin.ModelAdmin):
|
|||
ordering = ("order", "name")
|
||||
|
||||
|
||||
class ComputePlansInline(SortableAdminMixin, admin.ModelAdmin):
|
||||
class ComputePlanItemInline(admin.TabularInline):
|
||||
model = ComputePlan
|
||||
extra = 1
|
||||
fieldsets = ((None, {"fields": ("name", "vcpus", "ram", "price_chf")}),)
|
||||
fields = ("name", "vcpus", "ram", "active", "valid_from", "valid_to")
|
||||
|
||||
|
||||
@admin.register(CloudProvider)
|
||||
|
@ -192,6 +196,12 @@ class WebsiteFaqAdmin(SortableAdminMixin, admin.ModelAdmin):
|
|||
ordering = ("order",)
|
||||
|
||||
|
||||
class ComputePlanPriceInline(admin.TabularInline):
|
||||
model = ComputePlanPrice
|
||||
extra = 1
|
||||
fields = ("currency", "amount")
|
||||
|
||||
|
||||
class ComputePlanResource(resources.ModelResource):
|
||||
cloud_provider = Field(
|
||||
column_name="cloud_provider",
|
||||
|
@ -209,14 +219,83 @@ class ComputePlanResource(resources.ModelResource):
|
|||
"vcpus",
|
||||
"ram",
|
||||
"cpu_mem_ratio",
|
||||
"price_chf",
|
||||
"cloud_provider",
|
||||
"active",
|
||||
"valid_from",
|
||||
"valid_to",
|
||||
)
|
||||
|
||||
|
||||
@admin.register(ComputePlan)
|
||||
class ComputePlansAdmin(ImportExportModelAdmin):
|
||||
resource_class = ComputePlanResource
|
||||
list_display = ("name", "cloud_provider", "vcpus", "ram", "price_chf", "active")
|
||||
search_fields = ("name", "cloud_provider__name") # Search by cloud_provider name
|
||||
list_display = (
|
||||
"name",
|
||||
"cloud_provider",
|
||||
"vcpus",
|
||||
"ram",
|
||||
"display_prices",
|
||||
"active",
|
||||
)
|
||||
search_fields = ("name", "cloud_provider__name")
|
||||
list_filter = ("active", "cloud_provider")
|
||||
ordering = ("name",)
|
||||
inlines = [ComputePlanPriceInline]
|
||||
|
||||
def display_prices(self, obj):
|
||||
prices = obj.prices.all()
|
||||
if not prices:
|
||||
return "No prices set"
|
||||
return format_html("<br>".join([f"{p.amount} {p.currency}" for p in prices]))
|
||||
|
||||
display_prices.short_description = "Prices (Amount Currency)"
|
||||
|
||||
|
||||
class VSHNAppCatBaseFeeInline(admin.TabularInline):
|
||||
model = VSHNAppCatBaseFee
|
||||
extra = 1
|
||||
fields = ("currency", "amount")
|
||||
|
||||
|
||||
class VSHNAppCatUnitRateInline(admin.TabularInline):
|
||||
model = VSHNAppCatUnitRate
|
||||
extra = 1
|
||||
fields = ("currency", "service_level", "amount")
|
||||
|
||||
|
||||
@admin.register(VSHNAppCatPrice)
|
||||
class VSHNAppCatPriceAdmin(admin.ModelAdmin):
|
||||
list_display = (
|
||||
"service",
|
||||
"variable_unit",
|
||||
"admin_display_base_fees",
|
||||
"admin_display_unit_rates",
|
||||
)
|
||||
list_filter = ("variable_unit", "service")
|
||||
search_fields = ("service__name",)
|
||||
inlines = [VSHNAppCatBaseFeeInline, VSHNAppCatUnitRateInline]
|
||||
|
||||
def admin_display_base_fees(self, obj):
|
||||
fees = obj.base_fees.all()
|
||||
if not fees:
|
||||
return "No base fees"
|
||||
return format_html(
|
||||
"<br>".join([f"{fee.amount} {fee.currency}" for fee in fees])
|
||||
)
|
||||
|
||||
admin_display_base_fees.short_description = "Base Fees"
|
||||
|
||||
def admin_display_unit_rates(self, obj):
|
||||
rates = obj.unit_rates.all()
|
||||
if not rates:
|
||||
return "No unit rates"
|
||||
return format_html(
|
||||
"<br>".join(
|
||||
[
|
||||
f"{rate.amount} {rate.currency} ({rate.get_service_level_display()})"
|
||||
for rate in rates
|
||||
]
|
||||
)
|
||||
)
|
||||
|
||||
admin_display_unit_rates.short_description = "Unit Rates"
|
||||
|
|
|
@ -0,0 +1,223 @@
|
|||
# Generated by Django 5.2 on 2025-05-20 13:25
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("services", "0022_computeplan"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterModelOptions(
|
||||
name="computeplan",
|
||||
options={"ordering": ["name"]},
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name="computeplan",
|
||||
name="price_chf",
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name="computeplan",
|
||||
name="price_eur",
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name="computeplan",
|
||||
name="price_usd",
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name="VSHNAppCatPrice",
|
||||
fields=[
|
||||
(
|
||||
"id",
|
||||
models.BigAutoField(
|
||||
auto_created=True,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
verbose_name="ID",
|
||||
),
|
||||
),
|
||||
(
|
||||
"variable_unit",
|
||||
models.CharField(
|
||||
choices=[
|
||||
("RAM", "Memory (RAM)"),
|
||||
("CPU", "CPU (vCPU)"),
|
||||
("USR", "Users"),
|
||||
],
|
||||
default="RAM",
|
||||
max_length=3,
|
||||
),
|
||||
),
|
||||
(
|
||||
"ha_replica_min",
|
||||
models.IntegerField(
|
||||
default=1, help_text="Minimum of replicas for HA"
|
||||
),
|
||||
),
|
||||
(
|
||||
"ha_replica_max",
|
||||
models.IntegerField(
|
||||
default=1, help_text="Maximum supported replicas"
|
||||
),
|
||||
),
|
||||
(
|
||||
"service",
|
||||
models.ForeignKey(
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name="vshn_appcat_price",
|
||||
to="services.service",
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name="ComputePlanPrice",
|
||||
fields=[
|
||||
(
|
||||
"id",
|
||||
models.BigAutoField(
|
||||
auto_created=True,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
verbose_name="ID",
|
||||
),
|
||||
),
|
||||
(
|
||||
"currency",
|
||||
models.CharField(
|
||||
choices=[
|
||||
("CHF", "Swiss Franc"),
|
||||
("EUR", "Euro"),
|
||||
("USD", "US Dollar"),
|
||||
],
|
||||
max_length=3,
|
||||
),
|
||||
),
|
||||
(
|
||||
"amount",
|
||||
models.DecimalField(
|
||||
decimal_places=2,
|
||||
help_text="Price in the specified currency, excl. VAT",
|
||||
max_digits=10,
|
||||
),
|
||||
),
|
||||
(
|
||||
"compute_plan",
|
||||
models.ForeignKey(
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name="prices",
|
||||
to="services.computeplan",
|
||||
),
|
||||
),
|
||||
],
|
||||
options={
|
||||
"ordering": ["currency"],
|
||||
"unique_together": {("compute_plan", "currency")},
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name="VSHNAppCatBaseFee",
|
||||
fields=[
|
||||
(
|
||||
"id",
|
||||
models.BigAutoField(
|
||||
auto_created=True,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
verbose_name="ID",
|
||||
),
|
||||
),
|
||||
(
|
||||
"currency",
|
||||
models.CharField(
|
||||
choices=[
|
||||
("CHF", "Swiss Franc"),
|
||||
("EUR", "Euro"),
|
||||
("USD", "US Dollar"),
|
||||
],
|
||||
max_length=3,
|
||||
),
|
||||
),
|
||||
(
|
||||
"amount",
|
||||
models.DecimalField(
|
||||
decimal_places=2,
|
||||
help_text="Base fee in the specified currency, excl. VAT",
|
||||
max_digits=10,
|
||||
),
|
||||
),
|
||||
(
|
||||
"vshn_appcat_price_config",
|
||||
models.ForeignKey(
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name="base_fees",
|
||||
to="services.vshnappcatprice",
|
||||
),
|
||||
),
|
||||
],
|
||||
options={
|
||||
"ordering": ["currency"],
|
||||
"unique_together": {("vshn_appcat_price_config", "currency")},
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name="VSHNAppCatUnitRate",
|
||||
fields=[
|
||||
(
|
||||
"id",
|
||||
models.BigAutoField(
|
||||
auto_created=True,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
verbose_name="ID",
|
||||
),
|
||||
),
|
||||
(
|
||||
"currency",
|
||||
models.CharField(
|
||||
choices=[
|
||||
("CHF", "Swiss Franc"),
|
||||
("EUR", "Euro"),
|
||||
("USD", "US Dollar"),
|
||||
],
|
||||
max_length=3,
|
||||
),
|
||||
),
|
||||
(
|
||||
"service_level",
|
||||
models.CharField(
|
||||
choices=[
|
||||
("BE", "Best Effort"),
|
||||
("GA", "Guaranteed Availability"),
|
||||
],
|
||||
max_length=2,
|
||||
),
|
||||
),
|
||||
(
|
||||
"amount",
|
||||
models.DecimalField(
|
||||
decimal_places=4,
|
||||
help_text="Price per unit in the specified currency and service level, excl. VAT",
|
||||
max_digits=10,
|
||||
),
|
||||
),
|
||||
(
|
||||
"vshn_appcat_price_config",
|
||||
models.ForeignKey(
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name="unit_rates",
|
||||
to="services.vshnappcatprice",
|
||||
),
|
||||
),
|
||||
],
|
||||
options={
|
||||
"ordering": ["currency", "service_level"],
|
||||
"unique_together": {
|
||||
("vshn_appcat_price_config", "currency", "service_level")
|
||||
},
|
||||
},
|
||||
),
|
||||
]
|
|
@ -356,6 +356,34 @@ class WebsiteFaq(models.Model):
|
|||
return self.question
|
||||
|
||||
|
||||
class Currency(models.TextChoices):
|
||||
CHF = "CHF", "Swiss Franc"
|
||||
EUR = "EUR", "Euro"
|
||||
USD = "USD", "US Dollar"
|
||||
|
||||
|
||||
class ComputePlanPrice(models.Model):
|
||||
compute_plan = models.ForeignKey(
|
||||
"ComputePlan", on_delete=models.CASCADE, related_name="prices"
|
||||
)
|
||||
currency = models.CharField(
|
||||
max_length=3,
|
||||
choices=Currency.choices,
|
||||
)
|
||||
amount = models.DecimalField(
|
||||
max_digits=10,
|
||||
decimal_places=2,
|
||||
help_text="Price in the specified currency, excl. VAT",
|
||||
)
|
||||
|
||||
class Meta:
|
||||
unique_together = ("compute_plan", "currency")
|
||||
ordering = ["currency"]
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.compute_plan.name} - {self.amount} {self.currency}"
|
||||
|
||||
|
||||
class ComputePlan(models.Model):
|
||||
name = models.CharField(max_length=200)
|
||||
vcpus = models.FloatField(help_text="Number of available vCPUs")
|
||||
|
@ -363,13 +391,6 @@ class ComputePlan(models.Model):
|
|||
cpu_mem_ratio = models.FloatField(
|
||||
help_text="vCPU to Memory ratio. How much vCPU per GiB RAM is available?"
|
||||
)
|
||||
price_chf = models.FloatField(help_text="Plan price in CHF excl. VAT")
|
||||
price_eur = models.FloatField(
|
||||
help_text="Plan price in EUR excl. VAT", blank=True, null=True
|
||||
)
|
||||
price_usd = models.FloatField(
|
||||
help_text="Plan price in USD excl. VAT", blank=True, null=True
|
||||
)
|
||||
active = models.BooleanField(default=True, help_text="Is the plan active?")
|
||||
|
||||
cloud_provider = models.ForeignKey(
|
||||
|
@ -380,7 +401,119 @@ class ComputePlan(models.Model):
|
|||
valid_to = models.DateTimeField(blank=True, null=True)
|
||||
|
||||
class Meta:
|
||||
ordering = ["price_chf"]
|
||||
ordering = ["name"]
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
def get_price(self, currency_code: str):
|
||||
try:
|
||||
return self.prices.get(currency=currency_code).amount
|
||||
except ComputePlanPrice.DoesNotExist:
|
||||
return None
|
||||
|
||||
|
||||
class VSHNAppCatBaseFee(models.Model):
|
||||
vshn_appcat_price_config = models.ForeignKey(
|
||||
"VSHNAppCatPrice", on_delete=models.CASCADE, related_name="base_fees"
|
||||
)
|
||||
currency = models.CharField(
|
||||
max_length=3,
|
||||
choices=Currency.choices,
|
||||
)
|
||||
amount = models.DecimalField(
|
||||
max_digits=10,
|
||||
decimal_places=2,
|
||||
help_text="Base fee in the specified currency, excl. VAT",
|
||||
)
|
||||
|
||||
class Meta:
|
||||
unique_together = ("vshn_appcat_price_config", "currency")
|
||||
ordering = ["currency"]
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.vshn_appcat_price_config.service.name} Base Fee - {self.amount} {self.currency}"
|
||||
|
||||
|
||||
class VSHNAppCatPrice(models.Model):
|
||||
class VariableUnit(models.TextChoices):
|
||||
RAM = "RAM", "Memory (RAM)"
|
||||
CPU = "CPU", "CPU (vCPU)"
|
||||
USER = "USR", "Users"
|
||||
|
||||
class ServiceLevel(models.TextChoices):
|
||||
BEST_EFFORT = "BE", "Best Effort"
|
||||
GUARANTEED = "GA", "Guaranteed Availability"
|
||||
|
||||
service = models.ForeignKey(
|
||||
Service, on_delete=models.CASCADE, related_name="vshn_appcat_price"
|
||||
)
|
||||
variable_unit = models.CharField(
|
||||
max_length=3,
|
||||
choices=VariableUnit.choices,
|
||||
default=VariableUnit.RAM,
|
||||
)
|
||||
ha_replica_min = models.IntegerField(
|
||||
default=1, help_text="Minimum of replicas for HA"
|
||||
)
|
||||
ha_replica_max = models.IntegerField(
|
||||
default=1, help_text="Maximum supported replicas"
|
||||
)
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.service.name} - {self.get_variable_unit_display()} based pricing"
|
||||
|
||||
def get_base_fee(self, currency_code: str):
|
||||
try:
|
||||
return self.base_fees.get(currency=currency_code).amount
|
||||
except VSHNAppCatBaseFee.DoesNotExist:
|
||||
return None
|
||||
|
||||
def get_unit_rate(self, currency_code: str, service_level: str):
|
||||
try:
|
||||
return self.unit_rates.get(
|
||||
currency=currency_code, service_level=service_level
|
||||
).amount
|
||||
except VSHNAppCatUnitRate.DoesNotExist:
|
||||
return None
|
||||
|
||||
def calculate_final_price(
|
||||
self, currency_code: str, service_level: str, number_of_units: int
|
||||
):
|
||||
base_fee = self.get_base_fee(currency_code)
|
||||
unit_rate = self.get_unit_rate(currency_code, service_level)
|
||||
|
||||
if base_fee is None or unit_rate is None:
|
||||
return None
|
||||
|
||||
if number_of_units < 0:
|
||||
raise ValueError("Number of units cannot be negative")
|
||||
|
||||
total_price = base_fee + (unit_rate * number_of_units)
|
||||
return total_price
|
||||
|
||||
|
||||
class VSHNAppCatUnitRate(models.Model):
|
||||
vshn_appcat_price_config = models.ForeignKey(
|
||||
VSHNAppCatPrice, on_delete=models.CASCADE, related_name="unit_rates"
|
||||
)
|
||||
currency = models.CharField(
|
||||
max_length=3,
|
||||
choices=Currency.choices,
|
||||
)
|
||||
service_level = models.CharField(
|
||||
max_length=2,
|
||||
choices=VSHNAppCatPrice.ServiceLevel.choices,
|
||||
)
|
||||
amount = models.DecimalField(
|
||||
max_digits=10,
|
||||
decimal_places=4,
|
||||
help_text="Price per unit in the specified currency and service level, excl. VAT",
|
||||
)
|
||||
|
||||
class Meta:
|
||||
unique_together = ("vshn_appcat_price_config", "currency", "service_level")
|
||||
ordering = ["currency", "service_level"]
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.vshn_appcat_price_config.service.name} - {self.get_service_level_display()} Unit Rate - {self.amount} {self.currency}"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue