80 lines
3.0 KiB
Python
80 lines
3.0 KiB
Python
from django.db import models
|
|
from django.utils import timezone
|
|
from accounts.models import BaseModel, IAmPrincipalType
|
|
from django.core.exceptions import ValidationError
|
|
|
|
|
|
class Coupon(BaseModel):
|
|
title = models.CharField(max_length=255)
|
|
coupon_code = models.CharField(max_length=50, unique=True)
|
|
coupon_id = models.CharField(max_length=255, blank=True, null=True)
|
|
no_of_redeems = models.IntegerField(default=0)
|
|
description = models.TextField(null=True, blank=True)
|
|
image = models.ImageField(upload_to="coupon_img", null=True, blank=True)
|
|
discount_amount = models.DecimalField(
|
|
max_digits=10, decimal_places=2, null=True, blank=True
|
|
)
|
|
discount_percentage = models.DecimalField(
|
|
max_digits=5, decimal_places=2, null=True, blank=True
|
|
)
|
|
valid_from = models.DateTimeField()
|
|
valid_to = models.DateTimeField()
|
|
max_redeems = models.IntegerField(default=0)
|
|
|
|
class Meta:
|
|
db_table = "coupon"
|
|
|
|
def clean(self):
|
|
"""
|
|
Validate the Coupon instance. Ensure that the `max_redeems` is greater than 0,
|
|
that either `discount_amount` or `discount_percentage` is set, and that
|
|
`valid_from` is earlier than `valid_to`.
|
|
"""
|
|
if self.max_redeems < 1:
|
|
raise ValidationError({"max_redeems": "Redeems must be more than 1."})
|
|
|
|
# Ensure discount_amount is non-negative
|
|
if self.discount_amount is not None and self.discount_amount < 1:
|
|
raise ValidationError(
|
|
{"discount_amount": "Discount amount must be more than 1."}
|
|
)
|
|
|
|
# Ensure discount_percentage is non-negative
|
|
if self.discount_percentage is not None and self.discount_percentage < 1:
|
|
raise ValidationError(
|
|
{"discount_percentage": "Discount percentage must be more than 1."}
|
|
)
|
|
|
|
if self.discount_amount and self.discount_percentage:
|
|
raise ValidationError(
|
|
"You can only set either a discount amount or a discount percentage, not both."
|
|
)
|
|
|
|
if not self.discount_amount and not self.discount_percentage:
|
|
raise ValidationError(
|
|
"You must set either a discount amount or a discount percentage."
|
|
)
|
|
|
|
if self.valid_from and self.valid_to and self.valid_from >= self.valid_to:
|
|
raise ValidationError(
|
|
"The valid_from date must be earlier than the valid_to date."
|
|
)
|
|
|
|
def save(self, *args, **kwargs):
|
|
self.clean() # Call clean before saving to ensure validation
|
|
super().save(*args, **kwargs)
|
|
|
|
def __str__(self):
|
|
return self.coupon_code
|
|
|
|
# If max_redeems is 0, it means that we are allowing unlimited redeems
|
|
|
|
def is_valid(self):
|
|
now = timezone.now()
|
|
return (
|
|
self.active
|
|
and not self.deleted
|
|
and self.valid_from <= now <= self.valid_to
|
|
and (self.max_redeems == 0 or self.no_of_redeems < self.max_redeems)
|
|
)
|