diff --git a/core/__pycache__/forms.cpython-311.pyc b/core/__pycache__/forms.cpython-311.pyc index 950dabf..b9f3e54 100644 Binary files a/core/__pycache__/forms.cpython-311.pyc and b/core/__pycache__/forms.cpython-311.pyc differ diff --git a/core/__pycache__/models.cpython-311.pyc b/core/__pycache__/models.cpython-311.pyc index e198ee6..1c5c1d2 100644 Binary files a/core/__pycache__/models.cpython-311.pyc and b/core/__pycache__/models.cpython-311.pyc differ diff --git a/core/forms.py b/core/forms.py index 0e82b0d..b575ed7 100644 --- a/core/forms.py +++ b/core/forms.py @@ -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'})) diff --git a/core/models.py b/core/models.py index 4f11329..f1331d6 100644 --- a/core/models.py +++ b/core/models.py @@ -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) \ No newline at end of file + 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)