52
52
MovePageForm ,
53
53
)
54
54
from cms .admin .permissionadmin import PERMISSION_ADMIN_INLINES
55
+ from cms .admin .site_utils import get_site , get_site_from_request , get_sites_for_user , needs_site_redirect
55
56
from cms .cache .permissions import clear_permission_cache
56
57
from cms .constants import MODAL_HTML_REDIRECT
57
58
from cms .models import (
86
87
require_POST = method_decorator (require_POST )
87
88
88
89
89
- def get_site (request ):
90
- site_id = request .session .get ("cms_admin_site" )
91
-
92
- if not site_id :
93
- return get_current_site ()
94
-
95
- try :
96
- site = Site .objects ._get_site_by_id (site_id )
97
- except Site .DoesNotExist :
98
- site = get_current_site ()
99
- return site
100
-
101
-
102
90
class PageDeleteMessageMixin :
103
91
"""Expressive and simplified delete confirmation message for pages and translations."""
104
92
@@ -169,20 +157,19 @@ def has_change_permission(self, request, obj=None):
169
157
Return true if the current user has permission on the page.
170
158
Return the string 'All' if the user has all rights.
171
159
"""
172
- site = get_site (request )
173
160
if obj is None :
161
+ site = get_site_from_request (request )
174
162
# Checks if user can change at least one page
175
163
return page_permissions .user_can_change_at_least_one_page (
176
164
user = request .user ,
177
165
site = site ,
178
166
)
179
- return page_permissions .user_can_change_page (request .user , page = obj , site = site )
167
+ return page_permissions .user_can_change_page (request .user , page = obj , site = obj . site )
180
168
181
169
def has_change_advanced_settings_permission (self , request , obj = None ):
182
170
if not obj :
183
171
return False
184
- site = get_site (request )
185
- return page_permissions .user_can_change_page_advanced_settings (request .user , page = obj , site = site )
172
+ return page_permissions .user_can_change_page_advanced_settings (request .user , page = obj , site = obj .site )
186
173
187
174
def log_deletion (self , request , object , object_repr ):
188
175
# DJANGO_42
@@ -211,12 +198,6 @@ def get_preserved_filters(self, request):
211
198
preserved_filters ["language" ] = lang
212
199
return preserved_filters .urlencode ()
213
200
214
- def get_queryset (self , request ):
215
- site = get_site (request )
216
- queryset = super ().get_queryset (request )
217
- queryset = queryset .filter (site = site )
218
- return queryset
219
-
220
201
def get_page_from_id (self , page_id ):
221
202
page_id = self .model ._meta .pk .to_python (page_id )
222
203
@@ -259,22 +240,15 @@ def get_inline_instances(self, request, obj=None):
259
240
return super ().get_inline_instances (request , obj )
260
241
return []
261
242
262
- def get_form (self , request , obj = None , ** kwargs ):
263
- """
264
- Get PageForm for the Page model and modify its fields depending on
265
- the request.
266
- """
267
- form = super ().get_form (request , obj , ** kwargs )
268
- form ._site = get_site (request )
269
- form ._request = request
270
- return form
271
-
272
243
def actions_menu (self , request , object_id , extra_context = None ):
273
244
page = self .get_object (request , object_id = object_id )
274
245
275
246
if page is None :
276
247
raise self ._get_404_exception (object_id )
277
248
249
+ if not self .has_view_permission (request , obj = page ):
250
+ raise PermissionDenied ("No permission for actions menu" )
251
+
278
252
site = get_site (request )
279
253
paste_enabled = request .GET .get ("has_copy" ) or request .GET .get ("has_cut" )
280
254
context = {
@@ -363,11 +337,13 @@ def set_home(self, request, object_id):
363
337
set_restart_trigger ()
364
338
return HttpResponse ("ok" )
365
339
366
- def get_list (self , * args , ** kwargs ):
340
+ def get_list (self , request ):
367
341
"""
368
342
This view is used by the PageSmartLinkWidget as the user type to feed the autocomplete drop-down.
369
343
"""
370
- request = args [0 ]
344
+
345
+ if not self .has_view_permission (request ):
346
+ raise PermissionDenied ("No permission for page list view" )
371
347
372
348
if request .headers .get ("x-requested-with" ) == "XMLHttpRequest" :
373
349
query_term = request .GET .get ("q" , "" ).strip ("/" )
@@ -786,21 +762,19 @@ def get_preserved_filters(self, request):
786
762
This override is in place to preserve the "language" get parameter in
787
763
the "Save" page redirect
788
764
"""
789
- site = get_site (request )
765
+ site = get_site_from_request (request )
790
766
preserved_filters_encoded = super ().get_preserved_filters (request )
791
767
preserved_filters = QueryDict (preserved_filters_encoded ).copy ()
792
768
lang = get_site_language_from_request (request , site_id = site .pk )
793
769
794
770
if lang :
795
771
preserved_filters ["language" ] = lang
772
+ if site and site .pk != settings .SITE_ID :
773
+ preserved_filters ["site" ] = site .pk
796
774
return preserved_filters .urlencode ()
797
775
798
776
def get_queryset (self , request ):
799
- site = get_site (request )
800
- languages = get_language_list (site .pk )
801
- queryset = super ().get_queryset (request ).select_related ("page" )
802
- queryset = queryset .filter (language__in = languages , page__site = site )
803
- return queryset
777
+ return super ().get_queryset (request ).select_related ("page" )
804
778
805
779
def get_urls (self ):
806
780
"""Get the admin urls"""
@@ -845,6 +819,11 @@ def get_form(self, request, obj=None, **kwargs):
845
819
form ._request = request
846
820
return form
847
821
822
+ # def get_changeform_initial_data(self, request):
823
+ # site = get_site(request)
824
+ # language = get_site_language_from_request(request, site_id=site.pk)
825
+ # return {"language": language, "site": site}
826
+
848
827
def slug (self , obj ):
849
828
# For read-only views: Get slug from the page
850
829
if not hasattr (self , "url_obj" ):
@@ -877,6 +856,8 @@ def duplicate(self, request, object_id):
877
856
return self .add_view (request )
878
857
879
858
def add_view (self , request , form_url = "" , extra_context = None ):
859
+ if request .method == "GET" and (redirect_response := needs_site_redirect (request )):
860
+ return redirect_response
880
861
site = get_site (request )
881
862
language = get_site_language_from_request (request , site_id = site .pk )
882
863
@@ -929,7 +910,7 @@ def change_view(self, request, object_id, form_url="", extra_context=None):
929
910
if obj is None :
930
911
raise self ._get_404_exception (object_id )
931
912
932
- site = get_site ( request )
913
+ site = obj . page . site
933
914
context = {
934
915
"cms_page" : obj .page ,
935
916
"CMS_PERMISSION" : get_cms_setting ("PERMISSION" ),
@@ -962,7 +943,7 @@ def response_add(self, request, obj):
962
943
return super ().response_add (request , obj )
963
944
964
945
def get_filled_languages (self , request , page ):
965
- site_id = get_site ( request ) .pk
946
+ site_id = page . site .pk
966
947
filled_languages = page .get_languages ()
967
948
allowed_languages = [lang [0 ] for lang in get_language_tuple (site_id )]
968
949
return [lang for lang in filled_languages if lang in allowed_languages ]
@@ -978,7 +959,6 @@ def _get_404_exception(self, object_id):
978
959
return exception
979
960
980
961
def _has_add_permission_from_request (self , request ):
981
- site = get_site (request )
982
962
if parent_id := request .GET .get ("parent_page" ):
983
963
try :
984
964
parent_id = IntegerField ().clean (parent_id )
@@ -992,9 +972,10 @@ def _has_add_permission_from_request(self, request):
992
972
has_perm = page_permissions .user_can_add_subpage (
993
973
request .user ,
994
974
target = parent_item ,
995
- site = site ,
975
+ site = parent_item . site ,
996
976
)
997
977
else :
978
+ site = get_site_from_request (request )
998
979
has_perm = page_permissions .user_can_add_page (request .user , site = site )
999
980
return has_perm
1000
981
@@ -1009,13 +990,12 @@ def has_change_permission(self, request, obj=None):
1009
990
Return true if the current user has permission on the page.
1010
991
Return the string 'All' if the user has all rights.
1011
992
"""
1012
- site = get_site (request )
1013
993
1014
994
if obj :
1015
- return page_permissions .user_can_change_page (request .user , page = obj .page , site = site )
995
+ return page_permissions .user_can_change_page (request .user , page = obj .page , site = obj . page . site )
1016
996
can_change_page = page_permissions .user_can_change_at_least_one_page (
1017
997
user = request .user ,
1018
- site = site ,
998
+ site = get_site_from_request ( request ) ,
1019
999
)
1020
1000
return can_change_page
1021
1001
@@ -1026,13 +1006,12 @@ def has_view_permission(self, request, obj=None):
1026
1006
"""
1027
1007
# Identical to has_change_permission, but will remain untouched by any subclassing
1028
1008
# as done, e.g., by djangocms-versioning
1029
- site = get_site (request )
1030
1009
1031
1010
if obj :
1032
- return page_permissions .user_can_change_page (request .user , page = obj .page , site = site )
1011
+ return page_permissions .user_can_change_page (request .user , page = obj .page , site = obj . page . site )
1033
1012
can_view_page = page_permissions .user_can_change_at_least_one_page (
1034
1013
user = request .user ,
1035
- site = get_site (request ),
1014
+ site = get_site_from_request (request ),
1036
1015
use_cache = False ,
1037
1016
)
1038
1017
return can_view_page
@@ -1043,48 +1022,34 @@ def has_delete_permission(self, request, obj=None):
1043
1022
"""
1044
1023
if not obj :
1045
1024
return False
1046
- site = get_site (request )
1047
- return page_permissions .user_can_delete_page (request .user , page = obj .page , site = site )
1025
+ return page_permissions .user_can_delete_page (request .user , page = obj .page , site = obj .page .site )
1048
1026
1049
1027
def has_change_advanced_settings_permission (self , request , obj = None ):
1050
1028
if not obj :
1051
1029
return False
1052
- site = get_site (request )
1053
- return page_permissions .user_can_change_page_advanced_settings (request .user , page = obj .page , site = site )
1030
+ return page_permissions .user_can_change_page_advanced_settings (request .user , page = obj .page , site = obj .page .site )
1054
1031
1055
1032
def has_delete_translation_permission (self , request , language , obj = None ):
1056
1033
if not obj :
1057
1034
return False
1058
1035
1059
- site = get_site (request )
1060
1036
has_perm = page_permissions .user_can_delete_page_translation (
1061
1037
user = request .user ,
1062
1038
page = obj ,
1063
1039
language = language ,
1064
- site = site ,
1040
+ site = obj . site ,
1065
1041
)
1066
1042
return has_perm
1067
1043
1068
- def get_sites_for_user (self , user ):
1069
- sites = Site .objects .order_by ("name" )
1070
-
1071
- if not get_cms_setting ("PERMISSION" ) or user .is_superuser :
1072
- return sites
1073
- _has_perm = page_permissions .user_can_change_at_least_one_page
1074
- return [site for site in sites if _has_perm (user , site )]
1075
-
1076
1044
def changelist_view (self , request , extra_context = None ):
1077
1045
from django .contrib .admin .views .main import ERROR_FLAG
1078
1046
1047
+ if redirect_response := needs_site_redirect (request ):
1048
+ return redirect_response
1049
+
1079
1050
if not self .has_change_permission (request , obj = None ):
1080
1051
raise PermissionDenied
1081
1052
1082
- if request .method == "POST" and "site" in request .POST :
1083
- site_id = request .POST ["site" ]
1084
-
1085
- if site_id .isdigit () and Site .objects .filter (pk = site_id ).exists ():
1086
- request .session ["cms_admin_site" ] = site_id
1087
-
1088
1053
site = get_site (request )
1089
1054
language = get_site_language_from_request (request , site_id = site .pk )
1090
1055
query = request .GET .get ("q" , "" )
@@ -1142,7 +1107,7 @@ def changelist_view(self, request, extra_context=None):
1142
1107
"admin" : self ,
1143
1108
"tree" : {
1144
1109
"site" : site ,
1145
- "sites" : self . get_sites_for_user (request .user ),
1110
+ "sites" : get_sites_for_user (request .user ),
1146
1111
"query" : query ,
1147
1112
"is_filtered" : changelist_form .is_filtered (),
1148
1113
"items" : pages ,
0 commit comments