forked from sbussetti/django-admincommand
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.py
More file actions
30 lines (23 loc) · 941 Bytes
/
query.py
File metadata and controls
30 lines (23 loc) · 941 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from django.conf import settings
from django.contrib.auth.models import Permission
from sneak.query import ListQuerySet
from admincommand.models import AdminCommand
class CommandQuerySet(ListQuerySet):
"""Custom QuerySet to list runnable commands"""
def __init__(self, user, value=None):
self.user = user
if value is None:
self.value = self.filter().value
else:
self.value = value
def _clone(self):
return type(self)(self.user, self.value)
def filter(self, *args, **kwargs):
all = []
for command in AdminCommand.all():
# only list commands that the user can run
# to avoid useless 503 messages
full_permission_codename = 'admincommand.%s' % command.permission_codename()
if self.user.has_perm(full_permission_codename):
all.append(command)
return type(self)(self.user, all)