HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //home/arjun/projects/unlimited-leads/Unlimited-Leads-Be/user/models.py
import uuid
from django.db import models
from django.db.models import (
    UUIDField,
    ForeignKey,
    IntegerField,
    BigIntegerField,
    Model,
    CASCADE,
    SET_NULL,
    BooleanField,
    CharField,
    DateTimeField,
    JSONField,
)
from django.utils import timezone


from authorization.models import UnlimitedLeadUser

# Create your models here.
class EmailOrPhoneVerifyFileUpload(models.Model):
    NOTIFICATION_TYPE_CHOICES = [
        ('none', 'None'),
        ('email', 'Email'),
        ('sms', 'SMS'),
        ('both', 'Both'),
    ]
    PAYMENT_MODE_CHOICES = [
        ('credit', 'Credit'),
        ('normal', 'Normal Payment'),
    ]
    user = models.ForeignKey(UnlimitedLeadUser, on_delete=models.CASCADE)
    payment_status = models.BooleanField(default=False) 
    file_key = models.CharField(max_length=255, unique=True)
    status = models.CharField(max_length=20, default="uploaded", choices=[
        ('uploaded', 'Uploaded'),
        ('processing', 'Processing'),
        ('completed', 'Completed'),
        ('failed', 'Failed')
    ])
    uploaded_at = models.DateTimeField(auto_now_add=True)
    processed_at = models.DateTimeField(null=True, blank=True)
    transactionID = models.CharField(max_length=255,null=True, blank=True)
    type = models.CharField(max_length=255,null=True, blank=True)
    email_count = models.IntegerField(default=0,blank=True,null=True)
    phone_num_count = models.IntegerField(default=0,blank=True,null=True)
    uploaded_file_name = models.CharField(default='Data from Input Field',max_length=255,null=True, blank=True)
    amount_paid = models.FloatField(default=0,blank=True,null=True)
    notification_type = models.CharField(max_length=10, choices=NOTIFICATION_TYPE_CHOICES, default='none',blank=True,null=True)
    credit_used = models.IntegerField(default=0, blank=True, null=True)
    credit_to_return = models.IntegerField(default=0, blank=True, null=True)
    remaining_credit = models.IntegerField(default=0, blank=True, null=True)
    payment_mode = models.CharField(max_length=10, choices=PAYMENT_MODE_CHOICES, default='normal', blank=True, null=True)
    
    def __str__(self):
        return f"File Upload {self.id} - {self.user.username} - {self.status}"


class EmailOrPhoneVerifyProcessedFile(models.Model):
    NOTIFICATION_TYPE_CHOICES = [
        ('none', 'None'),
        ('email', 'Email'),
        ('sms', 'SMS'),
        ('both', 'Both'),
    ]
    file_key = models.CharField(max_length=255)
    user = models.ForeignKey(UnlimitedLeadUser, on_delete=models.CASCADE)  # Assuming `user` is the logged-in user
    total_deliverable_emails = models.IntegerField(default=0,null=True,blank=True)
    total_valid_phone_numbers = models.IntegerField(default=0,null=True,blank=True)
    processed_at = models.DateTimeField(auto_now_add=True)
    total_email_count = models.IntegerField(default=0,blank=True,null=True)
    total_phone_num_count = models.IntegerField(default=0,blank=True,null=True)
    type = models.CharField(max_length=255,null=True, blank=True)
    uploaded_file_name = models.CharField(default='Data from Input Field',max_length=255,null=True, blank=True)
    amount_paid = models.IntegerField(default=0,blank=True,null=True)
    notification_type = models.CharField(max_length=10, choices=NOTIFICATION_TYPE_CHOICES, default='none',blank=True,null=True)

    def __str__(self):
        return f"Processed File {self.id} - User: {self.user.username} - {self.total_deliverable_emails} emails, {self.total_valid_phone_numbers} phone numbers"
    

class ThirdPartyServicePerRequestCost(models.Model):
    service = models.CharField(max_length=10)
    amount = models.FloatField()
    currency = models.CharField(default='usd')

class UserLeadsSearchUsage(Model):
    """
        Track user search usage, reset when yearly, monthly subscription added,
        if free plan celery scheduler reset on per day basis.
    """

    id = UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    user = ForeignKey(UnlimitedLeadUser, on_delete=CASCADE)
    usage_reset_at = DateTimeField(default=timezone.now)
    usage_count = IntegerField(default=0)
    created_at = DateTimeField(auto_now_add=True)
    updated_at = DateTimeField(auto_now=True)

    def __str__(self):
        return f"{self.user}-{self.usage_count}"


class UserLeadSearch(Model):
    NO = 0
    EMAIL = 1
    SMS = 2
    BOTH = 3

    NOTIFICATION_CHOICES = (
        (NO, "No"),
        (EMAIL, "Email"),
        (SMS, "Sms"),
        (BOTH, "Both"),
    )

    id = UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    user = ForeignKey(UnlimitedLeadUser, on_delete=CASCADE)
    new_records_found = BooleanField(default=False)
    title = CharField(max_length=255, default="")
    receive_notification = IntegerField(default=NO, choices=NOTIFICATION_CHOICES)
    lead_version = IntegerField(default=1)
    leads_search_count = BigIntegerField(default=0)
    search_filter_parameter = JSONField(default=dict)
    is_saved = BooleanField(default=False)
    scrapped_search_id = IntegerField(blank=True,null=True)
    download_key = CharField(max_length=255, default="")
    is_upload_success = BooleanField(default=False)
    is_deleted = BooleanField(default=False)
    created_at = DateTimeField(auto_now_add=True)
    updated_at = DateTimeField(auto_now=True)

    def __str__(self) -> str:
        return f"{self.user}: {self.title}"
    

class ScrappingSearchTask(models.Model):
    LEAD_TYPE_CHOICES = [
        ("Business", "Business"),
        ("Consumer", "Consumer"),
    ]
    NOTIFICATION_TYPE_CHOICES = [
        ('none', 'None'),
        ('email', 'Email'),
        ('sms', 'SMS'),
        ('both', 'Both'),
    ]

    user = models.ForeignKey(UnlimitedLeadUser, on_delete=models.CASCADE)
    search_keyword = models.CharField(max_length=255,null=True,blank=True)
    lead_type = models.CharField(max_length=50, choices=LEAD_TYPE_CHOICES)
    location = models.CharField(max_length=255,null=True,blank=True)
    task_status = models.CharField(max_length=50, default="Pending")  # E.g., Pending, In Progress, Completed
    created_at = models.DateTimeField(auto_now_add=True)
    notification_type = models.CharField(max_length=10, choices=NOTIFICATION_TYPE_CHOICES, default='email',blank=True,null=True)
    download_key = models.CharField(max_length=225,null=True,blank=True)

    def __str__(self):
        return f"Task {self.id} - {self.search_keyword}"

# Second Table: SearchResult
class SrcappingSearchResult(models.Model):
    user = models.ForeignKey(UnlimitedLeadUser, on_delete=models.CASCADE)
    task = models.ForeignKey(ScrappingSearchTask, on_delete=models.CASCADE, related_name="results")
    website_name = models.CharField(max_length=255)
    website_link = models.URLField()
    phone_number = models.TextField(null=True, blank=True)
    email = models.TextField(null=True, blank=True)

    def __str__(self):
        return f"Result for Task {self.task.id}"