My code:
from django.contrib.auth.mixins import UserPassesTestMixin
from django.http import JsonResponse
class aiutanteRequiredMixin(UserPassesTestMixin):
def test_func(self):
user = self.request.user
return user.groups.filter(name="aiutante").exists()
class adminRequiredMixin(UserPassesTestMixin):
def test_func(self):
user = self.request.user
return user.groups.filter(name="admin").exists()
def handle_no_permission(self):
return JsonResponse(
{'message': 'Only company administrators have access to this view'}
)
class GestioneUtenti(aiutanteRequiredMixin, adminRequiredMixin,ListView):
model = User
group_required = [u"aiutante", u"admin"]
template_name = 'users_list.html'
Not working … how to use group level ??
My model:
from django.db import models
from django.utils.translation import gettext_lazy as _
from core.utils import *
from django.contrib.auth.models import AbstractUser
from .managers import UserProfileManager
from django.utils import timezone
from stdimage import StdImageField
from django.contrib.auth.models import Group
Sex_CHOICES = (
(0,"N/A"),
(1,"Uomo"),
(2, "Donna"),
)
class User(AbstractUser):
objects = UserProfileManager()
id = models.AutoField(primary_key=True)
# cognome
last_name = models.CharField(_('Last name'), null=True, blank=True, max_length=30)
# nome
first_name = models.CharField(_('First name'), null=True, blank=True, max_length=30)
state = models.CharField(_('State'), null=True, blank=True, max_length=255)
country = models.CharField(_('Country'), null=True, blank=True, max_length=255)
home_address = models.CharField(_('Home address'), null=True, blank=True, max_length=255)
postal_code = models.CharField(_('Postal code'), null=True, blank=True, max_length=255)
city = models.CharField(_('City'), null=True, blank=True, max_length=255)
email = models.EmailField(_('Email'), unique=True, error_messages={'unique':"Email già in uso."})
username = models.CharField(_('Username'), max_length=100, unique=True, null=True, default=None)
display_name = models.PositiveIntegerField(_('Display name'), null=True, default=None)
groups = models.ManyToManyField(Group)
sex = models.IntegerField(_('Sex'), choices=Sex_CHOICES, default=0)
birth_date=models.DateField(_('Birth date'), null=True, blank=True,)
information = models.TextField(_('Information'), blank=True, null=True)
image = StdImageField(_('Photo'), upload_to=PathRename('profile'), render_variations= resize_and_autorotate, blank=True , variations={
'avatar': (120, 120, True),
}, delete_orphans=True)
is_superuser = models.BooleanField( blank=True, null=True)
is_staff = models.BooleanField( blank=True, null=True)
is_active = models.BooleanField(default=True)
date_joined = models.DateTimeField(_('Date of registration'), default=timezone.now)
USERNAME_FIELD = "email"
REQUIRED_FIELDS = ['username','first_name', 'last_name']
def __str__(self):
if self.display_name == 1:
return self.first_name + ' ' + self.last_name
elif self.display_name == 2:
return self.last_name + ' ' + self.first_name
elif self.display_name == 3:
return self.username
else:
return self.first_name + ' ' + self.last_name
def save(self, *args, **kwargs):
super(User, self).save(*args, **kwargs)
groups = models.ManyToManyField(Group)
idea?. Thanks.