from django.db import models
from django.db.models import Value, Max
from django.db.models.functions import Concat
class ContactManager(models.Manager):
def get_queryset(self):
return super().get_queryset().values_list( 'company__compname', 'position__company', 'position__jobtitle', 'position__status', \
'firstname', 'lastname', 'interactions__duration', 'interactions__durationunits',\
'interactions__interactioncatid', 'position__origination').annotate(location=Concat('position__city', Value(" "), 'position__state'),\
followupdate=Max('interactions__taskdatetime'))
class Company(models.Model):
compname = models.CharField( max_length=50, unique = True)
compwebsite = models.CharField( max_length=250, blank=True, null=True)
def __str__(self):
return self.compname
class Position(models.Model):
company = models.ForeignKey(Company, on_delete=models.CASCADE)
jobtitle = models.CharField( max_length=50)
origination = models.CharField(max_length=100, blank=True, null=True)
status = models.CharField(max_length=50, blank=True, null=True)
city = models.CharField(max_length=50, blank=True, null=True)
state = models.CharField(max_length=20, blank=True, null=True)
objects = models.Manager() # to be added to model if customer manager is being used
poscomp_objects = PosCompManager() # position and company -specific manager.
def __str__(self):
return '{} {}'.format(self.jobtitle, str(self.createdate.date()))
class Contacts(models.Model):
company = models.ForeignKey(Company, on_delete=models.CASCADE)
position = models.ForeignKey(Position, on_delete=models.CASCADE)
firstname = models.CharField( max_length=50, blank=True, null=True)
lastname = models.CharField(max_length=50, blank=True, null=True)
objects = models.Manager() # to be added to model if customer manager is being used
Contact_objects = ContactManager()
class Meta:
unique_together = [['firstname', 'lastname']]
def __str__(self):
return self.firstname + " " + self.lastname
class Interactions(models.Model):
interactionCategories = (
(1, 'Call')
,(2, 'Text')
,(3, 'Email')
,(4, 'Skype')
)
contact = models.ForeignKey(Contacts, on_delete=models.CASCADE)
interactioncatid = models.IntegerField(choices=interactionCategories, default=3) # choices
taskdatetime = models.DateTimeField( blank=True, null=True)
duration = models.IntegerField(blank=True, null=True)
durationunits = models.CharField(max_length=20, blank=True, null=True)
objects = models.Manager()
def __str__(self):
return str(self.interactioncatid)