184 lines
6.6 KiB
Python
184 lines
6.6 KiB
Python
import uuid
|
|
from django.db import models
|
|
import random
|
|
import string
|
|
from accounts.models import BaseModel, IAmPrincipal, IAmPrincipalType
|
|
from manage_subscriptions.models import PrincipalSubscription, Subscription
|
|
|
|
|
|
# Create your models here.
|
|
class ReferralCode(BaseModel):
|
|
principal = models.ForeignKey(
|
|
IAmPrincipal, related_name="principal_referral", on_delete=models.CASCADE
|
|
)
|
|
principal_type = models.ForeignKey(
|
|
IAmPrincipalType,
|
|
related_name="principal_type_referral",
|
|
on_delete=models.CASCADE,
|
|
)
|
|
referral_code = models.CharField(max_length=50, unique=True)
|
|
|
|
class Meta:
|
|
db_table = "referral_code"
|
|
|
|
def __str__(self):
|
|
return f"{self.principal.first_name}"
|
|
|
|
@classmethod
|
|
def filter_referral_code(cls, principal):
|
|
return cls.objects.filter(principal=principal)
|
|
|
|
@classmethod
|
|
def generate_referral_code(cls, type, name):
|
|
"""
|
|
Generate a Referral Code.
|
|
|
|
This method generates a unique referral code based on the provided 'type' and 'name'.
|
|
|
|
Process:
|
|
1. Ensure 'type' and 'name' are converted to uppercase.
|
|
2. Create a 4-digit random number.
|
|
3. Combine 'type', the first 3 characters of 'name' (or 'NIF' if empty), and the random number to form a code.
|
|
4. Check if the generated code already exists in the database. If not, return it as a unique referral code.
|
|
5. Repeat the process until a unique code is generated.
|
|
|
|
The method ensures each generated code is unique to avoid conflicts.
|
|
"""
|
|
# type = type.upper()
|
|
type = type.replace("_", "").upper()
|
|
name = name[:3].upper() if name else "GDTM"
|
|
while True:
|
|
random_number = "".join(random.choice(string.digits) for _ in range(4))
|
|
code = f"{type}{name}{random_number}"
|
|
if not ReferralCode.objects.filter(referral_code=code).exists():
|
|
return code
|
|
|
|
@classmethod
|
|
def create_referral_code_for_user_manager(cls, principal, principal_type):
|
|
"""
|
|
Create Referral Codes for Player and Merchant Principals.
|
|
|
|
Process:
|
|
1. Determine the principal types for Player and Merchant.
|
|
2. Check if referral codes for Player and Merchant already exist for the principal.
|
|
3. If not, create referral codes for Player and Merchant, associating them with the principal.
|
|
4. Referral codes are generated based on the principal's first name and type (e.g., "PLA" for Player, "MER" for Merchant).
|
|
5. Save the created referral codes in the database.
|
|
|
|
This method ensures that a user has referral codes for both Player and Merchant roles when they don't already exist.
|
|
"""
|
|
user_type = IAmPrincipalType.get_principal_type(principal_type)
|
|
|
|
user_code = cls.objects.filter(
|
|
principal=principal,
|
|
principal_type=user_type,
|
|
).first()
|
|
|
|
if not user_code:
|
|
user_code = cls(
|
|
principal=principal,
|
|
principal_type=user_type,
|
|
referral_code=cls.generate_referral_code(
|
|
type=str(user_type).upper(), name=principal.first_name
|
|
),
|
|
)
|
|
user_code.save()
|
|
|
|
def save(self, *args, **kwargs):
|
|
super(ReferralCode, self).save(*args, **kwargs)
|
|
|
|
|
|
class ReferralRecord(BaseModel):
|
|
referrer_principal = models.ForeignKey(
|
|
IAmPrincipal,
|
|
on_delete=models.CASCADE,
|
|
related_name="referrals_by_referrer",
|
|
help_text="The principal who referred someone",
|
|
)
|
|
referred_principal = models.ForeignKey(
|
|
IAmPrincipal,
|
|
on_delete=models.CASCADE,
|
|
related_name="referrals_by_referred",
|
|
help_text="The principal who was referred",
|
|
)
|
|
principal_type = models.ForeignKey(
|
|
IAmPrincipalType,
|
|
on_delete=models.CASCADE,
|
|
help_text="The type of principal associated with this referral",
|
|
)
|
|
is_completed = models.BooleanField(
|
|
default=False, help_text="Indicates whether the referral is completed"
|
|
)
|
|
|
|
class Meta:
|
|
db_table = "referral_record"
|
|
|
|
def __str__(self):
|
|
return f"Reward ID: {self.id}, Referrar: {self.referrer_principal.first_name}, Referred to: {self.referred_principal.first_name}"
|
|
|
|
@classmethod
|
|
def filter_invite_records(cls, referrer_principal):
|
|
record_instance = cls.objects.filter(
|
|
referrer_principal=referrer_principal,
|
|
)
|
|
return record_instance
|
|
|
|
@classmethod
|
|
def get_invite_count(cls, referrer_principal):
|
|
filter_record = cls.filter_invite_records(referrer_principal)
|
|
return filter_record.count()
|
|
|
|
|
|
class ReferralRecordReward(BaseModel):
|
|
referral_record = models.ForeignKey(
|
|
ReferralRecord, on_delete=models.CASCADE, related_name="record_reward"
|
|
)
|
|
subscription = models.ForeignKey(
|
|
Subscription, on_delete=models.CASCADE, related_name="subscription_reward"
|
|
)
|
|
sell = models.BooleanField(default=False)
|
|
withdraw = models.BooleanField(default=False)
|
|
coins = models.PositiveBigIntegerField(default=1)
|
|
unique_token = models.UUIDField(
|
|
default=uuid.uuid4, editable=False
|
|
)
|
|
value = models.DecimalField(max_digits=14, decimal_places=2)
|
|
|
|
class Meta:
|
|
db_table = "referral_record_reward"
|
|
|
|
|
|
class ReferralTracking(BaseModel):
|
|
referral_record = models.ForeignKey(
|
|
ReferralRecord, on_delete=models.CASCADE, related_name="record_tracking"
|
|
)
|
|
referrer_subscription_id = models.PositiveIntegerField(null=True, blank=True)
|
|
referred_subscription_id = models.PositiveIntegerField(null=True, blank=True)
|
|
is_referrer_subscribed = models.BooleanField(
|
|
default=False,
|
|
help_text="is referrer subscribed at the time of referred subscription",
|
|
)
|
|
ip_address = models.GenericIPAddressField(blank=True, null=True)
|
|
user_agent = models.CharField(max_length=255, blank=True, null=True)
|
|
device_model = models.CharField(max_length=100, blank=True, null=True)
|
|
|
|
class Meta:
|
|
db_table = "referral_tracking"
|
|
|
|
def __str__(self):
|
|
return f"Referral Record ID: {self.referral_record.id}"
|
|
|
|
|
|
class GoodTimeCoins(BaseModel):
|
|
coins = models.IntegerField(default=1)
|
|
value_in_pound = models.DecimalField(max_digits=10, decimal_places=2)
|
|
comments = models.CharField(max_length=255, blank=True, null=True)
|
|
start_date = models.DateField(blank=True, null=True)
|
|
end_date = models.DateField(blank=True, null=True)
|
|
|
|
def __str__(self):
|
|
return f"Coins: {self.coins}, Value in Pound: {self.value_in_pound}"
|
|
|
|
class Meta:
|
|
db_table = "good_time_coins"
|