Skip to content

Commit c7cebd9

Browse files
author
John-Magne Bredal
committed
Save dashboard columns when choosing columns
1 parent c44c194 commit c7cebd9

8 files changed

Lines changed: 49 additions & 30 deletions

File tree

htdocs/static/js/src/webfront.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,11 @@ require([
111111

112112
$('.column-chooser').click(function() {
113113
$navletsContainer.empty();
114-
new NavletsController($navletsContainer, $(this).data('columns'));
114+
var columns = $(this).data('columns');
115+
new NavletsController($navletsContainer, columns);
115116
// Save number of columns
117+
var url = $(this).closest('.button-group').data('url');
118+
var request = $.post(url, {num_columns: columns});
116119
});
117120

118121

python/nav/models/profiles.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,6 +1368,7 @@ class AccountDashboard(models.Model):
13681368
"""Stores dashboards for each user"""
13691369
name = VarcharField()
13701370
is_default = models.BooleanField(default=False)
1371+
num_columns = models.IntegerField(default=3)
13711372
account = models.ForeignKey(Account)
13721373

13731374
def __unicode__(self):

python/nav/web/navlets/__init__.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
from nav.models.manage import Sensor
7474
from nav.django.auth import get_sudoer
7575
from nav.django.utils import get_account
76-
from nav.web.webfront import get_widget_columns
76+
from nav.web.webfront import get_widget_columns, find_dashboard
7777

7878
_logger = logging.getLogger(__name__)
7979

@@ -192,11 +192,8 @@ def get_navlet_from_name(navletmodule):
192192

193193
def get_user_navlets(request, dashboard_id=None):
194194
"""Gets all navlets that this user subscribes to for a given dashboard"""
195-
account = request.account
196-
kwargs = {'pk': dashboard_id} if dashboard_id else {'is_default': True}
197-
dash = AccountDashboard.objects.get(account=account, **kwargs)
198-
199-
usernavlets = dash.widgets.all()
195+
dashboard = find_dashboard(request.account, dashboard_id)
196+
usernavlets = dashboard.widgets.all()
200197

201198
navlets = []
202199
for usernavlet in usernavlets:
@@ -256,12 +253,11 @@ def add_user_navlet(request, dashboard_id=None):
256253
"""Add a navlet subscription to this user"""
257254
if request.method == 'POST' and 'navlet' in request.POST:
258255
account = request.account
259-
kwargs = {'pk': dashboard_id} if dashboard_id else {'is_default': True}
260-
dash = AccountDashboard.objects.get(account=account, **kwargs)
256+
dashboard = find_dashboard(account, dashboard_id=dashboard_id)
261257

262258
if can_modify_navlet(account, request):
263259
navlet_class = request.POST.get('navlet')
264-
navlet = add_navlet(account, navlet_class, dashboard=dash)
260+
navlet = add_navlet(account, navlet_class, dashboard=dashboard)
265261
return HttpResponse(json.dumps(create_navlet_object(navlet)),
266262
content_type="application/json")
267263

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,30 @@
11
"""NAV web common package."""
22

3+
from django.db.models import Count
4+
5+
from nav.models.profiles import AccountDashboard
6+
37
DEFAULT_WIDGET_COLUMNS = 2
48

9+
510
def get_widget_columns(account):
611
"""Get the preference for widget columns"""
712
return int(account.preferences.get(account.PREFERENCE_KEY_WIDGET_COLUMNS,
813
DEFAULT_WIDGET_COLUMNS))
14+
15+
16+
def find_dashboard(account, dashboard_id=None):
17+
"""Find a dashboard for this account
18+
19+
Either find a specific one or the default one. If none of those exist we
20+
find the one with the most widgets.
21+
"""
22+
kwargs = {'pk': dashboard_id} if dashboard_id else {'is_default': True}
23+
try:
24+
dashboard = AccountDashboard.objects.get(account=account, **kwargs)
25+
except AccountDashboard.DoesNotExist:
26+
# No default dashboard? Find the one with the most widgets
27+
dashboard = AccountDashboard.objects.filter(account=account).annotate(
28+
Count('widgets')).order_by('-widgets__count')[0]
29+
30+
return dashboard

python/nav/web/webfront/urls.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
name='rename-dashboard'),
3232
url(r'^index/dashboard/delete/(?P<did>\d+)/$', 'delete_dashboard',
3333
name='delete-dashboard'),
34+
url(r'^index/dashboard/columns/(?P<did>\d+)/$', 'save_dashboard_columns',
35+
name='save-dashboard-columns'),
3436

3537
url(r'^about/', 'about', name='webfront-about'),
3638
url(r'^toolbox/$', 'toolbox', name='webfront-toolbox'),

python/nav/web/webfront/views.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import logging
2222
from operator import attrgetter
2323

24-
from django.db.models import Count
2524
from django.core.urlresolvers import reverse
2625
from django.http import HttpResponseRedirect, HttpResponse, JsonResponse
2726
from django.views.decorators.http import require_POST
@@ -40,7 +39,7 @@
4039
DashboardForm)
4140
from nav.web.navlets import list_navlets
4241
from nav.web.message import new_message, Messages
43-
from nav.web.webfront import get_widget_columns
42+
from nav.web.webfront import get_widget_columns, find_dashboard
4443

4544
_logger = logging.getLogger('nav.web.tools')
4645

@@ -362,18 +361,13 @@ def rename_dashboard(request, did):
362361
'Dashboard renamed to &laquo;{}&raquo;'.format(dash.name))
363362

364363

365-
def find_dashboard(account, dashboard_id=None):
366-
"""Find a dashboard for this account
367-
368-
Either find a specific one or the default one. If none of those exist we
369-
find the one with the most widgets.
370-
"""
371-
kwargs = {'pk': dashboard_id} if dashboard_id else {'is_default': True}
372-
try:
373-
dashboard = AccountDashboard.objects.get(account=account, **kwargs)
374-
except AccountDashboard.DoesNotExist:
375-
# No default dashboard? Find the one with the most widgets
376-
dashboard = AccountDashboard.objects.filter(account=account).annotate(
377-
Count('widgets')).order_by('-widgets__count')[0]
364+
@require_POST
365+
def save_dashboard_columns(request, did):
366+
"""Save the number of columns for this dashboard"""
378367

379-
return dashboard
368+
# Explicit fetch on account to prevent other people to change settings
369+
dashboard = get_object_or_404(AccountDashboard, pk=did,
370+
account=request.account)
371+
dashboard.num_columns = request.POST.get('num_columns', 3)
372+
dashboard.save()
373+
return HttpResponse()

sql/changes/sc.04.06.0051.sql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ CREATE TABLE profiles.account_dashboard (
33
id SERIAL PRIMARY KEY,
44
name VARCHAR DEFAULT 'My dashboard',
55
is_default BOOLEAN DEFAULT FALSE,
6+
num_columns INT,
67
account_id INT REFERENCES account(id) ON UPDATE CASCADE ON DELETE CASCADE
78
);
89

@@ -20,8 +21,8 @@ BEGIN
2021
FOR thisaccount IN SELECT * FROM account LOOP
2122
RAISE NOTICE 'Creating dashboard for %s', quote_ident(thisaccount.login);
2223
WITH inserted AS (
23-
INSERT INTO account_dashboard (account_id, is_default)
24-
VALUES (thisaccount.id, TRUE) RETURNING id
24+
INSERT INTO account_dashboard (account_id, is_default, num_columns)
25+
VALUES (thisaccount.id, TRUE, 3) RETURNING id
2526
)
2627
UPDATE account_navlet
2728
SET dashboard_id=inserted.id

templates/webfront/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ <h5>{{ navlet.title }}</h5>
110110
</div>
111111

112112
<h5>Columns</h5>
113-
<ul class="button-group" style="margin-bottom: 1em;">
113+
<ul class="button-group" style="margin-bottom: 1em;" data-url="{% url 'save-dashboard-columns' dashboard.pk %}">
114114
<li><a href="#" class="button tiny secondary column-chooser" data-columns="2">Two</a></li>
115115
<li><a href="#" class="button tiny secondary column-chooser" data-columns="3">Three</a></li>
116116
<li><a href="#" class="button tiny secondary column-chooser" data-columns="4">Four</a></li>
@@ -157,7 +157,7 @@ <h5>Columns</h5>
157157
data-remove-navlet="{% url 'remove-user-navlet' %}"
158158
data-save-order-url="{% url 'save-navlet-order' %}"
159159
data-base-template-url="{% url 'navlet-base-template' %}"
160-
data-widget-columns="{{ widget_columns }}"
160+
data-widget-columns="{{ dashboard.num_columns }}"
161161
></div>
162162

163163
<ul id="joyride_for_desktop" data-joyride class="joyride-list">

0 commit comments

Comments
 (0)