forked from MicroPyramid/Django-CRM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
153 lines (129 loc) · 5.71 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import arrow
from django.db import models
from django.utils.translation import pgettext_lazy
from django.utils.translation import ugettext_lazy as _
from common.models import User, Company
from common.utils import INDCHOICES, COUNTRIES
from phonenumber_field.modelfields import PhoneNumberField
from django.utils.text import slugify
from contacts.models import Contact
from teams.models import Teams
from common import utils
class Tags(models.Model):
name = models.CharField(max_length=20)
slug = models.CharField(max_length=20, unique=True, blank=True)
def save(self, *args, **kwargs):
self.slug = slugify(self.name)
super(Tags, self).save(*args, **kwargs)
class Account(models.Model):
ACCOUNT_STATUS_CHOICE = (("open", "Open"), ("close", "Close"))
name = models.CharField(pgettext_lazy("Name of Account", "Name"), max_length=64)
email = models.EmailField()
phone = PhoneNumberField(null=True)
industry = models.CharField(
_("Industry Type"), max_length=255, choices=INDCHOICES, blank=True, null=True
)
# billing_address = models.ForeignKey(
# Address, related_name='account_billing_address', on_delete=models.CASCADE, blank=True, null=True)
# shipping_address = models.ForeignKey(
# Address, related_name='account_shipping_address', on_delete=models.CASCADE, blank=True, null=True)
billing_address_line = models.CharField(
_("Address"), max_length=255, blank=True, null=True
)
billing_street = models.CharField(_("Street"), max_length=55, blank=True, null=True)
billing_city = models.CharField(_("City"), max_length=255, blank=True, null=True)
billing_state = models.CharField(_("State"), max_length=255, blank=True, null=True)
billing_postcode = models.CharField(
_("Post/Zip-code"), max_length=64, blank=True, null=True
)
billing_country = models.CharField(
max_length=3, choices=COUNTRIES, blank=True, null=True
)
website = models.URLField(_("Website"), blank=True, null=True)
description = models.TextField(blank=True, null=True)
created_by = models.ForeignKey(
User, related_name="account_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)
status = models.CharField(
choices=ACCOUNT_STATUS_CHOICE, max_length=64, default="open"
)
lead = models.ForeignKey(
"leads.Lead", related_name="account_leads", on_delete=models.SET_NULL, null=True
)
contact_name = models.CharField(
pgettext_lazy("Name of Contact", "Contact Name"), max_length=120
)
contacts = models.ManyToManyField(
"contacts.Contact", related_name="account_contacts"
)
assigned_to = models.ManyToManyField(User, related_name="account_assigned_users")
teams = models.ManyToManyField(Teams, related_name="account_teams")
company = models.ForeignKey(
Company, on_delete=models.SET_NULL, null=True, blank=True
)
def __str__(self):
return self.name
class Meta:
ordering = ["-created_on"]
def get_complete_address(self):
"""Concatenates complete address."""
address = ""
add_to_address = [
self.billing_street,
self.billing_city,
self.billing_state,
self.billing_postcode,
self.get_billing_country_display(),
]
address = utils.append_str_to(address, *add_to_address)
return address
@property
def created_on_arrow(self):
return arrow.get(self.created_on).humanize()
@property
def contact_values(self):
contacts = list(self.contacts.values_list("id", flat=True))
return ",".join(str(contact) for contact in contacts)
@property
def get_team_users(self):
team_user_ids = list(self.teams.values_list("users__id", flat=True))
return User.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 User.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 User.objects.filter(id__in=list(user_ids))
class Email(models.Model):
from_account = models.ForeignKey(
Account, related_name="sent_email", on_delete=models.SET_NULL, null=True
)
recipients = models.ManyToManyField(Contact, related_name="recieved_email")
message_subject = models.TextField(null=True)
message_body = models.TextField(null=True)
timezone = models.CharField(max_length=100, default="UTC")
scheduled_date_time = models.DateTimeField(null=True)
scheduled_later = models.BooleanField(default=False)
created_on = models.DateTimeField(auto_now_add=True)
from_email = models.EmailField()
rendered_message_body = models.TextField(null=True)
def __str__(self):
return self.message_subject
class EmailLog(models.Model):
""" this model is used to track if the email is sent or not """
email = models.ForeignKey(
Email, related_name="email_log", on_delete=models.SET_NULL, null=True
)
contact = models.ForeignKey(
Contact, related_name="contact_email_log", on_delete=models.SET_NULL, null=True
)
is_sent = models.BooleanField(default=False)