@@ -36,10 +36,9 @@ def find_dashboard(account, dashboard_id=None):
3636
3737def _find_dashboard_by_id (account , dashboard_id ):
3838 """Find a specific dashboard by ID for this account"""
39- kwargs = {'pk' : dashboard_id }
4039 try :
4140 dashboard = AccountDashboard .objects .get (
42- (Q (account = account ) | Q (is_shared = True )), ** kwargs
41+ (Q (account = account ) | Q (is_shared = True )), pk = dashboard_id
4342 )
4443 return dashboard
4544
@@ -49,25 +48,26 @@ def _find_dashboard_by_id(account, dashboard_id):
4948
5049def _find_default_dashboard (account ):
5150 """Find the default dashboard for this account"""
52- kwargs = (
53- { 'pk' : account .default_dashboard .id } if account .has_default_dashboard else {}
51+ dashboard_id = (
52+ account .default_dashboard .pk if account .has_default_dashboard else None
5453 )
55- try :
56- dashboard = AccountDashboard .objects .get (
57- (Q (account = account ) | Q (is_shared = True )), ** kwargs
58- )
5954
60- except AccountDashboard .DoesNotExist :
61- # Do we have a dashboard at all?
62- dashboards = AccountDashboard .objects .filter (account = account )
63- if dashboards .count () == 0 :
64- raise Http404
65-
66- # No default dashboard? Find the one with the most widgets
67- dashboard = dashboards .annotate (Count ('widgets' )).order_by ('-widgets__count' )[0 ]
68- except AccountDashboard .MultipleObjectsReturned :
69- # Grab the first one
70- dashboard = AccountDashboard .objects .filter (account = account , ** kwargs )[0 ]
55+ if dashboard_id :
56+ dashboard = AccountDashboard .objects .filter (
57+ Q (account = account ) | Q (is_shared = True ), pk = dashboard_id
58+ ).first ()
59+ if dashboard :
60+ return dashboard
61+
62+ # No default dashboard? Find the one with the most widgets
63+ dashboards = AccountDashboard .objects .filter (account = account )
64+ if dashboards .count () == 0 :
65+ raise Http404
66+ dashboard = (
67+ dashboards .annotate (widget_count = Count ('widgets' ))
68+ .order_by ('-widget_count' )
69+ .first ()
70+ )
7171
7272 return dashboard
7373
0 commit comments