本文整理汇总了Python中guardian.shortcuts.get_objects_for_user方法的典型用法代码示例。如果您正苦于以下问题:Python shortcuts.get_objects_for_user方法的具体用法?Python shortcuts.get_objects_for_user怎么用?Python shortcuts.get_objects_for_user使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类guardian.shortcuts
的用法示例。
在下文中一共展示了shortcuts.get_objects_for_user方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_queryset
# 需要导入模块: from guardian import shortcuts [as 别名]
# 或者: from guardian.shortcuts import get_objects_for_user [as 别名]
def get_queryset(self):
"""
Get the list of items for this view
based on user's view_%(model_name)s permissions.
"""
# Sometime there is no model object
model = getattr(self, 'model', None)
self.model = model or getattr(self.queryset, 'model', None)
if self.request is not None and self.model is not None:
kwargs = {
'app_label': self.model._meta.app_label,
# module_name is now named model_name in django 1.8
'model_name': self.model._meta.model_name
}
perms = ['%(app_label)s.view_%(model_name)s' % kwargs]
return get_objects_for_user(self.request.user, perms, self.model)
if self.model is not None:
return self.model._default_manager.all()
raise ImproperlyConfigured("'%s' must define 'queryset' or 'model'"
% self.__class__.__name__)
示例2: project_search
# 需要导入模块: from guardian import shortcuts [as 别名]
# 或者: from guardian.shortcuts import get_objects_for_user [as 别名]
def project_search(request):
if request.method == "POST":
project_name = request.POST.get('project_name')
person_name = request.POST.get('person_name')
index = int(request.POST.get('index'))
if len(project_name) == 0 and len(person_name) == 0:
return JsonResponse(get_ajax_msg(0, 0, '搜索条件无效'))
else:
projects = ProjectInfo.objects.all()
if len(project_name) > 0:
projects = projects.filter(project_name__contains=project_name)
if len(person_name) > 0:
projects = projects.filter(responsible_name__contains=person_name)
if projects is None:
return JsonResponse(get_ajax_msg(0, 0, '查询出错'))
objects = get_objects_for_user(request.user, AUTH_VIEW, projects) # 根据用户权限筛选项目对象
projects = pagination_for_objects(objects, index)
count = objects.count()
data = dataToJson([model_to_dict(i) for i in projects])
return JsonResponse(get_ajax_msg(1, 1, '搜索成功', {'projects': data, 'count': count, 'currPage': index}))
示例3: list
# 需要导入模块: from guardian import shortcuts [as 别名]
# 或者: from guardian.shortcuts import get_objects_for_user [as 别名]
def list(self, request, collection, *args, **kwargs):
"""
return experiments for the collection that the user has permissions for
Args:
request: DRF request
collection : Collection name
*args:
**kwargs:
Returns: Experiments that user has view permissions on and are not marked for deletion
"""
collection_obj = Collection.objects.get(name=collection)
all_experiments = get_objects_for_user(request.user, 'read', klass=Experiment)\
.exclude(to_be_deleted__isnull=False)
experiments = all_experiments.filter(collection=collection_obj)
data = {"experiments": [experiment.name for experiment in experiments]}
return Response(data)
示例4: get_queryset
# 需要导入模块: from guardian import shortcuts [as 别名]
# 或者: from guardian.shortcuts import get_objects_for_user [as 别名]
def get_queryset(self):
user = self.request.user
partner = self.request.site.partner
if user.is_staff:
return serializers.OrganizationSerializer.prefetch_queryset(partner=partner)
else:
organizations = get_objects_for_user(
user,
OrganizationExtension.VIEW_COURSE,
OrganizationExtension,
use_groups=True,
with_superuser=False
).values_list('organization')
orgs_queryset = serializers.OrganizationSerializer.prefetch_queryset(partner=partner).filter(
pk__in=organizations
)
return orgs_queryset
示例5: apply_filters
# 需要导入模块: from guardian import shortcuts [as 别名]
# 或者: from guardian.shortcuts import get_objects_for_user [as 别名]
def apply_filters(self, request, applicable_filters):
permission = applicable_filters.pop('permission', None)
# NOTE: We change this filter name from type to geom_type because it
# overrides geonode type filter(vector,raster)
layer_geom_type = applicable_filters.pop('geom_type', None)
filtered = super(LayerFilterExtensionResource, self).apply_filters(
request, applicable_filters)
if layer_geom_type:
filtered = filtered.filter(
attribute_set__attribute_type__icontains=layer_geom_type)
if permission is not None:
try:
permitted_ids = get_objects_for_user(request.user,
permission).values('id')
except BaseException:
permitted_ids = get_objects_for_user(
request.user, permission, klass=filtered).values('id')
filtered = filtered.filter(id__in=permitted_ids)
return filtered
示例6: cartoview_processor
# 需要导入模块: from guardian import shortcuts [as 别名]
# 或者: from guardian.shortcuts import get_objects_for_user [as 别名]
def cartoview_processor(request):
permitted = get_objects_for_user(request.user,
'base.view_resourcebase')
cartoview_counters = {
"apps": App.objects.count(),
"app_instances": AppInstance.objects.filter(id__in=permitted).count(),
"maps": Map.objects.filter(id__in=permitted).count(),
"layers": Layer.objects.filter(id__in=permitted).count(),
"users": Profile.objects.exclude(username="AnonymousUser").count(),
"groups": Group.objects.exclude(name="anonymous").count()
}
defaults = {
'apps': App.objects.all().order_by('order'),
'CARTOVIEW_VERSION': get_version(__version__),
'APPS_MENU': settings.APPS_MENU,
'apps_instance_count': AppInstance.objects.all().count(),
"cartoview_counters": cartoview_counters,
'instances': AppInstance.objects.all().order_by('app__order')[:5]
}
return defaults
示例7: for_user
# 需要导入模块: from guardian import shortcuts [as 别名]
# 或者: from guardian.shortcuts import get_objects_for_user [as 别名]
def for_user(self, user, perms, any_perm=False, with_superuser=False):
"""Get a queryset filtered by perms for the user.
:param user: the user itself
:param perms: a string or list of perms to check for
:param any_perm: if any perm or all perms should be cosidered
:param with_superuser: if a superuser should skip the checks
"""
if not has_guardian:
return self.all()
perms = [perms] if isinstance(perms, str) else perms
return get_objects_for_user(
user,
perms,
klass=self.model,
any_perm=any_perm,
with_superuser=with_superuser,
)
示例8: filter_queryset
# 需要导入模块: from guardian import shortcuts [as 别名]
# 或者: from guardian.shortcuts import get_objects_for_user [as 别名]
def filter_queryset(self, request, queryset, view):
# We want to defer this import until run-time, rather than import-time.
# See https://github.com/encode/django-rest-framework/issues/4608
# (Also see #1624 for why we need to make this import explicitly)
from guardian.shortcuts import get_objects_for_user
extra = {}
user = request.user
model_cls = queryset.model
kwargs = {
'app_label': model_cls._meta.app_label,
'model_name': model_cls._meta.model_name
}
permission = self.perm_format % kwargs
if tuple(guardian.VERSION) >= (1, 3):
# Maintain behavior compatibility with versions prior to 1.3
extra = {'accept_global_perms': False}
else:
extra = {}
return get_objects_for_user(user, permission, queryset, **extra)
示例9: filter_owners
# 需要导入模块: from guardian import shortcuts [as 别名]
# 或者: from guardian.shortcuts import get_objects_for_user [as 别名]
def filter_owners(self, queryset, name, value):
"""Filter queryset by owner's id."""
try:
user = user_model.objects.get(pk=value)
except user_model.DoesNotExist:
return queryset.none()
return get_objects_for_user(
user, self.owner_permission, queryset, with_superuser=False
)
示例10: filter_owners_name
# 需要导入模块: from guardian import shortcuts [as 别名]
# 或者: from guardian.shortcuts import get_objects_for_user [as 别名]
def filter_owners_name(self, queryset, name, value):
"""Filter queryset by owner's name."""
result = queryset.model.objects.none()
user_subquery = self._get_user_subquery(value)
for user in user_subquery:
result = result.union(
get_objects_for_user(
user, self.owner_permission, queryset, with_superuser=False
)
)
# Union can no longer be filtered, so we have to create a new queryset
# for following filters.
return result.model.objects.filter(pk__in=Subquery(result.values("pk")))
示例11: list_keys
# 需要导入模块: from guardian import shortcuts [as 别名]
# 或者: from guardian.shortcuts import get_objects_for_user [as 别名]
def list_keys(request):
"""View to list all SSH keys for the logged-in user."""
ssh_keys = get_objects_for_user(
request.user,
"keys.view_sshkey",
SSHKey.objects.all().order_by("-created_at"),
use_groups=False,
with_superuser=False,
)
context = {"ssh_keys": ssh_keys}
return render(request, "atmo/keys/list.html", context)
示例12: get_queryset
# 需要导入模块: from guardian import shortcuts [as 别名]
# 或者: from guardian.shortcuts import get_objects_for_user [as 别名]
def get_queryset(self, *args, **kwargs):
"""
Gets the set of ``ObservationRecord`` objects associated with the targets that the user is authorized to view.
:returns: set of ObservationRecords
:rtype: QuerySet
"""
if settings.TARGET_PERMISSIONS_ONLY:
return ObservationRecord.objects.filter(
target__in=get_objects_for_user(self.request.user, 'tom_targets.view_target')
)
else:
return get_objects_for_user(self.request.user, 'tom_observations.view_observationrecord')
示例13: observation_list
# 需要导入模块: from guardian import shortcuts [as 别名]
# 或者: from guardian.shortcuts import get_objects_for_user [as 别名]
def observation_list(context, target=None):
"""
Displays a list of all observations in the TOM, limited to an individual target if specified.
"""
if target:
if settings.TARGET_PERMISSIONS_ONLY:
observations = target.observationrecord_set.all()
else:
observations = get_objects_for_user(
context['request'].user,
'tom_observations.view_observationrecord'
).filter(target=target)
else:
observations = ObservationRecord.objects.all().order_by('-created')
return {'observations': observations}
示例14: get_queryset
# 需要导入模块: from guardian import shortcuts [as 别名]
# 或者: from guardian.shortcuts import get_objects_for_user [as 别名]
def get_queryset(self, *args, **kwargs):
"""
Returns the queryset that will be used to look up the Target by limiting the result to targets that the user is
authorized to modify.
:returns: Set of targets
:rtype: QuerySet
"""
return get_objects_for_user(self.request.user, 'tom_targets.change_target')
示例15: recent_targets
# 需要导入模块: from guardian import shortcuts [as 别名]
# 或者: from guardian.shortcuts import get_objects_for_user [as 别名]
def recent_targets(context, limit=10):
"""
Displays a list of the most recently created targets in the TOM up to the given limit, or 10 if not specified.
"""
user = context['request'].user
return {'targets': get_objects_for_user(user, 'tom_targets.view_target').order_by('-created')[:limit]}