我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用django.contrib.auth.get_permission_codename()。
def save_permissions(data, obj): models = ( (Page, 'page'), (PageUser, 'pageuser'), (PageUserGroup, 'pageuser'), (PagePermission, 'pagepermission'), ) if not obj.pk: # save obj, otherwise we can't assign permissions to him obj.save() permission_accessor = get_permission_accessor(obj) for model, name in models: content_type = ContentType.objects.get_for_model(model) for key in ('add', 'change', 'delete'): # add permission `key` for model `model` codename = get_permission_codename(key, model._meta) permission = Permission.objects.get(content_type=content_type, codename=codename) if data.get('can_%s_%s' % (key, name), None): permission_accessor.add(permission) else: permission_accessor.remove(permission)
def _get_builtin_permissions(opts): """ Returns (codename, name) for all autogenerated permissions. By default, this is ('add', 'change', 'delete') """ perms = [] for action in opts.default_permissions: perms.append((get_permission_codename(action, opts), 'Can %s %s' % (action, opts.verbose_name_raw))) return perms
def has_add_permission(self, request): """ Returns True if the given request has permission to add an object. Can be overridden by the user in subclasses. """ opts = self.opts codename = get_permission_codename('add', opts) return request.user.has_perm("%s.%s" % (opts.app_label, codename))
def has_change_permission(self, request, obj=None): """ Returns True if the given request has permission to change the given Django model instance, the default implementation doesn't examine the `obj` parameter. Can be overridden by the user in subclasses. In such case it should return True if the given request has permission to change the `obj` model instance. If `obj` is None, this should return True if the given request has permission to change *any* object of the given type. """ opts = self.opts codename = get_permission_codename('change', opts) return request.user.has_perm("%s.%s" % (opts.app_label, codename))
def has_delete_permission(self, request, obj=None): """ Returns True if the given request has permission to change the given Django model instance, the default implementation doesn't examine the `obj` parameter. Can be overridden by the user in subclasses. In such case it should return True if the given request has permission to delete the `obj` model instance. If `obj` is None, this should return True if the given request has permission to delete *any* object of the given type. """ opts = self.opts codename = get_permission_codename('delete', opts) return request.user.has_perm("%s.%s" % (opts.app_label, codename))
def has_change_permission(self, request, obj=None): opts = self.opts if opts.auto_created: # The model was auto-created as intermediary for a # ManyToMany-relationship, find the target model for field in opts.fields: if field.remote_field and field.remote_field.model != self.parent_model: opts = field.remote_field.model._meta break codename = get_permission_codename('change', opts) return request.user.has_perm("%s.%s" % (opts.app_label, codename))
def has_view_permission(self, obj=None): view_codename = get_permission_codename('view', self.opts) change_codename = get_permission_codename('change', self.opts) return ('view' not in self.remove_permissions) and (self.user.has_perm('%s.%s' % (self.app_label, view_codename)) or \ self.user.has_perm('%s.%s' % (self.app_label, change_codename)))
def has_add_permission(self): codename = get_permission_codename('add', self.opts) return ('add' not in self.remove_permissions) and self.user.has_perm('%s.%s' % (self.app_label, codename))
def has_change_permission(self, obj=None): codename = get_permission_codename('change', self.opts) return ('change' not in self.remove_permissions) and self.user.has_perm('%s.%s' % (self.app_label, codename))
def has_delete_permission(self, obj=None): codename = get_permission_codename('delete', self.opts) return ('delete' not in self.remove_permissions) and self.user.has_perm('%s.%s' % (self.app_label, codename))
def has_add_permission(self): if self.opts.auto_created: return self.has_change_permission() codename = get_permission_codename('add', self.opts) return self.user.has_perm("%s.%s" % (self.opts.app_label, codename))
def has_delete_permission(self): if self.opts.auto_created: return self.has_change_permission() codename = get_permission_codename('delete', self.opts) return self.user.has_perm("%s.%s" % (self.opts.app_label, codename))
def get_all_permissions(self, user, obj=None): """Return a set of <app_label>.<permission> for this user. Note that these results will only be applicable to the admin panel. <permission> is something like add_mymodel or change_mymodel. """ if hasattr(user, '_baya_cached_all_permissions'): return user._baya_cached_all_permissions permissions = super(NestedLDAPGroupsBackend, self).get_all_permissions( user, obj) from baya.admin.sites import _admin_registry for admin_site in _admin_registry: for model, opts in six.iteritems(admin_site._registry): app = model._meta.app_label perm_name = partial(get_permission_codename, opts=model._meta) if (hasattr(opts, 'user_has_add_permission') and opts.user_has_add_permission(user)): permissions.add("%s.%s" % (app, perm_name('add'))) if (hasattr(opts, 'user_has_change_permission') and opts.user_has_change_permission(user)): permissions.add("%s.%s" % (app, perm_name('change'))) if (hasattr(opts, 'user_has_delete_permission') and opts.user_has_delete_permission(user)): permissions.add("%s.%s" % (app, perm_name('delete'))) user._baya_cached_all_permissions = permissions return permissions
def get_permission_codename(action, opts): return '%s_%s' % (action, opts.object_name.lower())