introduce plan ordering and marking as best

This commit is contained in:
Tobias Brunner 2025-06-23 13:13:27 +02:00
parent 9e0ccb6025
commit c93f8de717
No known key found for this signature in database
4 changed files with 132 additions and 21 deletions

View file

@ -139,16 +139,42 @@ class Plan(models.Model):
ServiceOffering, on_delete=models.CASCADE, related_name="plans"
)
# Ordering and highlighting fields
order = models.PositiveIntegerField(
default=0,
help_text="Order of this plan in the offering (lower numbers appear first)",
)
is_best = models.BooleanField(
default=False, help_text="Mark this plan as the best/recommended option"
)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
ordering = ["name"]
ordering = ["order", "name"]
unique_together = [["offering", "name"]]
def __str__(self):
return f"{self.offering} - {self.name}"
def clean(self):
# Ensure only one plan per offering can be marked as "best"
if self.is_best:
existing_best = Plan.objects.filter(
offering=self.offering, is_best=True
).exclude(pk=self.pk)
if existing_best.exists():
from django.core.exceptions import ValidationError
raise ValidationError(
"Only one plan per offering can be marked as the best option."
)
def save(self, *args, **kwargs):
self.clean()
super().save(*args, **kwargs)
def get_price(self, currency_code: str):
price_obj = PlanPrice.objects.filter(plan=self, currency=currency_code).first()
if price_obj: