querysets.py
862 Bytes
from django.db.models import QuerySet
from django.db.models import Q
from django.contrib.auth.models import Permission
class PublicadoQuerySet(QuerySet):
def get_queryset(self):
return self.filter(publicado__isnull=False)
def filter_queryset_by_permissions(queryset, user):
if not user.is_superuser:
grupos_usuario = user.groups.all()
organismo_codenames = Permission.objects.filter(
group__in=grupos_usuario,
codename__startswith='view_'
).values_list('codename', flat=True)
organismos_permitidos = [
codename[len('view_'):].replace('_', ' ')
for codename in organismo_codenames
]
query = Q()
for name in organismos_permitidos:
query |= Q(short_name__icontains=name)
return queryset.filter(query)
return queryset