Skip to content

Commit

Permalink
update user creation signals
Browse files Browse the repository at this point in the history
  • Loading branch information
TareqMonwer committed Nov 3, 2024
1 parent adb59a8 commit b1dec27
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
9 changes: 9 additions & 0 deletions django_school_management/accounts/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/<int:pk>/'
dashboard = 'dashboard/'
Expand Down
32 changes: 23 additions & 9 deletions django_school_management/accounts/signals.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit b1dec27

Please sign in to comment.