def queryset(self, request): qs = super(PageUserAdmin, self).queryset(request) try: user_id_set = get_subordinate_users(request.user).values_list('id', flat=True) return qs.filter(pk__in=user_id_set) except NoPermissionsException: return self.model.objects.get_empty_query_set()
def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None, initial=None, error_class=ErrorList, label_suffix=':', empty_permitted=False, instance=None): super(PagePermissionInlineAdminForm, self).__init__(data, files, auto_id, prefix, initial, error_class, label_suffix, empty_permitted, instance) user = get_current_user() # current user from threadlocals self.fields['user'].queryset = get_subordinate_users(user) self.fields['user'].widget.user = user # assign current user self.fields['group'].queryset = get_subordinate_groups(user)
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) user = get_current_user() # current user from threadlocals site = Site.objects.get_current() sub_users = get_subordinate_users(user, site) limit_choices = True use_raw_id = False # Unfortunately, if there are > 500 users in the system, non-superusers # won't see any benefit here because if we ask Django to put all the # user PKs in limit_choices_to in the query string of the popup we're # in danger of causing 414 errors so we fall back to the normal input # widget. if get_cms_setting('RAW_ID_USERS'): if sub_users.count() < 500: # If there aren't too many users, proceed as normal and use a # raw id field with limit_choices_to limit_choices = True use_raw_id = True elif get_user_permission_level(user, site) == ROOT_USER_LEVEL: # If there are enough choices to possibly cause a 414 request # URI too large error, we only proceed with the raw id field if # the user is a superuser & thus can legitimately circumvent # the limit_choices_to condition. limit_choices = False use_raw_id = True # We don't use the fancy custom widget if the admin form wants to use a # raw id field for the user if use_raw_id: from django.contrib.admin.widgets import ForeignKeyRawIdWidget # This check will be False if the number of users in the system # is less than the threshold set by the RAW_ID_USERS setting. if isinstance(self.fields['user'].widget, ForeignKeyRawIdWidget): # We can't set a queryset on a raw id lookup, but we can use # the fact that it respects the limit_choices_to parameter. if limit_choices: self.fields['user'].widget.rel.limit_choices_to = dict( id__in=list(sub_users.values_list('pk', flat=True))) else: self.fields['user'].widget = UserSelectAdminWidget() self.fields['user'].queryset = sub_users self.fields['user'].widget.user = user # assign current user self.fields['group'].queryset = get_subordinate_groups(user, site)
def __init__(self, *args, **kwargs): super(PagePermissionInlineAdminForm, self).__init__(*args, **kwargs) user = get_current_user() # current user from threadlocals site = Site.objects.get_current() sub_users = get_subordinate_users(user, site) limit_choices = True use_raw_id = False # Unfortunately, if there are > 500 users in the system, non-superusers # won't see any benefit here because if we ask Django to put all the # user PKs in limit_choices_to in the query string of the popup we're # in danger of causing 414 errors so we fall back to the normal input # widget. if get_cms_setting('RAW_ID_USERS'): if sub_users.count() < 500: # If there aren't too many users, proceed as normal and use a # raw id field with limit_choices_to limit_choices = True use_raw_id = True elif get_user_permission_level(user, site) == ROOT_USER_LEVEL: # If there are enough choices to possibly cause a 414 request # URI too large error, we only proceed with the raw id field if # the user is a superuser & thus can legitimately circumvent # the limit_choices_to condition. limit_choices = False use_raw_id = True # We don't use the fancy custom widget if the admin form wants to use a # raw id field for the user if use_raw_id: from django.contrib.admin.widgets import ForeignKeyRawIdWidget # This check will be False if the number of users in the system # is less than the threshold set by the RAW_ID_USERS setting. if isinstance(self.fields['user'].widget, ForeignKeyRawIdWidget): # We can't set a queryset on a raw id lookup, but we can use # the fact that it respects the limit_choices_to parameter. if limit_choices: self.fields['user'].widget.rel.limit_choices_to = dict( id__in=list(sub_users.values_list('pk', flat=True)) ) else: self.fields['user'].widget = UserSelectAdminWidget() self.fields['user'].queryset = sub_users self.fields['user'].widget.user = user # assign current user self.fields['group'].queryset = get_subordinate_groups(user, site)
def __init__(self, *args, **kwargs): super(PagePermissionInlineAdminForm, self).__init__(*args, **kwargs) user = get_current_user() # current user from threadlocals self.fields['user'].queryset = get_subordinate_users(user) self.fields['user'].widget.user = user # assign current user self.fields['group'].queryset = get_subordinate_groups(user)
def get_subordinates(self): subordinates = get_subordinate_users(self._current_user, self._current_site) return subordinates.filter(pageuser__isnull=True)
def get_subordinates(self, user, site): return get_subordinate_users(user, site).values_list('pk', flat=True)