Skip to content
Snippets Groups Projects
Commit 4cbdb5f9 authored by Hangzhi Yu's avatar Hangzhi Yu
Browse files

Fixed content type bug

parent 3302dfad
No related branches found
No related tags found
1 merge request!318Resolve "Add helper function to filter a queryset by a rule"
Pipeline #3134 failed
from django.contrib.auth.backends import ModelBackend
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.db.models import Model
from django.http import HttpRequest
......@@ -57,17 +58,17 @@ def has_any_object(perm: str, klass):
"""Check if has any object.
Build predicate which checks whether a user has access
to objects with the provided permission.
to objects with the provided permission or rule.
"""
name = f"has_any_object:{perm}"
@predicate(name)
def fn(user: User) -> bool:
return (
get_objects_for_user(user, perm, klass).exists()
or queryset_rules_filter(user, klass.objects.all(), perm).exists()
)
ct_perm = ContentType.objects.get(app_label=perm.split('.', 1)[0], permission__codename=perm.split('.', 1)[1])
if ct_perm and ct_perm.model_class() == klass:
return get_objects_for_user(user, perm, klass).exists()
else:
return queryset_rules_filter(user, klass.objects.all(), perm).exists()
return fn
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment