You've already forked Promocode-API
mirror of
https://github.com/devitq/Promocode-API.git
synced 2026-05-23 00:27:12 +00:00
feat: added promocode activation
also code reformatting and bug fixes
This commit is contained in:
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user