38 lines
1.2 KiB
Python
38 lines
1.2 KiB
Python
from django.db import models
|
|
from django.utils import timezone
|
|
from accounts.models import BaseModel, IAmPrincipalType
|
|
|
|
|
|
class Coupon(BaseModel):
|
|
title = models.CharField(max_length=255)
|
|
coupon_code = models.CharField(max_length=50, unique=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 __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)
|
|
)
|