|
from tortoise import fields |
|
from tortoise.models import Model |
|
from datetime import datetime, timedelta |
|
|
|
|
|
class Plan(Model): |
|
id = fields.UUIDField(pk=True) |
|
name = fields.CharField( |
|
max_length=100, unique=True, description="Name of the subscription plan" |
|
) |
|
amount = fields.DecimalField( |
|
max_digits=10, decimal_places=2, description="Cost of the plan" |
|
) |
|
duration = fields.IntField(description="Duration of the subscription in hours") |
|
download_speed = fields.FloatField(description="Download speed in Mbps") |
|
upload_speed = fields.FloatField(description="Upload speed in Mbps") |
|
expire_date = fields.DatetimeField( |
|
default=None, null=True, description="Expiration date of the plan" |
|
) |
|
is_promo = fields.BooleanField( |
|
default=False, description="Indicates if the plan is a promotional plan" |
|
) |
|
promo_duration_days = fields.IntField( |
|
default=None, null=True, description="Number of days the promotion is valid" |
|
) |
|
is_valid = fields.BooleanField( |
|
default=True, description="Indicates if the plan is valid" |
|
) |
|
|
|
class Meta: |
|
table = "plans" |
|
|
|
async def set_expiration(self): |
|
"""Set the expiration date based on the promo duration if applicable.""" |
|
if self.is_promo and self.promo_duration_days: |
|
self.expire_date = datetime.now() + timedelta(days=self.promo_duration_days) |
|
elif self.duration: |
|
self.expire_date = datetime.now() + timedelta( |
|
days=self.duration // 24 |
|
) |
|
await self.save() |
|
|
|
async def is_promo_valid(self) -> bool: |
|
"""Check if the promotional plan is still valid.""" |
|
if self.is_promo and self.expire_date: |
|
return datetime.now() < self.expire_date.replace(tzinfo=None) |
|
self.is_valid = False |
|
await self.save() |
|
return False |
|
|