Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/3585.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Change default dashboard trigger to use django signal
3 changes: 3 additions & 0 deletions python/nav/models/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
class NavModelsConfig(AppConfig):
name = 'nav.models'
verbose_name = 'NAV models'

def ready(self):
import nav.models.signals # noqa
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import nav.models.signals # noqa
import nav.models.signals # noqa: F401

just to be specific which rule you're intentionally breaking

32 changes: 32 additions & 0 deletions python/nav/models/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
signals.py
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Repeating the filename in the docstring is wholly unnecessary. However, the file lacks the license boilerplate, as mentioned in https://nav.readthedocs.io/en/latest/hacking/hacking.html#python-boilerplate-headers

Defines Django signal handlers for model events within the application.
Handles actions such as creating default dashboards and navlets when new Accounts
are created.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
are created.
are created.

"""

from django.db.models.signals import post_save
from django.dispatch import receiver
from .profiles import Account, AccountDashboard, AccountNavlet


@receiver(post_save, sender=Account)
def create_default_dashboard(sender, instance, created, **_kwargs):
"""
Signal handler that creates a default dashboard and navlets for a new Account.
"""
if created:
dashboard = AccountDashboard.objects.create(
name='My dashboard', account=instance, num_columns=4, is_default=True
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
name='My dashboard', account=instance, num_columns=4, is_default=True
name='My dashboard', account=instance, num_columns=3, is_default=True

)
default_navlets = AccountNavlet.objects.filter(account_id=0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
default_navlets = AccountNavlet.objects.filter(account_id=0)
default_navlets = AccountNavlet.objects.filter(account_id=Account.DEFAULT_ACCOUNT)

for navlet in default_navlets:
AccountNavlet.objects.create(
account=instance,
navlet=navlet.navlet,
order=navlet.order,
column=navlet.column,
preferences=navlet.preferences,
dashboard=dashboard,
)
5 changes: 5 additions & 0 deletions python/nav/models/sql/changes/sc.05.15.0002.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Drop the trigger
DROP TRIGGER IF EXISTS add_default_dashboard_on_account_create ON profiles.account;

-- Drop the function
DROP FUNCTION IF EXISTS create_new_dashboard() CASCADE;
Loading