diff --git a/django_school_management/accounts/constants.py b/django_school_management/accounts/constants.py index c4673a8..eb6e6a3 100644 --- a/django_school_management/accounts/constants.py +++ b/django_school_management/accounts/constants.py @@ -8,6 +8,15 @@ class ProfileApprovalStatusEnum(Enum): approved = 'a' +class AccountTypesEnum(Enum): + subscriber = 'subscriber' + student = 'student' + teacher = 'teacher' + editor = 'editor' + academic_officer = 'academic_officer' + admin = 'admin' + + class AccountURLEnums(Enum): approval_with_modification = 'modify-and-approve//' dashboard = 'dashboard/' diff --git a/django_school_management/accounts/signals.py b/django_school_management/accounts/signals.py index 592cc5e..4a9fa29 100644 --- a/django_school_management/accounts/signals.py +++ b/django_school_management/accounts/signals.py @@ -1,16 +1,30 @@ +from Tools.demo.mcast import sender from django.contrib.auth import get_user_model from django.db.models.signals import post_save from django.dispatch import receiver + +from .constants import ProfileApprovalStatusEnum, AccountTypesEnum from .models import CommonUserProfile +User = get_user_model() + +def create_profile_for_approved_account(user): + if user.approval_status == ProfileApprovalStatusEnum.approved.value: + profile, created = CommonUserProfile.objects.get_or_create(user=user) + return profile, created + return None, None + + @receiver(post_save, sender=get_user_model()) -def create_user_profile(sender, instance, created, **kwargs): - """ Create a profile for the verfiied user if a profile - already doesn't belong to the user. - """ - if instance.approval_status == 'a': - try: - profile = instance.profile - except CommonUserProfile.DoesNotExist: - CommonUserProfile.objects.create(user=instance) +def assign_all_permissions_to_superuser(sender, instance, created, **kwargs): + user = User.objects.get(id=instance.id) + user_profile = getattr(user, 'profile', None) + + if instance.is_superuser and not user_profile: + create_profile_for_approved_account(user) + user.approval_status = ProfileApprovalStatusEnum.approved.value + user.requested_role = AccountTypesEnum.admin.value + user.save() + elif created and not user_profile: + create_profile_for_approved_account(user)