accounts.signals
1from django.db.models.signals import post_save 2from django.dispatch import receiver 3from .models import User, Role 4 5@receiver(post_save, sender=User) 6def update_admin_staff_status(sender, instance, created, **kwargs): 7 """ 8 Signal handler to update staff status for users with the 'ADMIN' role. 9 10 - If a User's role is 'ADMIN', mark all users with that role as is_staff=True. 11 - If a User's role is not 'ADMIN', ensure is_staff is False unless the user is a superuser. 12 13 This ensures only users with the 'ADMIN' role are marked as staff, maintaining correct permissions. 14 """ 15 if instance.role and instance.role.name.upper() == 'ADMIN': 16 admin_role = instance.role 17 User.objects.filter(role=admin_role).update(is_staff=True) 18 else: 19 # If user's role is not admin, make sure they aren't marked staff accidentally 20 if instance.is_staff and not instance.is_superuser: 21 instance.is_staff = False 22 instance.save(update_fields=['is_staff'])
@receiver(post_save, sender=User)
def
update_admin_staff_status(sender, instance, created, **kwargs):
6@receiver(post_save, sender=User) 7def update_admin_staff_status(sender, instance, created, **kwargs): 8 """ 9 Signal handler to update staff status for users with the 'ADMIN' role. 10 11 - If a User's role is 'ADMIN', mark all users with that role as is_staff=True. 12 - If a User's role is not 'ADMIN', ensure is_staff is False unless the user is a superuser. 13 14 This ensures only users with the 'ADMIN' role are marked as staff, maintaining correct permissions. 15 """ 16 if instance.role and instance.role.name.upper() == 'ADMIN': 17 admin_role = instance.role 18 User.objects.filter(role=admin_role).update(is_staff=True) 19 else: 20 # If user's role is not admin, make sure they aren't marked staff accidentally 21 if instance.is_staff and not instance.is_superuser: 22 instance.is_staff = False 23 instance.save(update_fields=['is_staff'])
Signal handler to update staff status for users with the 'ADMIN' role.
- If a User's role is 'ADMIN', mark all users with that role as is_staff=True.
- If a User's role is not 'ADMIN', ensure is_staff is False unless the user is a superuser.
This ensures only users with the 'ADMIN' role are marked as staff, maintaining correct permissions.