Skip to content

Commit

Permalink
Added data migration to populate default statuses and default roles f…
Browse files Browse the repository at this point in the history
…or ContactAssociation model. (#5358)

* fixed #5220 and #5221

* added data migration to populate default statuses and roles for contact association

* removed unused code

* Update changes/5145.added

Co-authored-by: Gary Snider <75227981+gsnider2195@users.noreply.github.com>

* Update changes/5220.fixed

Co-authored-by: Gary Snider <75227981+gsnider2195@users.noreply.github.com>

* address PR feedback

* fix flaky tests

* refactored migration helper methods

* address PR feedback

* address PR feedback

* fix merge conflict

---------

Co-authored-by: Gary Snider <75227981+gsnider2195@users.noreply.github.com>
  • Loading branch information
HanlinMiao and gsnider2195 committed Mar 5, 2024
1 parent 1224733 commit f59b47e
Show file tree
Hide file tree
Showing 12 changed files with 302 additions and 101 deletions.
1 change: 1 addition & 0 deletions changes/5145.added
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added data migration to populate default statuses and default roles for the `ContactAssociation` model.
1 change: 1 addition & 0 deletions changes/5220.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed contacts field in "Add a new team" form not populating.
1 change: 1 addition & 0 deletions changes/5221.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed the return URL from adding/assigning a contact/team from ObjectDetailView to redirect to the contacts tab instead of the main tab.
3 changes: 2 additions & 1 deletion nautobot/core/management/commands/generate_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def _generate_factory_data(self, seed, db_name):
TagFactory,
TeamFactory,
)
from nautobot.extras.management import populate_status_choices
from nautobot.extras.management import populate_role_choices, populate_status_choices
from nautobot.extras.utils import TaggableClassesQuery
from nautobot.ipam.choices import PrefixTypeChoices
from nautobot.ipam.factory import (
Expand All @@ -100,6 +100,7 @@ def _generate_factory_data(self, seed, db_name):
factory.random.reseed_random(seed)

self.stdout.write("Creating Roles...")
populate_role_choices(verbosity=0, using=db_name)
RoleFactory.create_batch(20)
self.stdout.write("Creating Statuses...")
populate_status_choices(verbosity=0, using=db_name)
Expand Down
42 changes: 22 additions & 20 deletions nautobot/core/templates/generic/object_retrieve.html
Original file line number Diff line number Diff line change
Expand Up @@ -175,26 +175,28 @@ <h1>
<div class="table-responsive">
{% render_table associated_contacts_table 'inc/table.html' %}
</div>
<div class="panel-footer noprint">
{% if perms.extras.change_contactassociation %}
<button type="submit" name="_edit" formaction="{% url 'extras:contactassociation_bulk_edit' %}?return_url={{request.path}}" class="btn btn-warning btn-xs">
<span class="mdi mdi-pencil" aria-hidden="true"></span> Edit
</button>
{% endif %}
{% if perms.extras.delete_contactassociation %}
<button type="submit" formaction="{% url 'extras:contactassociation_bulk_delete' %}?return_url={{request.path}}" class="btn btn-danger btn-xs">
<span class="mdi mdi-trash-can-outline" aria-hidden="true"></span> Delete
</button>
{% endif %}
{% if perms.extras.add_contactassociation %}
<div class="pull-right">
<a href="{% url 'extras:object_contact_team_assign' %}?return_url={{request.path}}&associated_object_id={{object.id}}&associated_object_type={{content_type.id}}" class="btn btn-primary btn-xs">
<span class="mdi mdi-plus-thick" aria-hidden="true"></span> Add Contact
</a>
</div>
{% endif %}
<div class="clearfix"></div>
</div>
{% with request.path|add:"?tab=contacts"|urlencode as return_url %}
<div class="panel-footer noprint">
{% if perms.extras.change_contactassociation %}
<button type="submit" name="_edit" formaction="{% url 'extras:contactassociation_bulk_edit' %}?return_url={{return_url}}" class="btn btn-warning btn-xs">
<span class="mdi mdi-pencil" aria-hidden="true"></span> Edit
</button>
{% endif %}
{% if perms.extras.delete_contactassociation %}
<button type="submit" formaction="{% url 'extras:contactassociation_bulk_delete' %}?return_url={{return_url}}" class="btn btn-danger btn-xs">
<span class="mdi mdi-trash-can-outline" aria-hidden="true"></span> Delete
</button>
{% endif %}
{% if perms.extras.add_contactassociation %}
<div class="pull-right">
<a href="{% url 'extras:object_contact_team_assign' %}?return_url={{return_url}}&associated_object_id={{object.id}}&associated_object_type={{content_type.id}}" class="btn btn-primary btn-xs">
<span class="mdi mdi-plus-thick" aria-hidden="true"></span> Add Contact
</a>
</div>
{% endif %}
<div class="clearfix"></div>
</div>
{% endwith %}
</div>
</form>
</div>
Expand Down
42 changes: 22 additions & 20 deletions nautobot/dcim/templates/dcim/device.html
Original file line number Diff line number Diff line change
Expand Up @@ -362,26 +362,28 @@
<div class="table-responsive">
{% render_table associated_contacts_table 'inc/table.html' %}
</div>
<div class="panel-footer noprint">
{% if perms.extras.change_contactassociation %}
<button type="submit" name="_edit" formaction="{% url 'extras:contactassociation_bulk_edit' %}?return_url={{request.path}}" class="btn btn-warning btn-xs">
<span class="mdi mdi-pencil" aria-hidden="true"></span> Edit
</button>
{% endif %}
{% if perms.extras.delete_contactassociation %}
<button type="submit" formaction="{% url 'extras:contactassociation_bulk_delete' %}?return_url={{request.path}}" class="btn btn-danger btn-xs">
<span class="mdi mdi-trash-can-outline" aria-hidden="true"></span> Delete
</button>
{% endif %}
{% if perms.extras.add_contactassociation %}
<div class="pull-right">
<a href="{% url 'extras:object_contact_team_assign' %}?return_url={{request.path}}&associated_object_id={{object.id}}&associated_object_type={{content_type.id}}" class="btn btn-primary btn-xs">
<span class="mdi mdi-plus-thick" aria-hidden="true"></span> Add Contact
</a>
</div>
{% endif %}
<div class="clearfix"></div>
</div>
{% with request.path|add:"?tab=contacts"|urlencode as return_url %}
<div class="panel-footer noprint">
{% if perms.extras.change_contactassociation %}
<button type="submit" name="_edit" formaction="{% url 'extras:contactassociation_bulk_edit' %}?return_url={{return_url}}" class="btn btn-warning btn-xs">
<span class="mdi mdi-pencil" aria-hidden="true"></span> Edit
</button>
{% endif %}
{% if perms.extras.delete_contactassociation %}
<button type="submit" formaction="{% url 'extras:contactassociation_bulk_delete' %}?return_url={{return_url}}" class="btn btn-danger btn-xs">
<span class="mdi mdi-trash-can-outline" aria-hidden="true"></span> Delete
</button>
{% endif %}
{% if perms.extras.add_contactassociation %}
<div class="pull-right">
<a href="{% url 'extras:object_contact_team_assign' %}?return_url={{return_url}}&associated_object_id={{object.id}}&associated_object_type={{content_type.id}}" class="btn btn-primary btn-xs">
<span class="mdi mdi-plus-thick" aria-hidden="true"></span> Add Contact
</a>
</div>
{% endif %}
<div class="clearfix"></div>
</div>
{% endwith %}
</div>
</form>
</div>
Expand Down
35 changes: 35 additions & 0 deletions nautobot/extras/choices.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,41 @@ class BannerClassChoices(ChoiceSet):
)


#
# Contact Association
#


class ContactAssociationRoleChoices(ChoiceSet):
"""Role choices for contact association instances"""

ROLE_ADMINISTRATIVE = "administrative"
ROLE_BILLING = "billing"
ROLE_SUPPORT = "support"
ROLE_ON_SITE = "on site"

CHOICES = (
(ROLE_ADMINISTRATIVE, "Administrative"),
(ROLE_BILLING, "Billing"),
(ROLE_SUPPORT, "Support"),
(ROLE_ON_SITE, "On Site"),
)


class ContactAssociationStatusChoices(ChoiceSet):
"""Status choices for contact association instances"""

STATUS_PRIMARY = "primary"
STATUS_SECONDARY = "secondary"
STATUS_ACTIVE = "active"

CHOICES = (
(STATUS_PRIMARY, "Primary"),
(STATUS_SECONDARY, "Secondary"),
(STATUS_ACTIVE, "Active"),
)


#
# CustomFields
#
Expand Down
6 changes: 3 additions & 3 deletions nautobot/extras/forms/contacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class ObjectNewContactForm(NautobotModelForm):
associated_object_id = forms.CharField(required=True)
role = DynamicModelChoiceField(
queryset=Role.objects.all(),
required=False,
required=True,
query_params={"content_types": ContactAssociation._meta.label_lower},
)
status = DynamicModelChoiceField(
Expand Down Expand Up @@ -102,15 +102,15 @@ def save(self, *args, **kwargs):

class ObjectNewTeamForm(NautobotModelForm):
contacts = DynamicModelMultipleChoiceField(
queryset=Team.objects.all(),
queryset=Contact.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,
required=True,
query_params={"content_types": ContactAssociation._meta.label_lower},
)
status = DynamicModelChoiceField(
Expand Down

0 comments on commit f59b47e

Please sign in to comment.