-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
adb59a8
commit b1dec27
Showing
2 changed files
with
32 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |