add minimal working example of django-guardian

This commit is contained in:
Andreas Zweili 2019-06-10 21:46:29 +02:00
parent ad90e2f4d0
commit 9ab28b10c3
4 changed files with 17 additions and 2 deletions

View File

@ -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)

View File

@ -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

View File

@ -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/

View File

@ -3,3 +3,4 @@ Django==2.0
pyaml
pytz
psycopg2
django-guardian