You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
5.5 KiB

8 months ago
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
from django.db import models
class CustomUserManager(BaseUserManager):
def create_user(self, phone, password=None, **extra_fields):
if not phone:
raise ValueError('The Phone number must be set')
user = self.model(phone=phone, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, phone, password=None, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
if extra_fields.get('is_staff') is not True:
raise ValueError('Superuser must have is_staff=True.')
if extra_fields.get('is_superuser') is not True:
raise ValueError('Superuser must have is_superuser=True.')
return self.create_user(phone, password, **extra_fields)
class skyonnAdmin(AbstractBaseUser):
ADMIN_ID_PREFIX = 'SKYA' # Prefix for AdminId
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
phone = models.CharField(max_length=15, unique=True)
phone_number_2 = models.CharField(max_length=15, blank=True, null=True)
company_email = models.EmailField(null=True) # Additional email field
personal_email = models.EmailField(null=True) # Additional email field
address = models.TextField(null=True) # Additional address field
work_location = models.CharField(max_length=100, null=True) # Additional work location field
picture = models.ImageField(upload_to='profile_pics/', null=True) # Additional picture field
role = models.CharField(max_length=20) # Additional role field
admin_id = models.CharField(max_length=10, unique=True) # AdminId field
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
objects = CustomUserManager()
USERNAME_FIELD = 'phone'
def save(self, *args, **kwargs):
if not self.admin_id:
last_admin = skyonnAdmin.objects.order_by('-id').first()
if last_admin:
last_id = last_admin.admin_id[4:] # Extract the serial number part
new_id = int(last_id) + 1
else:
new_id = 1
self.admin_id = f'{self.ADMIN_ID_PREFIX}{new_id:03d}'
super().save(*args, **kwargs)
def __str__(self):
return self.phone
class Contact(models.Model):
client_id = models.CharField(max_length=20, null=True)
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
phone_no1 = models.CharField(max_length=20)
phone_no2 = models.CharField(max_length=20, blank=True, null=True)
company_email = models.EmailField()
designation = models.CharField(max_length=100)
department = models.CharField(max_length=100)
def __str__(self):
return f"{self.first_name} {self.last_name} - {self.company_email}"
class ClientDetails(models.Model):
parent_company = models.CharField(max_length=100)
location = models.CharField(max_length=100)
department = models.CharField(max_length=100)
gst_no = models.CharField(max_length=50)
address = models.CharField(max_length=200)
client_id = models.CharField(max_length=20, null=True)
admin_first_name = models.CharField(max_length=50)
admin_last_name = models.CharField(max_length=50)
admin_phone_no1 = models.CharField(max_length=20)
admin_phone_no2 = models.CharField(max_length=20, blank=True, null=True)
admin_company_email = models.EmailField()
admin_designation = models.CharField(max_length=100, null=True)
admin_department = models.CharField(max_length=100, null=True)
admin_location = models.CharField(max_length=100, null=True)
admin_contacts = models.ManyToManyField(Contact, related_name='admin_contacts')
hr_contacts = models.ManyToManyField(Contact, related_name='hr_contacts')
accounts_contacts = models.ManyToManyField(Contact, related_name='accounts_contacts')
recruiter_contacts = models.ManyToManyField(Contact, related_name='recruiter_contacts')
def __str__(self):
return f"{self.parent_company} - {self.location}"
class SubcompanyDetails(models.Model):
parent_company = models.CharField(max_length=100)
sub_company = models.CharField(max_length=100)
location = models.CharField(max_length=100)
department = models.CharField(max_length=100)
gst_no = models.CharField(max_length=50)
address = models.CharField(max_length=200)
client_id = models.CharField(max_length=20, null=True)
admin_first_name = models.CharField(max_length=50)
admin_last_name = models.CharField(max_length=50)
admin_phone_no1 = models.CharField(max_length=20)
admin_phone_no2 = models.CharField(max_length=20, blank=True, null=True)
admin_company_email = models.EmailField()
admin_designation = models.CharField(max_length=100, null=True)
admin_department = models.CharField(max_length=100, null=True)
admin_location = models.CharField(max_length=100, null=True)
admin_contacts = models.ManyToManyField(Contact, related_name='subcompany_admin_contacts')
hr_contacts = models.ManyToManyField(Contact, related_name='subcompany_hr_contacts')
accounts_contacts = models.ManyToManyField(Contact, related_name='subcompany_accounts_contacts')
recruiter_contacts = models.ManyToManyField(Contact, related_name='subcompany_recruiter_contacts')
def __str__(self):
return f"{self.parent_company} - {self.location}"