This commit is contained in:
Flatlogic Bot 2026-01-23 13:20:54 +00:00
parent 43c475e4c1
commit 756f4e839e
4 changed files with 21 additions and 3 deletions

View File

@ -7,7 +7,7 @@ from django.contrib.auth.models import User
class UserRegistrationForm(UserCreationForm):
email = forms.EmailField(required=True, widget=forms.EmailInput(attrs={'class': 'form-control'}))
confirm_email = forms.EmailField(required=True, widget=forms.EmailInput(attrs={'class': 'form-control'}), label=_("Confirm Email"))
role = forms.ChoiceField(choices=Profile.ROLE_CHOICES, widget=forms.Select(attrs={'class': 'form-select'}))
role = forms.ChoiceField(choices=[choice for choice in Profile.ROLE_CHOICES if choice[0] != 'ADMIN'], widget=forms.Select(attrs={'class': 'form-select'}))
country_code = forms.ChoiceField(choices=[], widget=forms.Select(attrs={'class': 'form-select'}))
phone_number = forms.CharField(max_length=20, required=True, widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': '123456789'}))

View File

@ -1,5 +1,5 @@
from django.db import models
from django.contrib.auth.models import User
from django.contrib.auth.models import User, Group
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.utils.translation import gettext_lazy as _
@ -184,4 +184,22 @@ def save_user_profile(sender, instance, **kwargs):
if hasattr(instance, 'profile'):
instance.profile.save()
else:
Profile.objects.create(user=instance)
Profile.objects.create(user=instance)
@receiver(post_save, sender=Profile)
def sync_user_groups(sender, instance, **kwargs):
"""
Automatically syncs Django Groups based on the Profile role.
"""
# Get or create the group for the current role
group, created = Group.objects.get_or_create(name=instance.role)
# Get all possible role-based groups to clean up
all_role_names = [role[0] for role in Profile.ROLE_CHOICES]
# Remove user from other role groups they might be in
other_groups = Group.objects.filter(name__in=all_role_names).exclude(name=instance.role)
instance.user.groups.remove(*other_groups)
# Add user to the correct group
instance.user.groups.add(group)