-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
82 lines (73 loc) · 3.16 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import arrow
from django.db import models
from django.utils.translation import pgettext_lazy
from django.utils.translation import ugettext_lazy as _
from accounts.models import Account, Tags
from contacts.models import Contact
from common.models import Org, Profile
from common.utils import STAGES, SOURCES, CURRENCY_CODES
from teams.models import Teams
class Opportunity(models.Model):
name = models.CharField(pgettext_lazy("Name of Opportunity", "Name"), max_length=64)
account = models.ForeignKey(
Account,
related_name="opportunities",
on_delete=models.CASCADE,
blank=True,
null=True,
)
stage = models.CharField(
pgettext_lazy("Stage of Opportunity", "Stage"), max_length=64, choices=STAGES
)
currency = models.CharField(
max_length=3, choices=CURRENCY_CODES, blank=True, null=True
)
amount = models.DecimalField(
_("Opportunity Amount"), decimal_places=2, max_digits=12, blank=True, null=True
)
lead_source = models.CharField(
_("Source of Lead"), max_length=255, choices=SOURCES, blank=True, null=True
)
probability = models.IntegerField(default=0, blank=True, null=True)
contacts = models.ManyToManyField(Contact)
closed_by = models.ForeignKey(Profile, on_delete=models.SET_NULL, null=True, blank=True, related_name="oppurtunity_closed_by")
# closed_on = models.DateTimeField(blank=True, null=True)
closed_on = models.DateField(blank=True, null=True)
description = models.TextField(blank=True, null=True)
assigned_to = models.ManyToManyField(Profile, related_name="opportunity_assigned_to")
created_by = models.ForeignKey(
Profile,
related_name="opportunity_created_by",
on_delete=models.SET_NULL,
null=True,
)
created_on = models.DateTimeField(_("Created on"), auto_now_add=True)
is_active = models.BooleanField(default=False)
tags = models.ManyToManyField(Tags, blank=True)
teams = models.ManyToManyField(Teams, related_name="oppurtunity_teams")
org = models.ForeignKey(
Org, on_delete=models.SET_NULL, null=True, blank=True, related_name="oppurtunity_org"
)
class Meta:
ordering = ["-created_on"]
def __str__(self):
return self.name
@property
def created_on_arrow(self):
return arrow.get(self.created_on).humanize()
@property
def get_team_users(self):
team_user_ids = list(self.teams.values_list("users__id", flat=True))
return Profile.objects.filter(id__in=team_user_ids)
@property
def get_team_and_assigned_users(self):
team_user_ids = list(self.teams.values_list("users__id", flat=True))
assigned_user_ids = list(self.assigned_to.values_list("id", flat=True))
user_ids = team_user_ids + assigned_user_ids
return Profile.objects.filter(id__in=user_ids)
@property
def get_assigned_users_not_in_teams(self):
team_user_ids = list(self.teams.values_list("users__id", flat=True))
assigned_user_ids = list(self.assigned_to.values_list("id", flat=True))
user_ids = set(assigned_user_ids) - set(team_user_ids)
return Profile.objects.filter(id__in=list(user_ids))