-
Notifications
You must be signed in to change notification settings - Fork 252
/
contacts.py
204 lines (172 loc) · 5.98 KB
/
contacts.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
from django import forms
from django.contrib.contenttypes.models import ContentType
from nautobot.core.forms import DynamicModelChoiceField, DynamicModelMultipleChoiceField
from nautobot.extras.models import Role, Status
from nautobot.extras.models.contacts import Contact, ContactAssociation, Team
from .base import NautobotBulkEditForm, NautobotFilterForm, NautobotModelForm
from .mixins import TagsBulkEditFormMixin
class ContactForm(NautobotModelForm):
teams = DynamicModelMultipleChoiceField(
queryset=Team.objects.all(),
required=False,
label="Team(s)",
)
class Meta:
model = Contact
fields = [
"name",
"phone",
"email",
"address",
"teams",
"comments",
"tags",
]
def save(self, *args, **kwargs):
"""
Since `teams` field on Contact Model is the reverse side of an M2M,
we have to override save() method to explictly set the teams for the Contact instance.
"""
teams = self.cleaned_data.get("teams", [])
obj = super().save(*args, **kwargs)
obj.teams.set(teams)
return obj
class ContactBulkEditForm(TagsBulkEditFormMixin, NautobotBulkEditForm):
pk = forms.ModelMultipleChoiceField(queryset=Contact.objects.all(), widget=forms.MultipleHiddenInput())
phone = forms.CharField(required=False)
email = forms.CharField(required=False)
address = forms.CharField(required=False, widget=forms.Textarea())
class Meta:
model = Contact
class ContactFilterForm(NautobotFilterForm):
model = Contact
q = forms.CharField(required=False, label="Search")
class ObjectNewContactForm(NautobotModelForm):
teams = DynamicModelMultipleChoiceField(
queryset=Team.objects.all(),
required=False,
label="Team(s)",
)
associated_object_type = DynamicModelChoiceField(queryset=ContentType.objects.all(), required=True)
associated_object_id = forms.CharField(required=True)
role = DynamicModelChoiceField(
queryset=Role.objects.all(),
required=False,
query_params={"content_types": ContactAssociation._meta.label_lower},
)
status = DynamicModelChoiceField(
queryset=Status.objects.all(),
required=True,
query_params={"content_types": ContactAssociation._meta.label_lower},
)
class Meta:
model = Contact
fields = [
"name",
"phone",
"email",
"address",
"teams",
"comments",
"tags",
"associated_object_type",
"associated_object_id",
"role",
"status",
]
def save(self, *args, **kwargs):
"""
Since `teams` field on Contact Model is the reverse side of an M2M,
we have to override save() method to explictly set the teams for the Contact instance.
"""
teams = self.cleaned_data.get("teams", [])
obj = super().save(*args, **kwargs)
obj.teams.set(teams)
return obj
class ObjectNewTeamForm(NautobotModelForm):
contacts = DynamicModelMultipleChoiceField(
queryset=Team.objects.all(),
required=False,
label="Contact(s)",
)
associated_object_type = DynamicModelChoiceField(queryset=ContentType.objects.all(), required=True)
associated_object_id = forms.CharField(required=True)
role = DynamicModelChoiceField(
queryset=Role.objects.all(),
required=False,
query_params={"content_types": ContactAssociation._meta.label_lower},
)
status = DynamicModelChoiceField(
queryset=Status.objects.all(),
required=True,
query_params={"content_types": ContactAssociation._meta.label_lower},
)
class Meta:
model = Team
fields = [
"name",
"phone",
"email",
"address",
"contacts",
"comments",
"tags",
"associated_object_type",
"associated_object_id",
"role",
"status",
]
class ContactAssociationForm(NautobotModelForm):
contact = DynamicModelChoiceField(queryset=Contact.objects.all(), required=False)
team = DynamicModelChoiceField(queryset=Team.objects.all(), required=False)
class Meta:
model = ContactAssociation
fields = [
"contact",
"team",
"associated_object_type",
"associated_object_id",
"role",
"status",
]
class ContactAssociationBulkEditForm(NautobotBulkEditForm):
pk = forms.ModelMultipleChoiceField(queryset=ContactAssociation.objects.all(), widget=forms.MultipleHiddenInput())
role = DynamicModelChoiceField(
queryset=Role.objects.all(),
required=False,
query_params={"content_types": ContactAssociation._meta.label_lower},
)
status = DynamicModelChoiceField(
queryset=Status.objects.all(),
required=False,
query_params={"content_types": ContactAssociation._meta.label_lower},
)
class Meta:
model = ContactAssociation
class TeamForm(NautobotModelForm):
contacts = DynamicModelMultipleChoiceField(
queryset=Contact.objects.all(),
required=False,
label="Contact(s)",
)
class Meta:
model = Team
fields = [
"name",
"phone",
"email",
"address",
"contacts",
"comments",
"tags",
]
class TeamBulkEditForm(TagsBulkEditFormMixin, NautobotBulkEditForm):
pk = forms.ModelMultipleChoiceField(queryset=Team.objects.all(), widget=forms.MultipleHiddenInput())
phone = forms.CharField(required=False)
email = forms.CharField(required=False)
address = forms.CharField(required=False, widget=forms.Textarea())
class Meta:
model = Team
class TeamFilterForm(NautobotFilterForm):
model = Team
q = forms.CharField(required=False, label="Search")