add minimal working example of django-guardian
This commit is contained in:
parent
ad90e2f4d0
commit
9ab28b10c3
|
@ -190,7 +190,9 @@ class Computer(Device):
|
|||
|
||||
class Meta:
|
||||
ordering = ['ip']
|
||||
|
||||
permissions = (
|
||||
('view_computer', 'Can view computer'),
|
||||
)
|
||||
|
||||
class ComputerDiskRelation(models.Model):
|
||||
disk = models.ForeignKey(Disk, on_delete=models.CASCADE)
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
from django.shortcuts import get_object_or_404, render
|
||||
from django.views.generic import ListView
|
||||
from guardian.shortcuts import get_objects_for_user
|
||||
from .models import (GeneralDevice, Computer, CronJob,
|
||||
ComputerRamRelation,
|
||||
ComputerDiskRelation,
|
||||
|
@ -36,6 +37,12 @@ class ComputerList(ListView):
|
|||
model = Computer
|
||||
template_name = 'inventory/computer_list.html'
|
||||
|
||||
def get_queryset(self):
|
||||
queryset = get_objects_for_user(self.request.user, 'inventory.view_computer', klass=Computer)
|
||||
#return super().get_queryset()
|
||||
return queryset
|
||||
|
||||
|
||||
|
||||
class CronJobList(ListView):
|
||||
model = CronJob
|
||||
|
|
|
@ -40,7 +40,8 @@ INSTALLED_APPS = [
|
|||
'django.contrib.sessions',
|
||||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
'inventory.apps.InventoryConfig'
|
||||
'inventory.apps.InventoryConfig',
|
||||
'guardian',
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
|
@ -102,6 +103,10 @@ AUTH_PASSWORD_VALIDATORS = [
|
|||
},
|
||||
]
|
||||
|
||||
AUTHENTICATION_BACKENDS = (
|
||||
'django.contrib.auth.backends.ModelBackend', # this is default
|
||||
'guardian.backends.ObjectPermissionBackend',
|
||||
)
|
||||
|
||||
# Internationalization
|
||||
# https://docs.djangoproject.com/en/2.0/topics/i18n/
|
||||
|
|
|
@ -3,3 +3,4 @@ Django==2.0
|
|||
pyaml
|
||||
pytz
|
||||
psycopg2
|
||||
django-guardian
|
||||
|
|
Loading…
Reference in New Issue