1
1
from django .conf .urls import url
2
2
from django .contrib import admin
3
3
from django .contrib .admin .views .decorators import staff_member_required
4
+ from django .contrib .auth .decorators import user_passes_test
4
5
from django .core .exceptions import ImproperlyConfigured
5
6
from django .http import Http404
6
7
from django .utils .decorators import method_decorator
7
8
from django .utils .module_loading import import_string
8
9
from django .views .generic .base import TemplateView
10
+ from django .contrib .auth .mixins import UserPassesTestMixin
11
+ from django .contrib .auth .mixins import LoginRequiredMixin
12
+ from django .shortcuts import redirect , render
13
+ from django .contrib .auth .decorators import login_required
9
14
15
+ from scope .models import UserProfile
10
16
from . import app_settings
11
17
12
18
13
19
class ControlCenter (object ):
20
+
14
21
def __init__ (self , view_class ):
15
22
self .view_class = view_class
16
23
@@ -35,29 +42,70 @@ def get_urls(self):
35
42
def urls (self ):
36
43
return self .get_urls (), 'controlcenter' , 'controlcenter'
37
44
45
+ @property
46
+ def urls_with_key (self ):
47
+ return self .get_urls (), 'controlcenter_with_key' , 'controlcenter_with_key'
48
+
49
+
50
+
51
+ # def user_corresponds_to_customer_key(user):
52
+ # user_profile = UserProfile.objects.get(user=request.user)
53
+ # return customer_key == user_profile.customer.customer_key:
54
+ # pass
55
+
56
+
57
+ class DashboardView (LoginRequiredMixin , TemplateView ): #UserPassesTestMixin,
38
58
39
- class DashboardView (TemplateView ):
40
59
dashboards = NotImplemented
41
60
template_name = 'controlcenter/dashboard.html'
61
+ # def determine_login_path(self):
62
+ # return redirect('/login/?next=%s' % self.request.path)
63
+
64
+ # login_url =
65
+ # @method_decorator(staff_member_required)
66
+ # def test_func(self):
67
+ # try:
68
+ # user_profile = UserProfile.objects.get(user=self.request.user)
69
+ # customer_key = self.kwargs['customer_key']
70
+ # return customer_key == user_profile.customer.customer_key
71
+ # except:
72
+ # return False
42
73
43
- @method_decorator (staff_member_required )
44
74
def dispatch (self , * args , ** kwargs ):
45
75
return super (DashboardView , self ).dispatch (* args , ** kwargs )
46
76
47
77
def get (self , request , * args , ** kwargs ):
48
78
pk = int (self .kwargs ['pk' ])
79
+ if self .request .user .is_superuser :
80
+ try :
81
+ customer_key = self .kwargs ['customer_key' ]
82
+ except :
83
+ return redirect ('/admin' )
84
+ else :
85
+ user_profile = UserProfile .objects .get (user = self .request .user )
86
+ customer_key = user_profile .customer .customer_key
87
+
88
+ for dashboard in self .dashboards :
89
+ dashboard .add_customer_key (customer_key )
49
90
try :
50
91
self .dashboard = self .dashboards [pk ]
51
92
except IndexError :
52
93
raise Http404 ('Dashboard not found.' )
53
94
return super (DashboardView , self ).get (request , * args , ** kwargs )
54
95
55
96
def get_context_data (self , ** kwargs ):
97
+ if self .request .user .is_superuser :
98
+ customer_key = self .kwargs ['customer_key' ]
99
+ else :
100
+ user_profile = UserProfile .objects .get (user = self .request .user )
101
+ customer_key = user_profile .customer .customer_key
102
+ options = {'customer_key' : customer_key }
56
103
context = {
104
+ 'customer_key' : customer_key ,
57
105
'title' : self .dashboard .title ,
58
106
'dashboard' : self .dashboard ,
59
107
'dashboards' : self .dashboards ,
60
- 'groups' : self .dashboard .get_widgets (self .request ),
108
+ 'groups' : self .dashboard .get_widgets (self .request , ** options ),
61
109
'sharp' : app_settings .SHARP ,
62
110
}
63
111
0 commit comments