feat: added promocode activation

also code reformatting and bug fixes
This commit is contained in:
ITQ
2025-01-26 11:33:03 +03:00
parent 43baa2af5c
commit 545201e4dd
11 changed files with 218 additions and 99 deletions
+20
View File
@@ -144,6 +144,23 @@ class Promocode(BaseModel):
PromocodeDurationValidator()(self)
def activate_promocode(self, user: User) -> str:
promocode: str | None = None
if self.mode == self.ModeChoices.COMMON:
promocode = self.promo_common
elif self.mode == self.ModeChoices.UNIQUE:
unused_promocodes = self.promo_unique[
len(self.promo_unique_activated) : :
]
promocode = unused_promocodes[0]
self.promo_unique_activated.append(promocode)
self.save()
PromocodeActivation.objects.create(promocode=self, user=user)
return promocode
@property
def active(self) -> bool:
current_date = timezone.datetime.today().date()
@@ -210,5 +227,8 @@ class PromocodeLike(BaseModel):
User, on_delete=models.CASCADE, related_name="liked_promocodes"
)
def __str__(self) -> str:
return f"{self.promocode.id} | {self.user.id}"
class Meta:
unique_together = ("promocode", "user")