1
- from copy import deepcopy
2
-
3
1
from django .contrib import admin
4
- from django .contrib .admin import site
5
2
from django .contrib .auth import get_user_model
3
+ from django .contrib .auth .models import Group
6
4
from django .contrib .auth .admin import UserAdmin
7
5
from django .contrib .sites .models import Site
8
- from django .db import OperationalError
9
6
from django .utils .translation import gettext_lazy as _
10
7
11
- from cms .admin .forms import (
12
- GlobalPagePermissionAdminForm ,
13
- PagePermissionInlineAdminForm ,
14
- ViewRestrictionInlineAdminForm ,
15
- )
16
8
from cms .exceptions import NoPermissionsException
17
9
from cms .models import GlobalPagePermission , PagePermission
18
10
from cms .utils import page_permissions , permissions
19
11
from cms .utils .conf import get_cms_setting
20
- from cms .utils .helpers import classproperty
12
+ from cms .utils .permissions import get_subordinate_users , get_subordinate_groups
21
13
22
14
PERMISSION_ADMIN_INLINES = []
23
15
24
16
user_model = get_user_model ()
25
17
admin_class = UserAdmin
26
- for model , admin_instance in site ._registry .items ():
18
+ for model , admin_instance in admin . site ._registry .items ():
27
19
if model == user_model :
28
20
admin_class = admin_instance .__class__
29
21
30
22
31
- class TabularInline (admin .TabularInline ):
32
- pass
33
-
34
-
35
- class PagePermissionInlineAdmin (TabularInline ):
36
- model = PagePermission
37
- # use special form, so we can override of user and group field
38
- form = PagePermissionInlineAdminForm
39
- extra = 0 # edit page load time boost
40
- show_with_view_permissions = False
41
-
23
+ class PagePermissionAdminMixin :
42
24
def has_change_permission (self , request , obj = None ):
43
25
if not obj :
44
26
return False
@@ -51,38 +33,45 @@ def has_change_permission(self, request, obj=None):
51
33
def has_add_permission (self , request , obj = None ):
52
34
return self .has_change_permission (request , obj )
53
35
54
- @classproperty
55
- def raw_id_fields (cls ):
56
- # Dynamically set raw_id_fields based on settings
57
- threshold = get_cms_setting ('RAW_ID_USERS' )
58
-
59
- # Given a fresh django-cms install and a django settings with the
60
- # CMS_RAW_ID_USERS = CMS_PERMISSION = True
61
- # django throws an OperationalError when running
62
- # ./manage migrate
63
- # because auth_user doesn't exists yet
64
- try :
65
- threshold = threshold and get_user_model ().objects .count () > threshold
66
- except OperationalError :
67
- threshold = False
68
-
69
- return ['user' ] if threshold else []
70
-
71
36
def get_queryset (self , request ):
72
37
"""
73
38
Queryset change, so user with global change permissions can see
74
- all permissions. Otherwise user can see only permissions for
39
+ all permissions. Otherwise, a user can see only permissions for
75
40
peoples which are under him (he can't see his permissions, because
76
41
this will lead to violation, when he can add more power to himself)
77
42
"""
78
43
site = Site .objects .get_current (request )
79
44
80
45
try :
81
46
# can see only permissions for users which are under him in tree
82
- qs = self .model .objects .subordinate_to_user (request .user , site )
47
+ queryset = self .model .objects .subordinate_to_user (request .user , site )
83
48
except NoPermissionsException :
84
- return self .model .objects .none ()
85
- return qs .filter (can_view = self .show_with_view_permissions )
49
+ queryset = self .model .objects .none ()
50
+ return queryset
51
+
52
+ def formfield_for_dbfield (self , db_field , request , ** kwargs ):
53
+ site = Site .objects .get_current (request )
54
+ formfield = super ().formfield_for_dbfield (db_field , request , ** kwargs )
55
+ if db_field .name == 'user' :
56
+ formfield ._queryset = get_subordinate_users (request .user , site )
57
+ if db_field .name == 'group' :
58
+ formfield ._queryset = get_subordinate_groups (request .user , site )
59
+ return formfield
60
+
61
+
62
+ class PagePermissionInlineAdmin (PagePermissionAdminMixin , admin .TabularInline ):
63
+ model = PagePermission
64
+ # use special form, so we can override of user and group field
65
+ # form = PagePermissionInlineAdminForm
66
+ classes = ['collapse' , 'collapsed' ]
67
+ fields = ['user' , 'group' , 'can_add' , 'can_change' , 'can_delete' , 'can_change_advanced_settings' ,
68
+ 'can_change_permissions' , 'can_move_page' , 'grant_on' ,
69
+ ]
70
+ extra = 0 # edit page load time boost
71
+ autocomplete_fields = ['user' , 'group' ]
72
+
73
+ def get_queryset (self , request ):
74
+ return super ().get_queryset (request ).filter (can_view = False )
86
75
87
76
def get_formset (self , request , obj = None , ** kwargs ):
88
77
"""
@@ -106,26 +95,36 @@ def get_formset(self, request, obj=None, **kwargs):
106
95
107
96
kwargs ['exclude' ] = exclude
108
97
formset_cls = super ().get_formset (request , obj = obj , ** kwargs )
109
- qs = self .get_queryset (request )
110
- if obj is not None :
111
- qs = qs .filter (page = obj )
112
- formset_cls ._queryset = qs
98
+ queryset = self .get_queryset (request )
99
+ if obj :
100
+ queryset = queryset .filter (page = obj )
101
+ formset_cls ._queryset = queryset
113
102
return formset_cls
114
103
115
104
116
- class ViewRestrictionInlineAdmin (PagePermissionInlineAdmin ):
105
+ class ViewRestrictionInlineAdmin (PagePermissionAdminMixin , admin .TabularInline ):
106
+ model = PagePermission
117
107
extra = 0 # edit page load time boost
118
- form = ViewRestrictionInlineAdminForm
119
108
verbose_name = _ ("View restriction" )
120
109
verbose_name_plural = _ ("View restrictions" )
121
- show_with_view_permissions = True
110
+ fields = ['user' , 'group' , 'grant_on' , 'can_view' ]
111
+ autocomplete_fields = ['user' , 'group' ]
112
+
113
+ def formfield_for_dbfield (self , db_field , request , ** kwargs ):
114
+ formfield = super ().formfield_for_dbfield (db_field , request , ** kwargs )
115
+ if db_field .name == 'can_view' :
116
+ formfield .widget = formfield .hidden_widget ()
117
+ formfield .initial = True
118
+ return formfield
119
+
120
+ def get_queryset (self , request ):
121
+ return super ().get_queryset (request ).filter (can_view = True )
122
122
123
123
124
124
class GlobalPagePermissionAdmin (admin .ModelAdmin ):
125
125
list_display = ['user' , 'group' , 'can_change' , 'can_delete' , 'can_publish' , 'can_change_permissions' ]
126
126
list_filter = ['user' , 'group' , 'can_change' , 'can_delete' , 'can_publish' , 'can_change_permissions' ]
127
127
128
- form = GlobalPagePermissionAdminForm
129
128
search_fields = []
130
129
for field in admin_class .search_fields :
131
130
search_fields .append ("user__%s" % field )
@@ -135,15 +134,13 @@ class GlobalPagePermissionAdmin(admin.ModelAdmin):
135
134
list_filter .append ('can_change_advanced_settings' )
136
135
137
136
def get_list_filter (self , request ):
138
- threshold = get_cms_setting ('RAW_ID_USERS' )
139
- try :
140
- threshold = threshold and get_user_model ().objects .count () > threshold
141
- except OperationalError :
142
- threshold = False
143
- filter_copy = deepcopy (self .list_filter )
144
- if threshold :
145
- filter_copy .remove ('user' )
146
- return filter_copy
137
+ list_filter = list (super ().get_list_filter (request ))
138
+ users_groups_threshold = get_cms_setting ('USERS_GROUPS_THRESHOLD' )
139
+ if Group .objects .count () <= users_groups_threshold :
140
+ list_filter .insert (0 , 'group' )
141
+ if get_user_model ().objects .count () <= users_groups_threshold :
142
+ list_filter .insert (0 , 'user' )
143
+ return list_filter
147
144
148
145
def has_add_permission (self , request ):
149
146
site = Site .objects .get_current (request )
@@ -157,23 +154,6 @@ def has_delete_permission(self, request, obj=None):
157
154
site = Site .objects .get_current (request )
158
155
return permissions .user_can_delete_global_permissions (request .user , site )
159
156
160
- @classproperty
161
- def raw_id_fields (cls ):
162
- # Dynamically set raw_id_fields based on settings
163
- threshold = get_cms_setting ('RAW_ID_USERS' )
164
-
165
- # Given a fresh django-cms install and a django settings with the
166
- # CMS_RAW_ID_USERS = CMS_PERMISSION = True
167
- # django throws an OperationalError when running
168
- # ./manage migrate
169
- # because auth_user doesn't exists yet
170
- try :
171
- threshold = threshold and get_user_model ().objects .count () > threshold
172
- except OperationalError :
173
- threshold = False
174
-
175
- return ['user' ] if threshold else []
176
-
177
157
178
158
if get_cms_setting ('PERMISSION' ):
179
159
admin .site .register (GlobalPagePermission , GlobalPagePermissionAdmin )
0 commit comments