website/hub/services/views/pricelist.py

87 lines
3.5 KiB
Python
Raw Normal View History

2025-05-22 16:34:15 +02:00
from django.shortcuts import render
from hub.services.models import ComputePlan, VSHNAppCatPrice, VSHNAppCatUnitRate
2025-05-23 08:49:08 +02:00
def pricelist(request):
2025-05-22 16:34:15 +02:00
# Get all compute plans and app catalog prices
compute_plans = (
ComputePlan.objects.all()
.select_related("cloud_provider")
.prefetch_related("prices")
)
appcat_prices = (
VSHNAppCatPrice.objects.all()
2025-05-23 08:49:08 +02:00
.select_related("service", "discount_model")
2025-05-22 16:34:15 +02:00
.prefetch_related("base_fees", "unit_rates")
)
plans_data = []
for plan in compute_plans:
plan_data = {"plan": plan, "calculated_prices": []}
for price_config in appcat_prices:
# Get all service levels for this price config
service_levels = (
VSHNAppCatUnitRate.objects.filter(vshn_appcat_price_config=price_config)
.values_list("service_level", flat=True)
.distinct()
)
# Determine number of units based on variable_unit
if price_config.variable_unit == VSHNAppCatPrice.VariableUnit.RAM:
units = int(plan.ram)
elif price_config.variable_unit == VSHNAppCatPrice.VariableUnit.CPU:
units = int(plan.vcpus)
else:
continue # Skip other unit type as we don't know yet how to handle them
# Get all currencies used in base fees
currencies = price_config.base_fees.values_list(
"currency", flat=True
).distinct()
# Calculate prices for all combinations
for service_level in service_levels:
for currency in currencies:
final_price = price_config.calculate_final_price(
currency_code=currency,
service_level=service_level,
number_of_units=units,
)
if final_price is not None:
service_level_display = dict(
VSHNAppCatPrice.ServiceLevel.choices
)[service_level]
2025-05-23 08:49:08 +02:00
# Include discount model information
discount_info = None
if (
price_config.discount_model
and price_config.discount_model.active
):
discount_info = {
"name": price_config.discount_model.name,
"description": price_config.discount_model.description,
}
2025-05-22 16:34:15 +02:00
plan_data["calculated_prices"].append(
{
"service": price_config.service.name,
"variable_unit": price_config.get_variable_unit_display(),
"service_level": service_level_display,
"units": units,
"currency": currency,
"price": final_price,
2025-05-23 08:49:08 +02:00
"plan_term": plan.get_term_display(),
"service_term": price_config.get_term_display(),
"discount_model": discount_info,
2025-05-22 16:34:15 +02:00
}
)
plans_data.append(plan_data)
context = {"plans_data": plans_data}
return render(request, "services/pricelist.html", context)